-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
alias_test.go
107 lines (86 loc) · 3.62 KB
/
alias_test.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package sprout
import (
"bytes"
"html/template"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestWithAlias checks that aliases are correctly added to a function.
func TestWithAlias(t *testing.T) {
handler := New()
originalFunc := "originalFunc"
alias1 := "alias1"
alias2 := "alias2"
// Apply the WithAlias option with two aliases.
require.NoError(t, WithAlias(originalFunc, alias1, alias2)(handler))
// Check that the aliases were added.
assert.Contains(t, handler.cachedFuncsAlias, originalFunc)
assert.Contains(t, handler.cachedFuncsAlias[originalFunc], alias1)
assert.Contains(t, handler.cachedFuncsAlias[originalFunc], alias2)
assert.Len(t, handler.cachedFuncsAlias[originalFunc], 2, "there should be exactly 2 aliases")
}
func TestWithAlias_Empty(t *testing.T) {
handler := New()
originalFunc := "originalFunc"
// Apply the WithAlias option with no aliases.
require.NoError(t, WithAlias(originalFunc)(handler))
// Check that no aliases were added.
assert.NotContains(t, handler.cachedFuncsAlias, originalFunc)
}
func TestWithAliases(t *testing.T) {
handler := New()
originalFunc1 := "originalFunc1"
alias1 := "alias1"
alias2 := "alias2"
originalFunc2 := "originalFunc2"
alias3 := "alias3"
// Apply the WithAliases option with two sets of aliases.
require.NoError(t, WithAliases(FunctionAliasMap{
originalFunc1: {alias1, alias2},
originalFunc2: {alias3},
})(handler))
// Check that the aliases were added.
assert.Contains(t, handler.cachedFuncsAlias, originalFunc1)
assert.Contains(t, handler.cachedFuncsAlias[originalFunc1], alias1)
assert.Contains(t, handler.cachedFuncsAlias[originalFunc1], alias2)
assert.Len(t, handler.cachedFuncsAlias[originalFunc1], 2, "there should be exactly 2 aliases")
assert.Contains(t, handler.cachedFuncsAlias, originalFunc2)
assert.Contains(t, handler.cachedFuncsAlias[originalFunc2], alias3)
assert.Len(t, handler.cachedFuncsAlias[originalFunc2], 1, "there should be exactly 1 alias")
}
// TestRegisterAliases checks that aliases are correctly registered in the function map.
func TestRegisterAliases(t *testing.T) {
handler := New()
originalFunc := "originalFunc"
alias1 := "alias1"
alias2 := "alias2"
// Mock a function for originalFunc and add it to funcsRegistry.
mockFunc := func() {}
handler.cachedFuncsMap[originalFunc] = mockFunc
// Apply the WithAlias option and then register the aliases.
require.NoError(t, WithAlias(originalFunc, alias1, alias2)(handler))
AssignAliases(handler)
// Check that the aliases are mapped to the same function as the original function in funcsRegistry.
assert.Equal(t, reflect.ValueOf(handler.cachedFuncsMap[originalFunc]).Pointer(), reflect.ValueOf(handler.cachedFuncsMap[alias1]).Pointer())
assert.Equal(t, reflect.ValueOf(handler.cachedFuncsMap[originalFunc]).Pointer(), reflect.ValueOf(handler.cachedFuncsMap[alias2]).Pointer())
}
func TestAliasesInTemplate(t *testing.T) {
handler := New()
originalFuncName := "originalFunc"
alias1 := "alias1"
alias2 := "alias2"
// Mock a function for originalFunc and add it to funcsRegistry.
mockFunc := func() string { return "cheese" }
handler.cachedFuncsMap[originalFuncName] = mockFunc
// Apply the WithAlias option and then register the aliases.
require.NoError(t, WithAlias(originalFuncName, alias1, alias2)(handler))
// Create a template with the aliases.
tmpl, err := template.New("test").Funcs(handler.Build()).Parse(`{{originalFunc}} {{alias1}} {{alias2}}`)
require.NoError(t, err)
var buf bytes.Buffer
err = tmpl.ExecuteTemplate(&buf, "test", nil)
require.NoError(t, err)
assert.Equal(t, "cheese cheese cheese", buf.String())
}