forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
location_test.go
194 lines (168 loc) · 4.7 KB
/
location_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
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
package ogen
import (
_ "embed"
"fmt"
"strings"
"testing"
"github.com/go-faster/yaml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ogen-go/ogen/location"
)
func TestLocator(t *testing.T) {
const testdata = `{
"openapi": "3.1.0",
"info": {
"title": "API",
"version": "0.1.0"
},
"components": {
"schemas": {
"User": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "Thomas A. Anderson",
"required": true
}
}
}
}
}
}`
ubool := func(input []byte) error {
var target bool
return yaml.Unmarshal(input, &target)
}
tests := []struct {
input string
target func([]byte) error
line, column int
}{
{"{}", ubool, 1, 1},
{"{}\n", ubool, 1, 1},
{"\x20{}", ubool, 1, 2},
{"\x20{}\n", ubool, 1, 2},
{"\n{}", ubool, 2, 1},
{"\n{}\n", ubool, 2, 1},
{"\n\n{}", ubool, 3, 1},
{"\n\n{}\n", ubool, 3, 1},
{"\x20\n{}", ubool, 2, 1},
{"\x20\n{}\n", ubool, 2, 1},
{"{\n\t\"a\":1,\n\t\"b\":2\n}", func(input []byte) error {
var target struct {
A int `json:"a"`
B bool `json:"b"`
}
return yaml.Unmarshal(input, &target)
}, 3, 6},
{"[\n0,\ntrue\n]", func(input []byte) error {
var target []int
return yaml.Unmarshal(input, &target)
}, 3, 1},
{testdata, func(input []byte) error {
var target *Spec
return yaml.Unmarshal(input, &target)
}, 15, 25},
}
for i, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) {
input := []byte(tt.input)
a := require.New(t)
err := tt.target(input)
a.Error(err)
msg := err.Error()
prefix := fmt.Sprintf("line %d:", tt.line)
a.Truef(strings.Contains(msg, prefix), "input: %q,\ncontains: %q,\nmsg: %q", tt.input, prefix, msg)
})
}
}
var (
//go:embed _testdata/location/location_spec.json
locationSpecJSON string
//go:embed _testdata/location/location_spec.yml
locationSpecYAML string
)
func TestPosition(t *testing.T) {
createEqualPos := func(a *assert.Assertions, data []byte) func(l location.Locatable, line, column int) {
var lines location.Lines
lines.Collect(data)
return func(l location.Locatable, line, column int) {
t.Helper()
pos, ok := l.Position()
a.True(ok)
getLine := func(n int) string {
start, end := lines.Line(n)
// Offset points exactly to the newline, trim it.
return strings.Trim(string(data[start:end]), "\n\r")
}
type comparePos struct {
Line, Column int
Data string
}
a.Equal(
comparePos{line, column, getLine(line)},
comparePos{pos.Line, pos.Column, getLine(pos.Line)},
)
}
}
t.Run("JSON", func(t *testing.T) {
a := assert.New(t)
equalLoc := createEqualPos(a, []byte(locationSpecJSON))
locationSpec, err := Parse([]byte(locationSpecJSON))
require.NoError(t, err)
var (
foo = locationSpec.Paths["/foo"]
post = foo.Post
get = foo.Get
body = post.RequestBody
media = body.Content["application/json"]
schema = media.Schema
)
// Compare PathItem.
equalLoc(&foo.Common.Locator, 8, 13)
// Compare post
equalLoc(&post.Common.Locator, 9, 15)
// Compare Parameters.
equalLoc(&post.Parameters[0].Common.Locator, 11, 11)
equalLoc(&post.Parameters[1].Common.Locator, 18, 11)
// Compare RequestBody.
equalLoc(&body.Common.Locator, 26, 24)
equalLoc(&media.Common.Locator, 28, 33)
equalLoc(&schema.Common.Locator, 29, 25)
// Compare get.
equalLoc(&get.Common.Locator, 48, 14)
user := locationSpec.Components.Schemas["User"]
equalLoc(&user.Common.Locator, 59, 15)
equalLoc(&user.Properties[0].Schema.Common.Locator, 62, 19)
})
t.Run("YAML", func(t *testing.T) {
a := assert.New(t)
equalLoc := createEqualPos(a, []byte(locationSpecYAML))
locationSpec, err := Parse([]byte(locationSpecYAML))
require.NoError(t, err)
var (
foo = locationSpec.Paths["/foo"]
post = foo.Post
body = post.RequestBody
requestMedia = body.Content["application/json"]
requestSchema = requestMedia.Schema
)
// FIXME(tdakkota): parser sets map/seq location to the first element.
// Compare PathItem and Operation.
equalLoc(&foo.Common.Locator, 7, 5)
equalLoc(&post.Common.Locator, 8, 7)
// Compare Parameters.
equalLoc(&post.Parameters[0].Common.Locator, 9, 11)
equalLoc(&post.Parameters[1].Common.Locator, 13, 11)
// Compare RequestBody.
equalLoc(&body.Common.Locator, 18, 9)
equalLoc(&requestMedia.Common.Locator, 20, 13)
equalLoc(&requestSchema.Common.Locator, 21, 15)
user := locationSpec.Components.Schemas["User"]
equalLoc(&user.Common.Locator, 36, 7)
equalLoc(&user.Properties[0].Schema.Common.Locator, 39, 11)
})
}