use hadron_sclang::toolchain::{diagnostics, lexer, parser}; mod common; // compare the parsing results against sparkler-generated output. pub fn check(source: lexer::NamedBuffer, expect: &str) { let mut diags = diagnostics::NullDiagnosticConsumer {}; let tokens = lexer::TokenizedBuffer::tokenize(&source, &mut diags); let tree = parser::Tree::parse(&tokens, &mut diags); let mut actual = "\n".to_owned(); actual.push_str(&tree.nodes().iter().map(|n| format!("{:?}\n", n)).collect::()); assert_eq!(actual, expect); } #[test] fn parse_and_compare() { common::for_each_file("tests/sprklr/**/*.sc", |buffer| { let mut diags = diagnostics::NullDiagnosticConsumer {}; let tokens = lexer::TokenizedBuffer::tokenize(buffer, &mut diags); let tree = parser::Tree::parse(&tokens, &mut diags); let actual = tree.nodes().iter().map(|n| format!("{:?}\n", n)).collect::(); // Look up reference file, read in for comparison. let sprklr = std::path::Path::new(buffer.name).with_extension("txt"); let sprklr_meta = std::fs::metadata(&sprklr).unwrap(); let source_meta = std::fs::metadata(buffer.name).unwrap(); if sprklr_meta.modified().unwrap() < source_meta.modified().unwrap() { println!( "source file {} modified more recently than reference file {}", buffer.name, sprklr.display() ); panic!("Reference file newer than source file, re-run `cargo xtask sprklr`."); } println!("checking parse for {} against {}", buffer.name, sprklr.display()); let expect = std::fs::read_to_string(sprklr).unwrap(); pretty_assertions::assert_str_eq!(actual, expect); }); }