-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflight_test.go
75 lines (68 loc) · 1.9 KB
/
preflight_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
package main
import (
"testing"
)
func TestPreflightAsset(t *testing.T) {
//byte slices
invalidUTF8 := []byte{0xff, 0xfe, 0xfd}
validJSON := []byte(`{ "foo": ["bar", "barfoo"] }`)
invalidJSON := []byte(`foo { "foo": ["bar", "barfoo"] } foo`)
validYAML := []byte(`"foo": "bar"`)
invalidYAML := []byte(`"foo":foo:bar: "bar"`)
multilineYAML := []byte(`"foo":
- "bar"
- "foobar"
- "boofar"
- "roobar"
`)
multilineYAMLConverted := []byte(`{"foo":["bar","foobar","boofar","roobar"]}`)
validJSONConverted := []byte(`{ "foo": ["bar", "barfoo"] }`)
var tests = []struct {
description string
data []byte
isJSON bool
success bool
}{
{"invalid_utf8", invalidUTF8, false, false},
{"valid_json", validJSON, false, true},
{"invalid_json", invalidJSON, false, false},
{"valid_yaml", validYAML, false, true},
{"invalid_yaml", invalidYAML, false, false},
{"multiline_yaml", multilineYAML, false, true},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
err := preflightAsset(&test.data, test.isJSON)
success := (err == nil)
if success != test.success {
expectation := "succeed"
if test.success == false {
expectation = "fail"
}
t.Errorf("test expected to %s", expectation)
}
})
}
var comparisons = []struct {
description string
dataIn []byte
dataOut []byte
isJSON bool
success bool
}{
{"multiline_yaml_conversion", multilineYAML, multilineYAMLConverted, false, true},
{"json_conversion", validJSON, validJSONConverted, true, true},
}
for _, test := range comparisons {
t.Run(test.description, func(t *testing.T) {
err := preflightAsset(&test.dataIn, test.isJSON)
if err != nil && test.success {
t.Errorf("test expected to run")
}
// quick comparison
if string(test.dataIn) != string(test.dataOut) {
t.Errorf("Expected %s to match %s", test.dataIn, test.dataOut)
}
})
}
}