2024-09-01 19:15:55 +02:00
|
|
|
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};
|
|
|
|
|
|
2024-09-01 20:47:52 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
|
pub fn run_test(path: impl Into<PathBuf>) {
|
2024-09-01 19:15:55 +02:00
|
|
|
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: Vec<String> = tokens
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|token| {
|
|
|
|
|
if let TokenType::Comment(s) = token.token_type {
|
|
|
|
|
if s.starts_with(" expect: ") {
|
|
|
|
|
Some(s.strip_prefix(" expect: ").unwrap().trim().to_owned())
|
2024-09-01 20:47:52 +02:00
|
|
|
} else if s.contains("Error") || s.contains("error") {
|
2024-09-01 19:15:55 +02:00
|
|
|
Some(s.trim().into())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
for comment in comments.iter() {
|
|
|
|
|
println!("Comment: \"{comment}\"")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print!("\n\n");
|
|
|
|
|
|
|
|
|
|
let output: Vec<u8> = Vec::new();
|
|
|
|
|
let output = Rc::new(RefCell::new(output));
|
|
|
|
|
|
|
|
|
|
{
|
2024-09-01 23:16:00 +02:00
|
|
|
let output = Rc::clone(&output);
|
|
|
|
|
|
2024-09-01 19:15:55 +02:00
|
|
|
let mut runtime = Runtime::new(Rc::new(RefCell::new(stdin())), output);
|
|
|
|
|
|
|
|
|
|
match run(&source, &mut runtime) {
|
|
|
|
|
Ok(()) => (),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
println!("{e}");
|
|
|
|
|
|
|
|
|
|
assert_eq!(comments.len(), 1);
|
|
|
|
|
|
|
|
|
|
let comment = &*comments[0];
|
|
|
|
|
|
|
|
|
|
assert!(comment.to_lowercase().contains("error"));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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<String> = output
|
|
|
|
|
.split_terminator('\n')
|
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2024-09-01 23:53:04 +02:00
|
|
|
assert_eq!(
|
|
|
|
|
lines.len(),
|
|
|
|
|
comments.len(),
|
|
|
|
|
"Didn't get as many outputs as expected"
|
|
|
|
|
);
|
2024-09-01 19:15:55 +02:00
|
|
|
|
|
|
|
|
for (line, comment) in std::iter::zip(lines.into_iter(), comments.into_iter()) {
|
|
|
|
|
assert_eq!(line, comment);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-01 20:47:52 +02:00
|
|
|
// #[test]
|
|
|
|
|
// fn run_all_tests() {
|
|
|
|
|
// for lox_file in glob::glob("tests/lox/**/*.lox").unwrap() {
|
|
|
|
|
// let lox_file = lox_file.unwrap();
|
2024-09-01 19:15:55 +02:00
|
|
|
|
2024-09-01 20:47:52 +02:00
|
|
|
// print!("\n\n\n");
|
|
|
|
|
// println!(
|
|
|
|
|
// "================================================================================\n"
|
|
|
|
|
// );
|
|
|
|
|
// println!("Running test for file {}\n", lox_file.display());
|
2024-09-01 19:15:55 +02:00
|
|
|
|
2024-09-01 20:47:52 +02:00
|
|
|
// run_test(lox_file);
|
|
|
|
|
// }
|
|
|
|
|
// }
|