2019-01-15 10:27:39 -08:00
|
|
|
use crate::loader::Loader;
|
2019-02-01 14:39:37 -08:00
|
|
|
use lazy_static::lazy_static;
|
2019-01-17 17:16:04 -08:00
|
|
|
use std::fs;
|
2019-01-15 10:27:39 -08:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use tree_sitter::Language;
|
|
|
|
|
|
2019-02-01 14:39:37 -08:00
|
|
|
include!("./dirs.rs");
|
|
|
|
|
|
2019-01-15 10:27:39 -08:00
|
|
|
lazy_static! {
|
|
|
|
|
static ref TEST_LOADER: Loader = Loader::new(SCRATCH_DIR.clone());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn fixtures_dir<'a>() -> &'static Path {
|
|
|
|
|
&FIXTURES_DIR
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get_language(name: &str) -> Language {
|
|
|
|
|
TEST_LOADER
|
2019-02-13 19:30:59 -08:00
|
|
|
.load_language_at_path(&GRAMMARS_DIR.join(name).join("src"), &HEADER_DIR)
|
2019-01-15 10:27:39 -08:00
|
|
|
.unwrap()
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 14:44:06 -08:00
|
|
|
pub fn get_test_language(name: &str, parser_code: &str, path: Option<&Path>) -> Language {
|
2019-01-15 10:27:39 -08:00
|
|
|
let parser_c_path = SCRATCH_DIR.join(&format!("{}-parser.c", name));
|
|
|
|
|
if !fs::read_to_string(&parser_c_path)
|
|
|
|
|
.map(|content| content == parser_code)
|
|
|
|
|
.unwrap_or(false)
|
|
|
|
|
{
|
|
|
|
|
fs::write(&parser_c_path, parser_code).unwrap();
|
|
|
|
|
}
|
2019-02-04 14:44:06 -08:00
|
|
|
let scanner_path = path.and_then(|p| {
|
|
|
|
|
let result = p.join("scanner.c");
|
|
|
|
|
if result.exists() {
|
|
|
|
|
Some(result)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-01-15 10:27:39 -08:00
|
|
|
TEST_LOADER
|
2019-01-17 17:16:04 -08:00
|
|
|
.load_language_from_sources(name, &HEADER_DIR, &parser_c_path, &scanner_path)
|
2019-01-15 10:27:39 -08:00
|
|
|
.unwrap()
|
|
|
|
|
}
|