diff --git a/googletest/tests/lib.rs b/googletest/tests/lib.rs index f5d8f75d..b613b5bb 100644 --- a/googletest/tests/lib.rs +++ b/googletest/tests/lib.rs @@ -19,7 +19,10 @@ mod colorized_diff_test; mod composition_test; mod elements_are_matcher_test; mod field_matcher_test; -mod matches_pattern_test; +mod matches_pattern_enum_test; +mod matches_pattern_struct_and_enum_test; +mod matches_pattern_struct_test; +mod matches_pattern_tuple_struct_test; mod pointwise_matcher_test; mod property_matcher_test; mod proptest_integration_test; diff --git a/googletest/tests/matches_pattern_enum_test.rs b/googletest/tests/matches_pattern_enum_test.rs new file mode 100644 index 00000000..1bda28ab --- /dev/null +++ b/googletest/tests/matches_pattern_enum_test.rs @@ -0,0 +1,469 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use googletest::prelude::*; +use googletest::Result; +use indoc::indoc; + +#[test] +fn matches_enum_without_field() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A, + } + let actual = AnEnum::A; + + verify_that!(actual, matches_pattern!(&AnEnum::A))?; + verify_that!(actual, matches_pattern!(&AnEnum::A,)) +} + +#[test] +fn matches_enum_without_field_ref_binding_mode() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A, + } + let actual = AnEnum::A; + + verify_that!(actual, matches_pattern!(AnEnum::A))?; + verify_that!(actual, matches_pattern!(AnEnum::A,)) +} + +#[test] +fn matches_enum_without_field_copy() -> Result<()> { + #[derive(Debug, Clone, Copy)] + enum AnEnum { + A, + } + let actual = AnEnum::A; + + verify_that!(actual, matches_pattern!(AnEnum::A))?; + verify_that!(actual, matches_pattern!(AnEnum::A,)) +} + +#[test] +fn generates_correct_failure_output_when_enum_variant_without_field_is_not_matched() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + #[allow(unused)] + A, + B, + } + let actual = AnEnum::B; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::A)); + + const EXPECTED: &str = "is not & AnEnum :: A"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn generates_correct_failure_output_when_enum_variant_without_field_is_matched() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A, + } + let actual = AnEnum::A; + + let result = verify_that!(actual, not(matches_pattern!(&AnEnum::A))); + + const EXPECTED: &str = "is & AnEnum :: A"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn has_meaningful_assertion_failure_message_when_wrong_enum_variant_is_used() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + #[allow(unused)] + A(u32), + #[allow(unused)] + B(u32), + } + let actual = AnEnum::A(123); + + let result = verify_that!(actual, matches_pattern!(&AnEnum::B(eq(123)))); + + verify_that!( + result, + err(displays_as(contains_substring(indoc! {" + Actual: A(123), + which has the wrong enum variant `A` + " + }))) + ) +} + +#[test] +fn matches_enum_struct_non_exhaustive() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a_field: u32, another_field: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a_field: 123, another_field: 234 }; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a_field: eq(123), .. })) +} + +#[test] +fn matches_enum_struct_with_all_non_exhaustive_fields() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { .. })) +} + +#[test] +fn has_failure_when_wrong_enum_struct_variant_is_matched_with_all_non_exhaustive_fields( +) -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { .. })); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant2 { .. } + Actual: Variant1 { a: 123, b: 234 }, + which is not & AnEnum :: Variant2 { .. } + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn matches_enum_struct_with_all_wildcard_fields() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a: _, b: _ })) +} + +#[test] +fn has_failure_when_wrong_enum_struct_variant_is_matched_with_all_wildcard_fields() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { c: _, d: _ })); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant2 { c : _, d : _, } + Actual: Variant1 { a: 123, b: 234 }, + which is not & AnEnum :: Variant2 { c : _, d : _, } + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn matches_enum_struct_non_exhaustive_with_wildcard_fields() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a: _, .. })) +} + +#[test] +fn has_failure_when_wrong_enum_struct_variant_is_matched_non_exhaustive_with_wildcard_fields( +) -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a: u32, b: u32 }, + Variant2 { c: u32, d: u32 }, + } + let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { c: _, .. })); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant2 { c : _, .. } + Actual: Variant1 { a: 123, b: 234 }, + which is not & AnEnum :: Variant2 { c : _, .. } + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn matches_enum_struct_exhaustive_with_multiple_variants() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1 { a_field: u32 }, + Variant2, + } + let actual: AnEnum = AnEnum::Variant1 { a_field: 123 }; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a_field: eq(123) })) +} + +#[test] +fn matches_match_pattern_literal() -> Result<()> { + let actual = false; + #[allow(clippy::redundant_pattern_matching)] + verify_that!(actual, matches_pattern!(false))?; + #[allow(clippy::redundant_pattern_matching)] + verify_that!(actual, matches_pattern!(false,))?; + let actual = 1; + verify_that!(actual, matches_pattern!(1))?; + verify_that!(actual, matches_pattern!(1,))?; + let actual = "test"; + verify_that!(actual, matches_pattern!(&"test"))?; + verify_that!(actual, matches_pattern!(&"test",)) +} + +#[test] +fn has_failure_when_wrong_enum_variant_is_matched_non_exhaustively() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant2; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(..))); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant1(..) + Actual: Variant2, + which is not & AnEnum :: Variant1(..) + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn has_failure_when_wrong_enum_variant_is_matched_with_underscore() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant2; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(_))); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant1(_) + Actual: Variant2, + which is not & AnEnum :: Variant1(_) + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn has_failure_when_wrong_enum_variant_is_matched_with_value() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant2; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(123))); + + const EXPECTED: &str = indoc!( + " + Expected: is & AnEnum :: Variant1 which has field `0`, which is equal to 123 + Actual: Variant2, + which has the wrong enum variant `Variant2` + " + ); + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn matches_enum_struct_field_with_mutliple_variants() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant2; + + verify_that!(actual, matches_pattern!(&AnEnum::Variant2)) +} + +#[test] +fn matches_enum_struct_field_with_multiple_variants_non_exhaustive() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant1(123); + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1(..))) +} + +#[test] +fn matches_enum_struct_field_with_multiple_variants_with_wildcard() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + enum AnEnum { + Variant1(i8), + Variant2, + } + let actual: AnEnum = AnEnum::Variant1(123); + + verify_that!(actual, matches_pattern!(&AnEnum::Variant1(_))) +} + +#[test] +fn matches_enum_with_field() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A(u32), + } + let actual = AnEnum::A(123); + + verify_that!(actual, matches_pattern!(&AnEnum::A(eq(123)))) +} + +#[test] +fn does_not_match_wrong_enum_value() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + #[allow(unused)] + A(u32), + B, + } + let actual = AnEnum::B; + + verify_that!(actual, not(matches_pattern!(&AnEnum::A(eq(123))))) +} + +#[test] +fn includes_enum_variant_in_description_with_field() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A(u32), + } + let actual = AnEnum::A(123); + + let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234)))); + + const EXPECTED: &str = "Expected: is & AnEnum :: A which has field `0`"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn includes_enum_variant_in_negative_description_with_field() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A(u32), + } + let actual = AnEnum::A(123); + + let result = verify_that!(actual, not(matches_pattern!(&AnEnum::A(eq(123))))); + + const EXPECTED: &str = "Expected: is not & AnEnum :: A which has field `0`, which is equal to"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn includes_enum_variant_in_description_with_two_fields() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A(u32, u32), + } + let actual = AnEnum::A(123, 234); + + let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234), eq(234)))); + + const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn includes_enum_variant_in_description_with_three_fields() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A(u32, u32, u32), + } + let actual = AnEnum::A(123, 234, 345); + + let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234), eq(234), eq(345)))); + + const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn includes_enum_variant_in_description_with_named_field() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A { field: u32 }, + } + let actual = AnEnum::A { field: 123 }; + + let result = verify_that!(actual, matches_pattern!(&AnEnum::A { field: eq(234) })); + + const EXPECTED: &str = "Expected: is & AnEnum :: A which has field `field`"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) +} + +#[test] +fn includes_enum_variant_in_description_with_two_named_fields() -> Result<()> { + #[derive(Debug)] + enum AnEnum { + A { field: u32, another_field: u32 }, + } + let actual = AnEnum::A { field: 123, another_field: 234 }; + + let result = verify_that!( + actual, + matches_pattern!(&AnEnum::A { field: eq(234), another_field: eq(234) }) + ); + + const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; + verify_that!(&result, err(displays_as(contains_substring(EXPECTED)))) +} diff --git a/googletest/tests/matches_pattern_struct_and_enum_test.rs b/googletest/tests/matches_pattern_struct_and_enum_test.rs new file mode 100644 index 00000000..58478a1e --- /dev/null +++ b/googletest/tests/matches_pattern_struct_and_enum_test.rs @@ -0,0 +1,234 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use googletest::prelude::*; +use googletest::Result; + +#[test] +fn matches_struct_with_a_method_taking_enum_value_parameter_followed_by_field() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + } + + impl AStruct { + fn get_field(&self, _value: AnEnum) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { get_field(AnEnum::AVariant): eq(1), another_field: eq(2), .. }) + ) +} + +#[test] +fn matches_struct_with_a_method_taking_enum_value_param_ret_ref_followed_by_field() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + } + + impl AStruct { + fn get_field_ref(&self, _value: AnEnum) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { + get_field_ref(AnEnum::AVariant): eq(&1), + another_field: eq(2), + .. + }) + ) +} + +#[test] +fn matches_struct_with_field_followed_by_method_taking_enum_value_param() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + } + + impl AStruct { + fn get_field(&self, _value: AnEnum) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { + another_field: eq(2), + get_field(AnEnum::AVariant): eq(1), + .. + }) + ) +} + +#[test] +fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + } + + impl AStruct { + fn get_field_ref(&self, _value: AnEnum) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { + another_field: eq(2), + get_field_ref(AnEnum::AVariant): eq(&1), + .. + }) + ) +} + +#[test] +fn matches_struct_with_field_followed_by_method_taking_enum_value_param_followed_by_field( +) -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + a_third_field: u32, + } + + impl AStruct { + fn get_field(&self, _value: AnEnum) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2, a_third_field: 3 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { + another_field: eq(2), + get_field(AnEnum::AVariant): eq(1), + a_third_field: eq(3), + .. + }) + ) +} + +#[test] +fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref_followed_by_field( +) -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + a_third_field: u32, + } + + impl AStruct { + fn get_field_ref(&self, _value: AnEnum) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 1, another_field: 2, a_third_field: 3 }; + + verify_that!( + actual, + matches_pattern!(&AStruct { + another_field: eq(2), + get_field_ref(AnEnum::AVariant): eq(&1), + a_third_field: eq(3), + .. + }) + ) +} + +#[test] +fn matches_struct_with_a_method_returning_reference_taking_enum_value_parameter() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + } + + impl AStruct { + fn get_field_ref(&self, _value: AnEnum) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 1 }; + + verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(AnEnum::AVariant): eq(&1) })) +} + +#[test] +fn matches_struct_with_a_method_taking_enum_value_parameter() -> Result<()> { + enum AnEnum { + AVariant, + } + + #[derive(Debug)] + struct AStruct { + a_field: u32, + } + + impl AStruct { + fn get_a_field(&self, _value: AnEnum) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 1 }; + + verify_that!(actual, matches_pattern!(&AStruct { get_a_field(AnEnum::AVariant): eq(1) })) +} diff --git a/googletest/tests/matches_pattern_test.rs b/googletest/tests/matches_pattern_struct_test.rs similarity index 56% rename from googletest/tests/matches_pattern_test.rs rename to googletest/tests/matches_pattern_struct_test.rs index ab00949e..30bbfd4a 100644 --- a/googletest/tests/matches_pattern_test.rs +++ b/googletest/tests/matches_pattern_struct_test.rs @@ -57,6 +57,7 @@ fn matches_braced_struct_with_no_fields() -> Result<()> { #[derive(Debug)] struct AStruct {} let actual = AStruct {}; + verify_that!(actual, matches_pattern!(AStruct {})) } @@ -197,6 +198,7 @@ fn has_correct_assertion_failure_message_for_single_field() -> Result<()> { a_field: u32, } let actual = AStruct { a_field: 123 }; + let result = verify_that!(actual, matches_pattern!(&AStruct { a_field: eq(234) })); const EXPECTED: &str = indoc!( @@ -207,7 +209,6 @@ fn has_correct_assertion_failure_message_for_single_field() -> Result<()> { which has field `a_field`, which isn't equal to 234 " ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } @@ -219,6 +220,7 @@ fn has_correct_assertion_failure_message_for_two_fields() -> Result<()> { another_field: u32, } let actual = AStruct { a_field: 123, another_field: 234 }; + let result = verify_that!( actual, matches_pattern!(&AStruct { a_field: eq(234), another_field: eq(123) }) @@ -234,7 +236,6 @@ fn has_correct_assertion_failure_message_for_two_fields() -> Result<()> { * which has field `a_field`, which isn't equal to 234 * which has field `another_field`, which isn't equal to 123" ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } @@ -251,6 +252,7 @@ fn has_correct_assertion_failure_message_for_field_and_property() -> Result<()> } } let actual = AStruct { a_field: 123, another_field: 234 }; + let result = verify_that!( actual, matches_pattern!(&AStruct { get_field(): eq(234), another_field: eq(123), .. }) @@ -266,32 +268,9 @@ fn has_correct_assertion_failure_message_for_field_and_property() -> Result<()> * whose property `get_field()` is `123`, which isn't equal to 234 * which has field `another_field`, which isn't equal to 123" ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } -#[test] -fn has_meaningful_assertion_failure_message_when_wrong_enum_variant_is_used() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - #[allow(unused)] - A(u32), - #[allow(unused)] - B(u32), - } - let actual = AnEnum::A(123); - let result = verify_that!(actual, matches_pattern!(&AnEnum::B(eq(123)))); - - verify_that!( - result, - err(displays_as(contains_substring(indoc! {" - Actual: A(123), - which has the wrong enum variant `A` - " - }))) - ) -} - #[test] fn supports_qualified_struct_names() -> Result<()> { mod a_module { @@ -306,988 +285,250 @@ fn supports_qualified_struct_names() -> Result<()> { } #[test] -fn matches_tuple_struct_containing_single_field() -> Result<()> { +fn matches_match_pattern_struct() -> Result<()> { + #[allow(dead_code)] #[derive(Debug)] - struct AStruct(u32); - let actual = AStruct(123); + struct AStruct { + a: u32, + } + let actual = AStruct { a: 123 }; - verify_that!(actual, matches_pattern!(&AStruct(eq(123)))) + verify_that!(actual, matches_pattern!(AStruct { .. })) } #[test] -fn matches_tuple_struct_containing_two_fields() -> Result<()> { +fn includes_struct_name_in_description_with_property() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, u32); - let actual = AStruct(123, 234); - - verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234)))) -} + struct AStruct { + field: u32, + } + impl AStruct { + fn get_field(&self) -> u32 { + self.field + } + } + let actual = AStruct { field: 123 }; -#[test] -fn matches_tuple_struct_containing_three_fields() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32, u32); - let actual = AStruct(123, 234, 345); + let result = verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(234) })); - verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345)))) + const EXPECTED: &str = "Expected: is & AStruct which has property `get_field()`"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } #[test] -fn matches_tuple_struct_containing_four_fields() -> Result<()> { +fn includes_struct_name_in_description_with_ref_property() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456); - - verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456)))) -} + struct AStruct { + field: u32, + } + impl AStruct { + fn get_field(&self) -> &u32 { + &self.field + } + } + let actual = AStruct { field: 123 }; -#[test] -fn matches_tuple_struct_containing_five_fields() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567); + let result = verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(&234) })); - verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567)))) + const EXPECTED: &str = "Expected: is & AStruct which has property `get_field()`"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } #[test] -fn matches_tuple_struct_containing_six_fields() -> Result<()> { +fn includes_struct_name_in_description_with_property_after_field() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678); - - verify_that!( - actual, - matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567), eq(678))) - ) -} + struct AStruct { + field: u32, + } + impl AStruct { + fn get_field(&self) -> u32 { + self.field + } + } + let actual = AStruct { field: 123 }; -#[test] -fn matches_tuple_struct_containing_seven_fields() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678, 789); + let result = + verify_that!(actual, matches_pattern!(&AStruct { field: eq(123), get_field(): eq(234) })); - verify_that!( - actual, - matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567), eq(678), eq(789))) - ) + const EXPECTED: &str = "Expected: is & AStruct which has all the following properties"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } #[test] -fn matches_tuple_struct_containing_eight_fields() -> Result<()> { +fn includes_struct_name_in_description_with_ref_property_after_field() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890); - - verify_that!( - actual, - matches_pattern!(&AStruct( - eq(123), - eq(234), - eq(345), - eq(456), - eq(567), - eq(678), - eq(789), - eq(890) - )) - ) -} + struct AStruct { + field: u32, + } + impl AStruct { + fn get_field(&self) -> &u32 { + &self.field + } + } + let actual = AStruct { field: 123 }; -#[test] -fn matches_tuple_struct_containing_nine_fields() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901); + let result = + verify_that!(actual, matches_pattern!(&AStruct { field: eq(123), get_field(): eq(&234) })); - verify_that!( - actual, - matches_pattern!(&AStruct( - eq(123), - eq(234), - eq(345), - eq(456), - eq(567), - eq(678), - eq(789), - eq(890), - eq(901) - )) - ) + const EXPECTED: &str = "Expected: is & AStruct which has all the following properties"; + verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) } #[test] -fn matches_tuple_struct_containing_ten_fields() -> Result<()> { +fn matches_struct_with_a_method() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901, 12); - - verify_that!( - actual, - matches_pattern!(&AStruct( - eq(123), - eq(234), - eq(345), - eq(456), - eq(567), - eq(678), - eq(789), - eq(890), - eq(901), - eq(12) - )) - ) -} + struct AStruct { + a_field: u32, + } -#[test] -fn matches_tuple_struct_containing_ten_fields_by_ref() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32, u32); - let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901, 12); + impl AStruct { + fn get_field(&self) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 123 }; - verify_that!( - actual, - matches_pattern!(&AStruct( - ref eq(&123), - ref eq(&234), - ref eq(&345), - ref eq(&456), - ref eq(&567), - ref eq(&678), - ref eq(&789), - ref eq(&890), - ref eq(&901), - ref eq(&12) - )) - ) + verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(123) })) } #[test] -fn matches_tuple_struct_with_trailing_comma() -> Result<()> { +fn matches_struct_with_a_method_and_trailing_comma() -> Result<()> { #[derive(Debug)] - struct AStruct(u32); - let actual = AStruct(123); - - verify_that!( - actual, - matches_pattern!(&AStruct( - eq(123), // Keep the trailing comma, block reformatting - )) - ) -} + struct AStruct { + a_field: u32, + } -#[test] -fn matches_tuple_struct_with_two_fields_and_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct(u32, u32); - let actual = AStruct(123, 234); + impl AStruct { + fn get_field(&self) -> u32 { + self.a_field + } + } + let actual = AStruct { a_field: 123 }; - verify_that!( - actual, - matches_pattern!(&AStruct( - eq(123), - eq(234), // Keep the trailing comma, block reformatting - )) - ) + verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(123), })) } #[test] -fn matches_tuple_struct_with_interleaved_underscore() -> Result<()> { - #[derive(Debug)] - struct NonCopy; +fn matches_struct_with_a_method_taking_parameter() -> Result<()> { #[derive(Debug)] - struct AStruct(u32, NonCopy, u32); - let actual = AStruct(1, NonCopy, 3); - - verify_that!(actual, matches_pattern!(&AStruct(eq(1), _, eq(3))))?; - verify_that!(actual, matches_pattern!(AStruct(eq(&1), _, eq(&3)))) -} + struct AStruct { + a_field: u32, + } -#[test] -fn matches_tuple_struct_non_exhaustive() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - struct AStruct(i32, u32); - let actual = AStruct(1, 3); + impl AStruct { + fn add_to_field(&self, a: u32) -> u32 { + self.a_field + a + } + } + let actual = AStruct { a_field: 1 }; - verify_that!(actual, matches_pattern!(&AStruct(_, ..)))?; - verify_that!(actual, matches_pattern!(AStruct(_, ..))) + verify_that!(actual, matches_pattern!(&AStruct { add_to_field(2): eq(3) })) } #[test] -fn matches_generic_tuple_struct_exhaustively() -> Result<()> { - #[allow(dead_code)] +fn matches_struct_with_a_method_taking_two_parameters() -> Result<()> { #[derive(Debug)] - struct AStruct(T, u32); - let actual = AStruct(1, 3); - - verify_that!(actual, matches_pattern!(&AStruct(_, _)))?; - verify_that!(actual, matches_pattern!(AStruct(_, _))) -} + struct AStruct { + a_field: u32, + } -#[test] -fn matches_enum_without_field() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A, + impl AStruct { + fn add_product_to_field(&self, a: u32, b: u32) -> u32 { + self.a_field + a * b + } } - let actual = AnEnum::A; + let actual = AStruct { a_field: 1 }; - verify_that!(actual, matches_pattern!(&AnEnum::A))?; - verify_that!(actual, matches_pattern!(&AnEnum::A,)) + verify_that!(actual, matches_pattern!(&AStruct { add_product_to_field(2, 3): eq(7) })) } #[test] -fn matches_enum_without_field_ref_binding_mode() -> Result<()> { +fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma() -> Result<()> { #[derive(Debug)] - enum AnEnum { - A, + struct AStruct { + a_field: u32, } - let actual = AnEnum::A; - - verify_that!(actual, matches_pattern!(AnEnum::A))?; - verify_that!(actual, matches_pattern!(AnEnum::A,)) -} -#[test] -fn matches_enum_without_field_copy() -> Result<()> { - #[derive(Debug, Clone, Copy)] - enum AnEnum { - A, + impl AStruct { + fn add_product_to_field(&self, a: u32, b: u32) -> u32 { + self.a_field + a * b + } } - let actual = AnEnum::A; + let actual = AStruct { a_field: 1 }; - verify_that!(actual, matches_pattern!(AnEnum::A))?; - verify_that!(actual, matches_pattern!(AnEnum::A,)) + verify_that!(actual, matches_pattern!(&AStruct { add_product_to_field(2, 3,): eq(7) })) } #[test] -fn matches_enum_struct_non_exhaustive() -> Result<()> { - #[allow(dead_code)] +fn matches_struct_with_a_method_returning_a_reference() -> Result<()> { #[derive(Debug)] - enum AnEnum { - Variant1 { a_field: u32, another_field: u32 }, + struct AStruct { + a_field: u32, } - let actual: AnEnum = AnEnum::Variant1 { a_field: 123, another_field: 234 }; - verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a_field: eq(123), .. })) -} -#[test] -fn matches_enum_struct_with_all_non_exhaustive_fields() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, + impl AStruct { + fn get_field_ref(&self) -> &u32 { + &self.a_field + } } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { .. })) + let actual = AStruct { a_field: 123 }; + + verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(): eq(&123) })) } #[test] -fn has_failure_when_wrong_enum_struct_variant_is_matched_with_all_non_exhaustive_fields( -) -> Result<()> { - #[allow(dead_code)] +fn matches_struct_with_a_method_returning_a_reference_with_trailing_comma() -> Result<()> { #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, + struct AStruct { + a_field: u32, } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { .. })); + impl AStruct { + fn get_field_ref(&self) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 123 }; - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant2 { .. } - Actual: Variant1 { a: 123, b: 234 }, - which is not & AnEnum :: Variant2 { .. } - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) + verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(): eq(&123), })) } #[test] -fn matches_enum_struct_with_all_wildcard_fields() -> Result<()> { - #[allow(dead_code)] +fn matches_struct_with_a_method_taking_two_parameters_ret_ref() -> Result<()> { #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, + struct AStruct { + a_field: u32, + } + + impl AStruct { + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { + &self.a_field + } } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a: _, b: _ })) + let actual = AStruct { a_field: 1 }; + + verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(2, 3): eq(&1) })) } #[test] -fn has_failure_when_wrong_enum_struct_variant_is_matched_with_all_wildcard_fields() -> Result<()> { - #[allow(dead_code)] +fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref() -> Result<()> { #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, + struct AStruct { + a_field: u32, } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { c: _, d: _ })); + impl AStruct { + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { + &self.a_field + } + } + let actual = AStruct { a_field: 1 }; - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant2 { c : _, d : _, } - Actual: Variant1 { a: 123, b: 234 }, - which is not & AnEnum :: Variant2 { c : _, d : _, } - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) + verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(2, 3,): eq(&1) })) } #[test] -fn matches_enum_struct_non_exhaustive_with_wildcard_fields() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, - } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a: _, .. })) -} - -#[test] -fn has_failure_when_wrong_enum_struct_variant_is_matched_non_exhaustive_with_wildcard_fields( -) -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1 { a: u32, b: u32 }, - Variant2 { c: u32, d: u32 }, - } - let actual: AnEnum = AnEnum::Variant1 { a: 123, b: 234 }; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant2 { c: _, .. })); - - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant2 { c : _, .. } - Actual: Variant1 { a: 123, b: 234 }, - which is not & AnEnum :: Variant2 { c : _, .. } - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn matches_enum_struct_exhaustive_with_multiple_variants() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1 { a_field: u32 }, - Variant2, - } - let actual: AnEnum = AnEnum::Variant1 { a_field: 123 }; - verify_that!(actual, matches_pattern!(&AnEnum::Variant1 { a_field: eq(123) })) -} - -#[test] -fn matches_match_pattern_literal() -> Result<()> { - let actual = false; - #[allow(clippy::redundant_pattern_matching)] - verify_that!(actual, matches_pattern!(false))?; - #[allow(clippy::redundant_pattern_matching)] - verify_that!(actual, matches_pattern!(false,))?; - let actual = 1; - verify_that!(actual, matches_pattern!(1))?; - verify_that!(actual, matches_pattern!(1,))?; - let actual = "test"; - verify_that!(actual, matches_pattern!(&"test"))?; - verify_that!(actual, matches_pattern!(&"test",)) -} - -#[test] -fn matches_match_pattern_struct() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - struct AStruct { - a: u32, - } - let actual = AStruct { a: 123 }; - verify_that!(actual, matches_pattern!(AStruct { .. })) -} - -#[test] -fn generates_correct_failure_output_when_enum_variant_without_field_is_not_matched() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - #[allow(unused)] - A, - B, - } - let actual = AnEnum::B; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::A)); - - const EXPECTED: &str = "is not & AnEnum :: A"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn generates_correct_failure_output_when_enum_variant_without_field_is_matched() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A, - } - let actual = AnEnum::A; - - let result = verify_that!(actual, not(matches_pattern!(&AnEnum::A))); - - const EXPECTED: &str = "is & AnEnum :: A"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn has_failure_when_wrong_enum_variant_is_matched_non_exhaustively() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant2; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(..))); - - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant1(..) - Actual: Variant2, - which is not & AnEnum :: Variant1(..) - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn has_failure_when_wrong_enum_variant_is_matched_with_underscore() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant2; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(_))); - - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant1(_) - Actual: Variant2, - which is not & AnEnum :: Variant1(_) - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn has_failure_when_wrong_enum_variant_is_matched_with_value() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant2; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::Variant1(123))); - - const EXPECTED: &str = indoc!( - " - Expected: is & AnEnum :: Variant1 which has field `0`, which is equal to 123 - Actual: Variant2, - which has the wrong enum variant `Variant2` - " - ); - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn matches_enum_struct_field_with_mutliple_variants() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant2; - - verify_that!(actual, matches_pattern!(&AnEnum::Variant2)) -} - -#[test] -fn matches_enum_struct_field_with_multiple_variants_non_exhaustive() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant1(123); - - verify_that!(actual, matches_pattern!(&AnEnum::Variant1(..))) -} - -#[test] -fn matches_enum_struct_field_with_multiple_variants_with_wildcard() -> Result<()> { - #[allow(dead_code)] - #[derive(Debug)] - enum AnEnum { - Variant1(i8), - Variant2, - } - let actual: AnEnum = AnEnum::Variant1(123); - - verify_that!(actual, matches_pattern!(&AnEnum::Variant1(_))) -} - -#[test] -fn matches_enum_with_field() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A(u32), - } - let actual = AnEnum::A(123); - - verify_that!(actual, matches_pattern!(&AnEnum::A(eq(123)))) -} -#[test] -fn does_not_match_wrong_enum_value() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - #[allow(unused)] - A(u32), - B, - } - let actual = AnEnum::B; - - verify_that!(actual, not(matches_pattern!(&AnEnum::A(eq(123))))) -} -#[test] -fn includes_enum_variant_in_description_with_field() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A(u32), - } - let actual = AnEnum::A(123); - - let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234)))); - - const EXPECTED: &str = "Expected: is & AnEnum :: A which has field `0`"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_enum_variant_in_negative_description_with_field() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A(u32), - } - let actual = AnEnum::A(123); - - let result = verify_that!(actual, not(matches_pattern!(&AnEnum::A(eq(123))))); - - const EXPECTED: &str = "Expected: is not & AnEnum :: A which has field `0`, which is equal to"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_enum_variant_in_description_with_two_fields() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A(u32, u32), - } - let actual = AnEnum::A(123, 234); - - let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234), eq(234)))); - - const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_enum_variant_in_description_with_three_fields() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A(u32, u32, u32), - } - let actual = AnEnum::A(123, 234, 345); - - let result = verify_that!(actual, matches_pattern!(&AnEnum::A(eq(234), eq(234), eq(345)))); - - const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_enum_variant_in_description_with_named_field() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A { field: u32 }, - } - let actual = AnEnum::A { field: 123 }; - - let result = verify_that!(actual, matches_pattern!(&AnEnum::A { field: eq(234) })); - - const EXPECTED: &str = "Expected: is & AnEnum :: A which has field `field`"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_enum_variant_in_description_with_two_named_fields() -> Result<()> { - #[derive(Debug)] - enum AnEnum { - A { field: u32, another_field: u32 }, - } - let actual = AnEnum::A { field: 123, another_field: 234 }; - - let result = verify_that!( - actual, - matches_pattern!(&AnEnum::A { field: eq(234), another_field: eq(234) }) - ); - - const EXPECTED: &str = "Expected: is & AnEnum :: A which has all the following properties"; - verify_that!(&result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_struct_name_in_description_with_property() -> Result<()> { - #[derive(Debug)] - struct AStruct { - field: u32, - } - impl AStruct { - fn get_field(&self) -> u32 { - self.field - } - } - let actual = AStruct { field: 123 }; - - let result = verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(234) })); - - const EXPECTED: &str = "Expected: is & AStruct which has property `get_field()`"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn includes_struct_name_in_description_with_ref_property() -> Result<()> { - #[derive(Debug)] - struct AStruct { - field: u32, - } - impl AStruct { - fn get_field(&self) -> &u32 { - &self.field - } - } - let actual = AStruct { field: 123 }; - - let result = verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(&234) })); - - const EXPECTED: &str = "Expected: is & AStruct which has property `get_field()`"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn includes_struct_name_in_description_with_property_after_field() -> Result<()> { - #[derive(Debug)] - struct AStruct { - field: u32, - } - impl AStruct { - fn get_field(&self) -> u32 { - self.field - } - } - let actual = AStruct { field: 123 }; - - let result = - verify_that!(actual, matches_pattern!(&AStruct { field: eq(123), get_field(): eq(234) })); - - const EXPECTED: &str = "Expected: is & AStruct which has all the following properties"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} - -#[test] -fn includes_struct_name_in_description_with_ref_property_after_field() -> Result<()> { - #[derive(Debug)] - struct AStruct { - field: u32, - } - impl AStruct { - fn get_field(&self) -> &u32 { - &self.field - } - } - let actual = AStruct { field: 123 }; - - let result = - verify_that!(actual, matches_pattern!(&AStruct { field: eq(123), get_field(): eq(&234) })); - - const EXPECTED: &str = "Expected: is & AStruct which has all the following properties"; - verify_that!(result, err(displays_as(contains_substring(EXPECTED)))) -} -#[test] -fn matches_struct_with_a_method() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field(&self) -> u32 { - self.a_field - } - } - - let actual = AStruct { a_field: 123 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(123) })) -} -#[test] -fn matches_struct_with_a_method_and_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field(&self) -> u32 { - self.a_field - } - } - - let actual = AStruct { a_field: 123 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field(): eq(123), })) -} -#[test] -fn matches_struct_with_a_method_taking_parameter() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn add_to_field(&self, a: u32) -> u32 { - self.a_field + a - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { add_to_field(2): eq(3) })) -} -#[test] -fn matches_struct_with_a_method_taking_two_parameters() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn add_product_to_field(&self, a: u32, b: u32) -> u32 { - self.a_field + a * b - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { add_product_to_field(2, 3): eq(7) })) -} -#[test] -fn matches_struct_with_a_method_taking_enum_value_parameter() -> Result<()> { - enum AnEnum { - AVariant, - } - - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_a_field(&self, _value: AnEnum) -> u32 { - self.a_field - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_a_field(AnEnum::AVariant): eq(1) })) -} -#[test] -fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn add_product_to_field(&self, a: u32, b: u32) -> u32 { - self.a_field + a * b - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { add_product_to_field(2, 3,): eq(7) })) -} -#[test] -fn matches_struct_with_a_method_returning_a_reference() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field_ref(&self) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 123 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(): eq(&123) })) -} -#[test] -fn matches_struct_with_a_method_returning_a_reference_with_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field_ref(&self) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 123 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(): eq(&123), })) -} -#[test] -fn matches_struct_with_a_method_taking_two_parameters_ret_ref() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(2, 3): eq(&1) })) -} -#[test] -fn matches_struct_with_a_method_returning_reference_taking_enum_value_parameter() -> Result<()> { - enum AnEnum { - AVariant, - } - - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field_ref(&self, _value: AnEnum) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(AnEnum::AVariant): eq(&1) })) -} -#[test] -fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 1 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_field_ref(2, 3,): eq(&1) })) -} -#[test] -fn matches_struct_with_a_method_followed_by_a_field() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn get_field(&self) -> u32 { - self.a_field - } - } - - let actual = AStruct { a_field: 123, another_field: 234 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { get_field(): eq(123), another_field: eq(234), .. }) - ) -} -#[test] -fn matches_struct_with_a_method_followed_by_a_field_with_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } - - impl AStruct { - fn get_value(&self) -> u32 { - 123 - } - } - - let actual = AStruct { a_field: 234 }; - - verify_that!(actual, matches_pattern!(&AStruct { get_value(): eq(123), a_field: eq(234), })) -} -#[test] -fn matches_struct_with_a_method_taking_two_parameters_and_field() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn add_product_to_field(&self, a: u32, b: u32) -> u32 { - self.a_field + a * b - } - } - - let actual = AStruct { a_field: 1, another_field: 123 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { add_product_to_field(2, 3): eq(7), another_field: eq(123), .. }) - ) -} -#[test] -fn matches_struct_with_a_method_taking_enum_value_parameter_followed_by_field() -> Result<()> { - enum AnEnum { - AVariant, - } - +fn matches_struct_with_a_method_followed_by_a_field() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, @@ -1295,248 +536,178 @@ fn matches_struct_with_a_method_taking_enum_value_parameter_followed_by_field() } impl AStruct { - fn get_field(&self, _value: AnEnum) -> u32 { + fn get_field(&self) -> u32 { self.a_field } } - - let actual = AStruct { a_field: 1, another_field: 2 }; + let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { get_field(AnEnum::AVariant): eq(1), another_field: eq(2), .. }) + matches_pattern!(&AStruct { get_field(): eq(123), another_field: eq(234), .. }) ) } + #[test] -fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_and_field() -> Result<()> -{ +fn matches_struct_with_a_method_followed_by_a_field_with_trailing_comma() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, - another_field: u32, } impl AStruct { - fn add_product_to_field(&self, a: u32, b: u32) -> u32 { - self.a_field + a * b + fn get_value(&self) -> u32 { + 123 } } + let actual = AStruct { a_field: 234 }; - let actual = AStruct { a_field: 1, another_field: 123 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { - add_product_to_field(2, 3,): eq(7), - another_field: eq(123), - .. - }) - ) + verify_that!(actual, matches_pattern!(&AStruct { get_value(): eq(123), a_field: eq(234), })) } + #[test] -fn matches_struct_with_a_method_returning_reference_followed_by_a_field() -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_with_params_trailing_comma_ret_ref_followed_by_a_field( +) -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, + a_third_field: u32, } impl AStruct { - fn get_field_ref(&self) -> &u32 { + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { &self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234 }; + let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; verify_that!( actual, - matches_pattern!(&AStruct { get_field_ref(): eq(&123), another_field: eq(234), .. }) + matches_pattern!(&AStruct { + another_field: eq(234), + get_field_ref(2, 3,): eq(&123), + a_third_field: eq(345), + .. + }) ) } + #[test] -fn matches_struct_with_a_method_returning_reference_followed_by_a_field_with_trailing_comma( -) -> Result<()> { +fn matches_struct_field_copy() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, } - - impl AStruct { - fn get_field_ref(&self) -> &u32 { - &self.a_field - } - } - let actual = AStruct { a_field: 123 }; - verify_that!( - actual, - matches_pattern!(&AStruct { get_field_ref(): eq(&123), a_field: eq(123), }) - ) + verify_that!(actual, matches_pattern!(&AStruct { a_field: eq(123) })) } + #[test] -fn matches_struct_with_a_method_taking_two_parameters_ret_ref_and_field() -> Result<()> { +fn matches_struct_field_non_copy() -> Result<()> { #[derive(Debug)] struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { - &self.a_field - } + a_field: String, } - - let actual = AStruct { a_field: 1, another_field: 123 }; + let actual = AStruct { a_field: "123".into() }; verify_that!( actual, - matches_pattern!(&AStruct { get_field_ref(2, 3): eq(&1), another_field: eq(123), .. }) + matches_pattern!(&AStruct { + a_field: ref eq("123"), + }) ) } -#[test] -fn matches_struct_with_a_method_taking_enum_value_param_ret_ref_followed_by_field() -> Result<()> { - enum AnEnum { - AVariant, - } - #[derive(Debug)] +#[test] +fn matches_copy_struct_field_copy() -> Result<()> { + #[derive(Debug, Clone, Copy)] struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn get_field_ref(&self, _value: AnEnum) -> &u32 { - &self.a_field - } + a_field: i32, } + let actual = AStruct { a_field: 123 }; - let actual = AStruct { a_field: 1, another_field: 2 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { - get_field_ref(AnEnum::AVariant): eq(&1), - another_field: eq(2), - .. - }) - ) + verify_that!(actual, matches_pattern!(AStruct { a_field: eq(123) })) } + #[test] -fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref_and_field( -) -> Result<()> { +fn matches_struct_property_copy() -> Result<()> { #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } + struct AStruct; impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { - &self.a_field + fn prop(&self) -> i32 { + 123 } } + let actual = AStruct; - let actual = AStruct { a_field: 1, another_field: 123 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { get_field_ref(2, 3,): eq(&1), another_field: eq(123), .. }) - ) + verify_that!(actual, matches_pattern!(&AStruct { prop(): eq(123) })) } + #[test] -fn matches_struct_with_a_field_followed_by_a_method() -> Result<()> { +fn matches_struct_property_non_copy() -> Result<()> { #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } + struct AStruct; impl AStruct { - fn get_field(&self) -> u32 { - self.a_field + fn prop(&self) -> String { + "123".into() } } + let actual = AStruct; - let actual = AStruct { a_field: 123, another_field: 234 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { another_field: eq(234), get_field(): eq(123), .. }) - ) + verify_that!(actual, matches_pattern!(&AStruct { prop(): ref eq("123") })) } + #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_trailing_comma() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - } +fn matches_copy_struct_property_copy() -> Result<()> { + #[derive(Debug, Clone, Copy)] + struct AStruct; impl AStruct { - fn get_value(&self) -> u32 { + fn prop(self) -> i32 { 123 } } + let actual = AStruct; - let actual = AStruct { a_field: 234 }; - - verify_that!(actual, matches_pattern!(&AStruct { a_field: eq(234), get_value(): eq(123), })) + verify_that!(actual, matches_pattern!(AStruct { prop(): eq(123) })) } + #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_params() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } +fn matches_copy_struct_property_non_copy() -> Result<()> { + #[derive(Debug, Clone, Copy)] + struct AStruct; impl AStruct { - fn add_product_to_field(&self, a: u32, b: u32) -> u32 { - self.a_field + a * b + fn prop(self) -> String { + "123".into() } } + let actual = AStruct; - let actual = AStruct { a_field: 1, another_field: 234 }; - - verify_that!( - actual, - matches_pattern!(&AStruct { another_field: eq(234), add_product_to_field(2, 3): eq(7), .. }) - ) + verify_that!(actual, matches_pattern!(AStruct { prop(): ref eq("123") })) } -#[test] -fn matches_struct_with_field_followed_by_method_taking_enum_value_param() -> Result<()> { - enum AnEnum { - AVariant, - } - #[derive(Debug)] +#[test] +fn matches_struct_auto_eq() -> Result<()> { + #[derive(Debug, Clone)] struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn get_field(&self, _value: AnEnum) -> u32 { - self.a_field - } + int: i32, + string: String, + option: Option, } - let actual = AStruct { a_field: 1, another_field: 2 }; - verify_that!( - actual, - matches_pattern!(&AStruct { - another_field: eq(2), - get_field(AnEnum::AVariant): eq(1), - .. - }) + AStruct { int: 123, string: "123".into(), option: Some(123) }, + matches_pattern!(&AStruct { int: 123, string: ref "123", option: Some(123) }) ) } + #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma() -> Result<()> { +fn matches_struct_with_a_method_taking_two_parameters_and_field() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, @@ -1548,41 +719,21 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com self.a_field + a * b } } - - let actual = AStruct { a_field: 1, another_field: 234 }; + let actual = AStruct { a_field: 1, another_field: 123 }; verify_that!( actual, - matches_pattern!(&AStruct { another_field: eq(234), add_product_to_field(2, 3,): eq(7), .. }) + matches_pattern!(&AStruct { add_product_to_field(2, 3): eq(7), another_field: eq(123), .. }) ) } -#[test] -fn matches_struct_with_a_field_followed_by_a_method_returning_reference() -> Result<()> { - #[derive(Debug)] - struct AStruct { - a_field: u32, - another_field: u32, - } - - impl AStruct { - fn get_field_ref(&self) -> &u32 { - &self.a_field - } - } - - let actual = AStruct { a_field: 123, another_field: 234 }; - verify_that!( - actual, - matches_pattern!(&AStruct { another_field: eq(234), get_field_ref(): eq(&123), .. }) - ) -} #[test] -fn matches_struct_with_a_field_followed_by_a_method_returning_ref_and_trailing_comma() -> Result<()> -{ +fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, + another_field: u32, + a_third_field: u32, } impl AStruct { @@ -1590,64 +741,75 @@ fn matches_struct_with_a_field_followed_by_a_method_returning_ref_and_trailing_c &self.a_field } } - - let actual = AStruct { a_field: 123 }; + let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; verify_that!( actual, - matches_pattern!(&AStruct { a_field: eq(123), get_field_ref(): eq(&123), }) + matches_pattern!(&AStruct { + another_field: eq(234), + get_field_ref(): eq(&123), + a_third_field: eq(345), + .. + }) ) } + #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref() -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_with_trailing_comma( +) -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, + a_third_field: u32, } impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { + fn get_field_ref(&self) -> &u32 { &self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234 }; + let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; verify_that!( actual, - matches_pattern!(&AStruct { another_field: eq(234), get_field_ref(2, 3): eq(&123), .. }) + matches_pattern!(&AStruct { + another_field: eq(234), + get_field_ref(): eq(&123), + a_third_field: eq(345), + .. + }) ) } -#[test] -fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref() -> Result<()> { - enum AnEnum { - AVariant, - } +#[test] +fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed_by_a_field( +) -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, + a_third_field: u32, } impl AStruct { - fn get_field_ref(&self, _value: AnEnum) -> &u32 { + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { &self.a_field } } - - let actual = AStruct { a_field: 1, another_field: 2 }; + let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; verify_that!( actual, matches_pattern!(&AStruct { - another_field: eq(2), - get_field_ref(AnEnum::AVariant): eq(&1), + another_field: eq(234), + get_field_ref(2, 3): eq(&123), + a_third_field: eq(345), .. }) ) } + #[test] fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma_ret_ref( ) -> Result<()> { @@ -1662,7 +824,6 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com &self.a_field } } - let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( @@ -1670,6 +831,7 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com matches_pattern!(&AStruct { another_field: eq(234), get_field_ref(2, 3,): eq(&123), .. }) ) } + #[test] fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field() -> Result<()> { #[derive(Debug)] @@ -1684,7 +846,6 @@ fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field() -> Res self.a_field } } - let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; verify_that!( @@ -1697,6 +858,7 @@ fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field() -> Res }) ) } + #[test] fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field_with_trailing_comma( ) -> Result<()> { @@ -1711,7 +873,6 @@ fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field_with_tra self.a_field } } - let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( @@ -1723,6 +884,7 @@ fn matches_struct_with_a_field_followed_by_a_method_followed_by_a_field_with_tra }) ) } + #[test] fn matches_struct_with_a_field_followed_by_a_method_with_params_followed_by_a_field() -> Result<()> { @@ -1738,7 +900,6 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_followed_by_a_fi self.a_field + a * b } } - let actual = AStruct { a_field: 1, another_field: 234, a_third_field: 345 }; verify_that!( @@ -1767,7 +928,6 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com self.a_field + a * b } } - let actual = AStruct { a_field: 1, another_field: 234, a_third_field: 345 }; verify_that!( @@ -1782,45 +942,32 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_com } #[test] -fn matches_struct_with_field_followed_by_method_taking_enum_value_param_followed_by_field( -) -> Result<()> { - enum AnEnum { - AVariant, - } - +fn matches_struct_with_a_field_followed_by_a_method_with_params_and_trailing_comma() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, - a_third_field: u32, } impl AStruct { - fn get_field(&self, _value: AnEnum) -> u32 { - self.a_field + fn add_product_to_field(&self, a: u32, b: u32) -> u32 { + self.a_field + a * b } } - - let actual = AStruct { a_field: 1, another_field: 2, a_third_field: 3 }; + let actual = AStruct { a_field: 1, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(2), - get_field(AnEnum::AVariant): eq(1), - a_third_field: eq(3), - .. - }) + matches_pattern!(&AStruct { another_field: eq(234), add_product_to_field(2, 3,): eq(7), .. }) ) } #[test] -fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field() -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_returning_reference() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, - a_third_field: u32, } impl AStruct { @@ -1828,28 +975,20 @@ fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field( &self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; + let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(234), - get_field_ref(): eq(&123), - a_third_field: eq(345), - .. - }) + matches_pattern!(&AStruct { another_field: eq(234), get_field_ref(): eq(&123), .. }) ) } #[test] -fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_with_trailing_comma( -) -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_returning_ref_and_trailing_comma() -> Result<()> +{ #[derive(Debug)] struct AStruct { a_field: u32, - another_field: u32, - a_third_field: u32, } impl AStruct { @@ -1857,28 +996,20 @@ fn matches_struct_with_a_field_followed_by_a_method_ret_ref_followed_by_a_field_ &self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; + let actual = AStruct { a_field: 123 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(234), - get_field_ref(): eq(&123), - a_third_field: eq(345), - .. - }) + matches_pattern!(&AStruct { a_field: eq(123), get_field_ref(): eq(&123), }) ) } #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed_by_a_field( -) -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, - a_third_field: u32, } impl AStruct { @@ -1886,197 +1017,180 @@ fn matches_struct_with_a_field_followed_by_a_method_with_params_ret_ref_followed &self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; + let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(234), - get_field_ref(2, 3): eq(&123), - a_third_field: eq(345), - .. - }) + matches_pattern!(&AStruct { another_field: eq(234), get_field_ref(2, 3): eq(&123), .. }) ) } #[test] -fn matches_struct_with_field_followed_by_method_taking_enum_value_param_ret_ref_followed_by_field( +fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_ret_ref_and_field( ) -> Result<()> { - enum AnEnum { - AVariant, - } - #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, - a_third_field: u32, } impl AStruct { - fn get_field_ref(&self, _value: AnEnum) -> &u32 { + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { &self.a_field } } - - let actual = AStruct { a_field: 1, another_field: 2, a_third_field: 3 }; + let actual = AStruct { a_field: 1, another_field: 123 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(2), - get_field_ref(AnEnum::AVariant): eq(&1), - a_third_field: eq(3), - .. - }) + matches_pattern!(&AStruct { get_field_ref(2, 3,): eq(&1), another_field: eq(123), .. }) ) } #[test] -fn matches_struct_with_a_field_followed_by_a_method_with_params_trailing_comma_ret_ref_followed_by_a_field( -) -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, another_field: u32, - a_third_field: u32, } impl AStruct { - fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { - &self.a_field + fn get_field(&self) -> u32 { + self.a_field } } - - let actual = AStruct { a_field: 123, another_field: 234, a_third_field: 345 }; + let actual = AStruct { a_field: 123, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { - another_field: eq(234), - get_field_ref(2, 3,): eq(&123), - a_third_field: eq(345), - .. - }) + matches_pattern!(&AStruct { another_field: eq(234), get_field(): eq(123), .. }) ) } #[test] -fn matches_struct_field_copy() -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_with_trailing_comma() -> Result<()> { #[derive(Debug)] struct AStruct { a_field: u32, } - let actual = AStruct { a_field: 123 }; + impl AStruct { + fn get_value(&self) -> u32 { + 123 + } + } + let actual = AStruct { a_field: 234 }; - verify_that!(actual, matches_pattern!(&AStruct { a_field: eq(123) })) + verify_that!(actual, matches_pattern!(&AStruct { a_field: eq(234), get_value(): eq(123), })) } #[test] -fn matches_struct_field_non_copy() -> Result<()> { +fn matches_struct_with_a_field_followed_by_a_method_with_params() -> Result<()> { #[derive(Debug)] struct AStruct { - a_field: String, + a_field: u32, + another_field: u32, } - let actual = AStruct { a_field: "123".into() }; + impl AStruct { + fn add_product_to_field(&self, a: u32, b: u32) -> u32 { + self.a_field + a * b + } + } + let actual = AStruct { a_field: 1, another_field: 234 }; verify_that!( actual, - matches_pattern!(&AStruct { - a_field: ref eq("123"), - }) + matches_pattern!(&AStruct { another_field: eq(234), add_product_to_field(2, 3): eq(7), .. }) ) } #[test] -fn matches_copy_struct_field_copy() -> Result<()> { - #[derive(Debug, Clone, Copy)] +fn matches_struct_with_a_method_taking_two_parameters_with_trailing_comma_and_field() -> Result<()> +{ + #[derive(Debug)] struct AStruct { - a_field: i32, + a_field: u32, + another_field: u32, } - let actual = AStruct { a_field: 123 }; - - verify_that!(actual, matches_pattern!(AStruct { a_field: eq(123) })) -} - -#[test] -fn matches_struct_property_copy() -> Result<()> { - #[derive(Debug)] - struct AStruct; - impl AStruct { - fn prop(&self) -> i32 { - 123 + fn add_product_to_field(&self, a: u32, b: u32) -> u32 { + self.a_field + a * b } } + let actual = AStruct { a_field: 1, another_field: 123 }; - let actual = AStruct; - - verify_that!(actual, matches_pattern!(&AStruct { prop(): eq(123) })) + verify_that!( + actual, + matches_pattern!(&AStruct { + add_product_to_field(2, 3,): eq(7), + another_field: eq(123), + .. + }) + ) } #[test] -fn matches_struct_property_non_copy() -> Result<()> { +fn matches_struct_with_a_method_returning_reference_followed_by_a_field() -> Result<()> { #[derive(Debug)] - struct AStruct; + struct AStruct { + a_field: u32, + another_field: u32, + } impl AStruct { - fn prop(&self) -> String { - "123".into() + fn get_field_ref(&self) -> &u32 { + &self.a_field } } + let actual = AStruct { a_field: 123, another_field: 234 }; - let actual = AStruct; - - verify_that!(actual, matches_pattern!(&AStruct { prop(): ref eq("123") })) + verify_that!( + actual, + matches_pattern!(&AStruct { get_field_ref(): eq(&123), another_field: eq(234), .. }) + ) } #[test] -fn matches_copy_struct_property_copy() -> Result<()> { - #[derive(Debug, Clone, Copy)] - struct AStruct; +fn matches_struct_with_a_method_returning_reference_followed_by_a_field_with_trailing_comma( +) -> Result<()> { + #[derive(Debug)] + struct AStruct { + a_field: u32, + } impl AStruct { - fn prop(self) -> i32 { - 123 + fn get_field_ref(&self) -> &u32 { + &self.a_field } } + let actual = AStruct { a_field: 123 }; - let actual = AStruct; - - verify_that!(actual, matches_pattern!(AStruct { prop(): eq(123) })) + verify_that!( + actual, + matches_pattern!(&AStruct { get_field_ref(): eq(&123), a_field: eq(123), }) + ) } #[test] -fn matches_copy_struct_property_non_copy() -> Result<()> { - #[derive(Debug, Clone, Copy)] - struct AStruct; +fn matches_struct_with_a_method_taking_two_parameters_ret_ref_and_field() -> Result<()> { + #[derive(Debug)] + struct AStruct { + a_field: u32, + another_field: u32, + } impl AStruct { - fn prop(self) -> String { - "123".into() + fn get_field_ref(&self, _a: u32, _b: u32) -> &u32 { + &self.a_field } } - let actual = AStruct; - - verify_that!(actual, matches_pattern!(AStruct { prop(): ref eq("123") })) -} - -#[test] -fn matches_struct_auto_eq() -> Result<()> { - #[derive(Debug, Clone)] - struct AStruct { - int: i32, - string: String, - option: Option, - } + let actual = AStruct { a_field: 1, another_field: 123 }; verify_that!( - AStruct { int: 123, string: "123".into(), option: Some(123) }, - matches_pattern!(&AStruct { int: 123, string: ref "123", option: Some(123) }) + actual, + matches_pattern!(&AStruct { get_field_ref(2, 3): eq(&1), another_field: eq(123), .. }) ) } diff --git a/googletest/tests/matches_pattern_tuple_struct_test.rs b/googletest/tests/matches_pattern_tuple_struct_test.rs new file mode 100644 index 00000000..25fe2b05 --- /dev/null +++ b/googletest/tests/matches_pattern_tuple_struct_test.rs @@ -0,0 +1,237 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use googletest::prelude::*; +use googletest::Result; + +#[test] +fn matches_tuple_struct_containing_single_field() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32); + let actual = AStruct(123); + + verify_that!(actual, matches_pattern!(&AStruct(eq(123)))) +} + +#[test] +fn matches_tuple_struct_containing_two_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32); + let actual = AStruct(123, 234); + + verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234)))) +} + +#[test] +fn matches_tuple_struct_containing_three_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32); + let actual = AStruct(123, 234, 345); + + verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345)))) +} + +#[test] +fn matches_tuple_struct_containing_four_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456); + + verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456)))) +} + +#[test] +fn matches_tuple_struct_containing_five_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567); + + verify_that!(actual, matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567)))) +} + +#[test] +fn matches_tuple_struct_containing_six_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678); + + verify_that!( + actual, + matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567), eq(678))) + ) +} + +#[test] +fn matches_tuple_struct_containing_seven_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678, 789); + + verify_that!( + actual, + matches_pattern!(&AStruct(eq(123), eq(234), eq(345), eq(456), eq(567), eq(678), eq(789))) + ) +} + +#[test] +fn matches_tuple_struct_containing_eight_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890); + + verify_that!( + actual, + matches_pattern!(&AStruct( + eq(123), + eq(234), + eq(345), + eq(456), + eq(567), + eq(678), + eq(789), + eq(890) + )) + ) +} + +#[test] +fn matches_tuple_struct_containing_nine_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901); + + verify_that!( + actual, + matches_pattern!(&AStruct( + eq(123), + eq(234), + eq(345), + eq(456), + eq(567), + eq(678), + eq(789), + eq(890), + eq(901) + )) + ) +} + +#[test] +fn matches_tuple_struct_containing_ten_fields() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901, 12); + + verify_that!( + actual, + matches_pattern!(&AStruct( + eq(123), + eq(234), + eq(345), + eq(456), + eq(567), + eq(678), + eq(789), + eq(890), + eq(901), + eq(12) + )) + ) +} + +#[test] +fn matches_tuple_struct_containing_ten_fields_by_ref() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32, u32, u32, u32, u32, u32, u32, u32, u32); + let actual = AStruct(123, 234, 345, 456, 567, 678, 789, 890, 901, 12); + + verify_that!( + actual, + matches_pattern!(&AStruct( + ref eq(&123), + ref eq(&234), + ref eq(&345), + ref eq(&456), + ref eq(&567), + ref eq(&678), + ref eq(&789), + ref eq(&890), + ref eq(&901), + ref eq(&12) + )) + ) +} + +#[test] +fn matches_tuple_struct_with_trailing_comma() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32); + let actual = AStruct(123); + + verify_that!( + actual, + matches_pattern!(&AStruct( + eq(123), // Keep the trailing comma, block reformatting + )) + ) +} + +#[test] +fn matches_tuple_struct_with_two_fields_and_trailing_comma() -> Result<()> { + #[derive(Debug)] + struct AStruct(u32, u32); + let actual = AStruct(123, 234); + + verify_that!( + actual, + matches_pattern!(&AStruct( + eq(123), + eq(234), // Keep the trailing comma, block reformatting + )) + ) +} + +#[test] +fn matches_tuple_struct_with_interleaved_underscore() -> Result<()> { + #[derive(Debug)] + struct NonCopy; + #[derive(Debug)] + struct AStruct(u32, NonCopy, u32); + let actual = AStruct(1, NonCopy, 3); + + verify_that!(actual, matches_pattern!(&AStruct(eq(1), _, eq(3))))?; + verify_that!(actual, matches_pattern!(AStruct(eq(&1), _, eq(&3)))) +} + +#[test] +fn matches_tuple_struct_non_exhaustive() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + struct AStruct(i32, u32); + let actual = AStruct(1, 3); + + verify_that!(actual, matches_pattern!(&AStruct(_, ..)))?; + verify_that!(actual, matches_pattern!(AStruct(_, ..))) +} + +#[test] +fn matches_generic_tuple_struct_exhaustively() -> Result<()> { + #[allow(dead_code)] + #[derive(Debug)] + struct AStruct(T, u32); + let actual = AStruct(1, 3); + + verify_that!(actual, matches_pattern!(&AStruct(_, _)))?; + verify_that!(actual, matches_pattern!(AStruct(_, _))) +}