use liquid;

#[test]
fn test_simple_variable() {
    assert_template_result!(r#"worked"#, r#"{{test}}"#, v!({"test": "worked"}));

    assert_template_result!(
        r#"worked wonderfully"#,
        r#"{{test}}"#,
        v!({"test": "worked wonderfully"}),
    );
}

#[test]
#[should_panic]
fn test_variable_render_calls_to_liquid() {
    panic!("to_liquid is implementation specific");
}

#[test]
fn test_simple_with_whitespaces() {
    assert_template_result!(r#"  worked  "#, r#"  {{ test }}  "#, v!({"test": "worked"}));

    assert_template_result!(
        r#"  worked wonderfully  "#,
        r#"  {{ test }}  "#,
        v!({"test": "worked wonderfully"}),
    );
}

#[test]
#[should_panic]
fn test_ignore_unknown() {
    assert_template_result!(r#""#, r#"{{ test }}"#);
    panic!("Requires `strict_variables: false`");
}

#[test]
fn test_using_blank_as_variable_name() {
    assert_template_result!(r#""#, r#"{% assign foo = blank %}{{ foo }}"#);
}

#[test]
fn test_using_empty_as_variable_name() {
    assert_template_result!(r#""#, r#"{% assign foo = empty %}{{ foo }}"#);
}

#[test]
fn test_hash_scoping() {
    assert_template_result!(
        r#"worked"#,
        r#"{{ test.test }}"#,
        v!({"test": {"test": "worked"}}),
    );
}

#[test]
fn test_false_renders_as_false() {
    assert_template_result!(r#"false"#, r#"{{ foo }}"#, v!({"foo": false}));

    assert_template_result!(r#"false"#, r#"{{ false }}"#);
}

#[test]
fn test_nil_renders_as_empty_string() {
    assert_template_result!(r#""#, r#"{{ nil }}"#);

    assert_template_result!(r#"cat"#, r#"{{ nil | append: 'cat' }}"#);
}

#[test]
#[should_panic]
fn test_preset_assigns() {
    panic!("Preset assigns are implementation specific");
}

#[test]
fn test_reuse_parsed_template() {
    let template = liquid::ParserBuilder::with_liquid()
        .build()
        .unwrap()
        .parse(r#"{{ greeting }} {{ name }}"#)
        .unwrap();

    let globals = v!({"greeting": "Hello", "name": "Tobi"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("Hello Tobi", rendered);

    // Modified due to strict_variables: true
    let globals = v!({"greeting": "Hello", "unknown": "Tobi"});
    template.render(globals.as_object().unwrap()).unwrap_err();

    let globals = v!({"greeting": "Hello", "name": "Brian"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("Hello Brian", rendered);

    // Preset assign cases ("Goodbye")( are implementation specific
}

#[test]
fn test_assigns_not_polluted_from_template() {
    let template = liquid::ParserBuilder::with_liquid()
        .build()
        .unwrap()
        .parse(r#"{{ test }}{% assign test = 'bar' %}{{ test }}"#)
        .unwrap();

    // All modified due to strict_variables: true

    let globals = v!({"test": "baz"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("bazbar", rendered);

    let globals = v!({"test": "baz"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("bazbar", rendered);

    let globals = v!({"test": "foo"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("foobar", rendered);

    let globals = v!({"test": "baz"});
    let rendered = template.render(globals.as_object().unwrap()).unwrap();
    assert_eq!("bazbar", rendered);
}

#[test]
#[should_panic]
fn test_hash_with_default_proc() {
    panic!("Default proc is implementation specific");
}

#[test]
fn test_multiline_variable() {
    assert_template_result!(r#"worked"#, "{{\ntest\n}}", v!({"test": "worked"}));
}

#[test]
#[should_panic]
fn test_render_symbol() {
    panic!("Symbols are implementation specific");
}
