-
Notifications
You must be signed in to change notification settings - Fork 7
/
tcurls_test.go
118 lines (105 loc) · 3.03 KB
/
tcurls_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
package tcurls
import (
"io/ioutil"
"strings"
"testing"
"gopkg.in/yaml.v2"
)
type TestCase struct {
Function string `yaml:"function"`
Expected map[string]string `yaml:"expected"`
ArgSets [][]string `yaml:"argSets"`
}
type TestsSpecification struct {
RootURLs map[string][]string `yaml:"rootURLs"`
Tests []TestCase `yaml:"tests"`
}
func getTests() (*TestsSpecification, error) {
data, err := ioutil.ReadFile("tests.yml")
if err != nil {
return nil, err
}
var spec TestsSpecification
err = yaml.Unmarshal([]byte(data), &spec)
if err != nil {
return nil, err
}
return &spec, nil
}
func testFunc(t *testing.T, function string, expectedURL string, root string, args ...string) {
var actualURL string
switch function {
case "api":
actualURL = API(root, args[0], args[1], args[2])
case "apiReference":
actualURL = APIReference(root, args[0], args[1])
case "docs":
actualURL = Docs(root, args[0])
case "exchangeReference":
actualURL = ExchangeReference(root, args[0], args[1])
case "schema":
actualURL = Schema(root, args[0], args[1])
case "apiReferenceSchema":
actualURL = APIReferenceSchema(root, args[0])
case "exchangesReferenceSchema":
actualURL = ExchangesReferenceSchema(root, args[0])
case "apiManifestSchema":
actualURL = APIManifestSchema(root, args[0])
case "metadataMetaschema":
actualURL = MetadataMetaschema(root)
case "ui":
actualURL = UI(root, args[0])
case "apiManifest":
actualURL = APIManifest(root)
default:
t.Errorf("Unknown function type: %s", function)
return
}
if expectedURL != actualURL {
t.Errorf("%v %v(%v) = `%v` but should be `%v`", redCross(), function, quotedList(root, args), actualURL, expectedURL)
return
}
t.Logf("%v %v(%v) = `%v`", greenTick(), function, quotedList(root, args), actualURL)
}
func TestURLs(t *testing.T) {
spec, err := getTests()
if err != nil {
t.Error(err)
}
for _, test := range spec.Tests {
for _, argSet := range test.ArgSets {
for cluster, rootURLs := range spec.RootURLs {
for _, rootURL := range rootURLs {
testFunc(t, test.Function, test.Expected[cluster], rootURL, argSet...)
}
}
}
}
}
func TestNormalize(t *testing.T) {
spec, err := getTests()
if err != nil {
t.Error(err)
}
expected := spec.RootURLs["new"][0]
for _, rootURL := range spec.RootURLs["new"] {
actual := NormalizeRootURL(rootURL)
if expected != actual {
t.Errorf("%v NormalizeRootURL(%v) = `%v` but should be `%v`", redCross(), rootURL, actual, expected)
}
t.Logf("%v NormalizeRootURL(%v) = `%v`", greenTick(), rootURL, actual)
}
}
// quotedList returns a backtick-quoted list of the arguments passed in
func quotedList(url string, args []string) string {
all := append([]string{url}, args...)
return "`" + strings.Join(all, "`, `") + "`"
}
// greenTick returns an ANSI string including escape codes to render a light
// green tick (✓) in a color console
func greenTick() string {
return string([]byte{0x1b, 0x5b, 0x33, 0x32, 0x6d, 0xe2, 0x9c, 0x93, 0x1b, 0x5b, 0x30, 0x6d})
}
func redCross() string {
return "❌"
}