aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/data/parser/0000_empty.rs0
-rw-r--r--tests/data/parser/0000_empty.txt1
-rw-r--r--tests/parser.rs76
3 files changed, 77 insertions, 0 deletions
diff --git a/tests/data/parser/0000_empty.rs b/tests/data/parser/0000_empty.rs
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/data/parser/0000_empty.rs
diff --git a/tests/data/parser/0000_empty.txt b/tests/data/parser/0000_empty.txt
new file mode 100644
index 000000000..843ea118d
--- /dev/null
+++ b/tests/data/parser/0000_empty.txt
@@ -0,0 +1 @@
FILE@[0; 0) \ No newline at end of file
diff --git a/tests/parser.rs b/tests/parser.rs
new file mode 100644
index 000000000..f61b6830b
--- /dev/null
+++ b/tests/parser.rs
@@ -0,0 +1,76 @@
1extern crate file;
2#[macro_use(assert_diff)]
3extern crate difference;
4extern crate libsyntax2;
5
6use std::path::{PathBuf, Path};
7use std::fs::read_dir;
8use std::fmt::Write;
9
10use libsyntax2::{tokenize, Token, Node, File, FileBuilder};
11
12#[test]
13fn parser_tests() {
14 for test_case in parser_test_cases() {
15 parser_test_case(&test_case);
16 }
17}
18
19fn parser_test_dir() -> PathBuf {
20 let dir = env!("CARGO_MANIFEST_DIR");
21 PathBuf::from(dir).join("tests/data/parser")
22}
23
24fn parser_test_cases() -> Vec<PathBuf> {
25 let mut acc = Vec::new();
26 let dir = parser_test_dir();
27 for file in read_dir(&dir).unwrap() {
28 let file = file.unwrap();
29 let path = file.path();
30 if path.extension().unwrap_or_default() == "rs" {
31 acc.push(path);
32 }
33 }
34 acc.sort();
35 acc
36}
37
38fn parser_test_case(path: &Path) {
39 let actual = {
40 let text = file::get_text(path).unwrap();
41 let tokens = tokenize(&text);
42 let file = parse(text, &tokens);
43 dump_tree(&file)
44 };
45 let expected = file::get_text(&path.with_extension("txt")).unwrap();
46 let expected = expected.as_str();
47 let actual = actual.as_str();
48 if expected == actual {
49 return
50 }
51 if expected.trim() == actual.trim() {
52 panic!("Whitespace difference!")
53 }
54 assert_diff!(expected, actual, "\n", 0)
55}
56
57fn dump_tree(file: &File) -> String {
58 let mut result = String::new();
59 go(file.root(), &mut result, 0);
60 return result;
61
62 fn go(node: Node, buff: &mut String, level: usize) {
63 buff.push_str(&String::from(" ").repeat(level));
64 write!(buff, "{:?}\n", node);
65 for child in node.children() {
66 go(child, buff, level + 1)
67 }
68 }
69}
70
71fn parse(text: String, tokens: &[Token]) -> File {
72 let mut builder = FileBuilder::new(text);
73 builder.start_internal(libsyntax2::syntax_kinds::FILE);
74 builder.finish_internal();
75 builder.finish()
76} \ No newline at end of file