use guartcl::guardian_interp;
use jimtcl::JimResult;

#[test]
fn parse_json_null() -> JimResult<()> {
    let interp = guardian_interp()?;
    let val = interp.eval("parse json {null}")?;
    assert!(val.is_empty());
    Ok(())
}

#[test]
fn parse_json_numlist() -> JimResult<()> {
    let interp = guardian_interp()?;
    let val = interp.eval("parse json {[5, 2, 4]}")?;
    assert_eq!(val.list_length(), 3);
    let items = Vec::<u32>::try_from(&val)?;
    assert_eq!(&items, &[5, 2, 4]);
    Ok(())
}

#[test]
fn parse_toml() -> JimResult<()> {
    let interp = guardian_interp()?;
    interp.set_variable("toml", include_str!("test.toml"))?;
    let val = interp.eval("parse toml $toml")?;
    println!("Parsed TOML: {}", val);
    assert_eq!(val.dict_size()?, 2);

    let scrolls = val.dict_get("scrolls")?.unwrap();
    assert_eq!(scrolls.list_length(), 1);
    let scroll = scrolls.list_index(0)?;
    assert_eq!(scroll.dict_get("label")?.unwrap().as_str()?, "HACKEM MUCHE");
    Ok(())
}

#[test]
fn parse_toml_file() -> JimResult<()> {
    let src = env!("CARGO_MANIFEST_DIR");
    let interp = guardian_interp()?;
    let cmd = format!("parse toml -file {}/tests/test.toml", src);
    eprintln!("running command: {}", cmd);
    let val = interp.eval(&cmd)?;
    println!("Parsed TOML: {}", val);
    assert_eq!(val.dict_size()?, 2);

    let scrolls = val.dict_get("scrolls")?.unwrap();
    assert_eq!(scrolls.list_length(), 1);
    let scroll = scrolls.list_index(0)?;
    assert_eq!(scroll.dict_get("label")?.unwrap().as_str()?, "HACKEM MUCHE");
    Ok(())
}

#[test]
fn parse_yaml() -> JimResult<()> {
    let interp = guardian_interp()?;
    interp.set_variable("yaml", include_str!("test.yaml"))?;
    let val = interp.eval("parse yaml $yaml")?;
    println!("Parsed yaml: {}", val);
    assert_eq!(val.dict_size()?, 2);

    let scrolls = val.dict_get("scrolls")?.unwrap();
    assert_eq!(scrolls.list_length(), 1);
    let scroll = scrolls.list_index(0)?;
    assert_eq!(scroll.dict_get("label")?.unwrap().as_str()?, "HACKEM MUCHE");
    Ok(())
}

#[test]
fn parse_yaml_file() -> JimResult<()> {
    let src = env!("CARGO_MANIFEST_DIR");
    let interp = guardian_interp()?;
    interp.set_variable("yaml", include_str!("test.yaml"))?;
    let cmd = format!("parse toml -file {}/tests/test.toml", src);
    let val = interp.eval(&cmd)?;
    println!("Parsed yaml: {}", val);
    assert_eq!(val.dict_size()?, 2);

    let scrolls = val.dict_get("scrolls")?.unwrap();
    assert_eq!(scrolls.list_length(), 1);
    let scroll = scrolls.list_index(0)?;
    assert_eq!(scroll.dict_get("label")?.unwrap().as_str()?, "HACKEM MUCHE");
    Ok(())
}
