-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_validator_test.go
106 lines (90 loc) · 1.93 KB
/
template_validator_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
package templator
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidator_Error(t *testing.T) {
t.Parallel()
validatorError := ValidationError{
TemplateName: "dummy-template-name",
FieldPath: "dummy-file-path",
Err: errors.New("dummy-error"),
}
got := validatorError.Error()
assert.Equal(t, "template 'dummy-template-name' validation error: 'dummy-file-path' - 'dummy-error'", got)
}
func TestUniqueFields(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
givenFields []string
expect []string
}{
{
name: "unique fields",
givenFields: []string{"foo", "bar"},
expect: []string{"foo", "bar"},
},
{
name: "duplicated fields",
givenFields: []string{"foo", "bar", "foo"},
expect: []string{"foo", "bar"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := uniqueFields(tc.givenFields)
assert.ElementsMatch(t, tc.expect, got)
})
}
}
func Test_validateField(t *testing.T) {
t.Parallel()
type testStruct struct {
testField string
}
testCases := []struct {
name string
typ reflect.Type
fieldPath string
wantErr bool
}{
{
name: "nil type",
typ: nil,
fieldPath: "testField",
wantErr: true,
},
{
name: "non-struct type",
typ: reflect.TypeOf(""),
fieldPath: "testField",
wantErr: true,
},
{
name: "field is a struct",
typ: reflect.TypeOf(testStruct{}),
fieldPath: "testField",
wantErr: false,
},
{
name: "field is a pointer",
typ: reflect.TypeOf(&testStruct{}),
fieldPath: "testField",
wantErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := validateField(tc.typ, tc.fieldPath)
if tc.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}