-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtesting_test.go
145 lines (108 loc) · 3.19 KB
/
testing_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
package vertex
import (
"bytes"
"encoding/json"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func mockTest(t *TestContext) {}
var api = &API{
Root: "/mock",
Name: "testung",
Version: "1.0",
Doc: "Our fancy testung API",
Title: "Testung API!",
Renderer: JSONRenderer{},
AllowInsecure: true,
Middleware: []Middleware{makeMockMW("Global middleware")},
Routes: Routes{
{
Path: "/test",
Description: "test",
Handler: VoidHandler{},
Methods: GET,
Test: WarningTest(mockTest),
},
},
}
func TestTestContext(t *testing.T) {
tc := &TestContext{
api: api,
serverURl: "http://localhost:1277",
routePath: "/test",
messages: []string{},
}
assert.Equal(t, tc.FormatUrl(nil), "http://localhost:1277/mock/test", "Badly formatted url")
assert.Equal(t, tc.ServerUrl(), "http://localhost:1277", "Bad server url")
tc.Log("Wor dawg")
assert.Len(t, tc.messages, 1)
vals := url.Values{}
vals.Set("foo", "bar")
req, err := tc.NewRequest("GET", vals, nil)
assert.NoError(t, err, "Error creating request")
assert.NotNil(t, req)
assert.Nil(t, req.Body)
assert.Equal(t, req.URL.String(), "http://localhost:1277/mock/test?foo=bar")
testResults := func(f func()) (ret testResult) {
defer func() {
x := recover()
if x != nil {
ret = x.(testResult)
}
}()
f()
t.Errorf("We shuold have paniced during f()")
return
}
res := testResults(func() {
tc.Fail("WAT %s", "WAT")
})
assert.Equal(t, res.Result, resultFailed)
assert.Equal(t, res.Message, "WAT WAT")
assert.True(t, strings.HasPrefix(res.FailPoint, "vertex.func"))
res = testResults(func() {
tc.Fatal("WAT %s", "WAT")
})
assert.Equal(t, res.Result, resultFatal)
assert.Equal(t, res.Message, "WAT WAT")
assert.True(t, strings.HasPrefix(res.FailPoint, "vertex.func"))
res = testResults(func() {
tc.Skip()
})
assert.Equal(t, res.Result, resultSkipped)
assert.Equal(t, res.Message, "")
assert.Equal(t, res.FailPoint, "")
}
func TestTestRunner(t *testing.T) {
outbuf := bytes.NewBuffer(nil)
runner := newTestRunner(outbuf, api, "127.0.0.1:9947", WarningTests, TestFormatText)
success := runner.Run()
if !success {
t.Error("Tests failed")
}
os := outbuf.String()
assert.True(t, strings.Contains(os, "- /test"))
assert.True(t, strings.Contains(os, "category: warning"))
assert.True(t, strings.Contains(os, "[PASS]"))
}
func TestTextFormatter(t *testing.T) {
buf := bytes.NewBuffer(nil)
formatter := newTextResultFormatter(buf)
res := newTestResult(resultPass, "Wat Wat", 1, &TestContext{routePath: "/foo", category: "warning"})
assert.NoError(t, formatter.format(res))
os := buf.String()
assert.True(t, strings.Contains(os, "- /foo"))
assert.True(t, strings.Contains(os, "category: warning"))
assert.True(t, strings.Contains(os, "[PASS]"))
}
func TestJsonFormatter(t *testing.T) {
buf := bytes.NewBuffer(nil)
formatter := newJsonResultFormatter(buf)
res := newTestResult(resultPass, "Wat Wat", 1, &TestContext{routePath: "/foo", category: "warning"})
assert.NoError(t, formatter.format(res))
var tr2 testResult
assert.NoError(t, json.Unmarshal(buf.Bytes(), &tr2))
assert.Equal(t, res, tr2)
}