2017-12-31 14:27:36 -06:00
|
|
|
extern crate file;
|
|
|
|
extern crate libsyntax2;
|
2018-01-07 05:56:08 -06:00
|
|
|
extern crate testutils;
|
2017-12-31 14:27:36 -06:00
|
|
|
|
2018-01-07 05:56:08 -06:00
|
|
|
use std::path::{Path};
|
2017-12-31 14:27:36 -06:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
2018-01-07 04:09:13 -06:00
|
|
|
use libsyntax2::{tokenize, parse, Node, File};
|
2018-01-07 05:56:08 -06:00
|
|
|
use testutils::{collect_tests, assert_equal_text};
|
2017-12-31 14:27:36 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parser_tests() {
|
2018-01-07 05:56:08 -06:00
|
|
|
for test_case in collect_tests(&["parser/ok", "parser/err"]) {
|
2017-12-31 14:27:36 -06:00
|
|
|
parser_test_case(&test_case);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
};
|
2018-01-07 05:56:08 -06:00
|
|
|
let expected_path = path.with_extension("txt");
|
|
|
|
let expected = file::get_text(&expected_path).expect(
|
|
|
|
&format!("Can't read {}", expected_path.display())
|
2018-01-07 03:32:29 -06:00
|
|
|
);
|
2017-12-31 14:27:36 -06:00
|
|
|
let expected = expected.as_str();
|
|
|
|
let actual = actual.as_str();
|
2018-01-07 05:56:08 -06:00
|
|
|
assert_equal_text(expected, actual, &expected_path);
|
2017-12-31 14:27:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07 04:09:13 -06:00
|
|
|
write!(buff, "{:?}\n", node).unwrap();
|
|
|
|
let my_errors = node.errors().filter(|e| e.after_child().is_none());
|
|
|
|
let parent_errors = node.parent().into_iter()
|
|
|
|
.flat_map(|n| n.errors())
|
|
|
|
.filter(|e| e.after_child() == Some(node));
|
|
|
|
|
|
|
|
for err in my_errors {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
|
|
|
write!(buff, "err: `{}`\n", err.message()).unwrap();
|
2018-01-07 03:13:01 -06:00
|
|
|
}
|
|
|
|
|
2017-12-31 14:27:36 -06:00
|
|
|
for child in node.children() {
|
|
|
|
go(child, buff, level + 1)
|
|
|
|
}
|
2018-01-07 04:09:13 -06:00
|
|
|
|
|
|
|
for err in parent_errors {
|
|
|
|
buff.push_str(&String::from(" ").repeat(level));
|
|
|
|
write!(buff, "err: `{}`\n", err.message()).unwrap();
|
|
|
|
}
|
2017-12-31 14:27:36 -06:00
|
|
|
}
|
|
|
|
}
|