-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrivia_test.go
More file actions
609 lines (536 loc) · 13.6 KB
/
Copy pathtrivia_test.go
File metadata and controls
609 lines (536 loc) · 13.6 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package scaf_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/rlch/scaf"
)
func TestCommentAttachment(t *testing.T) {
// These tests cannot run in parallel because they share lexer trivia state
tests := []struct {
name string
input string
wantSuite []string
wantFirstQuery []string
wantSecondQuery []string // optional, for multi-query tests
}{
{
name: "comment directly before query attaches to query",
input: "// This is a query doc comment\nfn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: nil,
wantFirstQuery: []string{"// This is a query doc comment"},
},
{
name: "comment separated by blank line attaches to suite",
input: "// This is a suite/file comment\n\n// This is a query doc comment\nfn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: []string{"// This is a suite/file comment"},
wantFirstQuery: []string{"// This is a query doc comment"},
},
{
name: "multi-line query doc comment stays together",
input: "// First line of query doc\n// Second line of query doc\nfn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: nil,
wantFirstQuery: []string{"// First line of query doc", "// Second line of query doc"},
},
{
name: "suite comment only when blank line before query",
input: "// Suite level comment\n\nfn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: []string{"// Suite level comment"},
wantFirstQuery: nil,
},
{
name: "multiple consecutive suite comments with blank line before query doc",
input: "// Suite comment 1\n// Suite comment 2\n\n// Query doc\nfn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: []string{"// Suite comment 1", "// Suite comment 2"},
wantFirstQuery: []string{"// Query doc"},
},
{
name: "multiple queries with separate doc comments",
input: "// Doc for first\nfn First() `Q1`\n\n// Doc for second\nfn Second() `Q2`\n",
wantSuite: nil,
wantFirstQuery: []string{"// Doc for first"},
wantSecondQuery: []string{"// Doc for second"},
},
{
name: "no comments",
input: "fn GetUser() `MATCH (u:User) RETURN u`\n",
wantSuite: nil,
wantFirstQuery: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
suite, err := scaf.Parse([]byte(tt.input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if diff := cmp.Diff(tt.wantSuite, suite.LeadingComments); diff != "" {
t.Errorf("Suite.LeadingComments mismatch (-want +got):\n%s", diff)
}
if len(suite.Functions) > 0 {
if diff := cmp.Diff(tt.wantFirstQuery, suite.Functions[0].LeadingComments); diff != "" {
t.Errorf("Functions[0].LeadingComments mismatch (-want +got):\n%s", diff)
}
}
if len(suite.Functions) > 1 && tt.wantSecondQuery != nil {
if diff := cmp.Diff(tt.wantSecondQuery, suite.Functions[1].LeadingComments); diff != "" {
t.Errorf("Functions[1].LeadingComments mismatch (-want +got):\n%s", diff)
}
}
})
}
}
func TestTrailingCommentAttachment(t *testing.T) {
input := "fn GetUser() `MATCH (u:User) RETURN u` // trailing comment\n"
suite, err := scaf.Parse([]byte(input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if len(suite.Functions) == 0 {
t.Fatal("Expected at least one query")
}
expected := "// trailing comment"
if suite.Functions[0].TrailingComment != expected {
t.Errorf("TrailingComment = %q, want %q", suite.Functions[0].TrailingComment, expected)
}
}
func TestScopeCommentAttachment(t *testing.T) {
input := `fn Q() ` + "`Q`" + `
// Scope doc comment
Q {
// Test doc comment
test "example" {
}
}
`
suite, err := scaf.Parse([]byte(input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if len(suite.Scopes) == 0 {
t.Fatal("Expected at least one scope")
}
scope := suite.Scopes[0]
wantScopeComments := []string{"// Scope doc comment"}
if diff := cmp.Diff(wantScopeComments, scope.LeadingComments); diff != "" {
t.Errorf("Scope.LeadingComments mismatch (-want +got):\n%s", diff)
}
if len(scope.Items) == 0 || scope.Items[0].Test == nil {
t.Fatal("Expected at least one test in scope")
}
test := scope.Items[0].Test
wantTestComments := []string{"// Test doc comment"}
if diff := cmp.Diff(wantTestComments, test.LeadingComments); diff != "" {
t.Errorf("Test.LeadingComments mismatch (-want +got):\n%s", diff)
}
}
func TestParameterCommentAttachment(t *testing.T) {
// Tests for parameter doc comments in multi-line function signatures
input := `fn CreateUser(
// The user's unique identifier
id: string,
// The user's display name
name: string,
) ` + "`CREATE (u:User {id: $id, name: $name}) RETURN u`" + `
`
suite, err := scaf.Parse([]byte(input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if len(suite.Functions) == 0 {
t.Fatal("Expected at least one function")
}
fn := suite.Functions[0]
if len(fn.Params) != 2 {
t.Fatalf("Expected 2 params, got %d", len(fn.Params))
}
// Check first param doc comment
wantIdComments := []string{"// The user's unique identifier"}
if diff := cmp.Diff(wantIdComments, fn.Params[0].LeadingComments); diff != "" {
t.Errorf("Params[0].LeadingComments mismatch (-want +got):\n%s", diff)
}
// Check second param doc comment
wantNameComments := []string{"// The user's display name"}
if diff := cmp.Diff(wantNameComments, fn.Params[1].LeadingComments); diff != "" {
t.Errorf("Params[1].LeadingComments mismatch (-want +got):\n%s", diff)
}
}
func TestStatementCommentAttachment(t *testing.T) {
// Tests for statement doc comments within tests
input := `fn Q() ` + "`Q`" + `
Q {
test "example" {
// Comment for input
$id: 1
// Comment for output
u.name: "Alice"
}
}
`
suite, err := scaf.Parse([]byte(input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if len(suite.Scopes) == 0 || len(suite.Scopes[0].Items) == 0 {
t.Fatal("Expected scope with test")
}
test := suite.Scopes[0].Items[0].Test
if len(test.Statements) != 2 {
t.Fatalf("Expected 2 statements, got %d", len(test.Statements))
}
// Check input statement comment
wantInputComments := []string{"// Comment for input"}
if diff := cmp.Diff(wantInputComments, test.Statements[0].LeadingComments); diff != "" {
t.Errorf("Statements[0].LeadingComments mismatch (-want +got):\n%s", diff)
}
// Check output statement comment
wantOutputComments := []string{"// Comment for output"}
if diff := cmp.Diff(wantOutputComments, test.Statements[1].LeadingComments); diff != "" {
t.Errorf("Statements[1].LeadingComments mismatch (-want +got):\n%s", diff)
}
}
func TestAssertCommentAttachment(t *testing.T) {
// Tests for assert doc comments
input := `fn Q() ` + "`Q`" + `
Q {
test "example" {
// Verify the user exists
assert (u != null)
}
}
`
suite, err := scaf.Parse([]byte(input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if len(suite.Scopes) == 0 || len(suite.Scopes[0].Items) == 0 {
t.Fatal("Expected scope with test")
}
test := suite.Scopes[0].Items[0].Test
if len(test.Asserts) == 0 {
t.Fatal("Expected at least one assert")
}
wantComments := []string{"// Verify the user exists"}
if diff := cmp.Diff(wantComments, test.Asserts[0].LeadingComments); diff != "" {
t.Errorf("Asserts[0].LeadingComments mismatch (-want +got):\n%s", diff)
}
}
// =============================================================================
// Round-trip tests for comment preservation
// =============================================================================
// TestCommentRoundTrip verifies that formatting preserves all comments.
// This is the comprehensive test that catches any missing comment handling.
func TestCommentRoundTrip(t *testing.T) {
tests := []struct {
name string
input string
}{
{
name: "function trailing comment",
input: `fn Q() ` + "`Q`" + ` // function trailing
`,
},
{
name: "function leading comment",
input: `// function doc
fn Q() ` + "`Q`" + `
`,
},
{
name: "scope trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
}
} // scope trailing
`,
},
{
name: "scope leading comment",
input: `fn Q() ` + "`Q`" + `
// scope doc
Q {
test "t" {
}
}
`,
},
{
name: "test trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
} // test trailing
}
`,
},
{
name: "test leading comment",
input: `fn Q() ` + "`Q`" + `
Q {
// test doc
test "t" {
}
}
`,
},
{
name: "group trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
group "g" {
test "t" {
}
} // group trailing
}
`,
},
{
name: "group leading comment",
input: `fn Q() ` + "`Q`" + `
Q {
// group doc
group "g" {
test "t" {
}
}
}
`,
},
{
name: "statement trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
$id: 1 // input trailing
}
}
`,
},
{
name: "statement leading comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
// input doc
$id: 1
}
}
`,
},
{
name: "assert shorthand trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
assert (x > 0) // assert trailing
}
}
`,
},
{
name: "assert shorthand leading comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
// assert doc
assert (x > 0)
}
}
`,
},
{
name: "assert block trailing comment",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
assert ` + "`Q`" + ` { (x > 0) } // assert trailing
}
}
`,
},
{
name: "import trailing comment",
input: `import "foo" // import trailing
fn Q() ` + "`Q`" + `
`,
},
{
name: "import leading comment",
input: `// import doc
import "foo"
fn Q() ` + "`Q`" + `
`,
},
{
name: "parameter comments in multi-line function",
input: `fn Q(
// param a doc
a: string,
// param b doc
b: int,
) ` + "`Q`" + `
`,
},
{
name: "multiple comments throughout file",
input: `// File header comment
// Import section
import "foo" // inline import comment
// Function docs
fn Q(
// param doc
id: string,
) ` + "`Q`" + ` // function trailing
// Scope docs
Q {
// Test docs
test "example" {
// Input section
$id: 1 // input comment
// Assert section
assert (x > 0) // assert comment
} // test trailing
} // scope trailing
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Parse
suite, err := scaf.Parse([]byte(tt.input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
// Format
output := scaf.Format(suite)
// Parse again
suite2, err := scaf.Parse([]byte(output))
if err != nil {
t.Fatalf("Parse(formatted) error = %v\n\nFormatted:\n%s", err, output)
}
// Format again (idempotency check)
output2 := scaf.Format(suite2)
// Check idempotency
if diff := cmp.Diff(output, output2); diff != "" {
t.Errorf("Format() not idempotent (-first +second):\n%s\n\nFirst:\n%s\n\nSecond:\n%s", diff, output, output2)
}
// The key test: check that all comments from input appear in output
// We can't do exact string matching because formatting might normalize whitespace
// But all comment text should be preserved
checkCommentsPreserved(t, tt.input, output)
})
}
}
// checkCommentsPreserved verifies all comment text from input appears in output.
func checkCommentsPreserved(t *testing.T, input, output string) {
t.Helper()
// Extract all comments from input (lines starting with // after trimming)
inputLines := splitLines(input)
for _, line := range inputLines {
trimmed := trimLeft(line)
if len(trimmed) >= 2 && trimmed[:2] == "//" {
// Found a comment - make sure it appears in output
if !containsString(output, trimmed) {
t.Errorf("Comment %q from input not found in output.\n\nInput:\n%s\n\nOutput:\n%s", trimmed, input, output)
}
}
}
}
// splitLines splits a string into lines.
func splitLines(s string) []string {
var lines []string
start := 0
for i := 0; i < len(s); i++ {
if s[i] == '\n' {
lines = append(lines, s[start:i])
start = i + 1
}
}
if start < len(s) {
lines = append(lines, s[start:])
}
return lines
}
// trimLeft trims leading whitespace from a string.
func trimLeft(s string) string {
for i := 0; i < len(s); i++ {
if s[i] != ' ' && s[i] != '\t' {
return s[i:]
}
}
return ""
}
// containsString checks if substr appears in s.
func containsString(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
func TestTrailingCommentOnAssertBlock(t *testing.T) {
// Specific test for the original issue - trailing comments after assert blocks
tests := []struct {
name string
input string
}{
{
name: "shorthand assert with trailing",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
assert (x > 0) // trailing
}
}
`,
},
{
name: "block assert single condition with trailing",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
assert ` + "`Q`" + ` { (x > 0) } // trailing
}
}
`,
},
{
name: "block assert multi-line with trailing",
input: `fn Q() ` + "`Q`" + `
Q {
test "t" {
assert ` + "`Q`" + ` {
(x > 0)
(y < 10)
} // trailing after multi-line
}
}
`,
},
{
name: "assert with query and trailing",
input: `fn Q() ` + "`Q`" + `
fn Check() ` + "`CHECK`" + `
Q {
test "t" {
assert Check() { (x > 0) } // trailing
}
}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
suite, err := scaf.Parse([]byte(tt.input))
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
output := scaf.Format(suite)
// The comment "// trailing" must appear in output
if !containsString(output, "// trailing") {
t.Errorf("Trailing comment not preserved.\n\nInput:\n%s\n\nOutput:\n%s", tt.input, output)
}
})
}
}