forked from corbym/gogiven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiven_test.go
225 lines (194 loc) · 7.24 KB
/
given_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package gogiven_test
import (
"fmt"
"os"
"testing"
"github.com/corbym/gocrest"
"github.com/corbym/gocrest/has"
"github.com/corbym/gocrest/is"
. "github.com/corbym/gocrest/then"
. "github.com/corbym/gogiven"
"github.com/corbym/gogiven/base"
"github.com/corbym/gogiven/testdata"
)
type StubHtmlGenerator struct{}
func (*StubHtmlGenerator) Generate(testContext *TestContext) string {
return "testing"
}
func (*StubHtmlGenerator) FileExtension() string {
return ".html"
}
func TestMain(testmain *testing.M) {
runOutput := testmain.Run()
GenerateTestOutput()
os.Exit(runOutput)
}
func TestGivenWhenSetsInterestingGiven(testing *testing.T) {
testing.Parallel()
Given(testing, someDataSetup).
When(someAction).
Then(func(t base.TestingT,
actual testdata.CapturedIO,
givens testdata.InterestingGivens) {
//do assertions
AssertThat(t, actual["foo"], is.EqualTo("foob"))
})
}
func TestGivenWhenExercisingRanges(testing *testing.T) {
var testMetaData []*base.TestMetaData
var testingT = &base.TestMetaData{TestId: "title"}
var some []*base.Some
testing.Parallel()
var someRange = []struct {
actual string
expected int
}{
{actual: "", expected: 0},
{actual: "a", expected: 2},
}
for _, test := range someRange {
given := Given(testingT, aFakeGenerator)
some = append(some, given)
given.When(func(actual testdata.CapturedIO, givens testdata.InterestingGivens) {
actual["value"] = test.actual
actual["expected"] = test.expected
return
}).
Then(func(t base.TestingT, actual testdata.CapturedIO, givens testdata.InterestingGivens) {
//do assertions
testMetaData = append(testMetaData, t.(*base.TestMetaData))
AssertThat(t, test.actual, has.Length(test.expected))
})
}
AssertThat(testing, some[0].CapturedIO()["value"], is.EqualTo(""))
AssertThat(testing, some[0].CapturedIO()["expected"], is.EqualTo(0))
AssertThat(testing, some[1].CapturedIO()["value"], is.EqualTo("a"))
AssertThat(testing, some[1].CapturedIO()["expected"], is.EqualTo(2))
AssertThat(testing, testMetaData[0].Failed(), is.EqualTo(false))
AssertThat(testing, testMetaData[1].Failed(), is.EqualTo(true))
}
func TestInnerTestRangesOverValues(t *testing.T) {
var someRange = []struct {
value string
expected int
}{
{value: "n", expected: 1},
{value: "aa", expected: 2},
}
for _, test := range someRange {
t.Run(test.value, func(tInner *testing.T) {
Given(tInner, givenSome_Stuffs("a")).
When(func(capturedIO testdata.CapturedIO, givens testdata.InterestingGivens) {
givens["value"] = test.value
givens["expected"] = test.expected
capturedIO["actual"] = len(test.value)
}).Then(func(testingT base.TestingT, actual testdata.CapturedIO, givens testdata.InterestingGivens) {
AssertThat(testingT, actual["actual"], is.EqualTo(test.expected))
})
})
}
}
func givenSome_Stuffs(myStuff string) base.GivenData {
return func(givens testdata.InterestingGivens) {
}
}
func TestGivenWhenStacksGivens(testing *testing.T) {
testing.Parallel()
Given(testing, someDataSetup, andMoreDataSetup).
When(someAction).
Then(func(testing base.TestingT, actual testdata.CapturedIO, givens testdata.InterestingGivens) {
//do assertions
AssertThat(testing, givens, has.AllKeys("1", "2", "blarg"))
AssertThat(testing, givens, is.ValueContaining("hi", 12, "foo"))
AssertThat(testing, actual, has.Key("foo"))
})
}
func TestGivenWhenSkips(testing *testing.T) {
testing.Parallel()
t := &base.TestMetaData{TestId: "skiptest"}
Given(t, someDataSetup, andMoreDataSetup).
SkippingThisOne("some reason").
When(someAction).
Then(func(testing base.TestingT, actual testdata.CapturedIO, givens testdata.InterestingGivens) {
//do assertions
})
AssertThat(testing, t.Skipped(), is.EqualTo(true))
AssertThat(testing, t.TestOutput(), is.EqualTo("some reason"))
}
func TestParseGivenWhenThen_Panics(t *testing.T) {
defer func() {
rcv := recover()
AssertThat(t, rcv, is.Not(is.Nil()))
}()
ParseGivenWhenThen("foo", "Arfg")
}
func TestParseGivenWhenThen_TextOutputContent(testing *testing.T) {
givenWhenThen := ParseGivenWhenThen(".TestMyFirst", "./example_test.go")
AssertThat(testing, givenWhenThen, has.Length(5))
AssertThat(testing, givenWhenThen[0], is.EqualTo("Given testing the system setup"))
AssertThat(testing, givenWhenThen[1], is.EqualTo("When something happens"))
AssertThat(testing, givenWhenThen[2], is.EqualTo("Then noting that passed in testing should be used for assertions"))
AssertThat(testing, givenWhenThen[3], is.EqualTo("Noting that we do some assertions here commenting why"))
AssertThat(testing, givenWhenThen[4], is.EqualTo("Assert that testing actual \"actual\" is equal to \"some output\""))
}
func TestParseGivenWhenThen_WithoutGiven(testing *testing.T) {
givenWhenThen := ParseGivenWhenThen(".TestWithoutGiven", "./example_test.go")
AssertThat(testing, givenWhenThen, has.Length(3))
AssertThat(testing, givenWhenThen[0], is.EqualTo("When t something happens"))
AssertThat(testing, givenWhenThen[1], is.EqualTo("Then"))
AssertThat(testing, givenWhenThen[2], is.EqualTo("Assert that testing actual \"actual\" is equal to \"some output\""))
}
func TestParseGivenWhenThen_FuncWithReturnType(testing *testing.T) {
givenWhenThen := ParseGivenWhenThen(".TestMyFirst_Skipped", "./example_test.go")
AssertThat(testing, givenWhenThen, has.Length(8))
AssertThat(testing, givenWhenThen[0], is.EqualTo("Given we are testing the system setup"))
AssertThat(testing, givenWhenThen[1], is.EqualTo("Givens \"actual\" = test actual"))
AssertThat(testing, givenWhenThen[2], is.EqualTo("Skipping this one if bool"))
AssertThat(testing, givenWhenThen[3], is.EqualTo("Test actual == \"fff\""))
// TODO: fix this
//AssertThat(testing, givenWhenThen[4], is.EqualTo("some data % s does not work yet \"test actual\""))
}
func TestParseGivenWhenThen_RangedTextOutput(testing *testing.T) {
givenWhenThen := ParseGivenWhenThen(".TestMyFirst_Ranged", "./example_test.go")
AssertThat(testing, givenWhenThen, has.Length(6))
AssertThat(testing, givenWhenThen[0], is.EqualTo("Given testing the system setup"))
AssertThat(testing, givenWhenThen[1], is.EqualTo("Givens \"actual\" = test actual"))
AssertThat(testing, givenWhenThen[2], is.EqualTo("When something happens with the test"))
AssertThat(testing, givenWhenThen[3], is.EqualTo("Then"))
}
func fileExists(pathToFile string) interface{} {
fileInfo, err := os.Stat(pathToFile)
if err != nil {
return err
}
return fileInfo
}
func inTmpDir() *gocrest.Matcher {
matcher := new(gocrest.Matcher)
matcher.Matches = func(actual interface{}) bool {
file, ok := actual.(os.FileInfo)
if ok {
matcher.Describe = fmt.Sprintf("%s", file.Name())
return true
}
return false
}
return matcher
}
func aFakeGenerator(givens testdata.InterestingGivens) {
//Generator = new(StubHtmlGenerator) // you too can override Generator and generate any kind of file output.
}
func someDataSetup(givens testdata.InterestingGivens) {
givens["1"] = "hi"
givens["2"] = "foo"
aFakeGenerator(givens)
}
func andMoreDataSetup(givens testdata.InterestingGivens) {
givens["blarg"] = 12
}
func someAction(capturedIo testdata.CapturedIO, givens testdata.InterestingGivens) {
capturedIo["foo"] = "foob"
}
func ofFileInTmpDir(fileName string) string {
return fmt.Sprintf("%s%c%s", os.TempDir(), os.PathSeparator, fileName)
}