11use ndc_lib:: interpreter:: Interpreter ;
22use owo_colors:: OwoColorize ;
33use std:: fs;
4- use std:: path:: { Path , PathBuf } ;
4+ use std:: path:: PathBuf ;
55
6- #[ test]
7- fn example_programs ( ) {
8- run_dir ( Path :: new ( "../tests/programs/" ) ) ;
9- }
10-
11- fn run_dir < P : AsRef < Path > > ( dir : P ) {
12- match fs:: read_dir ( dir) {
13- Ok ( files) => {
14- // Sort the files in to ensure the tests are executed in the intended order
15- let mut files = files. collect :: < Vec < _ > > ( ) ;
16- assert_ne ! ( files. len( ) , 0 , "no files were found at all" ) ;
17- files. sort_unstable_by_key ( |r| r. as_ref ( ) . map ( |e| e. path ( ) ) . ok ( ) ) ;
18-
19- for file in files {
20- match file {
21- Ok ( file) if file. path ( ) . extension ( ) == Some ( "ndct" . as_ref ( ) ) => {
22- run_test ( file. path ( ) ) . expect ( "something went wrong while running the test" )
23- }
24- Ok ( file) if file. path ( ) . is_dir ( ) => {
25- run_dir ( file. path ( ) ) ;
26- }
27- _ => panic ! ( "invalid test file: {file:?}" ) ,
28- }
29- }
30- }
31- Err ( err) => panic ! ( "Error reading test programs {err}" ) ,
32- }
33- }
6+ include ! ( concat!( env!( "OUT_DIR" ) , "/generated_tests.rs" ) ) ;
347
358fn run_test ( path : PathBuf ) -> Result < ( ) , std:: io:: Error > {
36- let contents = fs:: read_to_string ( path. clone ( ) ) ?;
9+ let contents = fs:: read_to_string ( & path) ?;
10+
3711 enum Mode {
3812 None ,
3913 Program ,
@@ -48,15 +22,9 @@ fn run_test(path: PathBuf) -> Result<(), std::io::Error> {
4822
4923 for line in contents. split_inclusive ( '\n' ) {
5024 match line {
51- "--PROGRAM--\n " => {
52- mode = Mode :: Program ;
53- }
54- "--EXPECT--\n " => {
55- mode = Mode :: Expect ;
56- }
57- "--EXPECT-ERROR--\n " => {
58- mode = Mode :: ExpectError ;
59- }
25+ "--PROGRAM--\n " => mode = Mode :: Program ,
26+ "--EXPECT--\n " => mode = Mode :: Expect ,
27+ "--EXPECT-ERROR--\n " => mode = Mode :: ExpectError ,
6028 _ => match mode {
6129 Mode :: None => panic ! ( "unexpected line in file, not in mode" ) ,
6230 Mode :: Program => program. push_str ( line) ,
@@ -65,22 +33,20 @@ fn run_test(path: PathBuf) -> Result<(), std::io::Error> {
6533 } ,
6634 }
6735 }
68- // For now let's trim end both result and expect to ensure that any trailing line breaks don't cause issues
36+
6937 print ! ( "Running {path:?}..." ) ;
7038
7139 let mut interpreter = Interpreter :: new ( Vec :: new ( ) ) ;
7240 let interpreter_result = interpreter. run_str ( & program, false ) ;
7341
7442 let program_had_error = interpreter_result. is_err ( ) ;
75-
7643 let actual_error = interpreter_result. unwrap_or_else ( |err| format ! ( "{err:?}" ) ) ;
7744
7845 let environment = interpreter. environment ( ) ;
7946 let environment = environment. borrow ( ) ;
8047 let output = environment
8148 . get_output ( )
8249 . expect ( "interpreter must have output in test context" ) ;
83-
8450 let output = String :: from_utf8 ( output) . expect ( "test output must be valid UTF-8" ) ;
8551
8652 assert ! (
0 commit comments