You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_test.go 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package templates
  4. import (
  5. "html/template"
  6. "io"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestDict(t *testing.T) {
  12. type M map[string]any
  13. cases := []struct {
  14. args []any
  15. want map[string]any
  16. }{
  17. {[]any{"a", 1, "b", 2}, M{"a": 1, "b": 2}},
  18. {[]any{".", M{"base": 1}, "b", 2}, M{"base": 1, "b": 2}},
  19. {[]any{"a", 1, ".", M{"extra": 2}}, M{"a": 1, "extra": 2}},
  20. {[]any{"a", 1, ".", map[string]int{"int": 2}}, M{"a": 1, "int": 2}},
  21. {[]any{".", nil, "b", 2}, M{"b": 2}},
  22. }
  23. for _, c := range cases {
  24. got, err := dict(c.args...)
  25. if assert.NoError(t, err) {
  26. assert.EqualValues(t, c.want, got)
  27. }
  28. }
  29. bads := []struct {
  30. args []any
  31. }{
  32. {[]any{"a", 1, "b"}},
  33. {[]any{1}},
  34. {[]any{struct{}{}}},
  35. }
  36. for _, c := range bads {
  37. _, err := dict(c.args...)
  38. assert.Error(t, err)
  39. }
  40. }
  41. func TestUtils(t *testing.T) {
  42. execTmpl := func(code string, data any) string {
  43. tmpl := template.New("test")
  44. tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils})
  45. template.Must(tmpl.Parse(code))
  46. w := &strings.Builder{}
  47. assert.NoError(t, tmpl.Execute(w, data))
  48. return w.String()
  49. }
  50. actual := execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "a"})
  51. assert.Equal(t, "true", actual)
  52. actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "x"})
  53. assert.Equal(t, "false", actual)
  54. actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []int64{1, 2}, "Value": int64(2)})
  55. assert.Equal(t, "true", actual)
  56. actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "b"})
  57. assert.Equal(t, "true", actual)
  58. actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "x"})
  59. assert.Equal(t, "false", actual)
  60. tmpl := template.New("test")
  61. tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils})
  62. template.Must(tmpl.Parse("{{SliceUtils.Contains .Slice .Value}}"))
  63. // error is like this: `template: test:1:12: executing "test" at <SliceUtils.Contains>: error calling Contains: ...`
  64. err := tmpl.Execute(io.Discard, map[string]any{"Slice": struct{}{}})
  65. assert.ErrorContains(t, err, "invalid type, expected slice or array")
  66. }