use std::cell::RefCell; use std::io::stdin; use std::path::PathBuf; use std::rc::Rc; use rlox2_frontend::lexer::TokenType; use rlox2_interpreter::{run, Runtime}; #[cfg(test)] pub fn run_test(path: impl Into) { use smol_str::SmolStr; let path = &path.into(); // path.insert_str(0, "./tests/lox/"); let source = std::fs::read_to_string(path).unwrap(); let tokens = rlox2_frontend::lexer::scan_tokens(&source).unwrap(); let comments: Result, SmolStr> = tokens.into_iter().try_fold(Vec::new(), |mut acc, token| { if let TokenType::Comment(s) = token.token_type { if s.contains("Error") || s.contains("error") { return Err(s); } else if s.starts_with(" expect: ") { acc.push(s.trim().strip_prefix("expect:").unwrap().trim().to_owned()); } } Ok(acc) }); match comments { Ok(ref comments) => { println!("Expecting positive result"); for comment in comments.iter() { println!("Comment: \"{comment}\"") } } Err(ref e) => { println!("Expecting error: {e}"); } } print!("\n\n"); let output: Vec = Vec::new(); let output = Rc::new(RefCell::new(output)); { let output = Rc::clone(&output); let mut runtime = Runtime::new(Rc::new(RefCell::new(stdin())), output); match run(&source, &mut runtime) { Ok(()) => (), Err(e) => { println!("{e}"); assert!(comments.is_err()); return; } } } let Ok(comments) = comments else { panic!("Positive outcome, but expected error"); }; let mut output = output.as_ref().borrow_mut(); let output = String::from_utf8(std::mem::take(&mut *output)).unwrap(); println!("output: len = {}\n---", output.len()); println!("{output}"); println!("---"); let lines: Vec = output .split_terminator('\n') .map(|s| s.to_owned()) .collect(); assert_eq!( lines.len(), comments.len(), "Didn't get as many outputs as expected" ); for (line, comment) in std::iter::zip(lines.into_iter(), comments.into_iter()) { assert_eq!(line, comment); } } // #[test] // fn run_all_tests() { // for lox_file in glob::glob("tests/lox/**/*.lox").unwrap() { // let lox_file = lox_file.unwrap(); // print!("\n\n\n"); // println!( // "================================================================================\n" // ); // println!("Running test for file {}\n", lox_file.display()); // run_test(lox_file); // } // }