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
|
package seq
import "testing"
type CPU struct {
Brand string `json:"brand"`
Model string `json:"model"`
}
type Robot struct {
Legs int `json:"legs"`
Arms int `json:"arms"`
Name string `json:"name"`
LikesGold bool `json:"likesGold"`
CPU CPU `json:"cpu"`
}
type Party struct {
Rating []int `json:"rating"`
Seating map[string]*Robot `json:"seating"`
}
func TestBool(t *testing.T) {
type dto struct {
Boolean bool `json:"boolean"`
}
result := Test(Map{"boolean": true}, &dto{true})
expectOk(result, t)
}
func TestNestedObject(t *testing.T) {
expect := Map{
"cpu.brand": "intel",
}
actual := Robot{
Legs: 2,
Arms: 2,
CPU: CPU{
Brand: "intel",
},
}
result := expect.Test(actual)
expectOk(result, t)
}
func TestPartialSimpleObject(t *testing.T) {
expect := Map{
"legs": 2,
}
actual := Robot{
Legs: 2,
Arms: 2,
}
result := expect.Test(actual)
expectOk(result, t)
}
func TestExactSimpleObject(t *testing.T) {
expect := Map{
"legs": 2,
"arms": 2,
"name": "benny",
"likesGold": false,
}
actual := Robot{
Legs: 2,
Arms: 2,
Name: "benny",
LikesGold: false,
}
result := expect.Test(actual)
expectOk(result, t)
}
func TestWrongSimpleObject(t *testing.T) {
expect := Map{
"legs": 2,
"arms": 2,
"name": "benny",
}
actual := Robot{
Legs: 2,
Arms: 2,
Name: "Bender",
LikesGold: true,
}
result := expect.Test(actual)
expectFail(result, t)
}
func TestSimpleArray(t *testing.T) {
expect := Map{
"[0]": Map{
"name": "R2D2",
},
"[1]": Map{
"name": "C3PO",
},
}
result := expect.Test([]*Robot{
&Robot{Name: "R2D2"},
&Robot{Name: "C3PO"},
})
expectOk(result, t)
}
func TestIndexSpecifierFolding(t *testing.T) {
type item struct {
Name string `json:"name"`
}
type dto struct {
Items []item `json:"items"`
}
actual := dto{
Items: []item{
item{Name: "this"},
},
}
expect := Map{
"items": Map{
"length": 1,
"[0]": Map{
"name": "this",
},
},
}
result := Test(expect, actual)
expectOk(result, t)
}
func TestExactComplexObject(t *testing.T) {
actual := &Party{
Rating: []int{4, 5, 4},
Seating: map[string]*Robot{
"front": &Robot{
Name: "R2D2",
Arms: 1,
Legs: 3,
},
"back": &Robot{
Name: "Marvin",
Legs: 2,
Arms: 2,
},
"right": &Robot{
Name: "C3PO",
Legs: 2,
Arms: 2,
},
},
}
expect := Map{
// array
"rating.length": 3,
"rating[1]": 5,
// flat path with value terminator
"seating.front.name": "R2D2",
"seating.front.arms": "1",
"seating.front.legs": 3,
// flat path with map terminator
"seating.right": Map{
"name": "C3PO",
},
// flat path with object terminator
"seating.back": &Robot{
Name: "Marvin",
Legs: 2,
Arms: 2,
},
}
result := expect.Test(actual)
expectOk(result, t)
}
func expectOk(result *Result, t *testing.T) {
if result.Ok() {
return
}
t.Error("Test should pass")
for i, l := range result.Issues {
t.Log(i, l)
}
}
func expectFail(result *Result, t *testing.T) {
if !result.Ok() {
return
}
t.Error("Test should fail")
for i, l := range result.Issues {
t.Log(i, l)
}
}
|