-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocd_test.go
162 lines (140 loc) · 3.43 KB
/
gocd_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
package gocd
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)
type TestCase struct {
Name string `yaml:"name"`
Before string `yaml:"before"`
After string `yaml:"after"`
Designator string `yaml:"des"`
DesignatorStd string `yaml:"des_std"`
Lang string `yaml:"lang"`
Position string `yaml:"position"`
Skip bool `yaml:"skip"`
SkipUnlessLang bool `yaml:"skip_unless_lang"`
}
func TestGOCDBasic(t *testing.T) {
tests := []struct {
input string
short string
des string
pos string
}{
{"Profound Networks LLC", "Profound Networks", "LLC", "end"},
{"Profound Networks LLC (Seattle)", "", "", ""},
}
p, err := New()
if err != nil {
t.Fatal(err)
}
for _, tc := range tests {
res, err := p.Parse(tc.input)
if err != nil {
t.Fatal(err)
}
if tc.short != "" {
assert.Equal(t, tc.input, res.Input, "Input matches")
assert.Equal(t, tc.input != tc.short, res.Matched, "Matched matches")
assert.Equal(t, tc.short, res.ShortName, "ShortName matches")
assert.Equal(t, tc.des, res.Designator, "Designator matches")
assert.Equal(t, tc.pos, res.Position.String(), "Position matches")
} else {
assert.Equal(t, tc.input, res.ShortName, "ShortName matches Input")
}
}
}
func fatal(msg string) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}
func loadTests() []TestCase {
var tests []TestCase
data, err := ioutil.ReadFile("data/tests.yml")
if err != nil {
fatal(err.Error())
}
err = yaml.Unmarshal(data, &tests)
if err != nil {
fatal(err.Error())
}
return tests
}
func loadStripTests() []TestCase {
tests := loadTests()
// Strip currently unsupported tests
var tests2 []TestCase
s := 0
mid := 0
for _, tc := range tests {
if tc.Position == "" {
fatal(fmt.Sprintf("missing position for test entry %q", tc.Name))
}
if tc.Skip || tc.SkipUnlessLang {
s++
continue
}
// We don't handle embedded matches yet
if tc.Position == "mid" {
mid++
continue
}
tests2 = append(tests2, tc)
}
//fmt.Fprintf(os.Stderr, "+ %d skip tests ignored\n", s)
//fmt.Fprintf(os.Stderr, "+ %d mid tests ignored\n", mid)
return tests2
}
func TestGOCDFull(t *testing.T) {
tests := loadStripTests()
p, err := New()
if err != nil {
t.Fatal(err)
}
//fmt.Fprintf(os.Stderr, "+ %d tests loaded\n", len(tests))
c := 0
for _, tc := range tests {
res, err := p.Parse(tc.Name)
if err != nil {
t.Fatal(err)
}
if tc.Before != "" {
c++
assert.Equal(t, tc.Name, res.Input, "Input matches")
assert.Equal(t, tc.Before, res.ShortName, "ShortName matches")
assert.Equal(t, tc.Designator, res.Designator, "Designator matches")
assert.Equal(t, tc.Position, res.Position.String(), "Position matches")
} else if tc.After != "" {
c++
assert.Equal(t, tc.Name, res.Input, "Input matches")
assert.Equal(t, tc.After, res.ShortName, "ShortName matches")
assert.Equal(t, tc.Designator, res.Designator, "Designator matches")
assert.Equal(t, tc.Position, res.Position.String(), "Position matches")
}
}
//fmt.Fprintf(os.Stderr, "+ %d tests completed\n", c)
}
func BenchmarkRE(b *testing.B) {
tests := loadStripTests()
p, err := New()
if err != nil {
b.Fatal(err)
}
// Benchmark loop, iterating over tests in tests
j := 0
for i := 0; i < b.N; i++ {
tc := tests[j]
_, err := p.Parse(tc.Name)
if err != nil {
b.Fatal(err)
}
j++
if j >= len(tests) {
j = 0
}
}
}