use std::fs;
use std::path::Path;

use gonidium::diag::Severity;
use gonidium::parse_dsl;

#[test]
fn parse_fixtures_cover_errors_and_success() {
    let dir = Path::new("tests/fixtures/parse");
    let mut count = 0usize;
    for entry in fs::read_dir(dir).expect("read fixtures dir") {
        let entry = entry.expect("read entry");
        let path = entry.path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("gon") {
            continue;
        }
        count += 1;
        let src = fs::read_to_string(&path).expect("read fixture");
        let err_path = path.with_extension("err");
        if err_path.exists() {
            let expected = fs::read_to_string(&err_path).expect("read error fixture");
            let err = parse_dsl(&src).expect_err("expected parse error");
            let report = err.report_string("fixture", &src, Severity::Error);
            let needle = expected.trim();
            assert!(
                report.contains(needle),
                "fixture {} expected error containing {needle:?}, got:\n{report}",
                path.display()
            );
        } else {
            parse_dsl(&src).expect("expected parse success");
        }
    }
    assert!(count > 0, "no .gon fixtures found in {}", dir.display());
}
