-
Notifications
You must be signed in to change notification settings - Fork 5
/
mustache_test.go
89 lines (80 loc) · 2.15 KB
/
mustache_test.go
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
// Copyright (c) 2014 Alex Kalyvitis
package mustache
import (
"bytes"
"strings"
"testing"
)
var tests = map[string]interface{}{
"some text {{foo}} here": map[string]string{"foo": "bar"},
"{{#foo}} foo is defined {{bar}} {{/foo}}": map[string]map[string]string{"foo": {"bar": "baz"}},
"{{^foo}} foo is defined {{bar}} {{/foo}}": map[string]map[string]string{"foo": {"bar": "baz"}},
}
func TestTemplate(t *testing.T) {
input := strings.NewReader("some text {{foo}} here")
template := New()
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]string{"foo": "bar"})
if err != nil {
t.Error(err)
}
expected := "some text bar here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestFalsyTemplate(t *testing.T) {
input := strings.NewReader("some text {{^foo}}{{foo}}{{/foo}} {{bar}} here")
template := New()
err := template.Parse(input)
if err != nil {
t.Error(err)
}
var output bytes.Buffer
err = template.Render(&output, map[string]interface{}{"foo": 0, "bar": false})
if err != nil {
t.Error(err)
}
expected := "some text 0 false here"
if output.String() != expected {
t.Errorf("expected %q got %q", expected, output.String())
}
}
func TestParseTree(t *testing.T) {
template := New()
template.elems = []node{
textNode("Lorem ipsum dolor sit "),
&varNode{"foo", false},
textNode(", "),
§ionNode{"bar", false, []node{
&varNode{"baz", true},
textNode(" adipiscing"),
}},
textNode(" elit. Proin commodo viverra elit "),
&varNode{"zer", false},
textNode("."),
}
data := map[string]interface{}{
"foo": "amet",
"bar": map[string]string{"baz": "consectetur"},
"zer": 0.11,
}
b := bytes.NewBuffer(nil)
w := newWriter(b)
for _, e := range template.elems {
err := e.render(template, w, data)
if err != nil {
t.Error(err)
}
}
w.flush()
expected := `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin commodo viverra elit 0.11.`
if expected != b.String() {
t.Errorf("output didn't match. expected %q got %q.", expected, b.String())
t.Log(b.String())
}
}