-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator_test.go
137 lines (123 loc) · 3.32 KB
/
generator_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
package jsonspec_test
import (
"bytes"
"encoding/json"
"github.com/corbym/gocrest"
"github.com/corbym/gocrest/is"
. "github.com/corbym/gocrest/then"
"github.com/corbym/gogiven/generator"
"github.com/corbym/jsonspec"
"testing"
"errors"
"github.com/corbym/gogiven/base"
)
var jsonString string
var underTest generator.GoGivensOutputGenerator
func init() {
underTest = jsonspec.NewTestOutputGenerator()
}
func TestTestOutputGenerator_Generate(testing *testing.T) {
fileIsConverted()
AssertThat(testing, jsonString, isValidJSON())
AssertThat(testing, jsonString, is.EqualToIgnoringWhitespace(
`{
"title": "Generator Test",
"test_state": [
{
"test_results": {
"id": "abc2124",
"failed": true,
"skipped": true,
"test_output": "well alrighty then"
},
"test_title": "test title",
"interesting_givens": {
"faff": "flap"
},
"captured_io": {
"foob": "barb"
},
"given_when_then": {
"given_when_then": [
"given",
"when",
"then"
],
"comment": [
"Fooing is best",
"done with friends"
]
}
}
]
}`))
}
func isValidJSON() *gocrest.Matcher {
matcher := &gocrest.Matcher{Describe: "valid jsonString"}
matcher.Matches = func(actual interface{}) bool {
buffer := &bytes.Buffer{}
buffer.WriteString(actual.(string))
var f interface{}
error := json.Unmarshal(buffer.Bytes(), &f)
return error == nil
}
return matcher
}
func TestTestOutputGenerator_GenerateConcurrently(testing *testing.T) {
data := newPageData(false, false)
for i := 0; i < 15; i++ {
go func() {
jsonContent := underTest.Generate(data)
buffer := new(bytes.Buffer)
buffer.ReadFrom(jsonContent)
AssertThat(testing, buffer.String(), is.ValueContaining("Generator Test"))
}()
}
}
func TestTestOutputGenerator_FileExtension(t *testing.T) {
AssertThat(t, underTest.ContentType(), is.EqualTo("application/json"))
}
func TestTestOutputGenerator_Errors(t *testing.T) {
localUnderTest := jsonspec.NewTestOutputGenerator()
jsonMarshaller := localUnderTest.MarshalJSON
defer func() {
recovered := recover()
localUnderTest.MarshalJSON = jsonMarshaller
AssertThat(t, recovered, is.Not(is.Nil()))
}()
localUnderTest.MarshalJSON = func(v interface{}) ([]byte, error) {
return nil, errors.New("bugger")
}
localUnderTest.Generate(newPageData(false, true))
}
func fileIsConverted() {
buffer := new(bytes.Buffer)
buffer.ReadFrom(underTest.Generate(newPageData(true, true)))
jsonString = buffer.String()
}
func newPageData(skipped bool, failed bool) generator.PageData {
var testData []generator.TestData
capturedIO := make(map[interface{}]interface{})
capturedIO["foob"] = "barb"
interestingGivens := make(map[interface{}]interface{})
interestingGivens["faff"] = "flap"
testData = append(testData, generator.TestData{
TestTitle: "test title",
ParsedTestContent: base.ParsedTestContent{
GivenWhenThen: []string{"given", "when", "then"},
Comment: []string{"Fooing is best", "done with friends"},
},
CapturedIO: capturedIO,
InterestingGivens: interestingGivens,
TestResult: generator.TestResult{
Failed: failed,
Skipped: skipped,
TestOutput: "well alrighty then",
TestID: "abc2124",
},
})
return generator.PageData{
TestData: testData,
Title: "Generator Test",
}
}