summaryrefslogtreecommitdiffstats
path: root/vendor/handlebars/tests/block_context.rs
blob: bbaa89a62486ce413a9b1349b33e3afccb8edf1f (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use handlebars::Handlebars;
use serde_json::json;

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

    let data = json!({
        "a": [
            {"b": 1},
            {"b": 2},
        ],
    });

    let template = "{{#*inline \"test\"}}{{b}};{{/inline}}{{#each a as |z|}}{{> test z}}{{/each}}";
    assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;");
}

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

    let data = json!({
        "a": [
            {"b": 1},
            {"b": 2},
        ],
        "b": 3,
    });

    let template =
        "{{#*inline \"test\"}}{{b}}:{{@root.b}};{{/inline}}{{#each a}}{{> test}}{{/each}}";
    assert_eq!(hbs.render_template(template, &data).unwrap(), "1:3;2:3;");
}

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

    let data = json!([
        {"value": 11},
        {"value": 22},
    ]);

    let template =
        "{{#each this as |b index|}}{{b.value}}{{#each this as |value key|}}:{{key}},{{/each}}{{/each}}";
    assert_eq!(
        hbs.render_template(template, &data).unwrap(),
        "11:value,22:value,"
    );
}

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

    let data = json!({
        "classes": [
            {
                "methods": [
                    {"id": 1},
                    {"id": 2}
                ]
            },
            {
                "methods": [
                    {"id": 3},
                    {"id": 4}
                ]
            },
        ],
    });

    let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{method.id}};{{/each}}{{/each}}";
    assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;3;4;");
}

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

    let data = json!({
        "classes": [
            {
                "methods": [
                    {"id": 1},
                    {"id": 2}
                ],
                "private": false
            },
            {
                "methods": [
                    {"id": 3},
                    {"id": 4}
                ],
                "private": true
            },
        ],
    });

    let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{class.private}}|{{method.id}};{{/each}}{{/each}}";
    assert_eq!(
        hbs.render_template(template, &data).unwrap(),
        "false|1;false|2;true|3;true|4;"
    );
}