-
Notifications
You must be signed in to change notification settings - Fork 3
/
class_test.go
200 lines (179 loc) · 4.51 KB
/
class_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package skipper_test
import (
"testing"
"github.com/lukasjarosch/skipper"
"github.com/spf13/afero"
"github.com/stretchr/testify/suite"
)
type ClassTestSuite struct {
suite.Suite
FileSystem afero.Fs
EmptyYamlFile *skipper.YamlFile
InvalidYamlFile *skipper.YamlFile
TooManyKeysYamlFile *skipper.YamlFile
ValidYamlFile *skipper.YamlFile
}
func TestClassTestSuite(t *testing.T) {
suite.Run(t, new(ClassTestSuite))
}
func (suite *ClassTestSuite) SetupTest() {
suite.FileSystem = afero.NewMemMapFs()
suite.EmptyYamlFile, _ = skipper.CreateNewYamlFile(suite.FileSystem, "/emptyYaml.yaml", []byte(nil))
suite.ValidYamlFile, _ = skipper.CreateNewYamlFile(suite.FileSystem, "/validYaml.yaml", []byte(nil))
suite.InvalidYamlFile, _ = skipper.CreateNewYamlFile(suite.FileSystem, "/invalidYaml.yaml", []byte(nil))
suite.TooManyKeysYamlFile, _ = skipper.CreateNewYamlFile(suite.FileSystem, "/tooManyKeys.yaml", []byte(nil))
}
func (suite *ClassTestSuite) TestNewClass() {
noRootKey := suite.EmptyYamlFile
noRootKey.Data = make(skipper.Data)
rootKeyFalse := suite.InvalidYamlFile
rootKeyFalse.Data = skipper.Data{
"asdasd": "test",
}
multipleRootKeys := suite.TooManyKeysYamlFile
multipleRootKeys.Data = skipper.Data{
"root1": skipper.Data{
"foo": "bar",
},
"root2": skipper.Data{
"bar": "baz",
},
}
validYaml := suite.ValidYamlFile
validYaml.Data = skipper.Data{
"validYaml": skipper.Data{
"foo": "bar",
},
}
table := []struct {
TestName string
ErrorExpected bool
YamlFile *skipper.YamlFile
RelativeClassPath string
}{
{
TestName: "YamlFileNil",
ErrorExpected: true,
YamlFile: nil,
RelativeClassPath: "",
},
{
TestName: "EmptyRelativeClassPath",
ErrorExpected: true,
YamlFile: suite.EmptyYamlFile,
RelativeClassPath: "",
},
{
TestName: "EmptyYamlFile",
ErrorExpected: true,
YamlFile: suite.EmptyYamlFile,
RelativeClassPath: "emptyYaml.yaml",
},
{
TestName: "DataNoRootKey",
ErrorExpected: true,
YamlFile: noRootKey,
RelativeClassPath: "emptyYaml.yaml",
},
{
TestName: "MultipleRootKeys",
ErrorExpected: true,
YamlFile: suite.TooManyKeysYamlFile,
RelativeClassPath: "tooManyKeys.yaml",
},
{
TestName: "RootKeyDoesNotMatchYamlFileName",
ErrorExpected: true,
YamlFile: rootKeyFalse,
RelativeClassPath: "invalidYaml.yaml",
},
{
TestName: "ValidYamlFile",
ErrorExpected: false,
YamlFile: validYaml,
RelativeClassPath: "validYaml.yaml",
},
}
for _, tt := range table {
suite.Run(tt.TestName, func() {
class, err := skipper.NewClass(tt.YamlFile, tt.RelativeClassPath)
if tt.ErrorExpected {
suite.Error(err, "Expected an error, but no error was returned")
suite.Nil(class)
} else {
suite.NoError(err, "Test should not return an error")
suite.NotNil(class)
}
})
}
}
func (suite *ClassTestSuite) TestRootKey() {
table := []struct {
TestName string
Data skipper.Data
ExpectedRootKey string
}{
{
TestName: "EmptyData",
Data: make(skipper.Data),
ExpectedRootKey: "",
},
{
TestName: "SingleRootKey",
Data: skipper.Data{
"foo": skipper.Data{
"bar": "baz",
},
},
ExpectedRootKey: "foo",
},
}
for _, tt := range table {
suite.Run(tt.TestName, func() {
sut := &skipper.Class{
File: &skipper.YamlFile{
File: skipper.File{
Path: "unused",
Bytes: []byte{},
},
Data: tt.Data,
},
Name: "unused",
}
rootKey := sut.RootKey()
suite.Equal(tt.ExpectedRootKey, rootKey)
})
}
}
func (suite *ClassTestSuite) TestNameAsIdentifier() {
table := []struct {
TestName string
ClassName string
ExpectedIdentifier []interface{}
}{
{
TestName: "EmptyName",
ClassName: "",
ExpectedIdentifier: []interface{}{""},
},
{
TestName: "SingleSeparatedName",
ClassName: "Foo.Bar",
ExpectedIdentifier: []interface{}{"Foo", "Bar"},
},
{
TestName: "MultipleSeparatedName",
ClassName: "Foo.Bar.Baz.Hello",
ExpectedIdentifier: []interface{}{"Foo", "Bar", "Baz", "Hello"},
},
}
for _, tt := range table {
suite.Run(tt.TestName, func() {
sut := &skipper.Class{
Name: tt.ClassName,
}
actual := sut.NameAsIdentifier()
suite.Equal(tt.ExpectedIdentifier, actual)
})
}
}