-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathexample_test.go
More file actions
209 lines (200 loc) · 5.67 KB
/
Copy pathexample_test.go
File metadata and controls
209 lines (200 loc) · 5.67 KB
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
209
package raml
import (
"testing"
orderedmap "github.com/wk8/go-ordered-map/v2"
"gopkg.in/yaml.v3"
)
// makeTestExample builds an Example backed by a fresh RAML instance.
// CustomDomainProperties is always initialised so domain-extension tests work out of the box.
func makeTestExample(t *testing.T, r *RAML) *Example {
t.Helper()
return &Example{
raml: r,
CustomDomainProperties: orderedmap.New[string, *DomainExtension](0),
}
}
// TestExample_decode verifies that individual key/value pairs are decoded into Example fields.
func TestExample_decode(t *testing.T) {
scalar := func(val string) *yaml.Node { return &yaml.Node{Kind: yaml.ScalarNode, Value: val} }
mapping := func(kv ...*yaml.Node) *yaml.Node {
return &yaml.Node{Kind: yaml.MappingNode, Content: kv}
}
tests := []struct {
name string
key string
valueNode *yaml.Node
wantErr bool
}{
{
name: "strict=true is accepted",
key: "strict",
valueNode: scalar("true"),
},
{
name: "displayName scalar is accepted",
key: "displayName",
valueNode: scalar("My Display Name"),
},
{
name: "description scalar is accepted",
key: "description",
valueNode: scalar("A description"),
},
{
name: "custom domain extension is accepted",
key: "(custom)",
valueNode: mapping(scalar("key"), scalar("value")),
},
{
name: "strict with mapping node is rejected",
key: "strict",
valueNode: &yaml.Node{Kind: yaml.MappingNode, Value: "invalid"},
wantErr: true,
},
{
name: "displayName with mapping node is rejected",
key: "displayName",
valueNode: &yaml.Node{Kind: yaml.MappingNode, Value: "invalid"},
wantErr: true,
},
{
name: "description with mapping node is rejected",
key: "description",
valueNode: &yaml.Node{Kind: yaml.MappingNode, Value: "invalid"},
wantErr: true,
},
{
name: "custom domain extension with invalid content is rejected",
key: "()",
valueNode: mapping(
&yaml.Node{Kind: yaml.ScalarNode, Value: "key", Tag: "!!int"},
&yaml.Node{Kind: yaml.MappingNode, Value: "invalid", Tag: "!!int"},
),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := makeTestRAML(t)
ex := makeTestExample(t, r)
keyNode := &yaml.Node{Value: tt.key}
if err := ex.decode(keyNode, tt.valueNode, "test.raml"); (err != nil) != tt.wantErr {
t.Errorf("decode() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// TestExample_fill verifies that a mapping node is parsed into an Example via the "value" key.
func TestExample_fill(t *testing.T) {
scalar := func(val string) *yaml.Node { return &yaml.Node{Kind: yaml.ScalarNode, Value: val} }
tests := []struct {
name string
value *yaml.Node
wantErr bool
}{
{
name: "mapping with value key is accepted",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("value"), scalar("example"),
}},
},
{
name: "mapping without value key returns ErrValueKeyNotFound",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("other"), scalar("data"),
}},
wantErr: true,
},
{
name: "value key present but subsequent decode fails",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("value"), scalar("data"),
scalar("strict"), scalar("123"),
}},
wantErr: true,
},
{
name: "value is present but makeRootNode fails on invalid tag",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("value"),
{Kind: yaml.MappingNode, Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "key", Tag: "!!int"},
{Kind: yaml.ScalarNode, Value: "val", Tag: "!!int"},
}},
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := makeTestRAML(t)
ex := makeTestExample(t, r)
if err := ex.fill("test.raml", tt.value); (err != nil) != tt.wantErr {
t.Errorf("fill() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// TestRAML_makeExample verifies the full example construction pipeline.
func TestRAML_makeExample(t *testing.T) {
scalar := func(val string) *yaml.Node { return &yaml.Node{Kind: yaml.ScalarNode, Value: val} }
tests := []struct {
name string
value *yaml.Node
wantErr bool
check func(*Example)
}{
{
name: "mapping with value key — fill path sets Data",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("value"), scalar("example"),
}},
check: func(ex *Example) {
if ex.Data == nil {
t.Error("Data is nil, want non-nil")
}
},
},
{
name: "scalar value — direct node path sets Data",
value: scalar("hello"),
check: func(ex *Example) {
if ex.Data == nil {
t.Error("Data is nil, want non-nil")
}
},
},
{
name: "mapping without value key — treated as object value, Data set",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("key"), scalar("val"),
}},
check: func(ex *Example) {
if ex.Data == nil {
t.Error("Data is nil, want non-nil")
}
},
},
{
name: "mapping fill error propagated",
value: &yaml.Node{Kind: yaml.MappingNode, Content: []*yaml.Node{
scalar("value"), scalar("data"),
scalar("strict"), scalar("not-a-bool"),
}},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := makeTestRAML(t)
got, err := r.makeExample(tt.value, "ex", "test.raml")
if (err != nil) != tt.wantErr {
t.Errorf("makeExample() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && tt.check != nil {
tt.check(got)
}
})
}
}