-
Notifications
You must be signed in to change notification settings - Fork 1
/
map_test.go
92 lines (73 loc) · 2.02 KB
/
map_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
package schema
import "testing"
func TestMap_Success(t *testing.T) {
data := dataFromJSON(t, `
{
"id": 12,
"name": "Max Mustermann"
}`)
err := Map{
"id": IsInteger,
"name": "Max Mustermann",
}.Match(data)
if err != nil {
t.Fatal(err)
}
}
func TestMap_ExtraKeys(t *testing.T) {
data := dataFromJSON(t, `{"hans":true, "wurst": "def"}`)
err := Map{}.Match(data)
if err == nil {
t.Fatal("Expected error got none")
}
// check twice because of non-deterministic order of keys
if err.Errors[selfField] != `Found extra keys: "hans, wurst"` {
t.Fatalf("wrong error msg: %s", err.Error())
}
}
func TestMap_MissingKeys(t *testing.T) {
data := dataFromJSON(t, `{}`)
err := Map{"id": 12}.Match(data)
if err == nil {
t.Fatal("Expected error got none")
}
if err.Errors["."] != `Missing keys: "id"` {
t.Fatalf("wrong error msg: %s", err.Error())
}
}
func TestMap_WrongValue(t *testing.T) {
data := dataFromJSON(t, `{"id": 47, "name": "hans", "footsize": "12 inches"}`)
err := Map{"id": 12, "name": "wurst", "footsize": 12}.Match(data)
if err == nil {
t.Fatal("Expected error got none")
}
if err.Errors["id"] != `47 != 12` {
t.Fatalf("wrong error msg: %s", err.Errors["id"])
}
if err.Errors["name"] != `"hans" != "wurst"` {
t.Fatalf("wrong error msg: %s", err.Errors["name"])
}
if err.Errors["footsize"] != `"12 inches" != 12` {
t.Fatalf("wrong error msg: %s", err.Errors["footsize"])
}
}
func TestMapIncluding_Success(t *testing.T) {
data := dataFromJSON(t, `{"id": 47, "name": "hans", "footsize": "12 inches"}`)
err := MapIncluding{"id": 47, "name": "hans"}.Match(data)
if err != nil {
t.Fatal(err)
}
}
func TestMapIncluding_Failure(t *testing.T) {
data := dataFromJSON(t, `{"id": 12}`)
err := MapIncluding{"id": 47, "name": "hans"}.Match(data)
if err == nil {
t.Fatal("expected error")
}
if err.Errors[selfField] != `Missing keys: "name"` {
t.Fatalf("wrong error msg: %s", err.Errors[selfField])
}
if err.Errors["id"] != `12 != 47` {
t.Fatalf("wrong error msg: %s", err.Errors["id"])
}
}