-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathcontext_test.go
More file actions
216 lines (178 loc) · 4.78 KB
/
Copy pathcontext_test.go
File metadata and controls
216 lines (178 loc) · 4.78 KB
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
package plush_test
import (
"fmt"
"html/template"
"testing"
"golang.org/x/sync/errgroup"
"github.com/gobuffalo/plush/v5"
"github.com/stretchr/testify/require"
)
func Test_Context_Set(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
r.Nil(c.Value("foo"))
c.Set("foo", "bar")
r.NotNil(c.Value("foo"))
}
func Test_Context_Set_Concurrency(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
wg := errgroup.Group{}
f := func() error {
c.Set("a", "b")
return nil
}
wg.Go(f)
wg.Go(f)
wg.Go(f)
err := wg.Wait()
r.NoError(err)
}
func Test_Context_Get(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
r.Nil(c.Value("foo"))
c.Set("foo", "bar")
r.Equal("bar", c.Value("foo"))
}
func Test_New_Sub_Context_Set(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
r.Nil(c.Value("foo"))
sc := c.New()
r.Nil(sc.Value("foo"))
sc.Set("foo", "bar")
r.Equal("bar", sc.Value("foo"))
r.Nil(c.Value("foo"))
}
func Test_New_Sub_Context_Get(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
c.Set("foo", "bar")
sc := c.New()
r.Equal("bar", sc.Value("foo"))
}
func Test_Context_Override_Helper(t *testing.T) {
r := require.New(t)
c := plush.NewContext()
c.Set("debug", func(i interface{}) template.HTML {
return template.HTML("DEBUG")
})
s := c.Value("debug").(func(interface{}) template.HTML)(nil)
r.Equal(template.HTML("DEBUG"), s)
}
func Test_New_Context_With_Override_Helper(t *testing.T) {
r := require.New(t)
c := plush.NewContextWith(map[string]interface{}{
"debug": func(i interface{}) template.HTML {
return template.HTML("DEBUG")
},
})
s := c.Value("debug").(func(interface{}) template.HTML)(nil)
r.Equal(template.HTML("DEBUG"), s)
}
func Test_New_Context_With_Nil_Helper_Override_Falls_Back_To_Global(t *testing.T) {
r := require.New(t)
c := plush.NewContextWith(map[string]interface{}{
"partial": nil,
})
r.NotNil(c.Value("partial"))
}
func Test_New_Context_With_Bulk_Construction_Matches_Set_Behavior(t *testing.T) {
r := require.New(t)
c := plush.NewContextWith(map[string]interface{}{
"name": "Mido",
"count": 3,
"empty": nil,
})
r.Equal("Mido", c.Value("name"))
r.Equal(3, c.Value("count"))
r.Nil(c.Value("empty"))
r.True(c.Has("name"))
r.False(c.Has("empty"))
id := c.InternID("name")
value, ok := c.LookupID(id)
r.True(ok)
r.Equal("Mido", value)
}
func Test_Context_Intern_I_Ds_Are_Stable_Across_Root_Contexts(t *testing.T) {
r := require.New(t)
first := plush.NewContextWith(map[string]interface{}{
"z": true,
"name": "Mido",
"title": "Engineer",
})
second := plush.NewContextWith(map[string]interface{}{
"a": true,
"title": "Pilot",
"name": "Fry",
})
nameID := first.InternID("name")
titleID := first.InternID("title")
r.Equal(nameID, second.InternID("name"))
r.Equal(titleID, second.InternID("title"))
value, ok := first.LookupID(nameID)
r.True(ok)
r.Equal("Mido", value)
value, ok = second.LookupID(nameID)
r.True(ok)
r.Equal("Fry", value)
value, ok = second.LookupID(titleID)
r.True(ok)
r.Equal("Pilot", value)
}
func Test_Context_Intern_I_Ds_Are_Safe_Across_Concurrent_Root_Contexts(t *testing.T) {
r := require.New(t)
keys := []string{"name", "items", "local", "__plush_internal_render_diagnostics_test__"}
wg := errgroup.Group{}
for i := 0; i < 64; i++ {
i := i
wg.Go(func() error {
name := fmt.Sprintf("name-%d", i)
ctx := plush.NewContextWith(map[string]interface{}{
"name": name,
"items": []string{"a", "b"},
})
ids := make([]int, len(keys))
for j := 0; j < 32; j++ {
ctx.InternIDs(keys, ids)
for keyIndex, key := range keys {
if ids[keyIndex] != ctx.InternID(key) {
return fmt.Errorf("intern id mismatch for %s", key)
}
}
value, ok := ctx.LookupID(ids[0])
if !ok || value != name {
return fmt.Errorf("lookup id mismatch: %v %v", value, ok)
}
_ = ctx.Value(keys[3])
}
return nil
})
}
r.NoError(wg.Wait())
}
func Test_Context_Shared_Helpers_Do_Not_Leak_Overrides(t *testing.T) {
r := require.New(t)
c1 := plush.NewContextWith(map[string]interface{}{
"debug": func(i interface{}) template.HTML {
return template.HTML("LOCAL")
},
})
c2 := plush.NewContext()
local := c1.Value("debug").(func(interface{}) template.HTML)(nil)
global := c2.Value("debug").(func(interface{}) template.HTML)(nil)
r.Equal(template.HTML("LOCAL"), local)
r.NotEqual(template.HTML("LOCAL"), global)
}
func Test_Context_Shared_Helpers_Refreshes_For_New_Helpers(t *testing.T) {
r := require.New(t)
name := fmt.Sprintf("test_helper_%p", t)
before := plush.NewContext()
r.False(before.Has(name))
r.NoError(plush.Helpers.Add(name, func() string { return "ok" }))
after := plush.NewContext()
r.False(before.Has(name))
r.True(after.Has(name))
r.Equal("ok", after.Value(name).(func() string)())
}