/
gojsonvalidator_test.go
156 lines (123 loc) · 4.28 KB
/
gojsonvalidator_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
package main
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
. "github.com/jacobsa/oglematchers"
"os"
"flag"
"io/ioutil"
"fmt"
"reflect"
"path/filepath"
)
func ShouldMatch(actual interface{}, expected ...interface{}) string {
if (len(expected) != 1) {
return fmt.Sprintf("This assertion requires exactly %d comparison values (you provided %d).",len(expected))
}
value,valueIsString := actual.(string)
regex,regexIsString := expected[0].(string)
if !valueIsString || !regexIsString {
return fmt.Sprintf("Both arguments to this assertion must be strings (you provided %v and %v).",reflect.TypeOf(actual),reflect.TypeOf(expected[0]))
}
matcher := MatchesRegexp(regex)
if err := matcher.Matches(value); err == nil {
return ""
} else {
return fmt.Sprintf("Expected '%v' to match '%v' (but it didn't)!",regex,value)
}
}
type IORedirecter struct {
oldRef **os.File
oldRefValue *os.File
writer *os.File
reader *os.File
}
func NewIORedirecter(fd **os.File) (result *IORedirecter) {
result = &IORedirecter{
oldRef: fd,
oldRefValue: *fd,
}
var err error
result.reader,result.writer,err = os.Pipe()
if err != nil {
return nil
}
return
}
func (ior *IORedirecter) start() {
*ior.oldRef = ior.writer
}
func (ior *IORedirecter) closeAndGet() string {
ior.writer.Close()
bytes,_ := ioutil.ReadAll(ior.reader)
*ior.oldRef = ior.oldRefValue
return string(bytes)
}
type TestCase struct {
schema string
documents []struct {
file string
isValid bool
}
}
func TestAll(t *testing.T) {
Convey("Validating JSON schemas should work",t,func() {
testMode = true
verbose = false
documents = nil
interactive = false
schemaFile = ""
flag.CommandLine = flag.NewFlagSet("test",flag.ContinueOnError)
stdoutRedirecter := NewIORedirecter(&os.Stdout)
stderrRedirecter := NewIORedirecter(&os.Stderr)
Convey("Parsing from STDIN should work",func() {
stdoutRedirecter.start()
stderrRedirecter.start()
os.Args = []string{"", "-s", filepath.Join("tests","products-schema.json"), "-i", "-v" }
r,w,_ := os.Pipe()
w.WriteString("[{\"id\":2,\"name\":\"An ice sculpture\",\"price\":12.50,\"tags\":[\"cold\",\"ice\"],\"dimensions\":{\"length\":7.0,\"width\":12.0,\"height\":9.5},\"warehouseLocation\":{\"latitude\":-78.75,\"longitude\":20.4}},{\"id\":3,\"name\":\"A blue mouse\",\"price\":25.50,\"dimensions\":{\"length\":3.1,\"width\":1.0,\"height\":1.0},\"warehouseLocation\":{\"latitude\":54.4,\"longitude\":-32.7}}]")
w.Close()
os.Stdin = r
main()
So(stdoutRedirecter.closeAndGet(),ShouldMatch,"^JSON is valid")
So(stderrRedirecter.closeAndGet(),ShouldEqual,"")
So(exitCode,ShouldEqual,0)
})
Convey("Valid products document should work",func() {
stdoutRedirecter.start()
stderrRedirecter.start()
os.Args = []string{"", "-s", filepath.Join("tests","products-schema.json"), "-f", filepath.Join("tests","products.json") }
main()
So(stdoutRedirecter.closeAndGet(),ShouldEqual,"")
So(stderrRedirecter.closeAndGet(),ShouldEqual,"")
So(exitCode,ShouldEqual,0)
})
Convey("Verbose flag should work",func() {
stdoutRedirecter.start()
stderrRedirecter.start()
os.Args = []string{"", "-s", filepath.Join("tests","products-schema.json"), "-f", filepath.Join("tests","products.json"), "-v" }
main()
So(stdoutRedirecter.closeAndGet(),ShouldMatch,"(?s)^JSON is valid")
So(stderrRedirecter.closeAndGet(),ShouldEqual,"")
So(exitCode,ShouldEqual,0)
})
Convey("Invalid products document should not work",func() {
stdoutRedirecter.start()
stderrRedirecter.start()
os.Args = []string{"", "-s", filepath.Join("tests","products-schema.json"), "-f", filepath.Join("tests","products-invalid.json") }
main()
So(stdoutRedirecter.closeAndGet(),ShouldEqual,"")
So(stderrRedirecter.closeAndGet(),ShouldMatch,"(?s)^JSON is invalid.+Invalid type. Expected: number, given: string\n$")
So(exitCode,ShouldEqual,1)
})
Convey("Invalid products schema should not work",func() {
stdoutRedirecter.start()
stderrRedirecter.start()
os.Args = []string{"", "-s", filepath.Join("tests","products-schema.json"), "-f", "bogus.txt" }
main()
So(stdoutRedirecter.closeAndGet(),ShouldEqual,"")
So(stderrRedirecter.closeAndGet(),ShouldMatch,"(?s)^invalid value \"bogus.txt\" for flag -f:")
So(exitCode,ShouldEqual,0)
})
})
}