aboutsummaryrefslogtreecommitdiffstats
path: root/modules/templates/vars/vars_test.go
blob: 1cd7669c0095e974da8edff1c85ec4403ece6b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package vars

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestExpandVars(t *testing.T) {
	kases := []struct {
		tmpl  string
		data  map[string]string
		out   string
		error bool
	}{
		{
			tmpl: "{a}",
			data: map[string]string{
				"a": "1",
			},
			out: "1",
		},
		{
			tmpl: "expand {a}, {b} and {c}, with non-var { } {#}",
			data: map[string]string{
				"a": "1",
				"b": "2",
				"c": "3",
			},
			out: "expand 1, 2 and 3, with non-var { } {#}",
		},
		{
			tmpl: "中文内容 {一}, {二} 和 {三} 中文结尾",
			data: map[string]string{
				"一": "11",
				"二": "22",
				"三": "33",
			},
			out: "中文内容 11, 22 和 33 中文结尾",
		},
		{
			tmpl: "expand {{a}, {b} and {c}",
			data: map[string]string{
				"a": "foo",
				"b": "bar",
			},
			out:   "expand {{a}, bar and {c}",
			error: true,
		},
		{
			tmpl:  "expand } {} and {",
			out:   "expand } {} and {",
			error: true,
		},
	}

	for _, kase := range kases {
		t.Run(kase.tmpl, func(t *testing.T) {
			res, err := Expand(kase.tmpl, kase.data)
			assert.EqualValues(t, kase.out, res)
			if kase.error {
				assert.Error(t, err)
			} else {
				assert.NoError(t, err)
			}
		})
	}
}