summaryrefslogtreecommitdiffstats
path: root/vendor/handlebars/tests/escape.rs
blob: 4a737f5ce5b4cdae7268d2b57b767f6b72adc94a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
extern crate handlebars;

#[macro_use]
extern crate serde_json;

use handlebars::{handlebars_helper, no_escape, Handlebars};

#[test]
fn test_escape_216() {
    let hbs = Handlebars::new();

    let data = json!({
        "FOO": "foo",
        "BAR": "bar"
    });

    assert_eq!(
        hbs.render_template(r"\\\\ {{FOO}} {{BAR}} {{FOO}}{{BAR}} {{FOO}}#{{BAR}} {{FOO}}//{{BAR}} {{FOO}}\\{{FOO}} {{FOO}}\\\\{{FOO}}\\\{{FOO}} \\\{{FOO}} \{{FOO}} \{{FOO}}", &data).unwrap(),
        r"\\\\ foo bar foobar foo#bar foo//bar foo\foo foo\\\foo\\foo \\foo {{FOO}} {{FOO}}"
    );
}

#[test]
fn test_string_no_escape_422() {
    let mut hbs = Handlebars::new();

    handlebars_helper!(replace: |input: str, from: str, to: str| {
        input.replace(from, to)
    });
    hbs.register_helper("replace", Box::new(replace));

    assert_eq!(
        r#"some\ path"#,
        hbs.render_template(r#"{{replace "some/path" "/" "\\ " }}"#, &())
            .unwrap()
    );

    assert_eq!(
        r#"some\path"#,
        hbs.render_template(r#"{{replace "some/path" "/" "\\" }}"#, &())
            .unwrap()
    );
}

#[test]
fn test_string_whitespace_467() {
    const TEMPLATE_UNQUOTED: &str = r#"{{#each synonyms}}
    {{this.name}} => '{{this.sym}}',
    {{/each}}
"#;

    let mut hbs = Handlebars::new();
    hbs.register_escape_fn(no_escape);
    hbs.register_template_string("perl", TEMPLATE_UNQUOTED)
        .unwrap();

    let r = hbs
        .render("perl", &json!({"synonyms": [{"name": "lt", "sym": "<"}]}))
        .unwrap();
    assert_eq!("    lt => '<',\n", r);
}

#[test]
fn test_triple_bracket_expression_471() {
    let mut hbs = Handlebars::new();

    handlebars_helper!(replace: |input: str| {
        input.replace("\n", "<br/>")
    });
    hbs.register_helper("replace", Box::new(replace));

    assert_eq!(
        "some&lt;br/&gt;path",
        hbs.render_template("{{replace h}}", &json!({"h": "some\npath"}))
            .unwrap()
    );

    assert_eq!(
        "some<br/>path",
        hbs.render_template("{{{replace h}}}", &json!({"h": "some\npath"}))
            .unwrap()
    );
}