aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java
blob: 1e2f69d7bc86610a15d605bf1aaf361c8b5738c5 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.template;

import org.junit.jupiter.api.Test;

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
 * @author hakonhall
 */
public class TemplateTest {
    @Test
    void verifyNewlineRemoval() {
        Template template = Template.from("a%{list a}\n" +
                                          "b%{end}\n" +
                                          "c%{list c-}\n" +
                                          "d%{end-}\n" +
                                          "e\n",
                                          new TemplateDescriptor().setRemoveNewline(false));
        template.add("a");
        template.add("c");

        assertEquals("a\n" +
                     "b\n" +
                     "cde\n",
                     template.render());
    }

    @Test
    void verifyIfSection() {
        Template template = Template.from("Hello%{if cond} world%{end}!");
        assertEquals("Hello world!", template.snapshot().set("cond", true).render());
        assertEquals("Hello!", template.snapshot().set("cond", false).render());
    }

    @Test
    void verifyComplexIfSection() {
        Template template = Template.from("%{if cond}\n" +
                                          "var: %{=varname}\n" +
                                          "if: %{if !inner}inner is false%{end-}\n" +
                                          "list: %{list formname}element%{end-}\n" +
                                          "%{end}\n");

        assertEquals("", template.snapshot().set("cond", false).render());

        assertEquals("var: varvalue\n" +
                     "if: \n" +
                     "list: \n",
                     template.snapshot()
                             .set("cond", true)
                             .set("varname", "varvalue")
                             .set("inner", true)
                             .render());

        Template template2 = template.snapshot()
                                     .set("cond", true)
                                     .set("varname", "varvalue")
                                     .set("inner", false);
        template2.add("formname");

        assertEquals("var: varvalue\n" +
                     "if: inner is false\n" +
                     "list: element\n", template2.render());
    }

    @Test
    void verifyElse() {
        var template = Template.from("%{if cond}\n" +
                                     "if body\n" +
                                     "%{else}\n" +
                                     "else body\n" +
                                     "%{end}\n");
        assertEquals("if body\n", template.snapshot().set("cond", true).render());
        assertEquals("else body\n", template.snapshot().set("cond", false).render());
    }

    @Test
    void verifySnapshotPreservesList() {
        var template = Template.from("%{list foo}hello %{=area}%{end}");
        template.add("foo")
                .set("area", "world");

        assertEquals("hello world", template.render());
        assertEquals("hello world", template.snapshot().render());

        Template snapshot = template.snapshot();
        snapshot.add("foo")
                .set("area", "Norway");
        assertEquals("hello worldhello Norway", snapshot.render());
    }

    @Test
    void verifyVariableSection() {
        Template template = getTemplate("template1.tmp");
        template.set("varname", "varvalue");
        assertEquals("variable section 'varvalue'\n" +
                     "end of text\n", template.render());
    }

    @Test
    void verifySimpleListSection() {
        Template template = getTemplate("template1.tmp");
        template.set("varname", "varvalue")
                .add("listname")
                .set("varname", "different varvalue")
                .set("varname2", "varvalue2");
        assertEquals("variable section 'varvalue'\n" +
                     "same variable section 'different varvalue'\n" +
                     "different variable section 'varvalue2'\n" +
                     "between ends\n" +
                     "end of text\n", template.render());
    }

    @Test
    void verifyNestedListSection() {
        Template template = getTemplate("template2.tmp");
        ListElement A0 = template.add("listA");
        ListElement A0B0 = A0.add("listB");
        ListElement A0B1 = A0.add("listB");

        ListElement A1 = template.add("listA");
        ListElement A1B0 = A1.add("listB");
        assertEquals("body A\n" +
                     "body B\n" +
                     "body B\n" +
                     "body A\n" +
                     "body B\n",
                     template.render());
    }

    @Test
    void verifyVariableReferences() {
        Template template = getTemplate("template3.tmp");
        template.set("varname", "varvalue")
                .set("innerVarSetAtTop", "val2");
        template.add("l");
        template.add("l")
                .set("varname", "varvalue2");
        assertEquals("varvalue\n" +
                     "varvalue\n" +
                     "inner varvalue\n" +
                     "val2\n" +
                     "inner varvalue2\n" +
                     "val2\n",
                     template.render());
    }

    @Test
    void badTemplates() {
        assertException(BadTemplateException.class, "Unknown section 'zoo' at line 2 and column 6",
                        () -> Template.from("foo\nbar%{zoo}"));

        assertException(BadTemplateException.class, "Expected identifier at line 1 and column 4",
                        () -> Template.from("%{="));

        assertException(BadTemplateException.class, "Expected identifier at line 1 and column 4",
                        () -> Template.from("%{=&notatoken}"));

        assertException(BadTemplateException.class, "Expected identifier at line 1 and column 8",
                        () -> Template.from("%{list &notatoken}"));

        assertException(BadTemplateException.class, "Missing end directive for section started at line 1 and column 12",
                        () -> Template.from("%{list foo}missing end"));

        assertException(BadTemplateException.class, "Stray 'end' at line 1 and column 3",
                        () -> Template.from("%{end}stray end"));

        assertException(TemplateNameNotSetException.class, "Variable at line 1 and column 4 has not been set: notset",
                        () -> Template.from("%{=notset}").render());

        assertException(TemplateNameNotSetException.class, "Variable at line 1 and column 6 has not been set: cond",
                        () -> Template.from("%{if cond}%{end}").render());

        assertException(NotBooleanValueTemplateException.class, "cond was set to a non-boolean value: must be true or false",
                        () -> Template.from("%{if cond}%{end}").set("cond", 1).render());

        assertException(NoSuchNameTemplateException.class, "No such element 'listname' in the template section starting at " +
                                                           "line 1 and column 1, and ending at line 1 and column 4",
                        () -> Template.from("foo").add("listname"));

        assertException(NameAlreadyExistsTemplateException.class,
                        "The name 'a' of the list section at line 1 and column 16 is in conflict with the identically " +
                        "named list section at line 1 and column 1",
                        () -> Template.from("%{list a}%{end}%{list a}%{end}"));

        assertException(NameAlreadyExistsTemplateException.class,
                        "The name 'a' of the list section at line 1 and column 6 is in conflict with the identically " +
                        "named variable section at line 1 and column 1",
                        () -> Template.from("%{=a}%{list a}%{end}"));

        assertException(NameAlreadyExistsTemplateException.class,
                        "The name 'a' of the variable section at line 1 and column 16 is in conflict with the identically " +
                        "named list section at line 1 and column 1",
                        () -> Template.from("%{list a}%{end}%{=a}"));

        assertException(NameAlreadyExistsTemplateException.class,
                        "The name 'a' of the list section at line 1 and column 14 is in conflict with the identically " +
                        "named if section at line 1 and column 1",
                        () -> Template.from("%{if a}%{end}%{list a}%{end}"));

        assertException(NameAlreadyExistsTemplateException.class,
                        "The name 'a' of the if section at line 1 and column 16 is in conflict with the identically " +
                        "named list section at line 1 and column 1",
                        () -> Template.from("%{list a}%{end}%{if a}%{end}"));
    }

    private <T extends Throwable> void assertException(Class<T> class_, String message, Runnable runnable) {
        T exception = assertThrows(class_, runnable::run);
        assertEquals(message, exception.getMessage());
    }

    private Template getTemplate(String filename) {
        return Template.at(Path.of("src/test/resources/" + filename));
    }
}