|
|
@ -7,214 +7,163 @@ |
|
|
|
|
|
|
|
|
|
|
|
//! Tests covering enum type generation.
|
|
|
|
//! Tests covering enum type generation.
|
|
|
|
|
|
|
|
|
|
|
|
use enums_proto::enums; |
|
|
|
use enums_proto::*; |
|
|
|
use googletest::prelude::*; |
|
|
|
use googletest::prelude::*; |
|
|
|
use unittest_proto::proto2_unittest; |
|
|
|
use unittest_proto::*; |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_nested_enum_values() { |
|
|
|
fn test_nested_enum_values() { |
|
|
|
assert_that!(i32::from(proto2_unittest::TestAllTypes_::NestedEnum::Foo), eq(1)); |
|
|
|
assert_that!(i32::from(TestAllTypes_::NestedEnum::Foo), eq(1)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestAllTypes_::NestedEnum::Bar), eq(2)); |
|
|
|
assert_that!(i32::from(TestAllTypes_::NestedEnum::Bar), eq(2)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestAllTypes_::NestedEnum::Baz), eq(3)); |
|
|
|
assert_that!(i32::from(TestAllTypes_::NestedEnum::Baz), eq(3)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestAllTypes_::NestedEnum::Neg), eq(-1)); |
|
|
|
assert_that!(i32::from(TestAllTypes_::NestedEnum::Neg), eq(-1)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_isolated_nested_enum() { |
|
|
|
fn test_isolated_nested_enum() { |
|
|
|
// Ensure that the enum is generated even when it's the only nested type for the
|
|
|
|
// Ensure that the enum is generated even when it's the only nested type for the
|
|
|
|
// message.
|
|
|
|
// message.
|
|
|
|
assert_that!(i32::from(proto2_unittest::TestRequiredEnumNoMask_::NestedEnum::Foo), eq(2)); |
|
|
|
assert_that!(i32::from(TestRequiredEnumNoMask_::NestedEnum::Foo), eq(2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_enum_value_name_same_as_enum() { |
|
|
|
fn test_enum_value_name_same_as_enum() { |
|
|
|
assert_that!(i32::from(enums::TestEnumValueNameSameAsEnum::TestEnumValueNameSameAsEnum), eq(1)); |
|
|
|
assert_that!(i32::from(TestEnumValueNameSameAsEnum::TestEnumValueNameSameAsEnum), eq(1)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_enum_defaults() { |
|
|
|
fn test_enum_defaults() { |
|
|
|
|
|
|
|
assert_that!(TestSparseEnum::default(), eq(TestSparseEnum::SparseA)); |
|
|
|
|
|
|
|
assert_that!(TestEnumWithDupValue::default(), eq(TestEnumWithDupValue::Foo1)); |
|
|
|
|
|
|
|
assert_that!(TestEnumWithDupValue::default(), eq(TestEnumWithDupValue::Foo2)); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
proto2_unittest::TestSparseEnum::default(), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::default(), |
|
|
|
eq(proto2_unittest::TestSparseEnum::SparseA) |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Unknown) |
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestEnumWithDupValue::default(), |
|
|
|
|
|
|
|
eq(proto2_unittest::TestEnumWithDupValue::Foo1) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestEnumWithDupValue::default(), |
|
|
|
|
|
|
|
eq(proto2_unittest::TestEnumWithDupValue::Foo2) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::default(), |
|
|
|
|
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Unknown) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestAllTypes_::NestedEnum::default(), |
|
|
|
|
|
|
|
eq(proto2_unittest::TestAllTypes_::NestedEnum::Foo) |
|
|
|
|
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
assert_that!(TestAllTypes_::NestedEnum::default(), eq(TestAllTypes_::NestedEnum::Foo)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
#[deny(unreachable_patterns)] |
|
|
|
#[deny(unreachable_patterns)] |
|
|
|
#[allow(clippy::let_unit_value)] |
|
|
|
#[allow(clippy::let_unit_value)] |
|
|
|
fn test_closed_enum_is_nonexhaustive() { |
|
|
|
fn test_closed_enum_is_nonexhaustive() { |
|
|
|
let val = proto2_unittest::ForeignEnum::ForeignFoo; |
|
|
|
let val = ForeignEnum::ForeignFoo; |
|
|
|
let _it_compiles: () = match val { |
|
|
|
let _it_compiles: () = match val { |
|
|
|
proto2_unittest::ForeignEnum::ForeignFoo => (), |
|
|
|
ForeignEnum::ForeignFoo => (), |
|
|
|
proto2_unittest::ForeignEnum::ForeignBar => (), |
|
|
|
ForeignEnum::ForeignBar => (), |
|
|
|
proto2_unittest::ForeignEnum::ForeignBaz => (), |
|
|
|
ForeignEnum::ForeignBaz => (), |
|
|
|
proto2_unittest::ForeignEnum::ForeignBax => (), |
|
|
|
ForeignEnum::ForeignBax => (), |
|
|
|
_ => unreachable!(), |
|
|
|
_ => unreachable!(), |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_closed_enum_conversion() { |
|
|
|
fn test_closed_enum_conversion() { |
|
|
|
assert_that!(i32::from(proto2_unittest::TestSparseEnum::SparseA), eq(123)); |
|
|
|
assert_that!(i32::from(TestSparseEnum::SparseA), eq(123)); |
|
|
|
assert_that!( |
|
|
|
assert_that!(TestSparseEnum::try_from(123), ok(eq(TestSparseEnum::SparseA))); |
|
|
|
proto2_unittest::TestSparseEnum::try_from(123), |
|
|
|
|
|
|
|
ok(eq(proto2_unittest::TestSparseEnum::SparseA)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_that!(i32::from(proto2_unittest::TestSparseEnum::SparseD), eq(-15)); |
|
|
|
assert_that!(i32::from(TestSparseEnum::SparseD), eq(-15)); |
|
|
|
assert_that!( |
|
|
|
assert_that!(TestSparseEnum::try_from(-15), ok(eq(TestSparseEnum::SparseD))); |
|
|
|
proto2_unittest::TestSparseEnum::try_from(-15), |
|
|
|
|
|
|
|
ok(eq(proto2_unittest::TestSparseEnum::SparseD)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_that!( |
|
|
|
assert_that!(TestSparseEnum::try_from(0), ok(eq(TestSparseEnum::SparseF))); |
|
|
|
proto2_unittest::TestSparseEnum::try_from(0), |
|
|
|
assert_that!(TestSparseEnum::try_from(1), err(anything())); |
|
|
|
ok(eq(proto2_unittest::TestSparseEnum::SparseF)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!(proto2_unittest::TestSparseEnum::try_from(1), err(anything())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_closed_aliased_enum_conversion() { |
|
|
|
fn test_closed_aliased_enum_conversion() { |
|
|
|
assert_that!(i32::from(proto2_unittest::TestEnumWithDupValue::Foo1), eq(1)); |
|
|
|
assert_that!(i32::from(TestEnumWithDupValue::Foo1), eq(1)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestEnumWithDupValue::Foo2), eq(1)); |
|
|
|
assert_that!(i32::from(TestEnumWithDupValue::Foo2), eq(1)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestEnumWithDupValue::Bar1), eq(2)); |
|
|
|
assert_that!(i32::from(TestEnumWithDupValue::Bar1), eq(2)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestEnumWithDupValue::Bar2), eq(2)); |
|
|
|
assert_that!(i32::from(TestEnumWithDupValue::Bar2), eq(2)); |
|
|
|
assert_that!(i32::from(proto2_unittest::TestEnumWithDupValue::Baz), eq(3)); |
|
|
|
assert_that!(i32::from(TestEnumWithDupValue::Baz), eq(3)); |
|
|
|
|
|
|
|
|
|
|
|
assert_that!( |
|
|
|
assert_that!(TestEnumWithDupValue::try_from(1), ok(eq(TestEnumWithDupValue::Foo1))); |
|
|
|
proto2_unittest::TestEnumWithDupValue::try_from(1), |
|
|
|
assert_that!(TestEnumWithDupValue::try_from(2), ok(eq(TestEnumWithDupValue::Bar1))); |
|
|
|
ok(eq(proto2_unittest::TestEnumWithDupValue::Foo1)) |
|
|
|
assert_that!(TestEnumWithDupValue::try_from(3), ok(eq(TestEnumWithDupValue::Baz))); |
|
|
|
); |
|
|
|
assert_that!(TestEnumWithDupValue::try_from(0), err(anything())); |
|
|
|
assert_that!( |
|
|
|
assert_that!(TestEnumWithDupValue::try_from(4), err(anything())); |
|
|
|
proto2_unittest::TestEnumWithDupValue::try_from(2), |
|
|
|
|
|
|
|
ok(eq(proto2_unittest::TestEnumWithDupValue::Bar1)) |
|
|
|
assert_that!(TestEnumWithDupValue::Foo1, eq(TestEnumWithDupValue::Foo2)); |
|
|
|
); |
|
|
|
assert_that!(TestEnumWithDupValue::Bar1, eq(TestEnumWithDupValue::Bar2)); |
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestEnumWithDupValue::try_from(3), |
|
|
|
|
|
|
|
ok(eq(proto2_unittest::TestEnumWithDupValue::Baz)) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!(proto2_unittest::TestEnumWithDupValue::try_from(0), err(anything())); |
|
|
|
|
|
|
|
assert_that!(proto2_unittest::TestEnumWithDupValue::try_from(4), err(anything())); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestEnumWithDupValue::Foo1, |
|
|
|
|
|
|
|
eq(proto2_unittest::TestEnumWithDupValue::Foo2) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
proto2_unittest::TestEnumWithDupValue::Bar1, |
|
|
|
|
|
|
|
eq(proto2_unittest::TestEnumWithDupValue::Bar2) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
#[deny(unreachable_patterns)] |
|
|
|
#[deny(unreachable_patterns)] |
|
|
|
#[allow(clippy::let_unit_value)] |
|
|
|
#[allow(clippy::let_unit_value)] |
|
|
|
fn test_open_enum_is_nonexhaustive() { |
|
|
|
fn test_open_enum_is_nonexhaustive() { |
|
|
|
let val = enums::TestEnumValueNameSameAsEnum::Unknown; |
|
|
|
let val = TestEnumValueNameSameAsEnum::Unknown; |
|
|
|
let _it_compiles: () = match val { |
|
|
|
let _it_compiles: () = match val { |
|
|
|
enums::TestEnumValueNameSameAsEnum::Unknown => (), |
|
|
|
TestEnumValueNameSameAsEnum::Unknown => (), |
|
|
|
enums::TestEnumValueNameSameAsEnum::TestEnumValueNameSameAsEnum => (), |
|
|
|
TestEnumValueNameSameAsEnum::TestEnumValueNameSameAsEnum => (), |
|
|
|
_ => unreachable!(), |
|
|
|
_ => unreachable!(), |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_open_enum_conversion() { |
|
|
|
fn test_open_enum_conversion() { |
|
|
|
assert_that!(i32::from(enums::TestEnumWithNumericNames::Unknown), eq(0)); |
|
|
|
assert_that!(i32::from(TestEnumWithNumericNames::Unknown), eq(0)); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithNumericNames::_2020), eq(1)); |
|
|
|
assert_that!(i32::from(TestEnumWithNumericNames::_2020), eq(1)); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithNumericNames::_2021), eq(2)); |
|
|
|
assert_that!(i32::from(TestEnumWithNumericNames::_2021), eq(2)); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithNumericNames::_2022), eq(3)); |
|
|
|
assert_that!(i32::from(TestEnumWithNumericNames::_2022), eq(3)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_that!(TestEnumWithNumericNames::from(0), eq(TestEnumWithNumericNames::Unknown)); |
|
|
|
|
|
|
|
assert_that!(TestEnumWithNumericNames::from(1), eq(TestEnumWithNumericNames::_2020)); |
|
|
|
|
|
|
|
assert_that!(TestEnumWithNumericNames::from(2), eq(TestEnumWithNumericNames::_2021)); |
|
|
|
|
|
|
|
assert_that!(TestEnumWithNumericNames::from(3), eq(TestEnumWithNumericNames::_2022)); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithNumericNames::from(0), |
|
|
|
TestEnumWithNumericNames::from(4), |
|
|
|
eq(enums::TestEnumWithNumericNames::Unknown) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
enums::TestEnumWithNumericNames::from(1), |
|
|
|
|
|
|
|
eq(enums::TestEnumWithNumericNames::_2020) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
enums::TestEnumWithNumericNames::from(2), |
|
|
|
|
|
|
|
eq(enums::TestEnumWithNumericNames::_2021) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
enums::TestEnumWithNumericNames::from(3), |
|
|
|
|
|
|
|
eq(enums::TestEnumWithNumericNames::_2022) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
assert_that!( |
|
|
|
|
|
|
|
enums::TestEnumWithNumericNames::from(4), |
|
|
|
|
|
|
|
not(any![ |
|
|
|
not(any![ |
|
|
|
eq(enums::TestEnumWithNumericNames::Unknown), |
|
|
|
eq(TestEnumWithNumericNames::Unknown), |
|
|
|
eq(enums::TestEnumWithNumericNames::_2020), |
|
|
|
eq(TestEnumWithNumericNames::_2020), |
|
|
|
eq(enums::TestEnumWithNumericNames::_2021), |
|
|
|
eq(TestEnumWithNumericNames::_2021), |
|
|
|
eq(enums::TestEnumWithNumericNames::_2022), |
|
|
|
eq(TestEnumWithNumericNames::_2022), |
|
|
|
]) |
|
|
|
]) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithNumericNames::from(-1)), eq(-1)); |
|
|
|
assert_that!(i32::from(TestEnumWithNumericNames::from(-1)), eq(-1)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_open_aliased_enum_conversion() { |
|
|
|
fn test_open_aliased_enum_conversion() { |
|
|
|
assert_that!(i32::from(enums::TestEnumWithDuplicateStrippedPrefixNames::Unknown), eq(0)); |
|
|
|
assert_that!(i32::from(TestEnumWithDuplicateStrippedPrefixNames::Unknown), eq(0)); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithDuplicateStrippedPrefixNames::Foo), eq(1)); |
|
|
|
assert_that!(i32::from(TestEnumWithDuplicateStrippedPrefixNames::Foo), eq(1)); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithDuplicateStrippedPrefixNames::Bar), eq(2)); |
|
|
|
assert_that!(i32::from(TestEnumWithDuplicateStrippedPrefixNames::Bar), eq(2)); |
|
|
|
assert_that!( |
|
|
|
assert_that!(i32::from(TestEnumWithDuplicateStrippedPrefixNames::DifferentNameAlias), eq(2)); |
|
|
|
i32::from(enums::TestEnumWithDuplicateStrippedPrefixNames::DifferentNameAlias), |
|
|
|
|
|
|
|
eq(2) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::from(0), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::from(0), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Unknown) |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Unknown) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::from(1), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::from(1), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Foo) |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Foo) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::from(2), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::from(2), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Bar) |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Bar) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::from(2), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::from(2), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::DifferentNameAlias) |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::DifferentNameAlias) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!( |
|
|
|
assert_that!( |
|
|
|
enums::TestEnumWithDuplicateStrippedPrefixNames::from(3), |
|
|
|
TestEnumWithDuplicateStrippedPrefixNames::from(3), |
|
|
|
not(any![ |
|
|
|
not(any![ |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Unknown), |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Unknown), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Foo), |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Foo), |
|
|
|
eq(enums::TestEnumWithDuplicateStrippedPrefixNames::Bar), |
|
|
|
eq(TestEnumWithDuplicateStrippedPrefixNames::Bar), |
|
|
|
]) |
|
|
|
]) |
|
|
|
); |
|
|
|
); |
|
|
|
assert_that!(i32::from(enums::TestEnumWithDuplicateStrippedPrefixNames::from(5)), eq(5)); |
|
|
|
assert_that!(i32::from(TestEnumWithDuplicateStrippedPrefixNames::from(5)), eq(5)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_enum_conversion_failure_display() { |
|
|
|
fn test_enum_conversion_failure_display() { |
|
|
|
let err = proto2_unittest::TestSparseEnum::try_from(1).unwrap_err(); |
|
|
|
let err = TestSparseEnum::try_from(1).unwrap_err(); |
|
|
|
assert_that!(format!("{err}"), eq("1 is not a known value for TestSparseEnum")); |
|
|
|
assert_that!(format!("{err}"), eq("1 is not a known value for TestSparseEnum")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_enum_conversion_failure_impls_std_error() { |
|
|
|
fn test_enum_conversion_failure_impls_std_error() { |
|
|
|
let err = proto2_unittest::TestSparseEnum::try_from(1).unwrap_err(); |
|
|
|
let err = TestSparseEnum::try_from(1).unwrap_err(); |
|
|
|
let _test_compiles: &dyn std::error::Error = &err; |
|
|
|
let _test_compiles: &dyn std::error::Error = &err; |
|
|
|
} |
|
|
|
} |
|
|
|