diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /modules/templates/util_test.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'modules/templates/util_test.go')
-rw-r--r-- | modules/templates/util_test.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/modules/templates/util_test.go b/modules/templates/util_test.go new file mode 100644 index 0000000..79aaba4 --- /dev/null +++ b/modules/templates/util_test.go @@ -0,0 +1,79 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package templates + +import ( + "html/template" + "io" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDict(t *testing.T) { + type M map[string]any + cases := []struct { + args []any + want map[string]any + }{ + {[]any{"a", 1, "b", 2}, M{"a": 1, "b": 2}}, + {[]any{".", M{"base": 1}, "b", 2}, M{"base": 1, "b": 2}}, + {[]any{"a", 1, ".", M{"extra": 2}}, M{"a": 1, "extra": 2}}, + {[]any{"a", 1, ".", map[string]int{"int": 2}}, M{"a": 1, "int": 2}}, + {[]any{".", nil, "b", 2}, M{"b": 2}}, + } + + for _, c := range cases { + got, err := dict(c.args...) + require.NoError(t, err) + assert.EqualValues(t, c.want, got) + } + + bads := []struct { + args []any + }{ + {[]any{"a", 1, "b"}}, + {[]any{1}}, + {[]any{struct{}{}}}, + } + for _, c := range bads { + _, err := dict(c.args...) + require.Error(t, err) + } +} + +func TestUtils(t *testing.T) { + execTmpl := func(code string, data any) string { + tmpl := template.New("test") + tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils}) + template.Must(tmpl.Parse(code)) + w := &strings.Builder{} + require.NoError(t, tmpl.Execute(w, data)) + return w.String() + } + + actual := execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "a"}) + assert.Equal(t, "true", actual) + + actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []string{"a", "b"}, "Value": "x"}) + assert.Equal(t, "false", actual) + + actual = execTmpl("{{SliceUtils.Contains .Slice .Value}}", map[string]any{"Slice": []int64{1, 2}, "Value": int64(2)}) + assert.Equal(t, "true", actual) + + actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "b"}) + assert.Equal(t, "true", actual) + + actual = execTmpl("{{StringUtils.Contains .String .Value}}", map[string]any{"String": "abc", "Value": "x"}) + assert.Equal(t, "false", actual) + + tmpl := template.New("test") + tmpl.Funcs(template.FuncMap{"SliceUtils": NewSliceUtils, "StringUtils": NewStringUtils}) + template.Must(tmpl.Parse("{{SliceUtils.Contains .Slice .Value}}")) + // error is like this: `template: test:1:12: executing "test" at <SliceUtils.Contains>: error calling Contains: ...` + err := tmpl.Execute(io.Discard, map[string]any{"Slice": struct{}{}}) + require.ErrorContains(t, err, "invalid type, expected slice or array") +} |