-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
731 lines (654 loc) · 16.5 KB
/
parser_test.go
File metadata and controls
731 lines (654 loc) · 16.5 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
// 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_test
import (
"errors"
"testing"
"github.com/ZaparooProject/go-zapscript"
"github.com/google/go-cmp/cmp"
)
func TestParse(t *testing.T) {
t.Parallel()
tests := []struct {
wantErr error
name string
input string
want zapscript.Script
}{
{
name: "single command with no args",
input: `**hello`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "hello"},
},
},
},
{
name: "multiple commands with no args",
input: `**hello||**goodbye||**world`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "hello"},
{Name: "goodbye"},
{Name: "world"},
},
},
},
{
name: "single command with args",
input: `**greet:hi,there`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "greet", Args: []string{"hi", "there"}},
},
},
},
{
name: "two commands separated",
input: `**first:1,2||**second:3,4`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "first", Args: []string{"1", "2"}},
{Name: "second", Args: []string{"3", "4"}},
},
},
},
{
name: "whitespace is trimmed in args 1",
input: ` **trim: a , b `,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "trim", Args: []string{"a", "b"}},
},
},
},
{
name: "missing command name",
input: `**:x,y`,
wantErr: zapscript.ErrEmptyCmdName,
},
{
name: "invalid character in command name",
input: `**he@llo`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "launch", Args: []string{`**he@llo`}},
},
},
},
{
name: "unexpected EOF after asterisk",
input: `*`,
wantErr: zapscript.ErrUnexpectedEOF,
},
{
name: "command with trailing ||",
input: `**cmd:1,2||`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "cmd", Args: []string{"1", "2"}},
},
},
},
{
name: "command with one advanced arg",
input: `**example?debug=true`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "example", AdvArgs: zapscript.NewAdvArgs(map[string]string{"debug": "true"})},
},
},
},
{
name: "command with args and one advanced arg",
input: `**download:file1.txt?verify=sha256`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{
Name: "download", Args: []string{"file1.txt"},
AdvArgs: zapscript.NewAdvArgs(map[string]string{"verify": "sha256"}),
},
},
},
},
{
name: "command with multiple advanced args",
input: `**launch:game.exe?platform=win&fullscreen=yes&lang=en`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "launch", Args: []string{"game.exe"}, AdvArgs: zapscript.NewAdvArgs(map[string]string{
"platform": "win",
"fullscreen": "yes",
"lang": "en",
})},
},
},
},
{
name: "generic launch 1",
input: `DOS/some/game/to/play.iso`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "launch", Args: []string{`DOS/some/game/to/play.iso`}},
},
},
},
{
name: "generic launch 2",
input: `/media/fat/games/DOS/some/game/to/play.iso`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "launch", Args: []string{`/media/fat/games/DOS/some/game/to/play.iso`}},
},
},
},
{
name: "generic launch 3",
input: `C:\game\to\to\play.iso`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "launch", Args: []string{`C:\game\to\to\play.iso`}},
},
},
},
{
name: "single quoted arg",
input: `**say:"hello, world"`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "say", Args: []string{"hello, world"}},
},
},
},
{
name: "unmatched quote in arg",
input: `**fail:"unterminated`,
wantErr: zapscript.ErrUnmatchedQuote,
},
{
name: "simple json argument",
input: `**config:{"key": "value"}`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "config", Args: []string{`{"key":"value"}`}},
},
},
},
{
name: "invalid json argument - missing closing brace",
input: `**config:{"key": "value"`,
wantErr: zapscript.ErrInvalidJSON,
},
{
name: "empty script",
input: "",
want: zapscript.Script{},
wantErr: zapscript.ErrEmptyZapScript,
},
{
name: "empty command name",
input: "**",
want: zapscript.Script{},
wantErr: zapscript.ErrEmptyCmdName,
},
{
name: "input.keyboard basic characters",
input: `**input.keyboard:abcXYZ123`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "input.keyboard", Args: []string{
"a", "b", "c", "X", "Y", "Z", "1", "2", "3",
}},
},
},
},
{
name: "input.gamepad basic directions",
input: `**input.gamepad:^^VV<><>`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "input.gamepad", Args: []string{
"^", "^", "V", "V", "<", ">", "<", ">",
}},
},
},
},
{
name: "simple expression in arg",
input: `**greet:Hello [[name]]`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "greet", Args: []string{"Hello " + zapscript.TokExpStart + "name" + zapscript.TokExprEnd}},
},
},
},
{
name: "unmatched expression - missing closing bracket",
input: `**test:[[variable`,
wantErr: zapscript.ErrUnmatchedExpression,
},
{
name: "escaped newline in arg",
input: `**echo:hello^nworld`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "echo", Args: []string{"hello\nworld"}},
},
},
},
{
name: "escaped tab in arg",
input: `**tabby:one^ttwo`,
want: zapscript.Script{
Cmds: []zapscript.Command{
{Name: "tabby", Args: []string{"one\ttwo"}},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
p := zapscript.NewParser(tt.input)
got, err := p.ParseScript()
if !errors.Is(err, tt.wantErr) {
t.Errorf("ParseScript() error = %v, wantErr = %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(zapscript.AdvArgs{})); diff != "" {
t.Errorf("ParseScript() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestParseExpressions(t *testing.T) {
t.Parallel()
tests := []struct {
wantErr error
name string
input string
want string
}{
{
name: "empty input",
input: "",
want: "",
},
{
name: "plain text only",
input: "hello world",
want: "hello world",
},
{
name: "single expression",
input: "hello [[name]]",
want: "hello " + zapscript.TokExpStart + "name" + zapscript.TokExprEnd,
},
{
name: "multiple expressions",
input: "[[first]] and [[second]]",
want: zapscript.TokExpStart + "first" + zapscript.TokExprEnd + " and " +
zapscript.TokExpStart + "second" + zapscript.TokExprEnd,
},
{
name: "unmatched opening brackets",
input: "test[[unclosed",
want: "test",
wantErr: zapscript.ErrUnmatchedExpression,
},
{
name: "closing brackets without opening",
input: "test]]closed",
want: "test]]closed",
},
{
name: "escaped brackets",
input: "text with ^[[escaped]] brackets",
want: "text with [[escaped]] brackets",
},
{
name: "escaped newline",
input: "line one^nline two",
want: "line one\nline two",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
p := zapscript.NewParser(tt.input)
got, err := p.ParseExpressions()
if !errors.Is(err, tt.wantErr) {
t.Errorf("ParseExpressions() error = %v, wantErr = %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(zapscript.AdvArgs{})); diff != "" {
t.Errorf("ParseExpressions() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestPostProcess(t *testing.T) {
t.Parallel()
tests := []struct {
wantErr error
name string
input string
want string
}{
{
name: "empty arg",
input: "",
want: "",
},
{
name: "value only",
input: "test",
want: "test",
},
{
name: "expression only",
input: zapscript.TokExpStart + " 2 + 2 + 2 " + " " + zapscript.TokExprEnd,
want: "6",
},
{
name: "test expression 1",
input: "something " + zapscript.TokExpStart + "platform" + zapscript.TokExprEnd,
want: `something mister`,
},
{
name: "test expression 2",
input: "something " + zapscript.TokExpStart + "2+2" + zapscript.TokExprEnd,
want: `something 4`,
},
{
name: "test expression bool 1",
input: "something " + zapscript.TokExpStart + "true" + zapscript.TokExprEnd,
want: `something true`,
},
{
name: "bad return type",
input: zapscript.TokExpStart + "device" + zapscript.TokExprEnd,
wantErr: zapscript.ErrBadExpressionReturn,
},
{
name: "test expression int",
input: zapscript.TokExpStart + "5+5" + zapscript.TokExprEnd,
want: `10`,
},
{
name: "test expression float 1",
input: zapscript.TokExpStart + "2.5" + zapscript.TokExprEnd,
want: `2.5`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
env := zapscript.ArgExprEnv{
Platform: "mister",
Version: "1.2.3",
MediaPlaying: true,
ScanMode: "tap",
Device: zapscript.ExprEnvDevice{
Hostname: "test-device",
OS: "linux",
Arch: "arm",
},
}
p := zapscript.NewParser(tt.input)
got, err := p.EvalExpressions(env)
if !errors.Is(err, tt.wantErr) {
t.Errorf("EvalExpressions() error = %v, wantErr = %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(zapscript.AdvArgs{})); diff != "" {
t.Errorf("EvalExpressions() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestAdvArgs_WithMustAssignReturn(t *testing.T) {
t.Parallel()
const testKey zapscript.Key = "test_key"
t.Run("With without assign on nil map loses changes", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
advArgs.With(testKey, "new_value")
got := advArgs.Get(testKey)
if got != "" {
t.Errorf("Expected empty string (change lost without assign), got %q", got)
}
})
t.Run("With does not mutate original", func(t *testing.T) {
t.Parallel()
original := zapscript.NewAdvArgs(map[string]string{
"existing": "value",
})
modified := original.With(testKey, "new_value")
if original.Get(testKey) != "" {
t.Error("Original should not have new key")
}
if modified.Get(testKey) != "new_value" {
t.Errorf("Modified should have new key, got %q", modified.Get(testKey))
}
if modified.Get("existing") != "value" {
t.Errorf("Modified should preserve existing keys, got %q", modified.Get("existing"))
}
})
t.Run("With assign always preserves changes", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
advArgs = advArgs.With(testKey, "new_value")
got := advArgs.Get(testKey)
if got != "new_value" {
t.Errorf("Expected %q, got %q", "new_value", got)
}
})
}
func TestAdvArgs_GetWhen(t *testing.T) {
t.Parallel()
t.Run("when key exists", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{
"when": "true",
})
val, ok := advArgs.GetWhen()
if !ok {
t.Error("Expected GetWhen to return ok=true when key exists")
}
if val != "true" {
t.Errorf("Expected %q, got %q", "true", val)
}
})
t.Run("when key missing", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{
"other": "value",
})
val, ok := advArgs.GetWhen()
if ok {
t.Error("Expected GetWhen to return ok=false when key missing")
}
if val != "" {
t.Errorf("Expected empty string, got %q", val)
}
})
t.Run("nil map", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
val, ok := advArgs.GetWhen()
if ok {
t.Error("Expected GetWhen to return ok=false for nil map")
}
if val != "" {
t.Errorf("Expected empty string, got %q", val)
}
})
}
func TestAdvArgs_IsEmpty(t *testing.T) {
t.Parallel()
t.Run("nil map is empty", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
if !advArgs.IsEmpty() {
t.Error("Expected nil map to be empty")
}
})
t.Run("empty map is empty", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{})
if !advArgs.IsEmpty() {
t.Error("Expected empty map to be empty")
}
})
t.Run("non-empty map is not empty", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{"key": "value"})
if advArgs.IsEmpty() {
t.Error("Expected non-empty map to not be empty")
}
})
}
func TestAdvArgs_Range(t *testing.T) {
t.Parallel()
t.Run("iterates all entries", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{
"a": "1",
"b": "2",
"c": "3",
})
collected := make(map[string]string)
advArgs.Range(func(k zapscript.Key, v string) bool {
collected[string(k)] = v
return true
})
if len(collected) != 3 {
t.Errorf("Expected 3 entries, got %d", len(collected))
}
if collected["a"] != "1" || collected["b"] != "2" || collected["c"] != "3" {
t.Errorf("Unexpected collected values: %v", collected)
}
})
t.Run("stops on false return", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{
"a": "1",
"b": "2",
"c": "3",
})
count := 0
advArgs.Range(func(_ zapscript.Key, _ string) bool {
count++
return false
})
if count != 1 {
t.Errorf("Expected 1 iteration, got %d", count)
}
})
t.Run("nil map", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
count := 0
advArgs.Range(func(_ zapscript.Key, _ string) bool {
count++
return true
})
if count != 0 {
t.Errorf("Expected 0 iterations for nil map, got %d", count)
}
})
}
func TestAdvArgs_Raw(t *testing.T) {
t.Parallel()
t.Run("returns underlying map", func(t *testing.T) {
t.Parallel()
m := map[string]string{"key": "value"}
advArgs := zapscript.NewAdvArgs(m)
raw := advArgs.Raw()
if raw["key"] != "value" {
t.Error("Expected raw map to contain key=value")
}
})
t.Run("nil map returns nil", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
if advArgs.Raw() != nil {
t.Error("Expected Raw() to return nil for nil map")
}
})
}
func TestAdvArgs_MarshalJSON(t *testing.T) {
t.Parallel()
t.Run("marshals non-nil map", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(map[string]string{
"key": "value",
})
data, err := advArgs.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON failed: %v", err)
}
expected := `{"key":"value"}`
if string(data) != expected {
t.Errorf("Expected %s, got %s", expected, string(data))
}
})
t.Run("marshals nil map as null", func(t *testing.T) {
t.Parallel()
advArgs := zapscript.NewAdvArgs(nil)
data, err := advArgs.MarshalJSON()
if err != nil {
t.Fatalf("MarshalJSON failed: %v", err)
}
if string(data) != "null" {
t.Errorf("Expected null, got %s", string(data))
}
})
}
func TestAdvArgs_UnmarshalJSON(t *testing.T) {
t.Parallel()
t.Run("unmarshals valid JSON", func(t *testing.T) {
t.Parallel()
var advArgs zapscript.AdvArgs
err := advArgs.UnmarshalJSON([]byte(`{"key":"value"}`))
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
if advArgs.Get("key") != "value" {
t.Error("Expected key=value after unmarshal")
}
})
t.Run("unmarshals null as nil map", func(t *testing.T) {
t.Parallel()
var advArgs zapscript.AdvArgs
err := advArgs.UnmarshalJSON([]byte(`null`))
if err != nil {
t.Fatalf("UnmarshalJSON failed: %v", err)
}
if !advArgs.IsEmpty() {
t.Error("Expected empty AdvArgs after unmarshalling null")
}
})
t.Run("invalid JSON returns error", func(t *testing.T) {
t.Parallel()
var advArgs zapscript.AdvArgs
err := advArgs.UnmarshalJSON([]byte(`{invalid}`))
if err == nil {
t.Error("Expected error for invalid JSON")
}
})
}