-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_test.go
More file actions
305 lines (252 loc) · 8.66 KB
/
parser_test.go
File metadata and controls
305 lines (252 loc) · 8.66 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
"fmt"
"go/ast"
"regexp"
"strings"
"testing"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
"github.com/go-openapi/spec"
)
// only used within this group of tests but never used within actual code base.
func newSchemaAnnotationParser(goName string) *schemaAnnotationParser {
return &schemaAnnotationParser{GoName: goName, rx: rxModelOverride}
}
type schemaAnnotationParser struct {
GoName string
Name string
rx *regexp.Regexp
}
func (sap *schemaAnnotationParser) Matches(line string) bool {
return sap.rx.MatchString(line)
}
func (sap *schemaAnnotationParser) Parse(lines []string) error {
if sap.Name != "" {
return nil
}
if len(lines) > 0 {
for _, line := range lines {
matches := sap.rx.FindStringSubmatch(line)
if len(matches) > 1 && len(matches[1]) > 0 {
sap.Name = matches[1]
return nil
}
}
}
return nil
}
func TestSectionedParser_TitleDescription(t *testing.T) {
const (
text = `This has a title, separated by a whitespace line
In this example the punctuation for the title should not matter for swagger.
For go it will still make a difference though.
`
text2 = `This has a title without whitespace.
The punctuation here does indeed matter. But it won't for go.
`
text3 = `This has a title, and markdown in the description
See how markdown works now, we can have lists:
+ first item
+ second item
+ third item
[Links works too](http://localhost)
`
text4 = `This has whitespace sensitive markdown in the description
|+ first item
| + nested item
| + also nested item
Sample code block:
| fmt.Println("Hello World!")
`
)
var err error
st := §ionedParser{}
st.setTitle = func(_ []string) {}
err = st.Parse(ascg(text))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title, separated by a whitespace line"}, st.Title())
assert.Equal(t, []string{"In this example the punctuation for the title should not matter for swagger.", "For go it will still make a difference though."}, st.Description())
st = §ionedParser{}
st.setTitle = func(_ []string) {}
err = st.Parse(ascg(text2))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title without whitespace."}, st.Title())
assert.Equal(t, []string{"The punctuation here does indeed matter. But it won't for go."}, st.Description())
st = §ionedParser{}
st.setTitle = func(_ []string) {}
err = st.Parse(ascg(text3))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title, and markdown in the description"}, st.Title())
assert.Equal(t, []string{
"See how markdown works now, we can have lists:", "",
"+ first item", "+ second item", "+ third item", "",
"[Links works too](http://localhost)",
}, st.Description())
st = §ionedParser{}
st.setTitle = func(_ []string) {}
err = st.Parse(ascg(text4))
require.NoError(t, err)
assert.Equal(t, []string{"This has whitespace sensitive markdown in the description"}, st.Title())
assert.Equal(t, []string{"+ first item", " + nested item", " + also nested item", "", "Sample code block:", "", " fmt.Println(\"Hello World!\")"}, st.Description())
}
func dummyBuilder() schemaValidations {
return schemaValidations{new(spec.Schema)}
}
func TestSectionedParser_TagsDescription(t *testing.T) {
const (
block = `This has a title without whitespace.
The punctuation here does indeed matter. But it won't for go.
minimum: 10
maximum: 20
`
block2 = `This has a title without whitespace.
The punctuation here does indeed matter. But it won't for go.
minimum: 10
maximum: 20
`
)
var err error
st := §ionedParser{}
st.setTitle = func(_ []string) {}
st.taggers = []tagParser{
{"Maximum", false, false, nil, &setMaximum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMaximumFmt, ""))}},
{"Minimum", false, false, nil, &setMinimum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMinimumFmt, ""))}},
{"MultipleOf", false, false, nil, &setMultipleOf{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMultipleOfFmt, ""))}},
}
err = st.Parse(ascg(block))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title without whitespace."}, st.Title())
assert.Equal(t, []string{"The punctuation here does indeed matter. But it won't for go."}, st.Description())
assert.Len(t, st.matched, 2)
_, ok := st.matched["Maximum"]
assert.TrueT(t, ok)
_, ok = st.matched["Minimum"]
assert.TrueT(t, ok)
st = §ionedParser{}
st.setTitle = func(_ []string) {}
st.taggers = []tagParser{
{"Maximum", false, false, nil, &setMaximum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMaximumFmt, ""))}},
{"Minimum", false, false, nil, &setMinimum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMinimumFmt, ""))}},
{"MultipleOf", false, false, nil, &setMultipleOf{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMultipleOfFmt, ""))}},
}
err = st.Parse(ascg(block2))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title without whitespace."}, st.Title())
assert.Equal(t, []string{"The punctuation here does indeed matter. But it won't for go."}, st.Description())
assert.Len(t, st.matched, 2)
_, ok = st.matched["Maximum"]
assert.TrueT(t, ok)
_, ok = st.matched["Minimum"]
assert.TrueT(t, ok)
}
func TestSectionedParser_Empty(t *testing.T) {
const block = `swagger:response someResponse`
var err error
st := §ionedParser{}
st.setTitle = func(_ []string) {}
ap := newSchemaAnnotationParser("SomeResponse")
ap.rx = rxResponseOverride
st.annotation = ap
err = st.Parse(ascg(block))
require.NoError(t, err)
assert.Empty(t, st.Title())
assert.Empty(t, st.Description())
assert.Empty(t, st.taggers)
assert.EqualT(t, "SomeResponse", ap.GoName)
assert.EqualT(t, "someResponse", ap.Name)
}
func testSectionedParserWithBlock(
t *testing.T,
block string,
expectedMatchedCount int,
maximumExpected bool,
) {
t.Helper()
st := §ionedParser{}
st.setTitle = func(_ []string) {}
ap := newSchemaAnnotationParser("SomeModel")
st.annotation = ap
st.taggers = []tagParser{
{"Maximum", false, false, nil, &setMaximum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMaximumFmt, ""))}},
{"Minimum", false, false, nil, &setMinimum{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMinimumFmt, ""))}},
{"MultipleOf", false, false, nil, &setMultipleOf{dummyBuilder(), regexp.MustCompile(fmt.Sprintf(rxMultipleOfFmt, ""))}},
}
err := st.Parse(ascg(block))
require.NoError(t, err)
assert.Equal(t, []string{"This has a title without whitespace."}, st.Title())
assert.Equal(t, []string{"The punctuation here does indeed matter. But it won't for go."}, st.Description())
assert.Len(t, st.matched, expectedMatchedCount)
_, ok := st.matched["Maximum"]
assert.EqualT(t, maximumExpected, ok)
_, ok = st.matched["Minimum"]
assert.TrueT(t, ok)
assert.EqualT(t, "SomeModel", ap.GoName)
assert.EqualT(t, "someModel", ap.Name)
}
func TestSectionedParser_SkipSectionAnnotation(t *testing.T) {
const block = `swagger:model someModel
This has a title without whitespace.
The punctuation here does indeed matter. But it won't for go.
minimum: 10
maximum: 20
`
testSectionedParserWithBlock(t, block, 2, true)
}
func TestSectionedParser_TerminateOnNewAnnotation(t *testing.T) {
const block = `swagger:model someModel
This has a title without whitespace.
The punctuation here does indeed matter. But it won't for go.
minimum: 10
swagger:meta
maximum: 20
`
testSectionedParserWithBlock(t, block, 1, false)
}
func ascg(txt string) *ast.CommentGroup {
var cg ast.CommentGroup
for line := range strings.SplitSeq(txt, "\n") {
var cmt ast.Comment
cmt.Text = "// " + line
cg.List = append(cg.List, &cmt)
}
return &cg
}
func TestShouldAcceptTag(t *testing.T) {
tagTests := []struct {
tags []string
includeTags map[string]bool
excludeTags map[string]bool
expected bool
}{
{nil, nil, nil, true},
{[]string{"app"}, map[string]bool{"app": true}, nil, true},
{[]string{"app"}, nil, map[string]bool{"app": true}, false},
}
for _, tt := range tagTests {
actual := shouldAcceptTag(tt.tags, tt.includeTags, tt.excludeTags)
assert.EqualT(t, tt.expected, actual)
}
}
func TestShouldAcceptPkg(t *testing.T) {
pkgTests := []struct {
path string
includePkgs []string
excludePkgs []string
expected bool
}{
{"", nil, nil, true},
{"", nil, []string{"app"}, true},
{"", []string{"app"}, nil, false},
{"app", []string{"app"}, nil, true},
{"app", nil, []string{"app"}, false},
{"vendor/app", []string{"app"}, nil, true},
{"vendor/app", nil, []string{"app"}, false},
}
for _, tt := range pkgTests {
actual := shouldAcceptPkg(tt.path, tt.includePkgs, tt.excludePkgs)
assert.EqualT(t, tt.expected, actual)
}
}