-
Notifications
You must be signed in to change notification settings - Fork 8
/
scribe_test.go
176 lines (163 loc) · 3.98 KB
/
scribe_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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor:
// - Aaron Meihm ameihm@mozilla.com
package scribe_test
import (
"fmt"
"strings"
"testing"
"github.com/mozilla/scribe"
)
func Example() {
docstr := `---
objects:
- object: raw
raw:
identifiers:
- identifier: test
value: Example
tests:
- test: example
name: an example test
object: raw
regexp:
value: Example
`
rdr := strings.NewReader(docstr)
// Initialize the library.
scribe.Bootstrap()
// Load the document from the reader, returning a Document type.
// LoadDocument() will also call ValidateDocument() to check for any
// consistency issues.
doc, err := scribe.LoadDocument(rdr)
if err != nil {
fmt.Println("LoadDocument:", err)
return
}
// Analyze the document.
err = scribe.AnalyzeDocument(doc)
if err != nil {
fmt.Println("AnalyzeDocument:", err)
return
}
// Grab the results for the test, most of the time you would loop
// through the results of GetTestIdentifiers() rather then call a result
// directly.
result, err := scribe.GetResults(&doc, "example")
if err != nil {
fmt.Println("GetResults:", err)
return
}
if result.MasterResult {
fmt.Println("true")
} else {
fmt.Println("false")
}
// Output: true
}
var resultsFormattingDoc = `
{
"objects": [
{
"object": "raw",
"raw": {
"identifiers": [
{
"identifier": "test",
"value": "value"
}
]
}
}
],
"tests": [
{
"test": "test1",
"name": "a test",
"object": "raw",
"regexp": {
"value": "^va.*e$"
}
}
]
}
`
func genericTestExec(t *testing.T, documentStr string) *scribe.Document {
rdr := strings.NewReader(documentStr)
scribe.Bootstrap()
scribe.TestHooks(true)
doc, err := scribe.LoadDocument(rdr)
if err != nil {
t.Fatalf("scribe.LoadDocument: %v", err)
}
err = scribe.AnalyzeDocument(doc)
if err != nil {
t.Fatalf("scribe.AnalyzeDocument: %v", err)
}
// Get results for each test and make sure the result matches what
// expectedresult is set to
testids := doc.GetTestIdentifiers()
for _, x := range testids {
stest, err := doc.GetTest(x)
if err != nil {
t.Fatalf("Document.GetTest: %v", err)
}
sres, err := scribe.GetResults(&doc, x)
if err != nil {
t.Fatalf("scribe.GetResults: %v", err)
}
if stest.ExpectError {
if !sres.IsError {
t.Fatalf("test %v should have been an error", x)
}
} else {
if sres.IsError {
t.Fatalf("test %v resulted in an error", x)
}
if sres.MasterResult != stest.ExpectedResult {
t.Fatalf("result incorrect for test %v", x)
}
}
}
return &doc
}
func TestResultsFormatting(t *testing.T) {
rdr := strings.NewReader(resultsFormattingDoc)
scribe.Bootstrap()
scribe.TestHooks(true)
doc, err := scribe.LoadDocument(rdr)
if err != nil {
t.Fatalf("scribe.LoadDocument: %v", err)
}
err = scribe.AnalyzeDocument(doc)
if err != nil {
t.Fatalf("scribe.AnalyzeDocument: %v", err)
}
res, err := scribe.GetResults(&doc, "test1")
if err != nil {
t.Fatalf("scribe.GetResults: %v", err)
}
slr := res.SingleLineResults()
if len(slr) != 2 {
t.Fatalf("single line results incorrect line count")
}
if slr[0] != "master [true] name:\"a test\" id:\"test1\" hastrue:true error:\"\"" {
t.Fatalf("single line result master has incorrect format")
}
if slr[1] != "sub [true] name:\"a test\" id:\"test1\" identifier:\"test\"" {
t.Fatalf("single line result sub has incorrect format")
}
hrr_compare := `result for "a test" (test1)
master result: true
[true] identifier: "test"`
if res.String() != hrr_compare {
t.Fatalf("human readable result has incorrect format")
}
json_compare := `{"testid":"test1","name":"a test","description":"","iserror":false,"error":"","masterresult":true,"hastrueresults":true,"results":[{"result":true,"identifier":"test"}]}`
if res.JSON() != json_compare {
t.Fatalf("json result has incorrect format")
}
}