-
Notifications
You must be signed in to change notification settings - Fork 5
/
lookup_test.go
116 lines (112 loc) · 2.29 KB
/
lookup_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
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
// Copyright (c) 2014 Alex Kalyvitis
package mustache
import (
"reflect"
"testing"
)
func TestSimpleLookup(t *testing.T) {
for _, test := range []struct {
context interface{}
assertions []struct {
name string
value interface{}
truth bool
}
}{
{
context: map[string]interface{}{
"integer": 123,
"string": "abc",
"boolean": true,
"map": map[string]interface{}{
"in": "I'm nested!",
},
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"integer", 123, true},
{"string", "abc", true},
{"boolean", true, true},
{"map.in", "I'm nested!", true},
},
},
{
context: struct {
Integer int
String string
Boolean bool
Nested struct{ Inside string }
}{
123, "abc", true, struct{ Inside string }{"I'm nested!"},
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"Integer", 123, true},
{"String", "abc", true},
{"Boolean", true, true},
{"Nested.Inside", "I'm nested!", true},
},
},
{
context: struct {
Integer int `template:"int"`
String string `template:"str"`
Boolean bool `template:"bool"`
Nested struct {
Inside string `template:"inside"`
} `template:"nested"`
}{
Integer: 123,
String: "abc",
Boolean: true,
Nested: struct {
Inside string `template:"inside"`
}{"I'm nested!"},
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"int", 123, true},
{"str", "abc", true},
{"bool", true, true},
{"nested.inside", "I'm nested!", true},
},
},
} {
for _, assertion := range test.assertions {
value, truth := lookup(assertion.name, test.context)
if value != assertion.value {
t.Errorf("Unexpected value %v != %v", value, assertion.value)
}
if truth != assertion.truth {
t.Errorf("Unexpected truth %t != %t", truth, assertion.truth)
}
}
}
}
func TestTruth(t *testing.T) {
for _, test := range []struct {
input interface{}
expected bool
}{
{"abc", true},
{"", false},
{123, true},
{0, false},
{true, true},
{false, false},
} {
truth := truth(reflect.ValueOf(test.input))
if truth != test.expected {
t.Errorf("Unexpected truth %t != %t", truth, test.expected)
}
}
}