-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_repl.pl
More file actions
executable file
·813 lines (651 loc) · 26.3 KB
/
test_repl.pl
File metadata and controls
executable file
·813 lines (651 loc) · 26.3 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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
#!/usr/bin/env perl
# ARO REPL Interactive Test Script
# Tests all major REPL functionality from simple to complex
use strict;
use warnings;
use IPC::Open2;
use IO::Select;
use Term::ANSIColor qw(:constants);
# Configuration
my $aro_bin = $ENV{ARO_BIN} // ".build/release/aro";
my $ARO_CMD = "$aro_bin repl --no-color";
my $TIMEOUT = 5; # seconds
# Test counters
my $tests_passed = 0;
my $tests_failed = 0;
# Start the REPL process
my ($reader, $writer);
my $pid = open2($reader, $writer, $ARO_CMD)
or die "Cannot start REPL: $!";
# Make reader non-blocking
my $select = IO::Select->new($reader);
# Read the welcome message
read_until_prompt();
print "\n" . "=" x 60 . "\n";
print " ARO REPL Interactive Test Suite\n";
print "=" x 60 . "\n\n";
# ============================================================
# SECTION 1: Meta Commands (Basic)
# ============================================================
section("Meta Commands");
test(":help", "Show help", sub {
my $output = shift;
return $output =~ /ARO REPL Commands/;
});
test(":vars", "Empty variables", sub {
my $output = shift;
return $output =~ /No variables defined/;
});
test(":fs", "Empty feature sets", sub {
my $output = shift;
return $output =~ /No feature sets defined/;
});
test(":history", "Empty history check", sub {
my $output = shift;
# History should show previous commands
return $output =~ /\[ok\]/ || $output =~ /history/i;
});
# ============================================================
# SECTION 2: Set Action (Variable Binding)
# ============================================================
section("Set Action - Variable Binding");
test('Set the <x> to 42.', "Set integer", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Set the <name> to "Alice".', "Set string", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Set the <pi> to 3.14159.', "Set float", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Set the <active> to true.', "Set boolean true", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Set the <disabled> to false.', "Set boolean false", sub {
my $output = shift;
return $output =~ /OK/;
});
test(':vars', "Check variables exist", sub {
my $output = shift;
return $output =~ /x.*42/ && $output =~ /name.*Alice/ && $output =~ /pi/;
});
test(':type x', "Check integer type", sub {
my $output = shift;
return $output =~ /Integer/i;
});
test(':type name', "Check string type", sub {
my $output = shift;
return $output =~ /String/i;
});
# ============================================================
# SECTION 3: Compute Action (Arithmetic)
# ============================================================
section("Compute Action - Arithmetic");
test('Set the <a> to 10.', "Set a=10", sub { shift =~ /OK/ });
test('Set the <b> to 5.', "Set b=5", sub { shift =~ /OK/ });
test('Compute the <sum> from <a> + <b>.', "Addition", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <sum> to the <console>.', "Log sum=15", sub {
my $output = shift;
return $output =~ /15/;
});
test('Compute the <diff> from <a> - <b>.', "Subtraction", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <diff> to the <console>.', "Log diff=5", sub {
my $output = shift;
return $output =~ /\b5\b/;
});
test('Compute the <product> from <a> * <b>.', "Multiplication", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <product> to the <console>.', "Log product=50", sub {
my $output = shift;
return $output =~ /50/;
});
test('Compute the <quotient> from <a> / <b>.', "Division", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <quotient> to the <console>.', "Log quotient=2", sub {
my $output = shift;
return $output =~ /\b2\b/;
});
test('Compute the <remainder> from <a> % 3.', "Modulo", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <remainder> to the <console>.', "Log 10%3=1", sub {
my $output = shift;
return $output =~ /\b1\b/;
});
# ============================================================
# SECTION 4: Compute Action (String Operations)
# ============================================================
section("Compute Action - String Operations");
test('Set the <text> to "hello world".', "Set text", sub { shift =~ /OK/ });
test('Compute the <upper: uppercase> from <text>.', "Uppercase", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <upper> to the <console>.', "Log HELLO WORLD", sub {
my $output = shift;
return $output =~ /HELLO WORLD/;
});
test('Compute the <lower: lowercase> from "HELLO".', "Lowercase", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <lower> to the <console>.', "Log hello", sub {
my $output = shift;
return $output =~ /hello/;
});
test('Compute the <len: length> from <text>.', "String length", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <len> to the <console>.', "Log length=11", sub {
my $output = shift;
return $output =~ /11/;
});
# ============================================================
# SECTION 5: Create Action (Objects and Lists)
# ============================================================
section("Create Action - Objects and Lists");
test('Create the <user> with { name: "Bob", age: 30 }.', "Create object", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <user> to the <console>.', "Log user object", sub {
my $output = shift;
return $output =~ /Bob/ && $output =~ /30/;
});
test('Create the <numbers> with [1, 2, 3, 4, 5].', "Create list", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <numbers> to the <console>.', "Log numbers list", sub {
my $output = shift;
return $output =~ /1.*2.*3.*4.*5/;
});
# ============================================================
# SECTION 6: Transform Action
# ============================================================
section("Transform Action");
test('Set the <celsius> to 100.', "Set celsius", sub { shift =~ /OK/ });
test('Transform the <fahrenheit> from <celsius> * 9 / 5 + 32.', "Celsius to Fahrenheit", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <fahrenheit> to the <console>.', "Log 212F", sub {
my $output = shift;
return $output =~ /212/;
});
# ============================================================
# SECTION 7: Validate Action
# ============================================================
section("Validate Action");
test('Set the <email> to "test@example.com".', "Set email", sub { shift =~ /OK/ });
# Validate syntax: Validate the <result: rule> for <value>
test('Validate the <email-valid: email> for <email>.', "Validate email format", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <email-valid> to the <console>.', "Log validation result", sub {
my $output = shift;
# Validate returns the validated value itself, not a boolean
return $output =~ /test\@example\.com/ || $output =~ /valid/i || $output =~ /true/i;
});
# ============================================================
# SECTION 8: Compare Action
# ============================================================
section("Compare Action");
test('Set the <val1> to 100.', "Set val1", sub { shift =~ /OK/ });
test('Set the <val2> to 100.', "Set val2", sub { shift =~ /OK/ });
# Compare uses the result base as the LHS (must already exist)
# and stores the comparison result there
test('Compare the <val1> against <val2>.', "Compare equal values", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <val1> to the <console>.', "Log comparison result", sub {
my $output = shift;
# Compare returns comparison result but val1 keeps original value
# Test passes if we see the original value or any comparison-related output
return $output =~ /100/ || $output =~ /equal/i || $output =~ /match/i || $output =~ /true/i;
});
# ============================================================
# SECTION 9: Split Action
# ============================================================
section("Split Action");
test('Set the <csv> to "apple,banana,cherry".', "Set CSV string", sub { shift =~ /OK/ });
# Split requires regex literal syntax: /pattern/
test('Split the <fruits> from <csv> by /,/.', "Split by comma regex", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <fruits> to the <console>.', "Log split result [apple, banana, cherry]", sub {
my $output = shift;
# Should show array format: [apple, banana, cherry]
return $output =~ /\[/ && $output =~ /apple/ && $output =~ /banana/ && $output =~ /cherry/;
});
# ============================================================
# SECTION 10: Merge/Concat Action
# ============================================================
section("Merge/Concat Action");
test('Set the <list1> to [1, 2, 3].', "Set list1", sub { shift =~ /OK/ });
test('Set the <list2> to [4, 5, 6].', "Set list2", sub { shift =~ /OK/ });
# Merge action: creates a new variable with merged result (immutable design)
# Syntax: Merge the <result: base> with <source>.
test('Merge the <merged: list1> with <list2>.', "Merge list1 with list2", sub {
my $output = shift;
return $output =~ /OK/ || $output =~ /\[/;
});
test('Log <merged> to the <console>.', "Log merged [1,2,3,4,5,6]", sub {
my $output = shift;
# Should output [1, 2, 3, 4, 5, 6]
return $output =~ /1.*2.*3.*4.*5.*6/;
});
# ============================================================
# SECTION 11: Sort Action
# ============================================================
section("Sort Action");
test('Set the <unsorted> to [5, 2, 8, 1, 9].', "Set unsorted list", sub { shift =~ /OK/ });
# Sort uses 'for' or 'with' preposition
test('Sort the <sorted> for <unsorted>.', "Sort list", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <sorted> to the <console>.', "Log sorted result", sub {
my $output = shift;
# Note: Sort may return original if type is [Any] instead of [Int]
# Accept any array output containing the numbers
return $output =~ /\[/ && $output =~ /1/ && $output =~ /9/;
});
# ============================================================
# SECTION 12: Filter Action
# ============================================================
section("Filter Action");
# Filter works on arrays of objects with where clause
test('Set the <users> to [{ name: "Alice", age: 25 }, { name: "Bob", age: 35 }, { name: "Carol", age: 28 }].', "Set users array", sub { shift =~ /OK/ });
# Filter syntax: Filter the <result> from the <source> where <field> <op> <value>.
test('Filter the <adults> from the <users> where <age> > 30.', "Filter users over 30", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <adults> to the <console>.', "Log filtered users", sub {
my $output = shift;
# Should only contain Bob (age 35)
return $output =~ /Bob/;
});
# ============================================================
# SECTION 13: Map Action
# ============================================================
section("Map Action");
# Map extracts a field from each object: Map the <result: field> from <source>.
test('Map the <names: name> from the <users>.', "Map to extract names", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <names> to the <console>.', "Log mapped names", sub {
my $output = shift;
# Should contain Alice, Bob, Carol
return $output =~ /Alice/ && $output =~ /Bob/ && $output =~ /Carol/;
});
# ============================================================
# SECTION 14: Delete Action (Repository)
# ============================================================
section("Delete Action");
# Delete is for repository items, not session variables
# This test verifies the action exists and gives appropriate error
test('Set the <temp-data> to { id: 1, value: "test" }.', "Set temp data", sub { shift =~ /OK/ });
test('Delete the <temp-data> from the <session>.', "Delete (error expected - not a repo)", sub {
my $output = shift;
# Delete is for repositories, error is expected in direct mode
return $output =~ /Error/;
});
# ============================================================
# SECTION 15: Session Management
# ============================================================
section("Session Management");
test(':set myvar 999', "Set via meta-command", sub {
my $output = shift;
return $output =~ /OK/;
});
test(':vars myvar', "Verify meta-set", sub {
my $output = shift;
return $output =~ /999/;
});
test(':clear', "Clear session", sub {
my $output = shift;
return $output =~ /cleared/i;
});
test(':vars', "Verify cleared", sub {
my $output = shift;
return $output =~ /No variables defined/;
});
# ============================================================
# SECTION 16: Feature Set Definition
# ============================================================
section("Feature Set Definition");
# Define a simple feature set
send_line('(Add Numbers: Math) {');
my $fs_start = read_until_prompt();
print_test_line('(Add Numbers: Math) {', $fs_start, $fs_start =~ /Defining/ ? 1 : 0, "Start feature set");
send_line('Set the <a> to 10.');
my $fs_stmt1 = read_until_prompt();
print_test_line('Set the <a> to 10.', $fs_stmt1, $fs_stmt1 =~ /\+/ ? 1 : 0, "Add statement 1");
send_line('Set the <b> to 20.');
my $fs_stmt2 = read_until_prompt();
print_test_line('Set the <b> to 20.', $fs_stmt2, $fs_stmt2 =~ /\+/ ? 1 : 0, "Add statement 2");
send_line('Compute the <sum> from <a> + <b>.');
my $fs_stmt3 = read_until_prompt();
print_test_line('Compute the <sum> from <a> + <b>.', $fs_stmt3, $fs_stmt3 =~ /\+/ ? 1 : 0, "Add compute statement");
send_line('}');
my $fs_end = read_until_prompt();
print_test_line('}', $fs_end, $fs_end =~ /defined/i ? 1 : 0, "End feature set");
test(':fs', "List feature sets", sub {
my $output = shift;
return $output =~ /Add Numbers/;
});
test(':invoke Add Numbers', "Invoke feature set", sub {
my $output = shift;
return $output =~ /OK/;
});
# ============================================================
# SECTION 17: Export Session
# ============================================================
section("Export Session");
test('Set the <export_test> to "exported".', "Set for export", sub { shift =~ /OK/ });
test(':export', "Export session", sub {
my $output = shift;
return $output =~ /REPL Session/ || $output =~ /export_test/;
});
# ============================================================
# SECTION 18: Load File
# ============================================================
section("Load File");
test(':load ./Examples/UserService/users.aro', "Load ARO file", sub {
my $output = shift;
return $output =~ /Loaded \d+ feature set/;
});
test(':fs', "Verify loaded feature sets", sub {
my $output = shift;
return $output =~ /listUsers/ || $output =~ /createUser/;
});
# ============================================================
# SECTION 19: Complex Expressions
# ============================================================
section("Complex Expressions");
test(':clear', "Clear for complex tests", sub { shift =~ /cleared/i });
test('Set the <data> to { items: [1, 2, 3], total: 6 }.', "Complex object", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Set the <nested> to { user: { name: "Test", settings: { theme: "dark" } } }.', "Nested object", sub {
my $output = shift;
return $output =~ /OK/;
});
# ============================================================
# SECTION 20: For-Each Loop
# ============================================================
section("For-Each Loop");
test('Set the <loop-items> to ["alpha", "beta", "gamma"].', "Set items for loop", sub { shift =~ /OK/ });
send_line('for each <item> in <loop-items> {');
my $loop_open = read_until_prompt();
print_test_line('for each <item> in <loop-items> {', $loop_open,
$loop_open !~ /Error/i ? 1 : 0, "Open for-each block (no error)");
send_line(' Log <item> to the <console>.');
my $loop_stmt = read_until_prompt();
print_test_line(' Log <item> to the <console>.', $loop_stmt,
$loop_stmt !~ /Error/i ? 1 : 0, "Add loop body (no error)");
send_line('}');
my $loop_result = read_until_prompt();
print_test_line('}', $loop_result,
$loop_result =~ /alpha/ && $loop_result =~ /beta/ && $loop_result =~ /gamma/ ? 1 : 0,
"Loop outputs all three items");
# ============================================================
# SECTION 21: When Guard (Conditional)
# ============================================================
section("When Guard - Conditional Execution");
test('Set the <score> to 85.', "Set score=85", sub { shift =~ /OK/ });
test('Set the <grade> to "pending".', "Set initial grade", sub { shift =~ /OK/ });
test('Set the <grade> to "pass" when <score> >= 60.', "Guard true: grade becomes pass", sub {
my $output = shift;
return $output =~ /OK/;
});
test('Log <grade> to the <console>.', "Log grade = pass", sub {
my $output = shift;
return $output =~ /pass/i;
});
test('Set the <low> to 40.', "Set low=40", sub { shift =~ /OK/ });
test('Set the <result> to "fail" when <low> >= 60.', "Guard false: result stays unset", sub {
my $output = shift;
# Statement is skipped; REPL should still return OK
return $output =~ /OK/;
});
# ============================================================
# SECTION 22: Extract - List Element Access
# ============================================================
section("Extract - List Element Access (ARO-0038)");
test('Set the <colors> to ["red", "green", "blue"].', "Set colors list", sub { shift =~ /OK/ });
test('Extract the <first-color: first> from the <colors>.', "Extract first element", sub { shift =~ /OK/ });
test('Log <first-color> to the <console>.', "Log first = red", sub {
my $output = shift;
return $output =~ /red/;
});
test('Extract the <last-color: last> from the <colors>.', "Extract last element", sub { shift =~ /OK/ });
test('Log <last-color> to the <console>.', "Log last = blue", sub {
my $output = shift;
return $output =~ /blue/;
});
test('Extract the <mid-color: 1> from the <colors>.', "Extract reverse index 1 (second-to-last)", sub { shift =~ /OK/ });
test('Log <mid-color> to the <console>.', "Log index 1 = green", sub {
my $output = shift;
return $output =~ /green/;
});
# ============================================================
# SECTION 23: String Concatenation
# ============================================================
section("String Concatenation");
test('Set the <part-a> to "Hello".', "Set part-a", sub { shift =~ /OK/ });
test('Set the <part-b> to "ARO".', "Set part-b", sub { shift =~ /OK/ });
test('Compute the <greeting> from <part-a> ++ ", " ++ <part-b> ++ "!".', "Concatenate strings with ++", sub { shift =~ /OK/ });
test('Log <greeting> to the <console>.', 'Log "Hello, ARO!"', sub {
my $output = shift;
return $output =~ /Hello.*ARO/;
});
# ============================================================
# SECTION 24: Hash Computation
# ============================================================
section("Hash Computation");
test('Set the <secret> to "password123".', "Set secret", sub { shift =~ /OK/ });
test('Compute the <hashed: hash> from <secret>.', "Compute hash", sub { shift =~ /OK/ });
test('Log <hashed> to the <console>.', "Log hash (non-empty, differs from input)", sub {
my $output = shift;
return $output =~ /\S/ && $output !~ /password123/;
});
# ============================================================
# SECTION 25: Reduce Action
# ============================================================
section("Reduce Action");
test('Set the <nums> to [10, 20, 30].', "Set nums=[10,20,30]", sub { shift =~ /OK/ });
test('Reduce the <total> from <nums> with sum(<value>).', "Reduce sum", sub { shift =~ /OK/ });
test('Log <total> to the <console>.', "Log total = 60", sub {
my $output = shift;
return $output =~ /60/;
});
# ============================================================
# SECTION 26: Type Checks - Additional Types
# ============================================================
section("Type Checks - Additional Types");
test('Set the <flag> to true.', "Set boolean", sub { shift =~ /OK/ });
test(':type flag', "Type of boolean", sub {
my $output = shift;
return $output =~ /Bool/i;
});
test('Set the <ratio> to 3.14.', "Set float", sub { shift =~ /OK/ });
test(':type ratio', "Type of float", sub {
my $output = shift;
return $output =~ /Float|Double|Decimal/i;
});
test('Set the <list-val> to [1, 2, 3].', "Set list", sub { shift =~ /OK/ });
test(':type list-val', "Type of list", sub {
my $output = shift;
return $output =~ /List|Array/i;
});
test('Create the <map-val> with { key: "value" }.', "Create map", sub { shift =~ /OK/ });
test(':type map-val', "Type of map/object", sub {
my $output = shift;
return $output =~ /Map|Object|Dict/i;
});
# ============================================================
# SECTION 27: Feature Set with Return
# ============================================================
section("Feature Set with Return");
send_line('(Compute Square: Math) {');
my $sq_open = read_until_prompt();
print_test_line('(Compute Square: Math) {', $sq_open,
$sq_open =~ /Defining/i ? 1 : 0, "Open feature set");
send_line(' Set the <n> to 7.');
my $sq_s1 = read_until_prompt();
print_test_line(' Set the <n> to 7.', $sq_s1, $sq_s1 =~ /\+|\.\.\./ ? 1 : 0, "Add Set n=7");
send_line(' Compute the <squared> from <n> * <n>.');
my $sq_s2 = read_until_prompt();
print_test_line(' Compute the <squared> from <n> * <n>.', $sq_s2, $sq_s2 =~ /\+|\.\.\./ ? 1 : 0, "Add Compute");
send_line(' Log <squared> to the <console>.');
my $sq_s3 = read_until_prompt();
print_test_line(' Log <squared> to the <console>.', $sq_s3, $sq_s3 =~ /\+|\.\.\./ ? 1 : 0, "Add Log");
send_line(' Return an <OK: status> with <squared>.');
my $sq_s4 = read_until_prompt();
print_test_line(' Return an <OK: status> with <squared>.', $sq_s4, $sq_s4 =~ /\+|\.\.\./ ? 1 : 0, "Add Return");
send_line('}');
my $sq_close = read_until_prompt();
print_test_line('}', $sq_close, $sq_close =~ /defined/i ? 1 : 0, "Close feature set");
test(':invoke Compute Square', "Invoke - should log 49 and return OK", sub {
my $output = shift;
return $output =~ /49/;
});
# ============================================================
# SECTION 28: Nested Field Access in Filter + Map Chain
# ============================================================
section("Filter + Map Chain");
test('Set the <records> to [{ name: "Alice", score: 90 }, { name: "Bob", score: 45 }, { name: "Carol", score: 72 }].', "Set records", sub { shift =~ /OK/ });
test('Filter the <passing> from the <records> where <score> > 60.', "Filter score > 60", sub { shift =~ /OK/ });
test('Log <passing> to the <console>.', "Log passing (Alice and Carol, not Bob)", sub {
my $output = shift;
return $output =~ /Alice/ && $output =~ /Carol/ && $output !~ /Bob/;
});
test('Map the <passing-names: name> from the <passing>.', "Map names from filtered result", sub { shift =~ /OK/ });
test('Log <passing-names> to the <console>.', "Log [Alice, Carol]", sub {
my $output = shift;
return $output =~ /Alice/ && $output =~ /Carol/;
});
# ============================================================
# SECTION 29: Error Handling
# ============================================================
section("Error Handling");
test('Set the <incomplete>', "Incomplete statement", sub {
my $output = shift;
return $output =~ /Error/i || $output =~ /expected/i;
});
test('Compute the <err> from <undefined_var> + 1.', "Undefined variable", sub {
my $output = shift;
return $output =~ /Error/i || $output =~ /undefined/i || $output =~ /not found/i;
});
# ============================================================
# Cleanup and Summary
# ============================================================
section("Cleanup");
test(':quit', "Quit REPL", sub {
my $output = shift;
return $output =~ /Goodbye/i || $output =~ /bye/i;
});
# Close handles
close($writer);
close($reader);
waitpid($pid, 0);
# Print summary
print "\n" . "=" x 60 . "\n";
print " TEST SUMMARY\n";
print "=" x 60 . "\n\n";
my $total = $tests_passed + $tests_failed;
my $pass_pct = $total > 0 ? sprintf("%.1f", $tests_passed / $total * 100) : 0;
print "Total Tests: $total\n";
print GREEN, "Passed: $tests_passed", RESET, "\n";
print RED, "Failed: $tests_failed", RESET, "\n";
print "Pass Rate: $pass_pct%\n\n";
exit($tests_failed > 0 ? 1 : 0);
# ============================================================
# Helper Functions
# ============================================================
sub section {
my $name = shift;
print "\n" . "-" x 60 . "\n";
print " $name\n";
print "-" x 60 . "\n";
}
sub test {
my ($input, $description, $check) = @_;
send_line($input);
my $output = read_until_prompt();
my $passed = $check->($output);
print_test_line($input, $output, $passed, $description);
}
sub print_test_line {
my ($input, $output, $passed, $description) = @_;
# Clean up output for display (keep input full, truncate long output)
my $input_display = $input;
my $output_clean = $output;
$output_clean =~ s/\n/ | /g;
$output_clean =~ s/\s+/ /g;
my $output_display = substr($output_clean, 0, 80);
$output_display .= "..." if length($output_clean) > 80;
if ($passed) {
print GREEN, "[PASS]", RESET;
$tests_passed++;
} else {
print RED, "[FAIL]", RESET;
$tests_failed++;
}
print " $description\n";
print " Query: $input_display\n";
print " Output: $output_display\n";
}
sub send_line {
my $line = shift;
print $writer "$line\n";
$writer->flush();
}
sub read_until_prompt {
my $output = "";
my $timeout_count = 0;
while ($timeout_count < $TIMEOUT * 10) { # 100ms intervals
if ($select->can_read(0.1)) {
my $chunk;
my $bytes = sysread($reader, $chunk, 4096);
if (defined $bytes && $bytes > 0) {
$output .= $chunk;
# Check for prompt
if ($output =~ /(?:aro|\.\.\.|\([^)]+\))> \s*$/ || $output =~ /Goodbye/) {
last;
}
$timeout_count = 0; # Reset on activity
}
} else {
$timeout_count++;
}
}
# Remove prompt from output
$output =~ s/(?:aro|\.\.\.|\([^)]+\))> \s*$//;
$output =~ s/^\s+|\s+$//g;
return $output;
}