-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_property_test.go
More file actions
502 lines (411 loc) · 13.8 KB
/
parser_property_test.go
File metadata and controls
502 lines (411 loc) · 13.8 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
// Copyright 2026 The Zaparoo Project Contributors.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zapscript
import (
"strings"
"testing"
"unicode"
"pgregory.net/rapid"
)
// ============================================================================
// Generators
// ============================================================================
// cmdNameGen generates valid command names (alphanumeric + dots).
func cmdNameGen() *rapid.Generator[string] {
return rapid.StringMatching(`[a-zA-Z][a-zA-Z0-9.]{0,19}`)
}
// argGen generates a simple argument string (no special chars).
func argGen() *rapid.Generator[string] {
return rapid.StringMatching(`[a-zA-Z0-9_]{1,20}`)
}
// advArgKeyGen generates valid advanced argument keys.
func advArgKeyGen() *rapid.Generator[string] {
return rapid.StringMatching(`[a-zA-Z][a-zA-Z0-9_]{0,15}`)
}
// advArgValueGen generates simple advanced argument values.
func advArgValueGen() *rapid.Generator[string] {
return rapid.StringMatching(`[a-zA-Z0-9_]{1,20}`)
}
// ============================================================================
// ParseScript Property Tests
// ============================================================================
// TestPropertyParseScriptDeterministic verifies same input produces same output.
func TestPropertyParseScriptDeterministic(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
script := "**" + cmdName
p1 := NewParser(script)
result1, err1 := p1.ParseScript()
p2 := NewParser(script)
result2, err2 := p2.ParseScript()
// Both should have same error status
if (err1 == nil) != (err2 == nil) {
t.Fatalf("Non-deterministic error: %v vs %v", err1, err2)
}
if err1 != nil {
return
}
// Same number of commands
if len(result1.Cmds) != len(result2.Cmds) {
t.Fatalf("Non-deterministic: %d vs %d commands",
len(result1.Cmds), len(result2.Cmds))
}
// Same command names
for i := range result1.Cmds {
if result1.Cmds[i].Name != result2.Cmds[i].Name {
t.Fatalf("Non-deterministic at cmd %d: %q vs %q",
i, result1.Cmds[i].Name, result2.Cmds[i].Name)
}
}
})
}
// TestPropertyParseScriptCommandNamesLowercased verifies command names are lowercased.
func TestPropertyParseScriptCommandNamesLowercased(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
script := "**" + cmdName
p := NewParser(script)
result, err := p.ParseScript()
if err != nil {
return // Invalid scripts are acceptable
}
for _, cmd := range result.Cmds {
if cmd.Name != strings.ToLower(cmd.Name) {
t.Fatalf("Command name not lowercased: %q", cmd.Name)
}
}
})
}
// TestPropertyParseScriptCaseInsensitiveCommandNames verifies case doesn't change result.
func TestPropertyParseScriptCaseInsensitiveCommandNames(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
scriptLower := "**" + strings.ToLower(cmdName)
scriptUpper := "**" + strings.ToUpper(cmdName)
p1 := NewParser(scriptLower)
result1, err1 := p1.ParseScript()
p2 := NewParser(scriptUpper)
result2, err2 := p2.ParseScript()
// Both should parse successfully
if err1 != nil || err2 != nil {
return
}
// Command names should be identical (both lowercased)
if result1.Cmds[0].Name != result2.Cmds[0].Name {
t.Fatalf("Case sensitivity: %q vs %q",
result1.Cmds[0].Name, result2.Cmds[0].Name)
}
})
}
// TestPropertyParseScriptAtLeastOneCommand verifies successful parse has ≥1 command.
func TestPropertyParseScriptAtLeastOneCommand(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
script := "**" + cmdName
p := NewParser(script)
result, err := p.ParseScript()
if err != nil {
return // Invalid scripts are acceptable
}
if len(result.Cmds) < 1 {
t.Fatal("Successful parse should have at least one command")
}
})
}
// TestPropertyParseScriptEmptyIsError verifies empty input returns error.
func TestPropertyParseScriptEmptyIsError(t *testing.T) {
t.Parallel()
emptyInputs := []string{"", " ", "\t", "\n", " \t\n "}
for _, input := range emptyInputs {
p := NewParser(input)
_, err := p.ParseScript()
if err == nil {
t.Fatalf("Expected error for empty/whitespace input: %q", input)
}
}
}
// TestPropertyParseScriptWithArgs verifies args are preserved.
func TestPropertyParseScriptWithArgs(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
args := rapid.SliceOfN(argGen(), 1, 5).Draw(t, "args")
script := "**" + cmdName + ":" + strings.Join(args, ",")
p := NewParser(script)
result, err := p.ParseScript()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(result.Cmds) != 1 {
t.Fatalf("Expected 1 command, got %d", len(result.Cmds))
}
// Args should match (after trimming)
if len(result.Cmds[0].Args) != len(args) {
t.Fatalf("Expected %d args, got %d", len(args), len(result.Cmds[0].Args))
}
for i, expected := range args {
got := strings.TrimSpace(result.Cmds[0].Args[i])
if got != expected {
t.Fatalf("Arg %d mismatch: expected %q, got %q", i, expected, got)
}
}
})
}
// TestPropertyParseScriptWithAdvArgs verifies advanced args are preserved.
func TestPropertyParseScriptWithAdvArgs(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
cmdName := cmdNameGen().Draw(t, "cmdName")
key := advArgKeyGen().Draw(t, "key")
value := advArgValueGen().Draw(t, "value")
script := "**" + cmdName + "?" + key + "=" + value
p := NewParser(script)
result, err := p.ParseScript()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(result.Cmds) != 1 {
t.Fatalf("Expected 1 command, got %d", len(result.Cmds))
}
// Advanced args should contain our key-value pair
if result.Cmds[0].AdvArgs.IsEmpty() {
t.Fatal("Expected advanced args to be present")
}
})
}
// TestPropertyParseScriptNeverPanics verifies parser never panics.
func TestPropertyParseScriptNeverPanics(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
input := rapid.String().Draw(t, "input")
p := NewParser(input)
// Should not panic
_, _ = p.ParseScript()
})
}
// ============================================================================
// ParseExpressions Property Tests
// ============================================================================
// TestPropertyParseExpressionsDeterministic verifies same input produces same output.
func TestPropertyParseExpressionsDeterministic(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
varName := rapid.StringMatching(`[a-z]{1,10}`).Draw(t, "varName")
input := "Hello [[" + varName + "]] world"
p1 := NewParser(input)
result1, err1 := p1.ParseExpressions()
p2 := NewParser(input)
result2, err2 := p2.ParseExpressions()
// Same error status
if (err1 == nil) != (err2 == nil) {
t.Fatalf("Non-deterministic error: %v vs %v", err1, err2)
}
if err1 != nil {
return
}
if result1 != result2 {
t.Fatalf("Non-deterministic: %q vs %q", result1, result2)
}
})
}
// TestPropertyParseExpressionsPreservesLiterals verifies text outside [[]] is preserved.
func TestPropertyParseExpressionsPreservesLiterals(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
// Generate text without expression markers or escape chars
text := rapid.StringMatching(`[a-zA-Z0-9 ]{1,50}`).Draw(t, "text")
p := NewParser(text)
result, err := p.ParseExpressions()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if result != text {
t.Fatalf("Text not preserved: expected %q, got %q", text, result)
}
})
}
// TestPropertyParseExpressionsNeverPanics verifies function never panics.
func TestPropertyParseExpressionsNeverPanics(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
input := rapid.String().Draw(t, "input")
p := NewParser(input)
// Should not panic
_, _ = p.ParseExpressions()
})
}
// ============================================================================
// Character Validation Tests
// ============================================================================
// TestPropertyIsCmdNameAlphanumeric verifies isCmdName only accepts valid chars.
func TestPropertyIsCmdNameAlphanumeric(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ch := rapid.Rune().Draw(t, "char")
result := isCmdName(ch)
// Expected: a-z, A-Z, 0-9, or .
expected := (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '.'
if result != expected {
t.Fatalf("isCmdName(%q) = %v, expected %v", ch, result, expected)
}
})
}
// TestPropertyIsAdvArgNameValid verifies isAdvArgName only accepts valid chars.
func TestPropertyIsAdvArgNameValid(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ch := rapid.Rune().Draw(t, "char")
result := isAdvArgName(ch)
// Expected: a-z, A-Z, 0-9, or _
expected := (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '_'
if result != expected {
t.Fatalf("isAdvArgName(%q) = %v, expected %v", ch, result, expected)
}
})
}
// TestPropertyIsWhitespaceCorrect verifies isWhitespace matches expected chars.
func TestPropertyIsWhitespaceCorrect(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
ch := rapid.Rune().Draw(t, "char")
result := isWhitespace(ch)
// Expected: space, tab, newline, carriage return
expected := ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
if result != expected {
t.Fatalf("isWhitespace(%q) = %v, expected %v", ch, result, expected)
}
})
}
// ============================================================================
// Escape Sequence Tests
// ============================================================================
// TestPropertyEscapeSequencesRecognized verifies known escape sequences.
func TestPropertyEscapeSequencesRecognized(t *testing.T) {
t.Parallel()
escapeTests := []struct {
input string
expected string
}{
{"^n", "\n"},
{"^r", "\r"},
{"^t", "\t"},
{"^^", "^"},
{`^"`, `"`},
{"^'", "'"},
}
for _, tt := range escapeTests {
p := NewParser(tt.input)
// Skip the ^ character
_, _ = p.read()
result, err := p.parseEscapeSeq()
if err != nil {
t.Fatalf("Error parsing %q: %v", tt.input, err)
}
if result != tt.expected {
t.Fatalf("Escape %q: expected %q, got %q", tt.input, tt.expected, result)
}
}
}
// ============================================================================
// AdvArgs Tests
// ============================================================================
// TestPropertyAdvArgsGetSetConsistent verifies Get returns what was set with With.
func TestPropertyAdvArgsGetSetConsistent(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
key := advArgKeyGen().Draw(t, "key")
value := advArgValueGen().Draw(t, "value")
// Start with empty AdvArgs
aa := NewAdvArgs(nil)
// Set a value
aa = aa.With(Key(key), value)
// Get should return the same value
got := aa.Get(Key(key))
if got != value {
t.Fatalf("Get(%q) = %q, expected %q", key, got, value)
}
})
}
// TestPropertyAdvArgsWithImmutable verifies With doesn't mutate original.
func TestPropertyAdvArgsWithImmutable(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
key := advArgKeyGen().Draw(t, "key")
value := advArgValueGen().Draw(t, "value")
original := NewAdvArgs(map[string]string{"existing": "value"})
modified := original.With(Key(key), value)
// Original should not have the new key
if original.Get(Key(key)) == value {
t.Fatal("With() should not mutate original")
}
// Modified should have the new key
if modified.Get(Key(key)) != value {
t.Fatal("With() should set value in returned copy")
}
})
}
// TestPropertyAdvArgsIsEmptyCorrect verifies IsEmpty behavior.
func TestPropertyAdvArgsIsEmptyCorrect(t *testing.T) {
t.Parallel()
// Empty AdvArgs
empty := NewAdvArgs(nil)
if !empty.IsEmpty() {
t.Fatal("nil AdvArgs should be empty")
}
empty2 := NewAdvArgs(map[string]string{})
if !empty2.IsEmpty() {
t.Fatal("Empty map AdvArgs should be empty")
}
// Non-empty AdvArgs
nonEmpty := NewAdvArgs(map[string]string{"key": "value"})
if nonEmpty.IsEmpty() {
t.Fatal("AdvArgs with data should not be empty")
}
}
// ============================================================================
// Unicode Handling Tests
// ============================================================================
// TestPropertyParseScriptHandlesUnicode verifies unicode in args is preserved.
func TestPropertyParseScriptHandlesUnicode(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
// Generate a string with letters from various scripts
chars := rapid.SliceOfN(rapid.RuneFrom(nil, unicode.Letter), 1, 20).Draw(t, "chars")
unicodeStr := string(chars)
script := "**cmd:" + unicodeStr
p := NewParser(script)
result, err := p.ParseScript()
if err != nil {
return // Some unicode might cause parse issues, that's acceptable
}
if len(result.Cmds) != 1 {
t.Fatalf("Expected 1 command, got %d", len(result.Cmds))
}
// The argument should contain the unicode string (trimmed)
if len(result.Cmds[0].Args) != 1 {
t.Fatalf("Expected 1 arg, got %d", len(result.Cmds[0].Args))
}
})
}