use miette::NamedSource;
use nexus_lang::Source;
use pretty_assertions::assert_eq;

#[test]
fn new() {
    let code = "fn main() {}";
    let source = Source::new(&code);

    assert_eq!(source.code(), code);
    assert_eq!(source.name(), None);
}

#[test]
fn new_with_name() {
    let code = "fn main() {}";
    let name = "main.rs";
    let source = Source::new_with_name(&code, &name);

    assert_eq!(source.code(), code);
    assert_eq!(source.name(), Some(name));
}

#[test]
fn with_name() {
    let code = "fn main() {}";
    let source = Source::new(&code);
    let source_named = source.with_name(&"main.rs");

    assert_eq!(source_named.code(), code);
    assert_eq!(source_named.name(), Some("main.rs"));
}

#[test]
fn from_str() {
    let code = "fn main() {}";
    let source: Source = code.into();

    assert_eq!(source.code(), code);
    assert_eq!(source.name(), None);
}

#[test]
fn into_named_source() {
    let code = "fn main() {}";
    let name = "main.rs";
    let source = Source::new_with_name(&code, &name);
    let named_source: NamedSource<String> = source.into();

    assert_eq!(named_source.name(), name);
    assert_eq!(named_source.inner(), code);
}

#[test]
fn into_named_source_without_name() {
    let code = "fn main() {}";
    let source = Source::new(&code);
    let named_source: NamedSource<String> = source.into();

    assert_eq!(named_source.name(), "");
    assert_eq!(named_source.inner(), code);
}

#[test]
fn display() {
    let code = "fn main() {}";
    let source: Source = code.into();

    let _ = format!("{source}");
}
