forked from alexkappa/mustache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup_test.go
135 lines (131 loc) · 2.78 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) 2014 Alex Kalyvitis
package mustache
import (
"reflect"
"testing"
)
func TestSimpleLookup(t *testing.T) {
var nilStrPtr *string
stringForPtr := "string"
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
Array [3]int
Slice []int
Boolean bool
Nested struct{ Inside string }
Tagged string `mustache:"newName"`
badTag string `mustache:"fail"`
bad string
ValidPtr *string
NilPtr *string
}{
123, "abc", [3]int{1, 2, 3}, []int{1}, true, struct{ Inside string }{"I'm nested!"}, "xyz", "bad", "bad", &stringForPtr, nil,
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"Integer", 123, true},
{"String", "abc", true},
{"Boolean", true, true},
{"Nested.Inside", "I'm nested!", true},
{"newName", "xyz", true},
{"Tagged", "xyz", true},
{"bad", nil, false},
{"badTag", nil, false},
{"fail", nil, false},
{"ValidPtr", &stringForPtr, true},
{"NilPtr", nilStrPtr, false},
{"Array.2", 3, true},
{"Slice.0", 1, true},
{"Slice.2", nil, false},
{"Slice.-1", nil, false},
{"Slice.a", nil, false},
},
},
{
context: map[string]interface{}{
"outer": []interface{}{[]int{1}, []int{1, 2}},
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"outer.1.0", 1, true},
},
},
{
context: []map[string]int{
{"a": 1},
{"b": 2},
},
assertions: []struct {
name string
value interface{}
truth bool
}{
{"1.b", 2, true},
},
},
} {
for _, assertion := range test.assertions {
value, truth := lookup(assertion.name, test.context)
if value != assertion.value {
t.Errorf("Unexpected value %v,%T != %v,%T", value, value, assertion.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)
}
}
}