-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
430 lines (408 loc) · 12.4 KB
/
parser_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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package parser_test
import (
"errors"
"os"
"testing"
"github.com/dnitsch/async-api-generator/internal/gendoc"
"github.com/dnitsch/async-api-generator/internal/lexer"
"github.com/dnitsch/async-api-generator/internal/parser"
log "github.com/dnitsch/simplelog"
)
var lexerSource = lexer.Source{FileName: "bar", FullPath: "/foo/bar"}
func Test_Initial_GenDocBlocks(t *testing.T) {
ttests := map[string]struct {
input string
expectedIdentifier string
expectedValue any
}{
"gendoc found and contains data": {`let x = 42;
//+gendoc category=message type=nameId id=MessageId parent=somechannel
//-gendoc
some other throw away
//+gendoc category=message type=nameId id=someid channelId=somechannel
stuff {
here string
}
//-gendoc
`, "category=message type=nameId", `stuff {
here string
}`},
"should not throw and error when content empty": {`let x = 42;
//+gendoc category=message type=nameId id=someid channelId=somechannel
ignore me
//-gendoc
//+gendoc category=message type=nameId id=someid channelId=somechannel
//-gendoc
`, "category=message type=nameId id=someid", ""},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
lexerSource.Input = tt.input
l := lexer.New(lexerSource)
p := parser.New(l, &parser.Config{}).WithLogger(log.New(os.Stderr, log.ErrorLvl))
parsed, errs := p.InitialParse()
if len(errs) > 0 {
t.Fatalf("parser had errors, expected <nil>\nerror: %v", errs)
}
if len(parsed) != 2 {
t.Fatalf("program.Statements does not contain 2 statements. got=%d",
len(parsed))
}
stmt := parsed[1]
if !testHelperGenDocBlock(t, stmt, tt.expectedIdentifier, tt.expectedValue) {
return
}
})
}
}
func Test_Parse_Id_and_type_of_nodes(t *testing.T) {
ttests := map[string]struct {
input string
config *parser.Config
wantCatType parser.NodeCategory
wantUrn string
wantId string
wantParentId string
}{
"service succeed with serviceId inherited in single repo mode": {`let x = 42;
//+gendoc category=info type=description
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.ServiceNode, "urn:::foo", "foo", "",
},
"service succeed with serviceId inherited in markdown": {`let x = 42;
<!-- //+gendoc category=info type=description -->
this is some description
<!-- //-gendoc -->`,
&parser.Config{ServiceId: "foo"},
parser.ServiceNode, "urn:::foo", "foo", "",
},
"service succeed with serviceId specified": {`let x = 42;
//+gendoc category=server type=description id=baz
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.ServiceNode, "urn:::baz", "baz", "",
},
"service succeed with serviceId extracted": {`let x = 42;
//+gendoc category=root type=nameId
bazquxservice.sample
//-gendoc`,
&parser.Config{},
parser.ServiceNode, "urn:::bazquxservice.sample", "bazquxservice.sample", "",
},
"service succeed with serviceId enriched with business domain and bounded domain": {`let x = 42;
//+gendoc category=root type=nameId
bazquxservice.sample
//-gendoc`,
&parser.Config{BusinessDomain: "dom1", BoundedDomain: "area51"},
parser.ServiceNode, "urn:dom1:area51:bazquxservice.sample", "bazquxservice.sample", "",
},
"channel succeed with id specified": {`let x = 42;
//+gendoc category=channel type=description id=baz
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.ChannelNode, "", "baz", "foo",
},
"operation succeed with id specified": {`let x = 42;
//+gendoc category=pubOperation type=description id=baz parent=parentBaz
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.OperationNode, "", "baz", "parentBaz",
},
"operation succeed with parent fallback": {`let x = 42;
//+gendoc category=pubOperation type=description id=baz channelId=topic-bat
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.OperationNode, "", "baz", "topic-bat",
},
"message succeed with id specified": {`let x = 42;
//+gendoc category=message type=description id=baz parent=bar
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.MessageNode, "", "baz", "bar",
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
lexerSource.Input = tt.input
l := lexer.New(lexerSource)
p := parser.New(l, tt.config).WithLogger(log.New(os.Stderr, log.ErrorLvl))
got, errs := p.InitialParse()
if len(errs) > 0 {
t.Fatalf("parser had errors, expected <nil>\nerror: %v", errs)
}
if len(got) != 1 {
t.Fatalf("expected 1 GenDocBlock to come back")
}
if got[0].NodeCategory != tt.wantCatType {
t.Errorf("parser incorrectly converted node category\ngot: %v\nexpected %v\n", got[0].NodeCategory, tt.wantCatType)
}
if got[0].Annotation.Id != tt.wantId {
t.Errorf("parser incorrectly converted node Id\ngot: %v\nexpected %v\n", got[0].Annotation.Id, tt.wantId)
}
if got[0].Annotation.ServiceURN != tt.wantUrn {
t.Errorf("parser incorrectly converted node ServieURN\ngot: %v\nexpected %v\n", got[0].Annotation.ServiceURN, tt.wantUrn)
}
if got[0].Annotation.Parent != tt.wantParentId {
t.Errorf("parser incorrectly converted node Parent\ngot: %v\nexpected %v\n", got[0].Annotation.Parent, tt.wantParentId)
}
})
}
}
func Test_Parse_Id_and_type_of_nodes_should_fail(t *testing.T) {
ttests := map[string]struct {
input string
config *parser.Config
wantErrType error
}{
"service without id inherited in single repo mode": {`let x = 42;
//+gendoc category=info type=description
this is some description
//-gendoc`,
&parser.Config{ServiceId: ""},
parser.ErrIdRequired,
},
"channel with id missing": {`let x = 42;
//+gendoc category=channel type=description
this is some description
//-gendoc`,
&parser.Config{ServiceId: "foo"},
parser.ErrIdRequired,
},
"channel with parent id missing": {`let x = 42;
//+gendoc category=channel type=description id=bad
this is some description
//-gendoc`,
&parser.Config{},
parser.ErrParentIdRequired,
},
"operation with parent id missing": {`let x = 42;
//+gendoc category=pubOperation type=description id=bad
this is some description
//-gendoc`,
&parser.Config{},
parser.ErrParentIdRequired,
},
"operation with id missing": {`let x = 42;
//+gendoc category=pubOperation type=description
this is some description
//-gendoc`,
&parser.Config{},
parser.ErrIdRequired,
},
"message with id missing": {`let x = 42;
//+gendoc category=message type=description
this is some description
//-gendoc`,
&parser.Config{},
parser.ErrIdRequired,
},
"message with description id missing": {`let x = 42;
//+gendoc category=message id=bad
this is some description
//-gendoc`,
&parser.Config{},
parser.ErrContentTypeRequired,
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
lexerSource.Input = tt.input
l := lexer.New(lexerSource)
p := parser.New(l, tt.config).WithLogger(log.New(os.Stderr, log.ErrorLvl))
_, errs := p.InitialParse()
if len(errs) == 0 {
t.Fatalf("parser had NO errors, expected errors to be non nil")
}
if !errors.Is(errs[0], tt.wantErrType) {
t.Errorf("incorrect error type, got: %v, want: %v", errs[0], tt.wantErrType)
}
})
}
}
func Test_ShouldError_when_no_End_tag_found(t *testing.T) {
input := `let x = 5;
//+gendoc category=message type=nameId parent=id1 id=id
`
lexerSource.Input = input
l := lexer.New(lexerSource)
p := parser.New(l, &parser.Config{}).WithLogger(log.New(os.Stderr, log.ErrorLvl))
_, errs := p.InitialParse()
if len(errs) != 1 {
t.Errorf("unexpected number of errors\n got: %v, wanted: 1", errs)
}
if !errors.Is(errs[0], parser.ErrNoEndTagFound) {
t.Errorf("unexpected error type\n got: %T, wanted: %T", errs, parser.ErrNoEndTagFound)
}
}
func Test_Error_on_unparseable_tag(t *testing.T) {
input := `let x = 5;
//+gendoc category=bar type=nameId
//-gendoc
`
lexerSource.Input = input
l := lexer.New(lexerSource)
p := parser.New(l, &parser.Config{}).WithLogger(log.New(os.Stderr, log.ErrorLvl))
_, errs := p.InitialParse()
if len(errs) != 1 {
t.Errorf("unexpected number of errors\n got: %v, wanted: 1", errs)
}
if !errors.Is(errs[0], gendoc.ErrIncorrectCategory) {
t.Errorf("unexpected error type\n got: %T, wanted: %T", errs[0], gendoc.ErrIncorrectCategory)
}
}
func testHelperGenDocBlock(t *testing.T, initialGenDocBlock parser.GenDocBlock, name string, content any) bool {
if initialGenDocBlock.Token.Literal != "//+gendoc" {
t.Errorf("got=%q, wanted initialGenDocBlock.TokenLiteral = '//+gendoc'.", initialGenDocBlock.Token.Literal)
return false
}
if initialGenDocBlock.Value != content {
t.Errorf("initialGenDocBlock.Value. got=%s, wanted=%s", initialGenDocBlock.Value, name)
return false
}
if initialGenDocBlock.EndToken.Literal != "//-gendoc" {
t.Errorf("initialGenDocBlock.EndToken incorrect\ngot=%v\nwanted non nil",
initialGenDocBlock.EndToken)
return false
}
return true
}
func Test_ExpandEnvVariables_succeeds(t *testing.T) {
ttests := map[string]struct {
input string
expect string
envVar []string
}{
"with single var": {
"some var is $var",
"some var is foo",
[]string{"var=foo"},
},
"with multiple var": {
"some var is $var and docs go [here]($DOC_LINK/stuff)",
"some var is foo and docs go [here](https://somestuff.com/stuff)",
[]string{"var=foo", "DOC_LINK=https://somestuff.com"},
},
"with no vars in content": {
"some var is foo and docs go [here](foo.com/stuff)",
"some var is foo and docs go [here](foo.com/stuff)",
[]string{"var=foo", "DOC_LINK=https://somestuff.com"},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
defer os.Clearenv()
got, err := parser.ExpandEnvVariables(tt.input, tt.envVar)
if err != nil {
t.Errorf("expected %v to be <nil>", err)
}
if got != tt.expect {
t.Errorf("want: %s, got: %s", got, tt.expect)
}
})
}
}
func Test_ExpandEnvVariables_fails(t *testing.T) {
ttests := map[string]struct {
input string
setup func() func()
envVar []string
}{
"with single var": {
"some var is $var",
func() func() {
return func() {
os.Clearenv()
}
},
[]string{"v=foo"},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
clear := tt.setup()
defer clear()
_, err := parser.ExpandEnvVariables(tt.input, tt.envVar)
if err == nil {
t.Errorf("wanted error, got <nil>")
}
})
}
}
func Test_Parse_WithOwnEnviron_passed_in_succeeds(t *testing.T) {
ttests := map[string]struct {
input string
expect string
environ []string
}{
"test1": {
input: `let x = 42;
//+gendoc category=message type=description id=foo
this is some description with $foo
//-gendoc`,
environ: []string{"foo=bar"},
expect: "this is some description with bar",
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
defer os.Clearenv()
lexerSource.Input = tt.input
l := lexer.New(lexerSource)
p := parser.New(l, &parser.Config{}).WithLogger(log.New(os.Stderr, log.ErrorLvl)).WithEnvironment(tt.environ)
got, errs := p.InitialParse()
if len(errs) > 0 {
t.Error(errs)
}
if got[0].Value != tt.expect {
t.Error("")
}
})
}
}
func Test_Parse_WithOwnEnviron_passed_in_fails(t *testing.T) {
ttests := map[string]struct {
input string
expect error
environ []string
}{
"if variable is not set": {
input: `let x = 42;
//+gendoc category=message type=description id=foo
this is some description with $foo
//-gendoc`,
expect: parser.ErrUnableToReplaceVarPlaceholder,
environ: []string{"notfoo=bar"},
},
"if variable is not set but empty": {
input: `let x = 42;
//+gendoc category=message type=description id=foo
this is some description with $foo
//-gendoc`,
expect: parser.ErrUnableToReplaceVarPlaceholder,
environ: []string{"foo="},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
defer os.Clearenv()
lexerSource.Input = tt.input
l := lexer.New(lexerSource)
p := parser.New(l, &parser.Config{}).WithLogger(log.New(os.Stderr, log.ErrorLvl)).WithEnvironment(tt.environ)
_, errs := p.InitialParse()
if len(errs) < 1 {
t.Error("expected errors to occur")
t.Fail()
}
if !errors.Is(errs[0], tt.expect) {
t.Errorf("unexpected error type\n got: %T, wanted: %T", errs, tt.expect)
}
})
}
}