use crate::get_test_file;
use anyhow::Context;
use std::io::Write;

pub fn generate_absolute_uri_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("absolute_uri_tests.rs")?;

    for (line, uri) in include_str!("../resources/tests/parsers/uri/absolute_uri/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/uri/absolute_uri/template.txt"),
            number = format!("{:03}", line),
            uri = uri
        )
        .context(format!("failed to write test absolute_uri::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_relative_uri_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("relative_uri_tests.rs")?;

    for (line, uri) in include_str!("../resources/tests/parsers/uri/relative_uri/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/uri/relative_uri/template.txt"),
            number = format!("{:03}", line),
            uri = uri
        )
        .context(format!("failed to write test relative_uri::case_{}", line))?;
    }

    Ok(())
}

pub fn generate_uri_reference_tests() -> anyhow::Result<()> {
    let mut test_file = get_test_file("uri_reference_tests.rs")?;

    for (line, uri) in include_str!("../resources/tests/valid.txt").lines().enumerate() {
        write!(
            test_file,
            include_str!("../resources/tests/parsers/uri/uri_reference/template.txt"),
            number = format!("{:03}", line),
            uri = uri
        )
        .context(format!("failed to write test uri_reference::case_{}", line))?;
    }

    Ok(())
}
