22// instead of using the macro which is designed for successful compilation tests
33
44use descend:: error:: CompileError ;
5- use std:: io:: ErrorKind ;
65
76#[ test]
8- fn test_fileio_error ( ) {
7+ fn fileio_error ( ) {
98 // Test compilation of a non-existent file which should fail with a FileIO error
10- let err = descend:: compile ( "examples/error-examples/nonexistent_file.desc" )
11- . err ( )
12- . unwrap ( ) ;
13-
14- // Also test the full miette diagnostic output
15- let miette_report = miette:: Report :: new ( err. clone ( ) ) ;
16-
17- // Use the graphical handler to get fancy output
18- let handler = miette:: GraphicalReportHandler :: new ( ) ;
19- let mut output = String :: new ( ) ;
20- handler
21- . render_report ( & mut output, miette_report. as_ref ( ) )
22- . unwrap ( ) ;
23-
24- insta:: assert_snapshot!( output) ;
25-
26- // Assert that compilation failed with a FileIO error
27- match err {
28- CompileError :: FileIO ( data) => {
29- assert_eq ! (
30- data. file_path,
31- "examples/error-examples/nonexistent_file.desc"
32- ) ;
33- assert_eq ! ( data. io_error_kind, ErrorKind :: NotFound ) ;
34- }
35- _ => panic ! ( "Expected FileIO error, got {:?}" , err) ,
36- }
9+ test_error_compilation (
10+ "examples/error-examples/nonexistent_file.desc" ,
11+ |err| matches ! ( err, CompileError :: FileIO ( _) ) ,
12+ "FileIO" ,
13+ ) ;
3714}
3815
3916#[ test]
40- fn test_missing_main_error ( ) {
41- // Test the missing main error directly by calling the type checker
42- use descend :: parser :: SourceCode ;
43- use descend :: ty_check :: error:: TyError ;
44-
45- // Parse the file first
46- let source = SourceCode :: from_file ( "examples/error-examples/missing_main.desc" ) . unwrap ( ) ;
47- let mut compil_unit = descend :: parser :: parse ( & source ) . unwrap ( ) ;
17+ fn missing_main_error ( ) {
18+ // Test compilation of a file without a main function which should fail with a MissingMain error
19+ test_error_compilation (
20+ "examples/ error-examples/missing_main.desc" ,
21+ |err| matches ! ( err , CompileError :: MissingMain ( _ ) ) ,
22+ "MissingMain" ,
23+ ) ;
24+ }
4825
49- // Call type checker directly to get the actual MissingMain error
50- let result = descend:: ty_check:: ty_check ( & mut compil_unit) ;
51- assert ! ( result. is_err( ) ) ; // Should fail with MissingMain error
26+ fn test_error_compilation < F > ( file_path : & str , error_checker : F , error_name : & str )
27+ where
28+ F : FnOnce ( & CompileError ) -> bool ,
29+ {
30+ // Test compilation of a file which should fail with the expected error
31+ let err = descend:: compile ( file_path) . err ( ) . unwrap ( ) ;
5232
53- // Test the actual MissingMain error diagnostic
54- let missing_main_error = TyError :: MissingMain ;
55- let miette_report = miette:: Report :: new ( missing_main_error) ;
33+ // Also test the full miette diagnostic output
34+ let miette_report = miette:: Report :: new ( err. clone ( ) ) ;
5635
5736 // Use the graphical handler to get fancy output
5837 let handler = miette:: GraphicalReportHandler :: new ( ) ;
@@ -61,5 +40,12 @@ fn test_missing_main_error() {
6140 . render_report ( & mut output, miette_report. as_ref ( ) )
6241 . unwrap ( ) ;
6342
64- insta:: assert_snapshot!( output) ;
43+ insta:: assert_snapshot!( error_name, output) ;
44+
45+ assert ! (
46+ error_checker( & err) ,
47+ "Expected {} error, got {:?}" ,
48+ error_name,
49+ err
50+ ) ;
6551}
0 commit comments