forked from gerald1248/timeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preflight_test.go
65 lines (55 loc) · 1.64 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
package timeline
import (
"testing"
)
func TestPreflightAsset(t *testing.T) {
jsonFileMock := "mock.json"
yamlFileMock := "mock.yaml"
//byte slices
invalidUtf8 := []byte{0xff, 0xfe, 0xfd}
xmlMarkup := []byte("<?xml version='1.0' encoding='UTF-8' standalone='yes'?><root/>")
validJson := []byte("{ \"foo\": [\"bar\", \"barfoo\"] }")
validYaml := []byte("\"foo\": \"bar\"")
multilineYaml := []byte(`"foo":
- "bar"
- "foobar"
- "boofar"
- "roobar"
`)
multilineYamlConverted := []byte("{\"foo\":[\"bar\",\"foobar\",\"boofar\",\"roobar\"]}")
//expect error
err := preflightAsset(&invalidUtf8, jsonFileMock)
if err == nil {
t.Error("Must reject invalid UTF8 with JSON filename")
}
err = preflightAsset(&invalidUtf8, yamlFileMock)
if err == nil {
t.Error("Must reject invalid UTF8 with YAML filename")
}
err = preflightAsset(&validYaml, jsonFileMock)
if err == nil {
t.Error("Must reject YAML with JSON filename")
}
//much JSON is also valid YAML, so don't disallow JSON with YAML filename
err = preflightAsset(&xmlMarkup, "")
if err == nil {
t.Error("Must reject XML markup")
}
//expect success
err = preflightAsset(&validYaml, yamlFileMock)
if err != nil {
t.Errorf("Must accept valid YAML: %v", err)
}
err = preflightAsset(&validJson, jsonFileMock)
if err != nil {
t.Errorf("Must accept valid JSON: %v", err)
}
//in-place conversion must match predefined result
err = preflightAsset(&multilineYaml, yamlFileMock)
if err != nil {
t.Errorf("Must accept valid multiline YAML: %v", err)
}
if string(multilineYaml) != string(multilineYamlConverted) {
t.Errorf("Expected %s to match %s", multilineYaml, multilineYamlConverted)
}
}