From 45fce4b3ef53b0ffc78aac7064701f1f31792a5a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 29 Dec 2017 00:27:56 +0300 Subject: Lexer testing infra --- Cargo.toml | 4 ++- tests/data/lexer/0001_hello.rs | 1 + tests/data/lexer/0001_hello.txt | 3 +++ tests/lexer.rs | 55 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/data/lexer/0001_hello.rs create mode 100644 tests/data/lexer/0001_hello.txt create mode 100644 tests/lexer.rs diff --git a/Cargo.toml b/Cargo.toml index c94b99fad..b16ca4c7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,6 @@ name = "libsyntax2" version = "0.1.0" authors = ["Aleksey Kladov "] -[dependencies] +[dev-dependencies] +file = "1.1.1" +difference = "1.0.0" diff --git a/tests/data/lexer/0001_hello.rs b/tests/data/lexer/0001_hello.rs new file mode 100644 index 000000000..95d09f2b1 --- /dev/null +++ b/tests/data/lexer/0001_hello.rs @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/tests/data/lexer/0001_hello.txt b/tests/data/lexer/0001_hello.txt new file mode 100644 index 000000000..5bec9be80 --- /dev/null +++ b/tests/data/lexer/0001_hello.txt @@ -0,0 +1,3 @@ +IDENT 5 +WHITESPACE 1 +IDENT 5 \ No newline at end of file diff --git a/tests/lexer.rs b/tests/lexer.rs new file mode 100644 index 000000000..de76f0a15 --- /dev/null +++ b/tests/lexer.rs @@ -0,0 +1,55 @@ +extern crate file; +#[macro_use(assert_diff)] +extern crate difference; + +use std::path::{PathBuf, Path}; +use std::fs::read_dir; + +#[test] +fn lexer_tests() { + for test_case in lexer_test_cases() { + lexer_test_case(&test_case); + } +} + +fn lexer_test_dir() -> PathBuf { + let dir = env!("CARGO_MANIFEST_DIR"); + PathBuf::from(dir).join("tests/data/lexer") +} + +fn lexer_test_cases() -> Vec { + let mut acc = Vec::new(); + let dir = lexer_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 +} + +fn lexer_test_case(path: &Path) { + let actual = { + let text = file::get_text(path).unwrap(); + let tokens = tokenize(&text); + dump_tokens(&tokens) + }; + let expected = file::get_text(&path.with_extension("txt")).unwrap(); + + assert_diff!( + expected.as_str(), + actual.as_str(), + "\n", + 0 + ) +} + +fn tokenize(text: &str) -> Vec<()> { + Vec::new() +} + +fn dump_tokens(tokens: &[()]) -> String { + "IDENT 5\nKEYWORD 1\nIDENT 5\n".to_string() +} \ No newline at end of file -- cgit v1.2.3