Fix parser generation error messages

This commit is contained in:
Max Brunsfeld 2019-01-11 17:26:45 -08:00
parent 0236de7963
commit 6592fdd24c
16 changed files with 252 additions and 164 deletions

View file

@ -3,7 +3,7 @@ use crate::generate;
use crate::loader::Loader;
use crate::test::{parse_tests, TestEntry};
use std::fs;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use tree_sitter::{Language, Parser};
lazy_static! {
@ -19,6 +19,7 @@ lazy_static! {
static ref HEADER_DIR: PathBuf = ROOT_DIR.join("lib").join("include");
static ref SCRATCH_DIR: PathBuf = ROOT_DIR.join("target").join("scratch");
static ref FIXTURES_DIR: PathBuf = ROOT_DIR.join("test").join("fixtures");
static ref EXEC_PATH: PathBuf = std::env::current_exe().unwrap();
}
#[test]
@ -38,27 +39,42 @@ fn test_real_language_corpus_files() {
fn test_feature_corpus_files() {
fs::create_dir_all(SCRATCH_DIR.as_path()).unwrap();
let filter = std::env::var("TREE_SITTER_TEST_FILTER").ok();
let mut loader = Loader::new(SCRATCH_DIR.clone());
let mut parser = Parser::new();
let test_grammars_dir = FIXTURES_DIR.join("test_grammars");
for entry in fs::read_dir(&test_grammars_dir).unwrap() {
let entry = entry.unwrap();
if !entry.metadata().unwrap().is_dir() {
continue;
}
let test_name = entry.file_name();
let test_name = test_name.to_str().unwrap();
eprintln!("test name: {}", test_name);
if let Some(filter) = filter.as_ref() {
if !test_name.contains(filter.as_str()) {
continue;
}
}
eprintln!("test: {:?}", test_name);
let test_path = entry.path();
let grammar_path = test_path.join("grammar.json");
let corpus_path = test_path.join("corpus.txt");
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);
if error_message_path.exists() {
continue;
let expected_message = fs::read_to_string(&error_message_path).unwrap();
if let Err(e) = generate_result {
assert_eq!(e.0, fs::read_to_string(&error_message_path).unwrap());
if e.0 != expected_message {
panic!(
"Unexpected error message.\n\nExpected:\n\n{}\nActual:\n\n{}\n",
expected_message, e.0
);
}
} else {
panic!(
"Expected error message but got none for test grammar '{}'",
@ -66,9 +82,15 @@ fn test_feature_corpus_files() {
);
}
} else {
let corpus_path = test_path.join("corpus.txt");
let c_code = generate_result.unwrap();
let parser_c_path = SCRATCH_DIR.join(&format!("{}-parser.c", test_name));
fs::write(&parser_c_path, c_code).unwrap();
if !fs::read_to_string(&parser_c_path)
.map(|content| content == c_code)
.unwrap_or(false)
{
fs::write(&parser_c_path, c_code).unwrap();
}
let scanner_path = test_path.join("scanner.c");
let scanner_path = if scanner_path.exists() {
Some(scanner_path)
@ -78,6 +100,7 @@ fn test_feature_corpus_files() {
let language = loader
.load_language_from_sources(test_name, &HEADER_DIR, &parser_c_path, &scanner_path)
.unwrap();
let test = parse_tests(&corpus_path).unwrap();
}
}