(t *testing.T)
| 262 | } |
| 263 | |
| 264 | func TestTemplate_Functions(t *testing.T) { |
| 265 | mydict := map[string]any{ |
| 266 | "foo": "bar", |
| 267 | "foobar": 8379, |
| 268 | } |
| 269 | |
| 270 | tests := []struct { |
| 271 | name string |
| 272 | template string |
| 273 | context pongo2.Context |
| 274 | want string |
| 275 | errorMessage string |
| 276 | wantErr bool |
| 277 | }{ |
| 278 | { |
| 279 | name: "NoError", |
| 280 | template: "{{ testFunc(mydict) }}", |
| 281 | context: pongo2.Context{ |
| 282 | "mydict": mydict, |
| 283 | "testFunc": func(i any) (string, error) { |
| 284 | d, err := json.Marshal(i) |
| 285 | return string(d), err |
| 286 | }, |
| 287 | }, |
| 288 | want: `{"foo":"bar","foobar":8379}`, |
| 289 | wantErr: false, |
| 290 | }, |
| 291 | { |
| 292 | name: "WithError", |
| 293 | template: "{{ testFunc(mydict) }}", |
| 294 | context: pongo2.Context{ |
| 295 | "mydict": mydict, |
| 296 | "testFunc": func(i any) (string, error) { |
| 297 | return "", errors.New("something went wrong") |
| 298 | }, |
| 299 | }, |
| 300 | errorMessage: "[Error (where: execution) in <string> | Line 1 Col 4 near 'testFunc'] something went wrong", |
| 301 | wantErr: true, |
| 302 | }, |
| 303 | { |
| 304 | name: "TooMuchArguments", |
| 305 | template: "{{ testFunc(mydict) }}", |
| 306 | context: pongo2.Context{ |
| 307 | "mydict": mydict, |
| 308 | "testFunc": func(i any) (string, int, error) { |
| 309 | return "", 0, nil |
| 310 | }, |
| 311 | }, |
| 312 | errorMessage: "[Error (where: execution) in <string> | Line 1 Col 4 near 'testFunc'] 'testFunc' must have exactly 1 or 2 output arguments, the second argument must be of type error", |
| 313 | wantErr: true, |
| 314 | }, |
| 315 | { |
| 316 | name: "InvalidArguments", |
| 317 | template: "{{ testFunc(mydict) }}", |
| 318 | context: pongo2.Context{ |
| 319 | "mydict": map[string]any{ |
| 320 | "foo": "bar", |
| 321 | "foobar": 8379, |
nothing calls this directly
no test coverage detected
searching dependent graphs…