1 package testutils
2
3 import (
4 "fmt"
5 "sort"
6
7 "strings"
8 "time"
9
10 "github.com/noirbizarre/gonja"
11 "github.com/noirbizarre/gonja/exec"
12 )
13
14 var adminList = []string{"user2"}
15
16 var time1 = time.Date(2014, 06, 10, 15, 30, 15, 0, time.UTC)
17 var time2 = time.Date(2011, 03, 21, 8, 37, 56, 12, time.UTC)
18
19 type post struct {
20 Text string
21 Created time.Time
22 }
23
24 type user struct {
25 Name string
26 Validated bool
27 }
28
29 func (u *user) String() string {
30 return u.Name
31 }
32
33 type comment struct {
34 Author *user
35 Date time.Time
36 Text string
37 }
38
39 type person struct {
40 FirstName string
41 LastName string
42 Gender string
43 }
44
45 func isAdmin(u *user) bool {
46 for _, a := range adminList {
47 if a == u.Name {
48 return true
49 }
50 }
51 return false
52 }
53
54 func (u *user) IsAdmin() *exec.Value {
55 return exec.AsValue(isAdmin(u))
56 }
57
58 func (u *user) IsAdmin2() bool {
59 return isAdmin(u)
60 }
61
62 func (p *post) String() string {
63 return ":-)"
64 }
65
66
69
70 var Fixtures = gonja.Context{
71 "number": 11,
72 "simple": map[string]interface{}{
73 "number": 42,
74 "name": "john doe",
75 "included_file": "INCLUDES.helper",
76 "included_file_not_exists": "INCLUDES.helper.not_exists",
77 "nil": nil,
78 "uint": uint(8),
79 "float": float64(3.1415),
80 "str": "string",
81 "chinese_hello_world": "你好世界",
82 "bool_true": true,
83 "bool_false": false,
84 "newline_text": `this is a text
85 with a new line in it`,
86 "long_text": `This is a simple text.
87
88 This too, as a paragraph.
89 Right?
90
91 Yep!`,
92 "escape_js_test": `escape sequences \r\n\'\" special chars "?!=$<>`,
93 "one_item_list": []int{99},
94 "multiple_item_list": []int{1, 1, 2, 3, 5, 8, 13, 21, 34, 55},
95 "unsorted_int_list": []int{192, 581, 22, 1, 249, 9999, 1828591, 8271},
96 "fixed_item_list": [...]int{1, 2, 3, 4},
97 "misc_list": []interface{}{"Hello", 99, 3.14, "good"},
98 "escape_text": "This is \\a Test. \"Yep\". 'Yep'.",
99 "xss": "<script>alert(\"uh oh\");</script>",
100 "intmap": map[int]string{
101 1: "one",
102 5: "five",
103 2: "two",
104 },
105 "strmap": map[string]string{
106 "abc": "def",
107 "bcd": "efg",
108 "zab": "cde",
109 "gh": "kqm",
110 "ukq": "qqa",
111 "aab": "aba",
112 },
113 "casedStrmap": map[string]string{
114 "a": "a",
115 "B": "B",
116 "c": "c",
117 "D": "D",
118 "e": "e",
119 "F": "F",
120 },
121 "func_add": func(a, b int) int {
122 return a + b
123 },
124 "func_add_iface": func(a, b interface{}) interface{} {
125 return a.(int) + b.(int)
126 },
127 "func_variadic": func(msg string, args ...interface{}) string {
128 return fmt.Sprintf(msg, args...)
129 },
130 "func_variadic_sum_int": func(args ...int) int {
131
132 s := 0
133 for _, i := range args {
134 s += i
135 }
136 return s
137 },
138 "func_variadic_sum_int2": func(args ...*exec.Value) *exec.Value {
139
140 s := 0
141 for _, i := range args {
142 s += i.Integer()
143 }
144 return exec.AsValue(s)
145 },
146 "func_with_varargs": func(params *exec.VarArgs) *exec.Value {
147
148 argsAsStr := []string{}
149 for _, arg := range params.Args {
150 argsAsStr = append(argsAsStr, arg.String())
151 }
152 kwargsAsStr := []string{}
153 for key, value := range params.KwArgs {
154 v := value.String()
155 if value.IsString() {
156 v = "\"" + v + "\""
157 }
158 pair := []string{key, v}
159 kwargsAsStr = append(kwargsAsStr, strings.Join(pair, "="))
160 }
161 sort.Strings(kwargsAsStr)
162 args := strings.Join(argsAsStr, ", ")
163 kwargs := strings.Join(kwargsAsStr, ", ")
164
165 str := fmt.Sprintf("VarArgs(args=[%s], kwargs={%s})", args, kwargs)
166 return exec.AsSafeValue(str)
167 },
168 },
169 "complex": map[string]interface{}{
170 "user": &user{
171 Name: "john doe",
172 Validated: true,
173 },
174 "is_admin": isAdmin,
175 "post": post{
176 Text: "<h2>Hello!</h2><p>Welcome to my new blog page. I'm using gonja which supports {{ variables }} and {% tags %}.</p>",
177 Created: time2,
178 },
179 "comments": []*comment{
180 &comment{
181 Author: &user{
182 Name: "user1",
183 Validated: true,
184 },
185 Date: time1,
186 Text: "\"gonja is nice!\"",
187 },
188 &comment{
189 Author: &user{
190 Name: "user2",
191 Validated: true,
192 },
193 Date: time2,
194 Text: "comment2 with <script>unsafe</script> tags in it",
195 },
196 &comment{
197 Author: &user{
198 Name: "user3",
199 Validated: false,
200 },
201 Date: time1,
202 Text: "<b>hello!</b> there",
203 },
204 },
205 "comments2": []*comment{
206 &comment{
207 Author: &user{
208 Name: "user1",
209 Validated: true,
210 },
211 Date: time2,
212 Text: "\"gonja is nice!\"",
213 },
214 &comment{
215 Author: &user{
216 Name: "user1",
217 Validated: true,
218 },
219 Date: time1,
220 Text: "comment2 with <script>unsafe</script> tags in it",
221 },
222 &comment{
223 Author: &user{
224 Name: "user3",
225 Validated: false,
226 },
227 Date: time1,
228 Text: "<b>hello!</b> there",
229 },
230 },
231 },
232 "persons": []*person{
233 {"John", "Doe", "male"},
234 {"Jane", "Doe", "female"},
235 {"Akira", "Toriyama", "male"},
236 {"Selina", "Kyle", "female"},
237 {"Axel", "Haustant", "male"},
238 },
239 "groupable": []map[string]string{
240 {"grouper": "group 1", "value": "value 1-1"},
241 {"grouper": "group 2", "value": "value 2-1"},
242 {"grouper": "group 3", "value": "value 3-1"},
243 {"grouper": "group 1", "value": "value 1-2"},
244 {"grouper": "group 2", "value": "value 2-2"},
245 {"grouper": "group 3", "value": "value 3-2"},
246 {"grouper": "group 1", "value": "value 1-3"},
247 {"grouper": "group 2", "value": "value 2-3"},
248 {"grouper": "group 3", "value": "value 3-3"},
249 },
250 }
251
View as plain text