2019-01-17 17:16:04 -08:00
|
|
|
use super::fixtures::{fixtures_dir, get_language, get_test_language};
|
2019-01-11 13:30:45 -08:00
|
|
|
use crate::generate;
|
2019-01-15 12:13:42 -08:00
|
|
|
use crate::test::{parse_tests, print_diff, print_diff_key, TestEntry};
|
2019-01-14 17:19:46 -08:00
|
|
|
use crate::util;
|
2019-01-11 13:30:45 -08:00
|
|
|
use std::fs;
|
2019-01-15 10:27:39 -08:00
|
|
|
use tree_sitter::{LogType, Parser};
|
|
|
|
|
|
|
|
|
|
const LANGUAGES: &'static [&'static str] = &[
|
|
|
|
|
"bash",
|
|
|
|
|
"c",
|
|
|
|
|
"cpp",
|
|
|
|
|
"embedded-template",
|
|
|
|
|
"go",
|
|
|
|
|
"html",
|
|
|
|
|
"javascript",
|
2019-01-15 12:13:42 -08:00
|
|
|
"python",
|
2019-01-15 10:27:39 -08:00
|
|
|
];
|
2019-01-10 17:11:57 -08:00
|
|
|
|
|
|
|
|
lazy_static! {
|
2019-01-14 17:19:46 -08:00
|
|
|
static ref LANGUAGE_FILTER: Option<String> =
|
|
|
|
|
std::env::var("TREE_SITTER_TEST_LANGUAGE_FILTER").ok();
|
|
|
|
|
static ref EXAMPLE_FILTER: Option<String> =
|
|
|
|
|
std::env::var("TREE_SITTER_TEST_EXAMPLE_FILTER").ok();
|
|
|
|
|
static ref LOG_ENABLED: bool = std::env::var("TREE_SITTER_ENABLE_LOG").is_ok();
|
|
|
|
|
static ref LOG_GRAPH_ENABLED: bool = std::env::var("TREE_SITTER_ENABLE_LOG_GRAPHS").is_ok();
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-01-11 13:30:45 -08:00
|
|
|
fn test_real_language_corpus_files() {
|
2019-01-14 17:19:46 -08:00
|
|
|
let mut log_session = None;
|
2019-01-10 17:11:57 -08:00
|
|
|
let mut parser = Parser::new();
|
2019-01-15 10:27:39 -08:00
|
|
|
let grammars_dir = fixtures_dir().join("grammars");
|
2019-01-10 17:11:57 -08:00
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
if *LOG_ENABLED {
|
|
|
|
|
parser.set_logger(Some(Box::new(|log_type, msg| {
|
|
|
|
|
if log_type == LogType::Lex {
|
|
|
|
|
eprintln!(" {}", msg);
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("{}", msg);
|
|
|
|
|
}
|
|
|
|
|
})));
|
|
|
|
|
} else if *LOG_GRAPH_ENABLED {
|
|
|
|
|
log_session = Some(util::log_graphs(&mut parser, "log.html").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 12:13:42 -08:00
|
|
|
let mut did_fail = false;
|
2019-01-15 10:27:39 -08:00
|
|
|
for language_name in LANGUAGES.iter().cloned() {
|
2019-01-14 17:19:46 -08:00
|
|
|
if let Some(filter) = LANGUAGE_FILTER.as_ref() {
|
2019-01-15 12:13:42 -08:00
|
|
|
if language_name != filter.as_str() {
|
2019-01-14 17:19:46 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eprintln!("language: {:?}", language_name);
|
|
|
|
|
|
2019-01-15 10:27:39 -08:00
|
|
|
let language = get_language(language_name);
|
2019-01-14 17:19:46 -08:00
|
|
|
let corpus_dir = grammars_dir.join(language_name).join("corpus");
|
2019-01-10 17:11:57 -08:00
|
|
|
let test = parse_tests(&corpus_dir).unwrap();
|
|
|
|
|
parser.set_language(language).unwrap();
|
2019-01-15 12:13:42 -08:00
|
|
|
did_fail |= run_mutation_tests(&mut parser, test);
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
2019-01-14 17:19:46 -08:00
|
|
|
|
|
|
|
|
drop(parser);
|
|
|
|
|
drop(log_session);
|
2019-01-15 12:13:42 -08:00
|
|
|
|
|
|
|
|
if did_fail {
|
|
|
|
|
panic!("Corpus tests failed");
|
|
|
|
|
}
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
#[test]
|
|
|
|
|
fn test_feature_corpus_files() {
|
2019-01-14 17:19:46 -08:00
|
|
|
let mut log_session = None;
|
2019-01-11 13:30:45 -08:00
|
|
|
let mut parser = Parser::new();
|
2019-01-15 10:27:39 -08:00
|
|
|
let test_grammars_dir = fixtures_dir().join("test_grammars");
|
2019-01-11 13:30:45 -08:00
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
if *LOG_ENABLED {
|
|
|
|
|
parser.set_logger(Some(Box::new(|log_type, msg| {
|
|
|
|
|
if log_type == LogType::Lex {
|
|
|
|
|
eprintln!(" {}", msg);
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("{}", msg);
|
|
|
|
|
}
|
|
|
|
|
})));
|
|
|
|
|
} else if *LOG_GRAPH_ENABLED {
|
|
|
|
|
log_session = Some(util::log_graphs(&mut parser, "log.html").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 12:13:42 -08:00
|
|
|
let mut did_fail = false;
|
2019-01-11 13:30:45 -08:00
|
|
|
for entry in fs::read_dir(&test_grammars_dir).unwrap() {
|
|
|
|
|
let entry = entry.unwrap();
|
2019-01-11 17:26:45 -08:00
|
|
|
if !entry.metadata().unwrap().is_dir() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-01-14 17:19:46 -08:00
|
|
|
let language_name = entry.file_name();
|
|
|
|
|
let language_name = language_name.to_str().unwrap();
|
2019-01-11 13:30:45 -08:00
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
if let Some(filter) = LANGUAGE_FILTER.as_ref() {
|
2019-01-15 12:13:42 -08:00
|
|
|
if language_name != filter.as_str() {
|
2019-01-11 17:26:45 -08:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
eprintln!("test language: {:?}", language_name);
|
2019-01-11 17:26:45 -08:00
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
let test_path = entry.path();
|
|
|
|
|
let grammar_path = test_path.join("grammar.json");
|
|
|
|
|
let error_message_path = test_path.join("expected_error.txt");
|
|
|
|
|
let grammar_json = fs::read_to_string(grammar_path).unwrap();
|
|
|
|
|
let generate_result = generate::generate_parser_for_grammar(&grammar_json);
|
2019-01-11 17:26:45 -08:00
|
|
|
|
2019-01-11 13:30:45 -08:00
|
|
|
if error_message_path.exists() {
|
2019-01-11 17:26:45 -08:00
|
|
|
let expected_message = fs::read_to_string(&error_message_path).unwrap();
|
2019-01-11 13:30:45 -08:00
|
|
|
if let Err(e) = generate_result {
|
2019-01-11 17:26:45 -08:00
|
|
|
if e.0 != expected_message {
|
|
|
|
|
panic!(
|
|
|
|
|
"Unexpected error message.\n\nExpected:\n\n{}\nActual:\n\n{}\n",
|
|
|
|
|
expected_message, e.0
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-11 13:30:45 -08:00
|
|
|
} else {
|
|
|
|
|
panic!(
|
|
|
|
|
"Expected error message but got none for test grammar '{}'",
|
2019-01-14 17:19:46 -08:00
|
|
|
language_name
|
2019-01-11 13:30:45 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-01-11 17:26:45 -08:00
|
|
|
let corpus_path = test_path.join("corpus.txt");
|
2019-01-16 13:53:01 -08:00
|
|
|
let c_code = generate_result.unwrap().1;
|
2019-01-15 10:27:39 -08:00
|
|
|
let language = get_test_language(language_name, c_code, &test_path);
|
2019-01-11 17:26:45 -08:00
|
|
|
let test = parse_tests(&corpus_path).unwrap();
|
2019-01-14 17:19:46 -08:00
|
|
|
parser.set_language(language).unwrap();
|
2019-01-15 12:13:42 -08:00
|
|
|
did_fail |= run_mutation_tests(&mut parser, test);
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
drop(parser);
|
|
|
|
|
drop(log_session);
|
2019-01-15 12:13:42 -08:00
|
|
|
|
|
|
|
|
if did_fail {
|
|
|
|
|
panic!("Corpus tests failed");
|
|
|
|
|
}
|
2019-01-11 13:30:45 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-15 12:13:42 -08:00
|
|
|
fn run_mutation_tests(parser: &mut Parser, test: TestEntry) -> bool {
|
2019-01-10 17:11:57 -08:00
|
|
|
match test {
|
|
|
|
|
TestEntry::Example {
|
|
|
|
|
name,
|
|
|
|
|
input,
|
|
|
|
|
output,
|
|
|
|
|
} => {
|
2019-01-14 17:19:46 -08:00
|
|
|
if let Some(filter) = EXAMPLE_FILTER.as_ref() {
|
|
|
|
|
if !name.contains(filter.as_str()) {
|
2019-01-15 12:13:42 -08:00
|
|
|
return false;
|
2019-01-14 17:19:46 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eprintln!(" example: {:?}", name);
|
|
|
|
|
|
2019-01-10 17:11:57 -08:00
|
|
|
let tree = parser
|
|
|
|
|
.parse_utf8(&mut |byte_offset, _| &input[byte_offset..], None)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let actual = tree.root_node().to_sexp();
|
2019-01-15 12:13:42 -08:00
|
|
|
if actual != output {
|
|
|
|
|
print_diff_key();
|
|
|
|
|
print_diff(&actual, &output);
|
|
|
|
|
println!("");
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
2019-01-15 10:27:39 -08:00
|
|
|
TestEntry::Group { children, .. } => {
|
2019-01-15 12:13:42 -08:00
|
|
|
let mut result = false;
|
2019-01-10 17:11:57 -08:00
|
|
|
for child in children {
|
2019-01-15 12:13:42 -08:00
|
|
|
result |= run_mutation_tests(parser, child);
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
2019-01-15 12:13:42 -08:00
|
|
|
result
|
2019-01-10 17:11:57 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|