Repository navigation
Expand file tree
/
Copy pathtest_tools.das
More file actions
3961 lines (3675 loc) · 178 KB
/
Copy pathtest_tools.das
File metadata and controls
3961 lines (3675 loc) · 178 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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
options gen2
options rtti
require dastest/testing_boost public
require daslib/json
require daslib/json_boost
require strings
require daslib/fio
require tools/compile_check public
require tools/list_functions public
require tools/list_types public
require tools/run_script public
require tools/run_test public
require tools/format_file public
require daslib/das_source_formatter
require tools/ast_dump public
require tools/list_modules public
require tools/find_symbol public
require tools/list_module_api public
require tools/list_requires public
require tools/convert_to_gen2 public
require tools/goto_definition public
require tools/type_of public
require tools/find_references public
require tools/eval_expression public
require tools/describe_type public
require tools/grep_usage public
require tools/common public
require tools/cpp_common public
require tools/outline public
require tools/cpp_grep_usage public
require tools/cpp_find_symbol public
require tools/cpp_outline public
require tools/cpp_goto_definition public
require tools/cpp_compile_check public
require tools/cpp_format_file public
require tools/cpp_status public
require tools/aot public
require tools/lint_tool public
require tools/program_log public
require tools/live public
require tools/detect_duplicates public
require tools/export_corpus public
require tools/arch_of public
require tools/arch_sites public
require tools/discover public
def parse_result(result : string; var text : string&; var is_error : bool&) : bool {
var err : string
var parsed = read_json(result, err)
if (parsed == null) return false
let content = parsed?["content"]
if (content == null) {
unsafe { delete parsed; }
return false
}
let first = content?[0]
if (first == null) {
unsafe { delete parsed; }
return false
}
let text_val = first?["text"]
if (text_val != null) {
text = text_val.value as _string
}
let err_val = parsed?["isError"]
if (err_val != null) {
is_error = err_val.value as _bool
}
unsafe { delete parsed; }
return true
}
def fixture_path(name : string) : string => "utils/mcp/tests/{name}"
def private require_ast_grep(t : T?) : bool {
if (!empty(get_ast_grep_cmd())) return true
t->log("ast-grep not installed — skipping test (install @ast-grep/cli to enable)")
return false
}
[test]
def test_compile_check_valid(t : T?) {
t |> run("valid file compiles") <| @(t : T?) {
var text : string
var is_error = false
let ok = parse_result(do_compile_check(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(ok, "parse result JSON")
t |> success(!is_error, "should not be error")
t |> success(find(text, "Compilation OK") >= 0, "should contain 'Compilation OK'")
}
t |> run("lists functions") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(find(text, "def greet") >= 0, "should list greet function")
t |> success(find(text, "def main") >= 0, "should list main function")
}
}
[test]
def test_compile_check_error(t : T?) {
t |> run("invalid file reports error") <| @(t : T?) {
var text : string
var is_error = false
let ok = parse_result(do_compile_check(fixture_path("_fixture_error.das")), text, is_error)
t |> success(ok, "parse result JSON")
t |> success(is_error, "should be error")
t |> success(find(text, "Compilation failed") >= 0, "should contain 'Compilation failed'")
}
}
[test]
def test_compile_check_missing(t : T?) {
t |> run("missing file reports error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check("nonexistent_file_12345.das"), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "not found") >= 0, "should mention file not found")
}
}
[test]
def test_compile_check_batch_comma(t : T?) {
t |> run("batch compile with comma-separated files") <| @(t : T?) {
var text : string
var is_error = false
let files = "{fixture_path("_fixture_valid.das")},{fixture_path("_fixture_error.das")}"
parse_result(do_compile_check(files), text, is_error)
t |> success(is_error, "batch with error file should report error")
t |> success(find(text, "1 passed") >= 0, "should have 1 passed")
t |> success(find(text, "1 failed") >= 0, "should have 1 failed")
t |> success(find(text, "PASS") >= 0, "should have PASS line")
t |> success(find(text, "FAIL") >= 0, "should have FAIL line")
}
}
[test]
def test_compile_check_batch_glob(t : T?) {
t |> run("batch compile with glob pattern") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check("utils/mcp/tests/*.das"), text, is_error)
t |> success(find(text, "PASS") >= 0, "should have at least one PASS")
t |> success(find(text, "files") >= 0, "should have summary line")
}
}
[test]
def test_compile_check_glob_no_match(t : T?) {
t |> run("glob with no matches reports error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check("nonexistent_dir_xyz/*.das"), text, is_error)
t |> success(is_error, "should be error when no files match")
t |> success(find(text, "no files matched") >= 0, "should mention no files matched")
}
}
[test]
def test_compile_check_invalid_project(t : T?) {
t |> run("missing project file reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check(fixture_path("_fixture_valid.das"), "nonexistent_xyz.das_project"), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "'project' file not found") >= 0, "should mention project file not found")
}
t |> run("non-.das_project extension reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check(fixture_path("_fixture_valid.das"), fixture_path("_fixture_valid.das")), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "expected a path to a .das_project file") >= 0, "should mention expected extension")
}
}
[test]
def test_run_script_invalid_project(t : T?) {
t |> run("missing project file reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_run_script("", "print(\"hi\")", "", false, "nonexistent_xyz.das_project"), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "'project' file not found") >= 0, "should mention project file not found")
}
t |> run("non-.das_project extension reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_run_script("", "print(\"hi\")", "", false, fixture_path("_fixture_valid.das")), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "expected a path to a .das_project file") >= 0, "should mention expected extension")
}
}
[test]
def test_eval_expression_invalid_project(t : T?) {
t |> run("missing project file reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_eval_expression("1 + 1", "", "nonexistent_xyz.das_project"), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "'project' file not found") >= 0, "should mention project file not found")
}
t |> run("non-.das_project extension reports focused error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_eval_expression("1 + 1", "", fixture_path("_fixture_valid.das")), text, is_error)
t |> success(is_error, "should be error")
t |> success(find(text, "expected a path to a .das_project file") >= 0, "should mention expected extension")
}
}
def has_path(files : array<string>; suffix : string) : bool {
for (f in files) {
if (find(f, suffix) >= 0) return true
}
return false
}
[test]
def test_parse_file_list_smoke(t : T?) {
t |> run("parse_file_list reachable through MCP require chain") <| @(t : T?) {
var files : array<string>
parse_file_list("{fixture_path("_fixture_valid.das")},utils/mcp/tests/_fixture_e*.das", files)
t |> success(has_path(files, "_fixture_valid.das"), "plain entry preserved")
t |> success(has_path(files, "_fixture_error.das"), "glob expanded")
}
}
[test]
def test_list_functions(t : T?) {
t |> run("lists user functions") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_functions(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "def greet") >= 0, "should list greet")
t |> success(find(text, "def main") >= 0, "should list main")
}
}
[test]
def test_list_types(t : T?) {
t |> run("lists structs, enums, classes") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_types(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "Point") >= 0, "should list Point struct")
t |> success(find(text, "Color") >= 0, "should list Color enum")
t |> success(find(text, "Animal") >= 0, "should list Animal class")
}
}
[test]
def test_run_script_inline(t : T?) {
t |> run("inline code executes") <| @(t : T?) {
var text : string
var is_error = false
let code = "options gen2\n[export]\ndef main() \{\n print(\"hello_mcp_test\\n\")\n\}\n"
parse_result(do_run_script("", code), text, is_error)
t->log("output assertions disabled: popen stdout capture is broken under dastest on Windows")
}
}
[test]
def test_run_script_error(t : T?) {
t |> run("bad code reports error") <| @(t : T?) {
var text : string
var is_error = false
let code = "options gen2\ndef broken() : int \{\n return \"nope\"\n\}\n"
parse_result(do_run_script("", code), text, is_error)
t |> success(is_error, "should be error")
}
}
[test]
def test_run_test_passing(t : T?) {
t |> run("passing test returns success") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_run_test(fixture_path("_fixture_test.das")), text, is_error)
t->log("result assertions disabled: popen stdout capture is broken under dastest on Windows")
}
}
[test]
def test_run_test_bad_file(t : T?) {
t |> run("nonexistent file reports error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_run_test("nonexistent_test_file.das"), text, is_error)
t |> success(is_error, "should be error")
}
}
[test]
def test_ast_dump_expression(t : T?) {
t |> run("expression mode returns S-expression") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_ast_dump("a + b", "", "", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "ExprOp2") >= 0, "should contain ExprOp2")
t |> success(find(text, "ExprVar") >= 0, "should contain ExprVar")
}
}
[test]
def test_ast_dump_file_ast_mode(t : T?) {
t |> run("file ast mode returns S-expression") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_ast_dump("", fixture_path("_fixture_valid.das"), "add", "ast_core", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "def add") >= 0, "should contain function header")
t |> success(find(text, "ExprReturn") >= 0, "should contain ExprReturn node")
}
}
[test]
def test_ast_dump_file_source_mode(t : T?) {
t |> run("file source mode returns describe output") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_ast_dump("", fixture_path("_fixture_valid.das"), "add", "source", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "def add") >= 0, "should contain function header")
t |> success(find(text, "return") >= 0, "should contain return statement")
t |> success(find(text, "ExprOp2") < 0, "should not contain S-expression nodes")
}
}
[test]
def test_ast_dump_export_all(t : T?) {
t |> run("export_all preserves non-main functions") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_ast_dump("", fixture_path("_fixture_valid.das"), "greet", "source", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "def greet") >= 0, "greet should be preserved by export_all")
}
}
[test]
def test_list_modules(t : T?) {
t |> run("lists builtin modules") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_modules(), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "math") >= 0, "should list math module")
t |> success(find(text, "strings") >= 0, "should list strings module")
}
t |> run("lists daslib modules") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_modules(), text, is_error)
t |> success(find(text, "json") >= 0, "should list json module")
}
t |> run("a module this program compiled from das is not a builtin, and the registered paths are a section") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_modules(true), text, is_error)
var err : string
var parsed = read_json(text, err)
t |> success(parsed != null, "json parses: {err}")
return if (parsed == null)
t |> success(json_array_has(parsed?["builtin"], "math"), "math is a builtin")
t |> success(!json_array_has(parsed?["builtin"], "json_boost"), "json_boost, which this program compiled, is not listed as a builtin")
t |> success(json_array_has(parsed?["daslib"], "json_boost"), "json_boost is a daslib module")
t |> success(parsed?["registered"] != null, "the registered require paths are listed")
unsafe { delete parsed; }
}
}
def private json_array_has(arr : JsonValue?; wanted : string) : bool {
if (arr == null || !(arr.value is _array)) return false
for (m in arr.value as _array) {
if (m != null && m.value is _string && (m.value as _string) == wanted) return true
}
return false
}
def private project_root_fixture() : string =>
path_join(get_das_root(), "utils/mcp/tests/_pretend_root")
//! the pretend root's second module folder: its descriptor registers the tree's UnitTest C++ module
//! and a require path of its own, and every tool asked with project_root sees both - as does a plain run
[test]
def test_project_root_fixture(t : T?) {
let root = project_root_fixture()
let script = path_join(root, "probe_consumer.das")
t |> run("list_modules with project_root lists the descriptor's C++ module and require path") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_modules(true, root), text, is_error)
t |> success(!is_error, "should not be error: {text}")
var err : string
var parsed = read_json(text, err)
t |> success(parsed != null, "json parses: {err}")
return if (parsed == null)
t |> success(json_array_has(parsed?["builtin"], "UnitTest"), "the C++ module the descriptor registered is a builtin")
t |> success(json_array_has(parsed?["registered"], "unit_probe/hello"), "the require path the descriptor registered is listed")
unsafe { delete parsed; }
}
t |> run("find_symbol with project_root finds a symbol from the project's own require path") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_find_symbol("probe_hello_value", "", script, "", root), text, is_error)
t |> success(!is_error, "should not be error: {text}")
t |> success(find(text, "probe_hello_value") >= 0, "should find the project's function")
}
t |> run("compile_check with project_root compiles a file requiring both") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_compile_check(script, "", root), text, is_error)
t |> success(!is_error, "should not be error: {text}")
t |> success(find(text, "Compilation OK") >= 0, "should compile: {text}")
}
t |> run("a plain run and an eager run of the file both reach the C++ module and the require path") <| @(t : T?) {
let exe = get_daslang_exe()
t |> success(!empty(exe), "daslang executable is known")
return if (empty(exe))
var plain_out : string
let plain_rc = run_and_capture([exe, "-project_root", root, script], plain_out, 120.0)
t |> equal(plain_rc, 0, "plain run exits 0: {plain_out}")
t |> success(find(plain_out, "probe 4242 1234") >= 0, "plain run prints the probe: {plain_out}")
var eager_out : string
let eager_rc = run_and_capture([exe, "-ignore-manifest", "-project_root", root, script], eager_out, 120.0)
t |> equal(eager_rc, 0, "eager run exits 0: {eager_out}")
t |> success(find(eager_out, "probe 4242 1234") >= 0, "eager run prints the probe: {eager_out}")
}
}
[test]
def test_find_symbol(t : T?) {
t |> run("finds builtin function") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_find_symbol("print", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "print") >= 0, "should find print")
}
t |> run("case-insensitive search") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_find_symbol("PRINT", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "print") >= 0, "should find print case-insensitively")
}
t |> run("exact match with = prefix") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_find_symbol("=print", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "print") >= 0, "should find exact 'print'")
}
t |> run("finds typedef aliases") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_find_symbol("Flags", "typedef", fixture_path("_fixture_refs.das")), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "typedef") >= 0, "should find typedef")
t |> success(find(text, "Flags") >= 0, "should name Flags")
t |> success(find(text, "bitfield") >= 0, "should show bitfield type")
}
}
[test]
def test_list_module_api(t : T?) {
t |> run("lists math module functions") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("math", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "sin") >= 0, "should list sin function")
t |> success(find(text, "cos") >= 0, "should list cos function")
}
t |> run("rewrites unqualified daslib name") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("ast", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "[resolved 'ast' as 'daslib/ast']") >= 0, "should announce rewrite")
t |> success(find(text, "AstVisitor") >= 0, "should list daslib/ast contents")
}
t |> run("no rewrite when name is already qualified") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("daslib/ast", "", ""), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "[resolved") < 0, "should not announce rewrite")
}
}
[test]
def test_list_module_api_annotations(t : T?) {
t |> run("lists function annotations from ast_boost") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("daslib/ast_boost", "", "annotations"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "[macro]") >= 0, "should list [macro] function annotation")
t |> success(find(text, "Function annotations") >= 0, "should have Function annotations section")
}
t |> run("lists structure annotations from ast_boost") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("daslib/ast_boost", "", "annotations"), text, is_error)
t |> success(find(text, "[function_macro]") >= 0, "should list [function_macro]")
t |> success(find(text, "Structure annotations") >= 0, "should have Structure annotations section")
}
t |> run("lists call macros from ast") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("ast_core", "", "annotations"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "quote") >= 0, "should list quote call macro")
t |> success(find(text, "Call macros") >= 0, "should have Call macros section")
}
t |> run("lists reader macros from regex_boost") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("daslib/regex_boost", "", "annotations"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "regex") >= 0, "should list regex reader macro")
t |> success(find(text, "Reader macros") >= 0, "should have Reader macros section")
}
t |> run("filter works on annotations") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_module_api("daslib/ast_boost", "macro", "annotations"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "[macro]") >= 0, "should match macro")
t |> success(find(text, "[contract]") < 0, "should not match contract")
}
}
def private parse_json_array(text : string; var parsed : JsonValue?&) : bool {
var jerr : string
parsed = read_json(text, jerr)
return parsed != null && parsed.value is _array
}
def private find_format_result(arr : JsonValue?; file_substr : string; var status : string&; var message : string&) : bool {
if (arr == null || !(arr.value is _array)) return false
for (entry in arr.value as _array) {
if (entry == null) continue
let file_val = entry?["file"]
if (file_val == null || !(file_val.value is _string)) continue
let file = file_val.value as _string
if (find(file, file_substr) < 0) continue
let status_val = entry?["status"]
let msg_val = entry?["message"]
if (status_val != null && status_val.value is _string) {
status = status_val.value as _string
}
if (msg_val != null && msg_val.value is _string) {
message = msg_val.value as _string
}
return true
}
return false
}
[test]
def test_format_file(t : T?) {
t |> run("single file returns JSON array with required fields") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_format_file(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(!is_error, "should not be error")
var parsed : JsonValue?
let ok = parse_json_array(text, parsed)
t |> success(ok, "inner payload should be a JSON array")
if (!ok) {
unsafe { delete parsed; }
return
}
t |> success(!empty(parsed.value as _array), "array should contain at least one result")
let first = (parsed.value as _array)[0]
t |> success(first != null, "first result should exist")
if (first != null) {
t |> success(first?["file"] != null, "result has file")
t |> success(first?["status"] != null, "result has status")
t |> success(first?["message"] != null, "result has message")
let st = first?["status"]
if (st != null && st.value is _string) {
let status = st.value as _string
t |> success(status == "formatted" || status == "already_formatted", "status is expected")
} else {
t |> failure("status should be a string")
}
}
unsafe { delete parsed; }
}
t |> run("comma-separated list returns one result per file") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_format_file("{fixture_path("_fixture_valid.das")},{fixture_path("_fixture_error.das")}"), text, is_error)
t |> success(!is_error, "both fixture files should be processable")
var parsed : JsonValue?
let ok = parse_json_array(text, parsed)
t |> success(ok, "inner payload should be a JSON array")
if (!ok) {
unsafe { delete parsed; }
return
}
t |> equal(length(parsed.value as _array), 2, "comma-separated list should yield two results")
var st1 = ""
var msg1 = ""
let has_valid = find_format_result(parsed, "_fixture_valid.das", st1, msg1)
t |> success(has_valid, "valid fixture result present")
t |> success(!empty(st1), "valid fixture has non-empty status")
t |> success(find(msg1, "fixture_valid") >= 0, "valid fixture message references file")
var st2 = ""
var msg2 = ""
let has_error_fixture = find_format_result(parsed, "_fixture_error.das", st2, msg2)
t |> success(has_error_fixture, "error fixture result present")
t |> success(!empty(st2), "error fixture has non-empty status")
t |> success(find(msg2, "fixture_error") >= 0, "error fixture message references file")
unsafe { delete parsed; }
}
}
[test]
def test_format_file_batches(t : T?) {
t |> run("glob pattern expands and returns matching file entry") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_format_file("utils/mcp/tests/_fixture_v*.das"), text, is_error)
t |> success(!is_error, "glob with matches should not be error")
var parsed : JsonValue?
let ok = parse_json_array(text, parsed)
t |> success(ok, "inner payload should be a JSON array")
if (!ok) {
unsafe { delete parsed; }
return
}
var st = ""
var msg = ""
let has_valid = find_format_result(parsed, "_fixture_valid.das", st, msg)
t |> success(has_valid, "glob should include _fixture_valid.das")
t |> success(st == "formatted" || st == "already_formatted", "glob result status is expected")
unsafe { delete parsed; }
}
t |> run("mixed valid + missing sets isError and reports per-file statuses") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_format_file("{fixture_path("_fixture_valid.das")},utils/mcp/tests/_fixture_does_not_exist_123.das"), text, is_error)
t |> success(is_error, "missing file in batch should set isError")
var parsed : JsonValue?
let ok = parse_json_array(text, parsed)
t |> success(ok, "inner payload should be a JSON array")
if (!ok) {
unsafe { delete parsed; }
return
}
t |> equal(length(parsed.value as _array), 2, "mixed batch should still return two result entries")
var st_ok = ""
var msg_ok = ""
let has_valid = find_format_result(parsed, "_fixture_valid.das", st_ok, msg_ok)
t |> success(has_valid, "valid file result present")
t |> success(st_ok == "formatted" || st_ok == "already_formatted", "valid file status is expected")
var st_missing = ""
var msg_missing = ""
let has_missing = find_format_result(parsed, "_fixture_does_not_exist_123.das", st_missing, msg_missing)
t |> success(has_missing, "missing file result present")
t |> equal(st_missing, "error")
t |> success(find(msg_missing, "Cannot open file") >= 0, "missing file reports open error")
unsafe { delete parsed; }
}
}
[test]
def test_format_strip_comments_source(t : T?) {
t |> run("keeps header, docs, fmt directives, nolint; strips the rest") <| @(t : T?) {
let src = fread(fixture_path("_fixture_comments.das"))
t |> success(!empty(src), "fixture readable")
let stripped = format_source_string(src, true)
t |> success(find(stripped, "// header block stays") >= 0, "header block kept")
t |> success(find(stripped, "//! doc comment survives") >= 0, "doc comment kept")
t |> success(find(stripped, "nolint:STYLE005") >= 0, "nolint suppression kept with its why")
t |> success(find(stripped, "@nolint") >= 0, "@nolint form kept")
t |> success(find(stripped, "narration") < 0, "narration stripped")
t |> success(find(stripped, "trailing dies") < 0, "trailing comment stripped")
t |> success(find(stripped, "block dies") < 0, "block comment stripped")
t |> success(find(stripped, "let z = x + y") >= 0, "multi-line inline comment joins its expression")
}
t |> run("license comments survive the strip anywhere in the file") <| @(t : T?) {
let src = "options gen2\n\ndef mid \{\n return 1\n\}\n\n// Copyright (c) 2008 Bjoern Hoehrmann\n// SPDX-License-Identifier: MIT\n// COPYRIGHT 2026 SHOUTING NOTICES TOO\ndef tail \{\n return 2\n\}\n"
let stripped = format_source_string(src, true)
t |> success(find(stripped, "Copyright (c) 2008") >= 0, "Copyright line kept mid-file")
t |> success(find(stripped, "SPDX-License-Identifier: MIT") >= 0, "SPDX line kept mid-file")
t |> success(find(stripped, "COPYRIGHT 2026") >= 0, "match is case-insensitive")
}
t |> run("a fmt directive ends a kept continuation chain") <| @(t : T?) {
let src = "options gen2\n\ndef head \{\n return 1\n\}\n\n//! documented contract\n//fmt:keep-blank-lines\n// narration dies here\ndef tail \{\n return 2\n\}\n"
let stripped = format_source_string(src, true)
t |> success(find(stripped, "documented contract") >= 0, "doc kept")
t |> success(find(stripped, "fmt:keep-blank-lines") >= 0, "directive kept")
t |> success(find(stripped, "narration dies") < 0, "narration after the directive is not a continuation")
}
t |> run("strip-disabled path leaves every comment in place") <| @(t : T?) {
let src = fread(fixture_path("_fixture_comments.das"))
let formatted = format_source_string(src, false)
t |> success(find(formatted, "narration dies") >= 0, "narration kept when not stripping")
t |> success(find(formatted, "block dies") >= 0, "block comment kept when not stripping")
}
t |> run("//fmt:ignore-file opts out of stripping too") <| @(t : T?) {
let src = "//fmt:ignore-file\nlet X = 1\n// narration survives ignore\nlet Y = 2\n"
let out = format_source_string(src, true)
t |> success(find(out, "narration survives ignore") >= 0, "ignore-file blocks the strip")
}
t |> run("header under options survives; a stripped banner leaves no blank-line scar") <| @(t : T?) {
let src = "options gen2\n\n// header under options stays\n\nrequire strings // trailing narration dies\n\ndef a() \{\n return\n\}\n\n\n// banner dies\n\n\ndef b() \{\n return\n\}\n"
let out = format_source_string(src, true)
t |> success(find(out, "header under options stays") >= 0, "header kept below the options line")
t |> success(find(out, "trailing narration") < 0, "trailing narration on a require line is not header")
t |> success(find(out, "banner") < 0, "banner stripped")
t |> success(find(out, "\n\n\n\n") < 0, "no 3+ blank run at the strip site")
}
t |> run("//fmt: directives inside code survive a strip") <| @(t : T?) {
let src = "options gen2\n\ndef a() \{\n //fmt:keep-alignment\n // narration dies\n return\n\}\n"
let out = format_source_string(src, true)
t |> success(find(out, "//fmt:keep-alignment") >= 0, "fmt directive kept")
t |> success(find(out, "narration") < 0, "narration stripped")
}
t |> run("a kept comment keeps its attached continuation lines, whole or not at all") <| @(t : T?) {
let src = "options gen2\n\ndef a() \{\n // nolint:STYLE014 — the why starts here and\n // continues on a second line without truncation\n return\n\}\n\ndef b() \{\n //! doc head\n // doc continuation line\n return\n\}\n"
let out = format_source_string(src, true)
t |> success(find(out, "continues on a second line") >= 0, "nolint why kept whole")
t |> success(find(out, "doc continuation line") >= 0, "//! continuation kept")
}
t |> run("the chain extends through a third line, and a block comment never continues it") <| @(t : T?) {
let src = "options gen2\n\ndef a() \{\n // nolint:STYLE014 — line one of the why,\n // line two of the why,\n // line three of the why\n return\n\}\n\ndef b() \{\n //! doc head\n /* block narration dies */\n return\n\}\n"
let out = format_source_string(src, true)
t |> success(find(out, "line three of the why") >= 0, "third continuation line kept")
t |> success(find(out, "block narration") < 0, "a /* */ under a kept head is not a continuation")
}
t |> run("a blank line or code breaks the continuation chain") <| @(t : T?) {
let src = "options gen2\n\ndef a() \{\n // nolint:STYLE014 — one-line why\n\n // separate narration dies\n return // nolint:STYLE014 — trailing why\n // not a continuation of a trailing head, dies\n\}\n"
let out = format_source_string(src, true)
t |> success(find(out, "one-line why") >= 0, "head kept")
t |> success(find(out, "separate narration") < 0, "blank line breaks the chain")
t |> success(find(out, "trailing why") >= 0, "trailing nolint kept")
t |> success(find(out, "not a continuation") < 0, "trailing head opens no chain")
}
t |> run("!is-prefixed identifiers survive formatting untouched") <| @(t : T?) {
let src = "options gen2\n\ndef check(f : bool) : bool \{\n return !is_unary(f)\n\}\n"
let out = format_source_string(src, false)
t |> success(find(out, "!is_unary(f)") >= 0,
"template matching has no word boundary: a '!is'/'!as' entry in greedy_match_safe_tokens would eat the prefix of !is_unary-style unary-not calls - those two tokens must stay OUT of the list")
}
}
def private make_policy_dir(name, config : string; var err : string&) : string {
let dir = create_temp_directory(name, err)
return "" if (empty(dir))
mkdir(path_join(dir, ".git"))
if (!empty(config)) {
fwrite(path_join(dir, ".lint_config"), config)
}
return dir
}
[test]
def test_format_file_comment_policy(t : T?) {
t |> run("force_clean_comments folder strips, and the compile verify passes") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_strip_test", "[format]\nforce_clean_comments = true\n", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let dst = path_join(dir, "victim.das")
let original = fread(fixture_path("_fixture_comments.das"))
t |> success(fwrite(dst, original), "fixture copied")
var text : string
var is_error = false
let ok = parse_result(do_format_file(dst), text, is_error)
t |> success(ok, "parse result JSON")
t |> success(find(text, "comments stripped") >= 0, "opted-in folder stripped -- text={text}")
t |> success(find(text, "rescue_advisory") >= 0, "strip batch appends the rescue advisory -- text={text}")
t |> success(find(text, "harvester") >= 0, "advisory names the harvester agent")
let after = fread(dst)
t |> success(find(after, "narration") < 0, "comments removed on disk")
t |> success(find(after, "//! doc comment survives") >= 0, "docs kept on disk")
rmdir_rec(dir)
}
t |> run("a reformat that strips no comments raises no advisory") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_nostrip_test", "[format]\nforce_clean_comments = true\n", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let dst = path_join(dir, "spacing.das")
fwrite(dst, "options gen2\n\ndef f() \{\n let x=1\n return x\n\}\n")
var text : string
var is_error = false
parse_result(do_format_file(dst), text, is_error)
t |> success(find(text, "\"status\":\"formatted\"") >= 0, "spacing-only file was reformatted -- text={text}")
t |> success(find(text, "rescue_advisory") < 0, "no comments deleted, no advisory")
rmdir_rec(dir)
}
t |> run("fail-closed: a strip whose compile_check fails restores the original bytes") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_revert_test", "[format]\nforce_clean_comments = true\n", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let dst = path_join(dir, "broken.das")
let src = "options gen2\n\ndef broken() \{\n // narration to strip\n return undefined_symbol_xyz\n\}\n"
t |> success(fwrite(dst, src), "broken source written")
var text : string
var is_error = false
let ok = parse_result(do_format_file(dst), text, is_error)
t |> success(ok, "parse result JSON")
t |> success(is_error, "reverted outcome reports as error")
t |> success(find(text, "reverted") >= 0, "status is reverted -- text={text}")
t |> success(find(text, "rescue_advisory") < 0, "a reverted strip keeps its comments — no rescue advisory")
t |> equal(fread(dst), src, "original bytes restored")
rmdir_rec(dir)
}
t |> run("no config: formats, comments stay") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_keep_test", "", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let dst = path_join(dir, "victim.das")
fwrite(dst, fread(fixture_path("_fixture_comments.das")))
var text : string
var is_error = false
parse_result(do_format_file(dst), text, is_error)
t |> success(!is_error, "default run should not error")
t |> success(find(text, "rescue_advisory") < 0, "nothing stripped — no rescue advisory")
let after = fread(dst)
t |> success(find(after, "narration dies") >= 0, "comments intact on disk")
rmdir_rec(dir)
}
}
[test]
def test_format_file_policy_scoping(t : T?) {
t |> run("format_enabled = false skips the file untouched") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_skip_test", "[format]\nformat_enabled = false\n", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let dst = path_join(dir, "corpus.das")
let src = "options gen2\ndef f() \{\n let x=1\n\}\n"
fwrite(dst, src)
var text : string
var is_error = false
parse_result(do_format_file(dst), text, is_error)
t |> success(find(text, "skipped") >= 0, "status is skipped -- text={text}")
t |> equal(fread(dst), src, "bytes untouched")
rmdir_rec(dir)
}
t |> run("nearest .lint_config wins per key") <| @(t : T?) {
var terr : string
let dir = make_policy_dir("mcp_cascade_test", "[format]\nforce_clean_comments = true\n", terr)
if (empty(dir)) {
t |> failure("temp dir: {terr}")
return
}
let sub = path_join(dir, "teaching")
mkdir(sub)
fwrite(path_join(sub, ".lint_config"), "[format]\nforce_clean_comments = false\n")
let dst = path_join(sub, "victim.das")
fwrite(dst, fread(fixture_path("_fixture_comments.das")))
var text : string
var is_error = false
parse_result(do_format_file(dst), text, is_error)
t |> success(!is_error, "cascade run should not error")
let after = fread(dst)
t |> success(find(after, "narration dies") >= 0, "child override kept comments")
rmdir_rec(dir)
}
}
[test]
def test_list_requires(t : T?) {
t |> run("lists direct requires") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_requires(fixture_path("_fixture_requires.das")), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "Direct requires") >= 0, "should have Direct requires section")
t |> success(find(text, "require math") >= 0, "should list math")
t |> success(find(text, "require strings") >= 0, "should list strings")
}
t |> run("marks public requires") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_requires(fixture_path("_fixture_requires.das")), text, is_error)
t |> success(find(text, "strings public") >= 0, "should mark strings as public")
}
t |> run("annotates builtin modules") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_requires(fixture_path("_fixture_requires.das")), text, is_error)
t |> success(find(text, "// builtin") >= 0, "should annotate builtin modules")
}
t |> run("file with no explicit requires") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_list_requires(fixture_path("_fixture_valid.das")), text, is_error)
t |> success(!is_error, "should not be error")
}
}
[test]
def test_convert_to_gen2(t : T?) {
t |> run("converts gen1 to gen2 stdout") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_convert_to_gen2(fixture_path("_fixture_gen1.das"), false), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "options gen2") >= 0, "should add options gen2")
t |> success(find(text, "def add(") >= 0, "should contain add function")
t |> success(find(text, "\{") >= 0, "should contain braces")
}
t |> run("adds parentheses to control flow") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_convert_to_gen2(fixture_path("_fixture_gen1.das"), false), text, is_error)
t |> success(find(text, "if (") >= 0, "should add parens to if")
}
t |> run("missing file reports error") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_convert_to_gen2("nonexistent_file_12345.das", false), text, is_error)
t |> success(is_error, "should be error for missing file")
}
}
[test]
def test_goto_definition_function(t : T?) {
t |> run("resolves function call to definition") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_goto_definition(fixture_path("_fixture_goto.das"), "33", "13"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "Kind: function") >= 0, "should be function kind")
t |> success(find(text, "Symbol: distance") >= 0, "should resolve to distance")
t |> success(find(text, "Line: 14") >= 0, "should point to line 14")
}
}
[test]
def test_goto_definition_variable(t : T?) {
t |> run("resolves variable reference to declaration") <| @(t : T?) {
var text : string
var is_error = false
parse_result(do_goto_definition(fixture_path("_fixture_goto.das"), "33", "22"), text, is_error)
t |> success(!is_error, "should not be error")
t |> success(find(text, "Kind: variable") >= 0, "should be variable kind")
t |> success(find(text, "Symbol: p1") >= 0, "should resolve to p1")
t |> success(find(text, "Line: 31") >= 0, "should point to line 31")
}