From ce898183b83eb2cf86c8c6ceeeac16ef68fdc802 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 10:03:03 +0300 Subject: [PATCH] Simplify tests --- crates/libsyntax2/Cargo.toml | 2 +- crates/libsyntax2/tests/lexer.rs | 28 -------------- crates/libsyntax2/tests/parser.rs | 14 ------- .../{testutils/src/lib.rs => test/main.rs} | 38 +++++++++++++++++-- crates/libsyntax2/tests/testutils/Cargo.toml | 7 ---- 5 files changed, 36 insertions(+), 53 deletions(-) delete mode 100644 crates/libsyntax2/tests/lexer.rs delete mode 100644 crates/libsyntax2/tests/parser.rs rename crates/libsyntax2/tests/{testutils/src/lib.rs => test/main.rs} (76%) delete mode 100644 crates/libsyntax2/tests/testutils/Cargo.toml diff --git a/crates/libsyntax2/Cargo.toml b/crates/libsyntax2/Cargo.toml index f677355404..5a76ea82b3 100644 --- a/crates/libsyntax2/Cargo.toml +++ b/crates/libsyntax2/Cargo.toml @@ -12,4 +12,4 @@ drop_bomb = "0.1.4" parking_lot = "0.6.0" [dev-dependencies] -testutils = { path = "./tests/testutils" } +difference = "2.0.0" diff --git a/crates/libsyntax2/tests/lexer.rs b/crates/libsyntax2/tests/lexer.rs deleted file mode 100644 index 46ac9fedd4..0000000000 --- a/crates/libsyntax2/tests/lexer.rs +++ /dev/null @@ -1,28 +0,0 @@ -extern crate libsyntax2; -extern crate testutils; - -use std::fmt::Write; - -use libsyntax2::{tokenize, Token}; -use testutils::dir_tests; - -#[test] -fn lexer_tests() { - dir_tests(&["lexer"], |text| { - let tokens = tokenize(text); - dump_tokens(&tokens, text) - }) -} - -fn dump_tokens(tokens: &[Token], text: &str) -> String { - let mut acc = String::new(); - let mut offset = 0; - for token in tokens { - let len: u32 = token.len.into(); - let len = len as usize; - let token_text = &text[offset..offset + len]; - offset += len; - write!(acc, "{:?} {} {:?}\n", token.kind, token.len, token_text).unwrap() - } - acc -} diff --git a/crates/libsyntax2/tests/parser.rs b/crates/libsyntax2/tests/parser.rs deleted file mode 100644 index af2ae11bb3..0000000000 --- a/crates/libsyntax2/tests/parser.rs +++ /dev/null @@ -1,14 +0,0 @@ -extern crate libsyntax2; -extern crate testutils; - -use libsyntax2::parse; -use libsyntax2::utils::dump_tree; -use testutils::dir_tests; - -#[test] -fn parser_tests() { - dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { - let file = parse(text); - dump_tree(&file) - }) -} diff --git a/crates/libsyntax2/tests/testutils/src/lib.rs b/crates/libsyntax2/tests/test/main.rs similarity index 76% rename from crates/libsyntax2/tests/testutils/src/lib.rs rename to crates/libsyntax2/tests/test/main.rs index 39c8216612..18e5bc4d46 100644 --- a/crates/libsyntax2/tests/testutils/src/lib.rs +++ b/crates/libsyntax2/tests/test/main.rs @@ -1,12 +1,31 @@ +extern crate libsyntax2; extern crate difference; use std::{ fs, path::{Path, PathBuf}, + fmt::Write, }; use difference::Changeset; +#[test] +fn lexer_tests() { + dir_tests(&["lexer"], |text| { + let tokens = libsyntax2::tokenize(text); + dump_tokens(&tokens, text) + }) +} + +#[test] +fn parser_tests() { + dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| { + let file = libsyntax2::parse(text); + libsyntax2::utils::dump_tree(&file) + }) +} + + /// Read file and normalize newlines. /// /// `rustc` seems to always normalize `\r\n` newlines to `\n`: @@ -23,8 +42,8 @@ fn read_text(path: &Path) -> String { } pub fn dir_tests(paths: &[&str], f: F) -where - F: Fn(&str) -> String, + where + F: Fn(&str) -> String, { for path in collect_tests(paths) { let input_code = read_text(&path); @@ -107,5 +126,18 @@ fn project_dir() -> PathBuf { } fn test_data_dir() -> PathBuf { - project_dir().join("tests/data") + project_dir().join("crates/libsyntax2/tests/data") +} + +fn dump_tokens(tokens: &[libsyntax2::Token], text: &str) -> String { + let mut acc = String::new(); + let mut offset = 0; + for token in tokens { + let len: u32 = token.len.into(); + let len = len as usize; + let token_text = &text[offset..offset + len]; + offset += len; + write!(acc, "{:?} {} {:?}\n", token.kind, token.len, token_text).unwrap() + } + acc } diff --git a/crates/libsyntax2/tests/testutils/Cargo.toml b/crates/libsyntax2/tests/testutils/Cargo.toml deleted file mode 100644 index 53b20f17bc..0000000000 --- a/crates/libsyntax2/tests/testutils/Cargo.toml +++ /dev/null @@ -1,7 +0,0 @@ -[package] -name = "testutils" -version = "0.1.0" -authors = ["Aleksey Kladov "] - -[dependencies] -difference = "2.0.0"