File tree Expand file tree Collapse file tree 3 files changed +70
-5
lines changed Expand file tree Collapse file tree 3 files changed +70
-5
lines changed Original file line number Diff line number Diff line change @@ -183,10 +183,10 @@ pub mod internal {
183183 self . inner. explain_match( actual)
184184 )
185185 } else {
186- // TODO(hovinen): This message could be misinterpreted to mean that there were a
187- // typo in the field, when it actually means that the actual value uses the
188- // wrong enum variant. Reword this appropriately.
189- format ! ( "which has no field `{ }`" , self . field_path )
186+ let formatted_actual_value = format ! ( "{actual:?}" ) ;
187+ let without_fields = formatted_actual_value . split ( '(' ) . next ( ) . unwrap_or ( "" ) ;
188+ let without_fields = without_fields . split ( '{' ) . next ( ) . unwrap_or ( "" ) . trim_end ( ) ;
189+ format ! ( "which has the wrong enum variant `{without_fields }`" )
190190 }
191191 }
192192
Original file line number Diff line number Diff line change @@ -121,7 +121,49 @@ fn shows_correct_failure_message_for_wrong_enum_value() -> Result<()> {
121121
122122 let result = verify_that ! ( value, field!( AnEnum :: AValue . a, eq( 123 ) ) ) ;
123123
124- verify_that ! ( result, err( displays_as( contains_substring( "which has no field `a`" ) ) ) )
124+ verify_that ! (
125+ result,
126+ err( displays_as( contains_substring( "which has the wrong enum variant `AnotherValue`" ) ) )
127+ )
128+ }
129+
130+ #[ test]
131+ fn shows_correct_failure_message_for_wrong_enum_value_with_tuple_field ( ) -> Result < ( ) > {
132+ #[ derive( Debug ) ]
133+ enum AnEnum {
134+ #[ allow( dead_code) ] // This variant is intentionally unused.
135+ AValue ( u32 ) ,
136+ AnotherValue ( u32 ) ,
137+ }
138+ let value = AnEnum :: AnotherValue ( 123 ) ;
139+
140+ let result = verify_that ! ( value, field!( AnEnum :: AValue . 0 , eq( 123 ) ) ) ;
141+
142+ verify_that ! (
143+ result,
144+ err( displays_as( contains_substring( "which has the wrong enum variant `AnotherValue`" ) ) )
145+ )
146+ }
147+
148+ #[ test]
149+ fn shows_correct_failure_message_for_wrong_enum_value_with_named_field ( ) -> Result < ( ) > {
150+ #[ derive( Debug ) ]
151+ enum AnEnum {
152+ #[ allow( dead_code) ] // This variant is intentionally unused.
153+ AValue ( u32 ) ,
154+ AnotherValue {
155+ #[ allow( unused) ]
156+ a : u32 ,
157+ } ,
158+ }
159+ let value = AnEnum :: AnotherValue { a : 123 } ;
160+
161+ let result = verify_that ! ( value, field!( AnEnum :: AValue . 0 , eq( 123 ) ) ) ;
162+
163+ verify_that ! (
164+ result,
165+ err( displays_as( contains_substring( "which has the wrong enum variant `AnotherValue`" ) ) )
166+ )
125167}
126168
127169#[ test]
Original file line number Diff line number Diff line change @@ -188,6 +188,29 @@ fn has_correct_assertion_failure_message_for_field_and_property() -> Result<()>
188188 )
189189}
190190
191+ #[ test]
192+ fn has_meaningful_assertion_failure_message_when_wrong_enum_variant_is_used ( ) -> Result < ( ) > {
193+ #[ derive( Debug ) ]
194+ enum AnEnum {
195+ A ( u32 ) ,
196+ #[ allow( unused) ]
197+ B ( u32 ) ,
198+ }
199+ let actual = AnEnum :: A ( 123 ) ;
200+ let result = verify_that ! ( actual, matches_pattern!( AnEnum :: B ( eq( 123 ) ) ) ) ;
201+
202+ verify_that ! (
203+ result,
204+ err( displays_as( contains_substring( indoc! { "
205+ Value of: actual
206+ Expected: is AnEnum :: B which has field `0`, which is equal to 123
207+ Actual: A(123),
208+ which has the wrong enum variant `A`
209+ "
210+ } ) ) )
211+ )
212+ }
213+
191214#[ test]
192215fn supports_qualified_struct_names ( ) -> Result < ( ) > {
193216 mod a_module {
You can’t perform that action at this time.
0 commit comments