//! Test runner for official [RDF Test Cases](https://www.w3.org/TR/rdf11-testcases/)
//!
//! This test runner can read the test manifests published by W3C and generate test
//! cases for each test defined in the suite.
//!
//! Test results can be written in [EARL]((https://www.w3.org/2001/03/earl/) for
//! submission as RDF implementation report by setting the `TAGANAK_TEST_EARL_FILE`
//! environment variable to a path.

mod report;
mod tests;
use crate::report::*;
use crate::tests::*;

use libtest_mimic::{Arguments, Trial};

/// Explicit list of manifest paths to be loaded and tested
const TESTED_MANIFESTS: &[&str] = &[
    "rdf/rdf11/rdf-n-triples/manifest.ttl",
    "rdf/rdf11/rdf-turtle/manifest.ttl",
    "rdf/rdf12/rdf-n-triples/syntax/manifest.ttl",
];

/// Collect all trials from all manifests referenced by paths
fn collect_trials(files: &[&str]) -> Vec<Trial> {
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .unwrap();

    files
        .iter()
        .map(|f| {
            rt.block_on(async { Into::<Vec<Trial>>::into(Manifest::from_file(f).await.unwrap()) })
        })
        .flatten()
        .collect()
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Arguments::from_args();
    let trials = collect_trials(TESTED_MANIFESTS);

    let res = libtest_mimic::run(&args, trials);

    if let Ok(path) = std::env::var("TAGANAK_TEST_EARL_FILE") {
        write_earl_file(&path).unwrap();
    }

    res.exit();
}
