-
Notifications
You must be signed in to change notification settings - Fork 33
/
reference_test.go
131 lines (125 loc) · 2.65 KB
/
reference_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
package schematic
import (
"reflect"
"testing"
)
var refResolveTests = []struct {
Ref string
Schema *Schema
Resolved *Schema
}{
{
Ref: "#/definitions/uuid",
Schema: &Schema{
Definitions: map[string]*Schema{
"uuid": {
Title: "Identifier",
},
},
},
Resolved: &Schema{
Title: "Identifier",
},
},
{
Ref: "#/definitions/struct/definitions/uuid",
Schema: &Schema{
Definitions: map[string]*Schema{
"struct": {
Definitions: map[string]*Schema{
"uuid": {
Title: "Identifier",
},
},
},
},
},
Resolved: &Schema{
Title: "Identifier",
},
},
}
func TestReferenceResolve(t *testing.T) {
for i, rt := range refResolveTests {
ref := Reference(rt.Ref)
rsl := ref.Resolve(rt.Schema)
if !reflect.DeepEqual(rsl, rt.Resolved) {
t.Errorf("%d: resolved schema don't match, got %v, wants %v", i, rsl, rt.Resolved)
}
}
}
var hrefTests = []struct {
HRef string
Schema *Schema
Order []string
Resolved map[string]*Schema
}{
{
HRef: "/app/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fuuid)}",
Schema: &Schema{
Definitions: map[string]*Schema{
"app": {
Definitions: map[string]*Schema{
"uuid": {
Title: "Identifier",
},
},
},
},
},
Order: []string{"appUUID"},
Resolved: map[string]*Schema{
"appUUID": {
Title: "Identifier",
},
},
},
{
HRef: "/app/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fidentity)}/struct/{(%23%2Fdefinitions%2Fstruct%2Fdefinitions%2Fidentity)}",
Schema: &Schema{
Definitions: map[string]*Schema{
"app": {
Definitions: map[string]*Schema{
"identity": {
Title: "App Identifier",
},
},
},
"struct": {
Definitions: map[string]*Schema{
"identity": {
Title: "Struct Identifier",
},
},
},
},
},
Order: []string{"appIdentity", "structIdentity"},
Resolved: map[string]*Schema{
"appIdentity": {
Title: "App Identifier",
},
"structIdentity": {
Title: "Struct Identifier",
},
},
},
}
func TestHREfResolve(t *testing.T) {
for i, ht := range hrefTests {
href := NewHRef(ht.HRef)
href.Resolve(ht.Schema, ResolvedSet{})
if !reflect.DeepEqual(href.Order, ht.Order) {
t.Errorf("%d: resolved order don't match, got %v, wants %v", i, href.Order, ht.Order)
}
if !reflect.DeepEqual(href.Schemas, ht.Resolved) {
t.Errorf("%d: resolved schemas don't match, got %v, wants %v", i, href.Schemas, ht.Resolved)
}
}
}
func TestHRefString(t *testing.T) {
href := NewHRef("/app/{(%23%2Fdefinitions%2Fstruct%2Fdefinitions%2Fuuid)}")
if href.String() != "/app/%v" {
t.Errorf("wants %v, got %v", "/app/%v", href.String())
}
}