mirror of
				https://github.com/haraldh/chainerror.git
				synced 2025-10-31 14:41:37 +01:00 
			
		
		
		
	tests: add tests for annotated error display, debug, and chaining
Introduced tests to verify the behavior of `AnnotatedError` in display, debug, and error chaining scenarios. Ensured proper formatting for standalone errors and preservation of the error chain for wrapped errors.
This commit is contained in:
		
							parent
							
								
									d164662537
								
							
						
					
					
						commit
						75b7fdf363
					
				
					 1 changed files with 33 additions and 0 deletions
				
			
		
							
								
								
									
										33
									
								
								src/lib.rs
									
										
									
									
									
								
							
							
						
						
									
										33
									
								
								src/lib.rs
									
										
									
									
									
								
							|  | @ -874,10 +874,43 @@ mod tests { | ||||||
| 
 | 
 | ||||||
|         let err = TestError::from(TestErrorKind::Basic("test".into())); |         let err = TestError::from(TestErrorKind::Basic("test".into())); | ||||||
|         assert!(matches!(err.kind(), TestErrorKind::Basic(_))); |         assert!(matches!(err.kind(), TestErrorKind::Basic(_))); | ||||||
|  |         // The annotated error should display "(passed error)" even in a chain
 | ||||||
|  |         assert_eq!(format!("{}", err), "Basic error: test"); | ||||||
|  |         assert_eq!(format!("{:?}", err), "Basic(\"test\")"); | ||||||
| 
 | 
 | ||||||
|         let complex_err = TestError::from(TestErrorKind::Complex { |         let complex_err = TestError::from(TestErrorKind::Complex { | ||||||
|             message: "test".into(), |             message: "test".into(), | ||||||
|         }); |         }); | ||||||
|         assert!(matches!(complex_err.kind(), TestErrorKind::Complex { .. })); |         assert!(matches!(complex_err.kind(), TestErrorKind::Complex { .. })); | ||||||
|  |         // The annotated error should display "(passed error)" even in a chain
 | ||||||
|  |         assert_eq!(format!("{}", complex_err), "Complex error: test"); | ||||||
|  |         assert_eq!( | ||||||
|  |             format!("{:?}", complex_err), | ||||||
|  |             "Complex { message: \"test\" }" | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  |     #[test] | ||||||
|  |     fn test_annotated_error_display_and_debug() { | ||||||
|  |         let annotated = AnnotatedError(()); | ||||||
|  | 
 | ||||||
|  |         // Test Display formatting
 | ||||||
|  |         assert_eq!(format!("{}", annotated), "(passed error)"); | ||||||
|  | 
 | ||||||
|  |         // Test Debug formatting
 | ||||||
|  |         assert_eq!(format!("{:?}", annotated), "(passed error)"); | ||||||
|  | 
 | ||||||
|  |         // Test with error chain
 | ||||||
|  |         let io_error = io::Error::new(io::ErrorKind::NotFound, "file not found"); | ||||||
|  |         let err = Result::<(), _>::Err(io_error.into()) | ||||||
|  |             .annotate() | ||||||
|  |             .unwrap_err(); | ||||||
|  | 
 | ||||||
|  |         // The annotated error should display "(passed error)" even in a chain
 | ||||||
|  |         assert_eq!(format!("{}", err), "(passed error)"); | ||||||
|  |         assert!(format!("{:?}", err).contains("(passed error)")); | ||||||
|  | 
 | ||||||
|  |         // Verify the error chain is preserved
 | ||||||
|  |         assert!(err.source().is_some()); | ||||||
|  |         assert!(err.source().unwrap().is_chain::<io::Error>()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue