<commit_before>extern crate file;
extern crate tempdir;
extern crate difference;

use std::process;
use std::env;
use std::path::{PathBuf, Path};

use tempdir::TempDir;

fn generator_path() -> PathBuf {
    let test_exe = env::current_exe().unwrap();
    test_exe.parent().unwrap().parent().unwrap().join("gen")
}

fn check_by_path<T: AsRef<Path>>(grammar: T) {
    let grammar = grammar.as_ref();
    let input = file::get_text(grammar).unwrap();
    let expected = file::get_text(grammar.with_extension("rs")).unwrap();
    do_test(&input, &expected);
}

fn do_test(grammar: &str, expected: &str) {
    let dir = TempDir::new("gen-tests").unwrap();
    let grammar_path = dir.path().join("weird.fall");
    file::put_text(&grammar_path, grammar).unwrap();

    let output = process::Command::new(generator_path())
        .arg(&grammar_path)
        .output()
        .expect("Failed to execute process");

    if !output.status.success() {
        panic!("Generator exited with code {:?}\n----\n{}\n----\n",
               output.status.code(), std::str::from_utf8(&output.stderr).unwrap())
    }

    let actual = file::get_text(grammar_path.with_extension("rs")).unwrap();

    let expected = expected.trim();
    let actual = actual.trim();
    if expected != actual {
        let difference = difference::Changeset::new(expected, actual, "\n");
        println!("{}", difference);
        panic!("Mismatch!")
    }
}

#[test]
fn test_grammars_are_fresh() {
    check_by_path("../fall_test/src/sexp.fall");
    check_by_path("../fall_test/src/rust.fall");
    check_by_path("../fall_test/src/weird.fall");
    check_by_path("../fall_test/src/json.fall");
    check_by_path("./src/syntax.fall");
}
<commit_msg>Better test reporting<commit_after>extern crate file;
extern crate tempdir;
extern crate difference;

use std::process;
use std::env;
use std::path::{PathBuf, Path};

use tempdir::TempDir;

fn generator_path() -> PathBuf {
    let test_exe = env::current_exe().unwrap();
    test_exe.parent().unwrap().parent().unwrap().join("gen")
}

fn check_by_path<T: AsRef<Path>>(grammar: T) {
    let grammar = grammar.as_ref();
    let file_name = grammar.display().to_string();
    let input = file::get_text(grammar).unwrap();
    let expected = file::get_text(grammar.with_extension("rs")).unwrap();
    do_test(&input, &expected, &file_name);
}

fn do_test(grammar: &str, expected: &str, file_name: &str) {
    let dir = TempDir::new("gen-tests").unwrap();
    let grammar_path = dir.path().join("weird.fall");
    file::put_text(&grammar_path, grammar).unwrap();

    let output = process::Command::new(generator_path())
        .arg(&grammar_path)
        .output()
        .expect("Failed to execute process");

    if !output.status.success() {
        panic!("Generator exited with code {:?}\n----\n{}\n----\n",
               output.status.code(), std::str::from_utf8(&output.stderr).unwrap())
    }

    let actual = file::get_text(grammar_path.with_extension("rs")).unwrap();

    let expected = expected.trim();
    let actual = actual.trim();
    if expected != actual {
        let difference = difference::Changeset::new(expected, actual, "\n");
        println!("{}\n{}", file_name, difference);
        panic!("Mismatch!")
    }
}

#[test]
fn test_grammars_are_fresh() {
    check_by_path("../fall_test/src/sexp.fall");
    check_by_path("../fall_test/src/rust.fall");
    check_by_path("../fall_test/src/weird.fall");
    check_by_path("../fall_test/src/json.fall");
    check_by_path("./src/syntax.fall");
}

{'fraction_non_alphanumeric': 0.1893574865553354, 'fraction_numerical': 0.0005660911406736484, 'mean_word_length': 3.7372654155495977, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '196517', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1185, 'n_words': 207}