2019-01-07 17:57:27 -08:00
|
|
|
use super::error::Result;
|
2019-01-08 21:03:51 -08:00
|
|
|
use super::util;
|
2019-01-07 17:57:27 -08:00
|
|
|
use ansi_term::Colour;
|
2019-01-07 22:01:40 -08:00
|
|
|
use difference::{Changeset, Difference};
|
2019-01-07 17:57:27 -08:00
|
|
|
use regex::bytes::{Regex as ByteRegex, RegexBuilder as ByteRegexBuilder};
|
|
|
|
|
use regex::Regex;
|
|
|
|
|
use std::char;
|
|
|
|
|
use std::fs;
|
2019-01-08 21:03:51 -08:00
|
|
|
use std::io::{self, Write};
|
2019-01-07 17:57:27 -08:00
|
|
|
use std::path::Path;
|
|
|
|
|
use std::str;
|
2019-01-08 21:03:51 -08:00
|
|
|
use tree_sitter::{Language, LogType, Parser};
|
2019-01-07 17:57:27 -08:00
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
|
static ref HEADER_REGEX: ByteRegex = ByteRegexBuilder::new(r"^===+\r?\n([^=]*)\r?\n===+\r?\n")
|
|
|
|
|
.multi_line(true)
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
2019-01-11 09:48:45 -08:00
|
|
|
static ref DIVIDER_REGEX: ByteRegex = ByteRegexBuilder::new(r"\r?\n---+\r?\n")
|
2019-01-07 17:57:27 -08:00
|
|
|
.multi_line(true)
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
static ref WHITESPACE_REGEX: Regex = Regex::new(r"\s+").unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum TestEntry {
|
|
|
|
|
Group {
|
|
|
|
|
name: String,
|
|
|
|
|
children: Vec<TestEntry>,
|
|
|
|
|
},
|
|
|
|
|
Example {
|
|
|
|
|
name: String,
|
|
|
|
|
input: Vec<u8>,
|
|
|
|
|
output: String,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-25 12:05:21 -08:00
|
|
|
impl Default for TestEntry {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
TestEntry::Group { name: String::new(), children: Vec::new() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 21:03:51 -08:00
|
|
|
pub fn run_tests_at_path(
|
|
|
|
|
language: Language,
|
|
|
|
|
path: &Path,
|
|
|
|
|
debug: bool,
|
|
|
|
|
debug_graph: bool,
|
|
|
|
|
filter: Option<&str>,
|
2019-01-27 09:53:49 -08:00
|
|
|
) -> Result<bool> {
|
2019-01-07 17:57:27 -08:00
|
|
|
let test_entry = parse_tests(path)?;
|
2019-01-17 12:40:21 -08:00
|
|
|
let mut _log_session = None;
|
2019-01-07 17:57:27 -08:00
|
|
|
let mut parser = Parser::new();
|
|
|
|
|
parser.set_language(language)?;
|
|
|
|
|
|
2019-01-08 21:03:51 -08:00
|
|
|
if debug_graph {
|
2019-01-17 12:40:21 -08:00
|
|
|
_log_session = Some(util::log_graphs(&mut parser, "log.html")?);
|
2019-01-08 21:03:51 -08:00
|
|
|
} else if debug {
|
|
|
|
|
parser.set_logger(Some(Box::new(|log_type, message| {
|
|
|
|
|
if log_type == LogType::Lex {
|
|
|
|
|
io::stderr().write(b" ").unwrap();
|
|
|
|
|
}
|
|
|
|
|
write!(&mut io::stderr(), "{}\n", message).unwrap();
|
|
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
let mut failures = Vec::new();
|
|
|
|
|
if let TestEntry::Group { children, .. } = test_entry {
|
|
|
|
|
for child in children {
|
2019-01-08 21:03:51 -08:00
|
|
|
run_tests(&mut parser, child, filter, 0, &mut failures)?;
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if failures.len() > 0 {
|
|
|
|
|
println!("");
|
|
|
|
|
|
|
|
|
|
if failures.len() == 1 {
|
|
|
|
|
println!("1 failure:")
|
|
|
|
|
} else {
|
|
|
|
|
println!("{} failures:", failures.len())
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 12:13:42 -08:00
|
|
|
print_diff_key();
|
2019-01-07 22:01:40 -08:00
|
|
|
for (i, (name, actual, expected)) in failures.iter().enumerate() {
|
|
|
|
|
println!("\n {}. {}:", i + 1, name);
|
2019-01-15 12:13:42 -08:00
|
|
|
print_diff(actual, expected);
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
2019-01-27 09:53:49 -08:00
|
|
|
Ok(true)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(false)
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 12:13:42 -08:00
|
|
|
pub fn print_diff_key() {
|
|
|
|
|
println!(
|
|
|
|
|
"\n{} / {}",
|
|
|
|
|
Colour::Green.paint("expected"),
|
|
|
|
|
Colour::Red.paint("actual")
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn print_diff(actual: &String, expected: &String) {
|
|
|
|
|
let changeset = Changeset::new(actual, expected, " ");
|
|
|
|
|
print!(" ");
|
|
|
|
|
for diff in &changeset.diffs {
|
|
|
|
|
match diff {
|
|
|
|
|
Difference::Same(part) => {
|
|
|
|
|
print!("{}{}", part, changeset.split);
|
|
|
|
|
}
|
|
|
|
|
Difference::Add(part) => {
|
|
|
|
|
print!("{}{}", Colour::Green.paint(part), changeset.split);
|
|
|
|
|
}
|
|
|
|
|
Difference::Rem(part) => {
|
|
|
|
|
print!("{}{}", Colour::Red.paint(part), changeset.split);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println!("");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 17:57:27 -08:00
|
|
|
fn run_tests(
|
|
|
|
|
parser: &mut Parser,
|
|
|
|
|
test_entry: TestEntry,
|
2019-01-08 21:03:51 -08:00
|
|
|
filter: Option<&str>,
|
2019-01-07 17:57:27 -08:00
|
|
|
mut indent_level: i32,
|
|
|
|
|
failures: &mut Vec<(String, String, String)>,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
match test_entry {
|
|
|
|
|
TestEntry::Example {
|
|
|
|
|
name,
|
|
|
|
|
input,
|
|
|
|
|
output,
|
|
|
|
|
} => {
|
2019-01-08 21:03:51 -08:00
|
|
|
if let Some(filter) = filter {
|
|
|
|
|
if !name.contains(filter) {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
let tree = parser
|
|
|
|
|
.parse_utf8(&mut |byte_offset, _| &input[byte_offset..], None)
|
|
|
|
|
.unwrap();
|
|
|
|
|
let actual = tree.root_node().to_sexp();
|
2019-01-08 21:03:51 -08:00
|
|
|
for _ in 0..indent_level {
|
|
|
|
|
print!(" ");
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
if actual == output {
|
|
|
|
|
println!("✓ {}", Colour::Green.paint(&name));
|
|
|
|
|
} else {
|
|
|
|
|
println!("✗ {}", Colour::Red.paint(&name));
|
|
|
|
|
failures.push((name, actual, output));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TestEntry::Group { name, children } => {
|
2019-01-08 21:03:51 -08:00
|
|
|
for _ in 0..indent_level {
|
|
|
|
|
print!(" ");
|
|
|
|
|
}
|
2019-01-07 17:57:27 -08:00
|
|
|
println!("{}:", name);
|
|
|
|
|
indent_level += 1;
|
|
|
|
|
for child in children {
|
2019-01-08 21:03:51 -08:00
|
|
|
run_tests(parser, child, filter, indent_level, failures)?;
|
2019-01-07 17:57:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse_tests(path: &Path) -> io::Result<TestEntry> {
|
|
|
|
|
let name = path
|
2019-01-25 12:05:21 -08:00
|
|
|
.file_stem()
|
2019-01-07 17:57:27 -08:00
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
|
.unwrap_or("")
|
|
|
|
|
.to_string();
|
|
|
|
|
if path.is_dir() {
|
|
|
|
|
let mut children = Vec::new();
|
|
|
|
|
for entry in fs::read_dir(path)? {
|
|
|
|
|
let entry = entry?;
|
|
|
|
|
children.push(parse_tests(&entry.path())?);
|
|
|
|
|
}
|
|
|
|
|
Ok(TestEntry::Group { name, children })
|
|
|
|
|
} else {
|
|
|
|
|
let content = fs::read_to_string(path)?;
|
|
|
|
|
Ok(parse_test_content(name, content))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_test_content(name: String, content: String) -> TestEntry {
|
|
|
|
|
let mut children = Vec::new();
|
|
|
|
|
let bytes = content.as_bytes();
|
|
|
|
|
let mut previous_name = String::new();
|
|
|
|
|
let mut previous_header_end = 0;
|
|
|
|
|
for header_match in HEADER_REGEX
|
|
|
|
|
.find_iter(&bytes)
|
|
|
|
|
.map(|m| (m.start(), m.end()))
|
|
|
|
|
.chain(Some((bytes.len(), bytes.len())))
|
|
|
|
|
{
|
|
|
|
|
let (header_start, header_end) = header_match;
|
|
|
|
|
if previous_header_end > 0 {
|
|
|
|
|
if let Some(divider_match) =
|
|
|
|
|
DIVIDER_REGEX.find(&bytes[previous_header_end..header_start])
|
|
|
|
|
{
|
|
|
|
|
let (divider_start, divider_end) = (
|
|
|
|
|
previous_header_end + divider_match.start(),
|
|
|
|
|
previous_header_end + divider_match.end(),
|
|
|
|
|
);
|
|
|
|
|
if let Ok(output) = str::from_utf8(&bytes[divider_end..header_start]) {
|
|
|
|
|
let input = bytes[previous_header_end..divider_start].to_vec();
|
|
|
|
|
let output = WHITESPACE_REGEX.replace_all(output.trim(), " ").to_string();
|
2019-01-17 12:40:21 -08:00
|
|
|
let output = output.replace(" )", ")");
|
2019-01-07 17:57:27 -08:00
|
|
|
children.push(TestEntry::Example {
|
|
|
|
|
name: previous_name,
|
|
|
|
|
input,
|
|
|
|
|
output,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
previous_name = String::from_utf8_lossy(&bytes[header_start..header_end])
|
|
|
|
|
.trim_matches(|c| char::is_whitespace(c) || c == '=')
|
|
|
|
|
.to_string();
|
|
|
|
|
previous_header_end = header_end;
|
|
|
|
|
}
|
|
|
|
|
TestEntry::Group { name, children }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_parse_test_content() {
|
|
|
|
|
let entry = parse_test_content(
|
|
|
|
|
"the-filename".to_string(),
|
|
|
|
|
r#"
|
|
|
|
|
===============
|
|
|
|
|
The first test
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
a b c
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
(a
|
|
|
|
|
(b c))
|
|
|
|
|
|
|
|
|
|
================
|
|
|
|
|
The second test
|
|
|
|
|
================
|
|
|
|
|
d
|
|
|
|
|
---
|
|
|
|
|
(d)
|
|
|
|
|
"#
|
|
|
|
|
.trim()
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
entry,
|
|
|
|
|
TestEntry::Group {
|
|
|
|
|
name: "the-filename".to_string(),
|
|
|
|
|
children: vec![
|
|
|
|
|
TestEntry::Example {
|
|
|
|
|
name: "The first test".to_string(),
|
2019-01-11 09:48:45 -08:00
|
|
|
input: "\na b c\n".as_bytes().to_vec(),
|
2019-01-07 17:57:27 -08:00
|
|
|
output: "(a (b c))".to_string(),
|
|
|
|
|
},
|
|
|
|
|
TestEntry::Example {
|
|
|
|
|
name: "The second test".to_string(),
|
2019-01-11 09:48:45 -08:00
|
|
|
input: "d".as_bytes().to_vec(),
|
2019-01-07 17:57:27 -08:00
|
|
|
output: "(d)".to_string(),
|
|
|
|
|
},
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|