use serde_json::json;
use skabelon::Templates;

#[test]
fn for_loops() {
    let template_str = "@for(item in items) {<span>{{item.index}}: {{item.value}}</span>}";

    let mut templates = Templates::new();
    templates.load_str("test", template_str);

    let arr = ["A", "B", "C"];
    let arr: Vec<_> = arr
        .into_iter()
        .enumerate()
        .map(|x| json!({"index": x.0+1, "value": x.1}))
        .collect();

    let ctx = json!({"items": arr});

    let output = templates.render("test", &ctx);

    let expected = "<span>1: A</span><span>2: B</span><span>3: C</span>";

    assert_eq!(output, expected);
}

#[test]
fn for_loop_with_space() {
    let template_str = "@for(item in items) {{{item}}} hello";

    let mut templates = Templates::new();
    templates.load_str("test", template_str);

    let arr = ["A", "B", "C"];
    let ctx = json!({"items": arr});

    let output = templates.render("test", &ctx);

    let expected = "ABC hello";

    assert_eq!(output, expected);
}

#[test]
fn empty_for_loop() {
    let template_str = "@for(item in items) {{{item}}} hello";

    let mut templates = Templates::new();
    templates.load_str("test", template_str);

    let arr: Vec<String> = vec![];
    let ctx = json!({"items": arr});

    let output = templates.render("test", &ctx);

    let expected = "hello";

    assert_eq!(output, expected);
}

#[test]
fn index() {
    let template_str = "@for(item in items) {<span>{{index}}: {{item}}</span>}";

    let mut templates = Templates::new();
    templates.load_str("test", template_str);

    let arr = ["A", "B", "C"];

    let ctx = json!({"items": arr});

    let output = templates.render("test", &ctx);

    let expected = "<span>0: A</span><span>1: B</span><span>2: C</span>";

    assert_eq!(output, expected);
}
