Start work on running test corpus tests

This commit is contained in:
Max Brunsfeld 2019-01-11 13:30:45 -08:00
parent 0d85a1ef53
commit e64f7a64a1
15 changed files with 328 additions and 168 deletions

View file

@ -1,6 +1,9 @@
use super::languages;
use crate::generate;
use crate::loader::Loader;
use crate::test::{parse_tests, TestEntry};
use std::path::PathBuf;
use std::fs;
use std::path::{Path, PathBuf};
use tree_sitter::{Language, Parser};
lazy_static! {
@ -12,20 +15,16 @@ lazy_static! {
("html", languages::html()),
("javascript", languages::javascript()),
];
static ref ROOT_DIR: PathBuf = [env!("CARGO_MANIFEST_DIR"), ".."].iter().collect();
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");
}
#[test]
fn test_corpus_files() {
fn test_real_language_corpus_files() {
let mut parser = Parser::new();
let grammars_dir: PathBuf = [
env!("CARGO_MANIFEST_DIR"),
"..",
"test",
"fixtures",
"grammars",
]
.iter()
.collect();
let grammars_dir = FIXTURES_DIR.join("grammars");
for (name, language) in LANGUAGES.iter().cloned() {
let corpus_dir = grammars_dir.join(name).join("corpus");
@ -35,6 +34,61 @@ fn test_corpus_files() {
}
}
#[test]
fn test_feature_corpus_files() {
fs::create_dir_all(SCRATCH_DIR.as_path()).unwrap();
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();
let test_name = entry.file_name();
let test_name = test_name.to_str().unwrap();
eprintln!("test name: {}", 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;
if let Err(e) = generate_result {
assert_eq!(e.0, fs::read_to_string(&error_message_path).unwrap());
} else {
panic!(
"Expected error message but got none for test grammar '{}'",
test_name
);
}
} else {
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();
let scanner_path = test_path.join("scanner.c");
let scanner_path = if scanner_path.exists() {
Some(scanner_path)
} else {
None
};
let language = loader
.load_language_from_sources(test_name, &HEADER_DIR, &parser_c_path, &scanner_path)
.unwrap();
}
}
// for (name, language) in LANGUAGES.iter().cloned() {
// let corpus_dir = grammars_dir.join(name).join("corpus");
// let test = parse_tests(&corpus_dir).unwrap();
// parser.set_language(language).unwrap();
// run_mutation_tests(&mut parser, test);
// }
}
fn run_mutation_tests(parser: &mut Parser, test: TestEntry) {
match test {
TestEntry::Example {

View file

@ -1,6 +1,6 @@
use super::languages::rust;
use std::thread;
use tree_sitter::{InputEdit, LogType, Parser, Point, PropertySheet, Range};
use tree_sitter::{InputEdit, LogType, Parser, Point, PropertySheet};
#[test]
fn test_basic_parsing() {