2023-04-07 10:39:08 -04:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2023-04-22 14:16:22 -04:00
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2023-04-07 10:39:08 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-04-07 10:39:08 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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...)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, c.want, got)
|
2023-04-07 10:39:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bads := []struct {
|
|
|
|
args []any
|
|
|
|
}{
|
|
|
|
{[]any{"a", 1, "b"}},
|
|
|
|
{[]any{1}},
|
|
|
|
{[]any{struct{}{}}},
|
|
|
|
}
|
|
|
|
for _, c := range bads {
|
|
|
|
_, err := dict(c.args...)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2023-04-07 10:39:08 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 14:16:22 -04:00
|
|
|
|
|
|
|
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{}
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, tmpl.Execute(w, data))
|
2023-04-22 14:16:22 -04:00
|
|
|
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{}{}})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.ErrorContains(t, err, "invalid type, expected slice or array")
|
2023-04-22 14:16:22 -04:00
|
|
|
}
|