2017-12-31 20:27:36 +00:00
|
|
|
extern crate file;
|
|
|
|
#[macro_use(assert_diff)]
|
|
|
|
extern crate difference;
|
|
|
|
extern crate libsyntax2;
|
|
|
|
|
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
use std::fs::read_dir;
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
2017-12-31 20:34:29 +00:00
|
|
|
use libsyntax2::{tokenize, parse, Token, Node, File, FileBuilder};
|
2017-12-31 20:27:36 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_tests() {
|
|
|
|
for test_case in parser_test_cases() {
|
|
|
|
parser_test_case(&test_case);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parser_test_dir() -> PathBuf {
|
|
|
|
let dir = env!("CARGO_MANIFEST_DIR");
|
|
|
|
PathBuf::from(dir).join("tests/data/parser")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parser_test_cases() -> Vec<PathBuf> {
|
|
|
|
let mut acc = Vec::new();
|
|
|
|
let dir = parser_test_dir();
|
|
|
|
for file in read_dir(&dir).unwrap() {
|
|
|
|
let file = file.unwrap();
|
|
|
|
let path = file.path();
|
|
|
|
if path.extension().unwrap_or_default() == "rs" {
|
|
|
|
acc.push(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc.sort();
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parser_test_case(path: &Path) {
|
|
|
|
let actual = {
|
|
|
|
let text = file::get_text(path).unwrap();
|
|
|
|
let tokens = tokenize(&text);
|
|
|
|
let file = parse(text, &tokens);
|
|
|
|
dump_tree(&file)
|
|
|
|
};
|
|
|
|
let expected = file::get_text(&path.with_extension("txt")).unwrap();
|
|
|
|
let expected = expected.as_str();
|
|
|
|
let actual = actual.as_str();
|
|
|
|
if expected == actual {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if expected.trim() == actual.trim() {
|
2018-01-06 18:54:55 +00:00
|
|
|
panic!("Whitespace difference! {}", path.display())
|
2017-12-31 20:27:36 +00:00
|
|
|
}
|
|
|
|
assert_diff!(expected, actual, "\n", 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dump_tree(file: &File) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
go(file.root(), &mut result, 0);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
fn go(node: Node, buff: &mut String, level: usize) {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
2018-01-06 14:16:00 +00:00
|
|
|
write!(buff, "{:?}\n", node)
|
|
|
|
.unwrap();
|
2017-12-31 20:27:36 +00:00
|
|
|
for child in node.children() {
|
|
|
|
go(child, buff, level + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|