-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncmap_test.go
137 lines (129 loc) · 2.49 KB
/
funcmap_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"testing"
"text/template"
)
func TestSearchFunc(t *testing.T) {
tests := []struct {
title string
funcMap template.FuncMap
key string
expected []string
}{
{
"not found",
map[string]interface{}{},
"",
[]string{},
},
{
"empty key gets all functions",
map[string]interface{}{
"a": func() {},
"b": func() {},
},
"",
[]string{"a", "b"},
},
{
"key gets all functions whose key starts with it",
map[string]interface{}{
"fail": func() {},
"find": func() {},
"finish": func() {},
"get": func() {},
},
"fi",
[]string{"find", "finish"},
},
}
for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf(tt.title), func(t *testing.T) {
actual := searchFunc(tt.funcMap)(tt.key)
if len(actual) != len(tt.expected) {
t.Fatalf("wrong length. got %d, expected %d", len(actual), len(tt.expected))
}
for i, elem := range actual {
if elem != tt.expected[i] {
t.Errorf("actual[%d] is wrong. got %s, expected %s",
i, elem, tt.expected[i])
}
}
})
}
}
func TestDocFunc(t *testing.T) {
tests := []struct {
title string
funcMap template.FuncMap
key string
expected string
}{
{
"arity 0, return 0",
map[string]interface{}{
"fail": func() {},
},
"fail",
"fail -> ()",
},
{
"arity 1, return 1",
map[string]interface{}{
"itoa": func(i int) string { return fmt.Sprint(i) },
},
"itoa",
"itoa int -> (string)",
},
{
"arity 3, return 1",
map[string]interface{}{
"myTernary": func(i int, s string, f float64) bool { return false },
},
"myTernary",
"myTernary int string float64 -> (bool)",
},
{
"arity 1, return 3",
map[string]interface{}{
"myFunc": func(i int) (string, bool, error) { return "", true, nil },
},
"myFunc",
"myFunc int -> (string bool error)",
},
{
"variadic",
map[string]interface{}{
"myFunc": func(i int, strs ...string) {},
},
"myFunc",
"myFunc int ...string -> ()",
},
{
"not found",
map[string]interface{}{},
"myFunc",
"function myFunc is not defined",
},
{
"not function",
map[string]interface{}{
"val": 1,
},
"val",
"val is not a function",
},
}
for _, tt := range tests {
tt := tt
t.Run(fmt.Sprintf(tt.title), func(t *testing.T) {
actual := docFunc(tt.funcMap)(tt.key)
if actual != tt.expected {
t.Errorf("wrong output. expected `%s`, got `%s`",
tt.expected, actual)
}
})
}
}