11//! Implementation of assertions for `Result` values.
22
33use crate :: assertions:: {
4- AssertHasError , AssertHasErrorMessage , AssertHasValue , AssertResult , AssertResultValue ,
4+ AssertBorrowedResultValue , AssertHasError , AssertHasErrorMessage , AssertHasValue , AssertResult ,
5+ AssertResultValue ,
56} ;
67use crate :: colored:: { mark_missing, mark_unexpected} ;
78use crate :: expectations:: { HasError , HasValue , IsEqualTo , IsErr , IsOk } ;
2728 }
2829}
2930
31+ impl < T , E , R > AssertResult for Spec < ' _ , & Result < T , E > , R >
32+ where
33+ T : Debug ,
34+ E : Debug ,
35+ R : FailingStrategy ,
36+ {
37+ fn is_ok ( self ) -> Self {
38+ self . expecting ( IsOk )
39+ }
40+
41+ fn is_err ( self ) -> Self {
42+ self . expecting ( IsErr )
43+ }
44+ }
45+
3046impl < ' a , T , E , R > AssertResultValue < ' a , T , E , R > for Spec < ' a , Result < T , E > , R >
3147where
3248 T : Debug ,
5167 }
5268}
5369
70+ impl < ' a , T , E , R > AssertBorrowedResultValue < ' a , T , E , R > for Spec < ' a , & ' a Result < T , E > , R >
71+ where
72+ T : Debug ,
73+ E : Debug ,
74+ {
75+ fn ok ( self ) -> Spec < ' a , & ' a T , R > {
76+ self . mapping ( |subject| match subject {
77+ Ok ( value) => value,
78+ Err ( error) => {
79+ panic ! ( "assertion failed: expected the subject to be `Ok(_)`, but was `Err({error:?})`" )
80+ } ,
81+ } )
82+ }
83+
84+ fn err ( self ) -> Spec < ' a , & ' a E , R > {
85+ self . mapping ( |subject| match subject {
86+ Ok ( value) => {
87+ panic ! ( "assertion failed: expected the subject to be `Err(_)`, but was `Ok({value:?})`" )
88+ } ,
89+ Err ( error) => error,
90+ } )
91+ }
92+ }
93+
5494impl < T , E , X , R > AssertHasValue < X > for Spec < ' _ , Result < T , E > , R >
5595where
5696 T : PartialEq < X > + Debug ,
@@ -63,6 +103,18 @@ where
63103 }
64104}
65105
106+ impl < T , E , X , R > AssertHasValue < X > for Spec < ' _ , & Result < T , E > , R >
107+ where
108+ T : PartialEq < X > + Debug ,
109+ E : Debug ,
110+ X : Debug ,
111+ R : FailingStrategy ,
112+ {
113+ fn has_value ( self , expected : X ) -> Self {
114+ self . expecting ( HasValue { expected } )
115+ }
116+ }
117+
66118impl < T , E , X , R > AssertHasError < X > for Spec < ' _ , Result < T , E > , R >
67119where
68120 T : Debug ,
@@ -75,6 +127,18 @@ where
75127 }
76128}
77129
130+ impl < T , E , X , R > AssertHasError < X > for Spec < ' _ , & Result < T , E > , R >
131+ where
132+ T : Debug ,
133+ E : PartialEq < X > + Debug ,
134+ X : Debug ,
135+ R : FailingStrategy ,
136+ {
137+ fn has_error ( self , expected : X ) -> Self {
138+ self . expecting ( HasError { expected } )
139+ }
140+ }
141+
78142impl < ' a , T , E , X , R > AssertHasErrorMessage < ' a , X , R > for Spec < ' a , Result < T , E > , R >
79143where
80144 T : Debug ,
@@ -95,6 +159,26 @@ where
95159 }
96160}
97161
162+ impl < ' a , T , E , X , R > AssertHasErrorMessage < ' a , X , R > for Spec < ' a , & Result < T , E > , R >
163+ where
164+ T : Debug ,
165+ E : Display ,
166+ X : Debug ,
167+ String : PartialEq < X > ,
168+ R : FailingStrategy ,
169+ {
170+ fn has_error_message ( self , expected : X ) -> Spec < ' a , String , R > {
171+ self . mapping ( |result| match result {
172+ Ok ( value) => panic ! (
173+ r"assertion failed: expected the subject to be `Err(_)` with message {expected:?}, but was `Ok({value:?})`"
174+ ) ,
175+ Err ( error) => {
176+ error. to_string ( )
177+ } ,
178+ } ) . expecting ( IsEqualTo { expected} )
179+ }
180+ }
181+
98182impl < T , E > Expectation < Result < T , E > > for IsOk
99183where
100184 T : Debug ,
@@ -143,6 +227,54 @@ where
143227 }
144228}
145229
230+ impl < T , E > Expectation < & Result < T , E > > for IsOk
231+ where
232+ T : Debug ,
233+ E : Debug ,
234+ {
235+ fn test ( & mut self , subject : & & Result < T , E > ) -> bool {
236+ subject. is_ok ( )
237+ }
238+
239+ fn message (
240+ & self ,
241+ expression : & Expression < ' _ > ,
242+ actual : & & Result < T , E > ,
243+ format : & DiffFormat ,
244+ ) -> String {
245+ let expected = Ok :: < _ , Unknown > ( Unknown ) ;
246+ let marked_actual = mark_unexpected ( actual, format) ;
247+ let marked_expected = mark_missing ( & expected, format) ;
248+ format ! (
249+ "expected {expression} is {expected:?}\n but was: {marked_actual}\n expected: {marked_expected}"
250+ )
251+ }
252+ }
253+
254+ impl < T , E > Expectation < & Result < T , E > > for IsErr
255+ where
256+ T : Debug ,
257+ E : Debug ,
258+ {
259+ fn test ( & mut self , subject : & & Result < T , E > ) -> bool {
260+ subject. is_err ( )
261+ }
262+
263+ fn message (
264+ & self ,
265+ expression : & Expression < ' _ > ,
266+ actual : & & Result < T , E > ,
267+ format : & DiffFormat ,
268+ ) -> String {
269+ let expected = Err :: < Unknown , Unknown > ( Unknown ) ;
270+ let marked_actual = mark_unexpected ( actual, format) ;
271+ let marked_expected = mark_missing ( & expected, format) ;
272+ format ! (
273+ "expected {expression} is {expected:?}\n but was: {marked_actual}\n expected: {marked_expected}"
274+ )
275+ }
276+ }
277+
146278impl < T , E , X > Expectation < Result < T , E > > for HasValue < X >
147279where
148280 T : PartialEq < X > + Debug ,
@@ -168,6 +300,31 @@ where
168300 }
169301}
170302
303+ impl < T , E , X > Expectation < & Result < T , E > > for HasValue < X >
304+ where
305+ T : PartialEq < X > + Debug ,
306+ E : Debug ,
307+ X : Debug ,
308+ {
309+ fn test ( & mut self , subject : & & Result < T , E > ) -> bool {
310+ subject. as_ref ( ) . is_ok_and ( |value| value == & self . expected )
311+ }
312+
313+ fn message (
314+ & self ,
315+ expression : & Expression < ' _ > ,
316+ actual : & & Result < T , E > ,
317+ format : & DiffFormat ,
318+ ) -> String {
319+ let expected = & self . expected ;
320+ let marked_actual = mark_unexpected ( actual, format) ;
321+ let marked_expected = mark_missing ( & Ok :: < _ , E > ( expected) , format) ;
322+ format ! (
323+ "expected {expression} is ok containing {expected:?}\n but was: {marked_actual}\n expected: {marked_expected}"
324+ )
325+ }
326+ }
327+
171328impl < T , E , X > Expectation < Result < T , E > > for HasError < X >
172329where
173330 T : Debug ,
@@ -193,5 +350,30 @@ where
193350 }
194351}
195352
353+ impl < T , E , X > Expectation < & Result < T , E > > for HasError < X >
354+ where
355+ T : Debug ,
356+ E : PartialEq < X > + Debug ,
357+ X : Debug ,
358+ {
359+ fn test ( & mut self , subject : & & Result < T , E > ) -> bool {
360+ subject. as_ref ( ) . is_err_and ( |err| err == & self . expected )
361+ }
362+
363+ fn message (
364+ & self ,
365+ expression : & Expression < ' _ > ,
366+ actual : & & Result < T , E > ,
367+ format : & DiffFormat ,
368+ ) -> String {
369+ let expected = & self . expected ;
370+ let marked_actual = mark_unexpected ( actual, format) ;
371+ let marked_expected = mark_missing ( & Err :: < T , _ > ( expected) , format) ;
372+ format ! (
373+ "expected {expression} is error containing {expected:?}\n but was: {marked_actual}\n expected: {marked_expected}"
374+ )
375+ }
376+ }
377+
196378#[ cfg( test) ]
197379mod tests;
0 commit comments