forked from DeusData/codebase-memory-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_extraction_inheritance.c
More file actions
1679 lines (1626 loc) · 75 KB
/
Copy pathtest_extraction_inheritance.c
File metadata and controls
1679 lines (1626 loc) · 75 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
/*
* test_extraction_inheritance.c — Reproduce-first suite for class-inheritance
* base-class extraction across the 9 hybrid-LSP languages.
*
* DESIGN INTENT
* ─────────────
* Every row asserts the CORRECT behaviour: that `CBMDefinition.base_classes`
* contains real type names, not keywords or punctuation. For the three broken
* languages the rows are EXPECTED RED until the extractors are fixed:
*
* RED (broken extractors — rows FAIL until fixed):
* TypeScript / TSX — extractor stores the keyword "extends" / "implements"
* instead of the following type name.
* PHP — base_classes is never populated (always NULL / empty).
* Kotlin — `:` supertype syntax is not parsed → base_classes empty.
*
* GREEN (correct extractors — rows PASS as regression guards):
* Python — NOTE: the simple `class Dog(Animal):` case is ALSO red because
* collect_bases_from_field does not yet match the plain `identifier`
* node used by tree-sitter-python (it grabs the argument_list text
* "(Animal)" with parens). The reproduction for that root-cause is
* already in test_extraction.c (python_class_base_extracted_bare).
* This suite adds multi-base, generic-base, and qualified-base
* variants that share the same root bug — they are all RED until the
* identifier-node fix lands. Python cases that use
* `type_identifier` nodes (not yet confirmed) are tentatively
* labelled RED.
* Java — single-base and multi-interface extraction is correct (regression
* guard for the fix landed with #279).
* C# — extraction is correct (regression guards).
* C++ — extraction is correct (regression guards).
* Rust — impl_traits array (not base_classes) is the capture point; Rust
* rows verify impl_traits entries for `impl Trait for Struct`.
*
* STRUCTURE
* ─────────
* inherit_case_t — one test row.
* run_inherit_case — generic runner: extract → find def → check base_classes
* (or impl_traits for Rust).
* One TEST() per language group so failures are readable at the suite level.
* SUITE(extraction_inheritance) wires them together via RUN_TEST().
*
* HOW TO ADD TO THE RUNNER
* ─────────────────────────
* In test_main.c add:
* extern void suite_extraction_inheritance(void);
* RUN_SUITE(extraction_inheritance);
* (Do NOT do this here — another workstream owns test_main.c.)
*/
#include "test_framework.h"
#include "cbm.h"
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
/* ── Shared helpers ─────────────────────────────────────────────── */
/* Find a definition by label+name; returns NULL if not found. */
static CBMDefinition *find_def(CBMFileResult *r, const char *label, const char *name) {
for (int i = 0; i < r->defs.count; i++) {
CBMDefinition *d = &r->defs.items[i];
if (d->label && strcmp(d->label, label) == 0 && d->name && strcmp(d->name, name) == 0)
return d;
}
return NULL;
}
/* Find a definition by name alone (any label). */
static CBMDefinition *find_def_any(CBMFileResult *r, const char *name) {
for (int i = 0; i < r->defs.count; i++) {
CBMDefinition *d = &r->defs.items[i];
if (d->name && strcmp(d->name, name) == 0)
return d;
}
return NULL;
}
/* Return 1 if base_classes contains `want` exactly. */
static int bases_contain(CBMDefinition *d, const char *want) {
if (!d->base_classes)
return 0;
for (const char **b = d->base_classes; *b; b++) {
if (strcmp(*b, want) == 0)
return 1;
}
return 0;
}
/* Return 1 if ANY base_classes entry contains `substr` as a substring.
* Used to assert that keywords like "extends" / "implements" / ":" do NOT
* appear inside any captured base name. */
static int bases_contain_substr(CBMDefinition *d, const char *substr) {
if (!d->base_classes)
return 0;
for (const char **b = d->base_classes; *b; b++) {
if (strstr(*b, substr))
return 1;
}
return 0;
}
/* Count entries in a NULL-terminated base_classes array. */
static int bases_count(CBMDefinition *d) {
if (!d->base_classes)
return 0;
int n = 0;
for (const char **b = d->base_classes; *b; b++)
n++;
return n;
}
/* ── Table-driven case type ─────────────────────────────────────── */
/* Labels used to look up the class definition in the extraction result.
* The extractor uses "Class", "Interface", "Struct" — we try all three in
* find_def_flex() below. */
typedef struct {
CBMLanguage lang;
const char *path; /* file path hint (sets language via extension too) */
const char *src; /* source snippet */
const char *class_name; /* name to look up */
const char *expected_bases[8]; /* NULL-terminated list of real type names to find */
/* bad_strings: substrings that must NOT appear inside any base_classes entry.
* Used to catch keyword-capture bugs. NULL-terminated, or {NULL} if unused. */
const char *bad_strings[6];
int min_base_count; /* >= this many entries expected (0 = don't check count) */
} inherit_case_t;
/* Find def trying Class / Interface / Struct / any-label fallback. */
static CBMDefinition *find_def_flex(CBMFileResult *r, const char *name) {
CBMDefinition *d;
if ((d = find_def(r, "Class", name)))
return d;
if ((d = find_def(r, "Interface", name)))
return d;
if ((d = find_def(r, "Struct", name)))
return d;
/* Fallback: any label — covers languages where the extractor uses custom labels */
return find_def_any(r, name);
}
/*
* Generic runner for one inherit_case_t row.
* Returns 0 on pass, 1 on failure (sets FAIL path via printf + return).
* Cannot use ASSERT macros directly here because they do `return 1` — which
* is fine since this helper also returns int. We use manual checks so we
* can include the case description in the failure message.
*/
static int run_inherit_case(const inherit_case_t *tc) {
CBMFileResult *r =
cbm_extract_file(tc->src, (int)strlen(tc->src), tc->lang, "t", tc->path, 0, NULL, NULL);
if (!r) {
printf(" FAIL [%s] cbm_extract_file returned NULL\n", tc->class_name);
return 1;
}
CBMDefinition *cls = find_def_flex(r, tc->class_name);
if (!cls) {
printf(" FAIL [%s] definition not found in extraction result\n", tc->class_name);
cbm_free_result(r);
return 1;
}
/* When no bases are expected and no min count is set, this is a
* no-crash / sanity row — skip the rest of the checks. */
if (!tc->expected_bases[0] && tc->min_base_count == 0) {
cbm_free_result(r);
return 0;
}
/* Assert base_classes is non-NULL (at least one base expected). */
if (!cls->base_classes) {
printf(" FAIL [%s] base_classes is NULL (expected non-empty)\n", tc->class_name);
cbm_free_result(r);
return 1;
}
/* Assert each expected base name is present. */
int ok = 1;
for (int i = 0; tc->expected_bases[i]; i++) {
if (!bases_contain(cls, tc->expected_bases[i])) {
printf(" FAIL [%s] expected base \"%s\" not found in base_classes\n", tc->class_name,
tc->expected_bases[i]);
ok = 0;
}
}
/* Assert no bad keyword strings appear inside any base name. */
for (int i = 0; tc->bad_strings[i]; i++) {
if (bases_contain_substr(cls, tc->bad_strings[i])) {
printf(" FAIL [%s] bad string \"%s\" found inside a base_classes entry\n",
tc->class_name, tc->bad_strings[i]);
ok = 0;
}
}
/* Assert minimum count. */
if (tc->min_base_count > 0) {
int cnt = bases_count(cls);
if (cnt < tc->min_base_count) {
printf(" FAIL [%s] base_classes has %d entries, expected >= %d\n", tc->class_name,
cnt, tc->min_base_count);
ok = 0;
}
}
cbm_free_result(r);
return ok ? 0 : 1;
}
/* Convenience macro: run a table of cases, accumulate failures. */
#define RUN_CASES(table) \
do { \
int _fail = 0; \
int _n = (int)(sizeof(table) / sizeof(table[0])); \
for (int _i = 0; _i < _n; _i++) { \
if (run_inherit_case(&table[_i]) != 0) \
_fail++; \
} \
if (_fail > 0) { \
printf(" FAIL %d / %d cases above failed\n", _fail, _n); \
return 1; \
} \
} while (0)
/* ═══════════════════════════════════════════════════════════════════
* PYTHON (expected: RED until identifier-node fix in extract_defs.c)
*
* Root cause: collect_bases_from_field() does not match the plain
* `identifier` node used by tree-sitter-python for unqualified base
* names → raw argument_list text "(Base)" (with parens) is stored
* instead of "Base". Fix: accept `identifier` as a valid child type.
* ═══════════════════════════════════════════════════════════════════ */
static const inherit_case_t python_cases[] = {
/* ── single base (bare identifier) ────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"class Animal:\n pass\n\nclass Dog(Animal):\n pass\n",
"Dog",
{"Animal", NULL},
{"(", ")", NULL},
1},
/* ── multiple bases ─────────────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"class A: pass\nclass B: pass\nclass C(A, B): pass\n",
"C",
{"A", "B", NULL},
{"(", ")", NULL},
2},
/* ── base + mixin ───────────────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"class Base: pass\nclass Mixin: pass\nclass Service(Base, Mixin): pass\n",
"Service",
{"Base", "Mixin", NULL},
{"(", ")", NULL},
2},
/* ── generic base (subscript, e.g. Generic[T]) ──────────────── */
{CBM_LANG_PYTHON,
"m.py",
"from typing import Generic, TypeVar\nT = TypeVar('T')\n"
"class Stack(Generic[T]):\n pass\n",
"Stack",
{"Generic", NULL},
{"(", ")", NULL},
1},
/* ── qualified base (dotted: module.Base) ───────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"import django.db\nclass MyModel(django.db.Model):\n pass\n",
"MyModel",
{"django.db.Model", NULL},
{"(", ")", NULL},
1},
/* ── abstract base (abc.ABC) ────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"from abc import ABC, abstractmethod\nclass Shape(ABC):\n @abstractmethod\n def "
"area(self): pass\n",
"Shape",
{"ABC", NULL},
{"(", ")", NULL},
1},
/* ── dataclass with base ────────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"from dataclasses import dataclass\n@dataclass\nclass Point:\n x: float\n\n"
"@dataclass\nclass Point3D(Point):\n z: float\n",
"Point3D",
{"Point", NULL},
{"(", ")", NULL},
1},
/* ── exception subclass ─────────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"class AppError(Exception): pass\n",
"AppError",
{"Exception", NULL},
{"(", ")", NULL},
1},
/* ── three bases ────────────────────────────────────────────── */
{CBM_LANG_PYTHON,
"m.py",
"class X: pass\nclass Y: pass\nclass Z: pass\nclass Multi(X, Y, Z): pass\n",
"Multi",
{"X", "Y", "Z", NULL},
{"(", ")", NULL},
3},
/* ── base with keyword argument (metaclass=) should not bleed ── */
{CBM_LANG_PYTHON,
"m.py",
"class Meta: pass\nclass MyClass(object, metaclass=Meta): pass\n",
"MyClass",
{"object", NULL},
{"(", ")", "metaclass", NULL},
1},
};
TEST(inherit_python) {
RUN_CASES(python_cases);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* JAVA (expected: GREEN — regression guards for #279 fix)
* ═══════════════════════════════════════════════════════════════════ */
static const inherit_case_t java_cases[] = {
/* ── extends only ───────────────────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Dog extends Animal { }",
"Dog",
{"Animal", NULL},
{"extends", "implements", NULL},
1},
/* ── implements single interface ─────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class ConcreteList implements List { }",
"ConcreteList",
{"List", NULL},
{"extends", "implements", NULL},
1},
/* ── extends + implements one ────────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class MyThread extends Thread implements Runnable { }",
"MyThread",
{"Thread", "Runnable", NULL},
{"extends", "implements", NULL},
2},
/* ── extends + implements two (regression for #279) ─────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class DefaultLinkTool extends DefaultDiagramTool implements ILinkTool, Closeable { }",
"DefaultLinkTool",
{"DefaultDiagramTool", "ILinkTool", "Closeable", NULL},
{"extends", "implements", NULL},
3},
/* ── implements three interfaces ─────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Svc implements A, B, C { }",
"Svc",
{"A", "B", "C", NULL},
{"extends", "implements", NULL},
3},
/* ── generic base (extends List<String>) ─────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"import java.util.*; public class MyList extends ArrayList<String> { }",
"MyList",
{"ArrayList", NULL},
{"extends", "implements", NULL},
1},
/* ── generic implements (Comparable<T>) ──────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Box<T> implements Comparable<Box<T>> { public int compareTo(Box<T> o) { return "
"0; } }",
"Box",
{"Comparable", NULL},
{"extends", "implements", NULL},
1},
/* ── abstract class extends ──────────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public abstract class AbstractSvc extends BaseService { protected abstract void run(); }",
"AbstractSvc",
{"BaseService", NULL},
{"extends", "implements", NULL},
1},
/* ── interface extends interface ─────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public interface ReadWriteRepo extends ReadRepo, WriteRepo { }",
"ReadWriteRepo",
{"ReadRepo", "WriteRepo", NULL},
{"extends", "implements", NULL},
2},
/* ── enum implements interface ───────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public enum Status implements Displayable { OPEN, CLOSED; public String display() { return "
"name(); } }",
"Status",
{"Displayable", NULL},
{"extends", "implements", NULL},
1},
/* ── qualified (imported) type name ─────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Handler extends java.net.ServerSocket { }",
"Handler",
{"java.net.ServerSocket", NULL},
{"extends", "implements", NULL},
1},
/* ── extends + implements four ───────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Mega extends Base implements A, B, C, D { }",
"Mega",
{"Base", "A", "B", "C", "D", NULL},
{"extends", "implements", NULL},
5},
/* ── nested class with base ──────────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Outer { public static class Inner extends Outer { } }",
"Inner",
{"Outer", NULL},
{"extends", "implements", NULL},
1},
/* ── record implements interface (Java 16+) ──────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public record Point(int x, int y) implements Comparable<Point> { "
"public int compareTo(Point o) { return Integer.compare(x, o.x); } }",
"Point",
{"Comparable", NULL},
{"extends", "implements", NULL},
1},
/* ── sealed class (Java 17+) ─────────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public sealed class Shape permits Circle, Rectangle { }",
"Shape",
{NULL}, /* permitted-types are not base_classes of Shape; no bases to assert */
{NULL},
0},
/* ── class implements Serializable ──────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"import java.io.*; public class Data extends BaseData implements Serializable, Cloneable { }",
"Data",
{"BaseData", "Serializable", "Cloneable", NULL},
{"extends", "implements", NULL},
3},
/* ── generic class extends generic base ──────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Pair<A, B> extends AbstractPair<A, B> implements Iterable<A> { "
"public java.util.Iterator<A> iterator() { return null; } }",
"Pair",
{"AbstractPair", "Iterable", NULL},
{"extends", "implements", NULL},
2},
/* ── class extending Exception ───────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class AppException extends RuntimeException { "
"public AppException(String msg) { super(msg); } }",
"AppException",
{"RuntimeException", NULL},
{"extends", "implements", NULL},
1},
/* ── multiple interfaces no extends ──────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"public class Codec implements Encoder, Decoder, Closeable { }",
"Codec",
{"Encoder", "Decoder", "Closeable", NULL},
{"extends", "implements", NULL},
3},
/* ── annotated class with base ───────────────────────────────── */
{CBM_LANG_JAVA,
"Svc.java",
"@Override public class AnnotatedSvc extends BaseSvc { }",
"AnnotatedSvc",
{"BaseSvc", NULL},
{"extends", "implements", NULL},
1},
};
TEST(inherit_java) {
RUN_CASES(java_cases);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* C# (expected: GREEN — regression guards)
* ═══════════════════════════════════════════════════════════════════ */
static const inherit_case_t csharp_cases[] = {
/* ── single base class ──────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Dog : Animal { }",
"Dog",
{"Animal", NULL},
{":", NULL},
1},
/* ── implements single interface ─────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Repo : IRepository { }",
"Repo",
{"IRepository", NULL},
{":", NULL},
1},
/* ── base + interface ────────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Service : BaseService, IService { }",
"Service",
{"BaseService", "IService", NULL},
{":", NULL},
2},
/* ── base + two interfaces ───────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Worker : BackgroundService, IWorker, IDisposable { }",
"Worker",
{"BackgroundService", "IWorker", "IDisposable", NULL},
{":", NULL},
3},
/* ── generic base ────────────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class OrderList : List<Order> { }",
"OrderList",
{"List", NULL},
{":", NULL},
1},
/* ── generic base + interface ────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Stack<T> : Collection<T>, IStack<T> { }",
"Stack",
{"Collection", "IStack", NULL},
{":", NULL},
2},
/* ── interface extends two interfaces ────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public interface ICrud : IRead, IWrite { }",
"ICrud",
{"IRead", "IWrite", NULL},
{":", NULL},
2},
/* ── abstract class with base ────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public abstract class AbstractHandler : BaseHandler { protected abstract void Handle(); }",
"AbstractHandler",
{"BaseHandler", NULL},
{":", NULL},
1},
/* ── sealed class ────────────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public sealed class SingletonService : BaseService { }",
"SingletonService",
{"BaseService", NULL},
{":", NULL},
1},
/* ── namespace-qualified base ────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"namespace App { public class Handler : System.Net.Http.HttpMessageHandler { "
"protected override System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> "
"SendAsync(System.Net.Http.HttpRequestMessage r, System.Threading.CancellationToken c) "
"{ return null; } } }",
"Handler",
{"System.Net.Http.HttpMessageHandler", NULL},
{":", NULL},
1},
/* ── partial class with base ─────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public partial class PartialSvc : BaseSvc, IPartial { }",
"PartialSvc",
{"BaseSvc", "IPartial", NULL},
{":", NULL},
2},
/* ── record with base ────────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public record OrderRecord(int Id) : BaseRecord(Id), IRecord { }",
"OrderRecord",
{"BaseRecord", "IRecord", NULL},
{":", NULL},
2},
/* ── struct implements interface ─────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public struct Point : IEquatable<Point> { public bool Equals(Point other) => true; }",
"Point",
{"IEquatable", NULL},
{":", NULL},
1},
/* ── class with four interfaces ──────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Mega : Base, IA, IB, IC, ID { }",
"Mega",
{"Base", "IA", "IB", "IC", "ID", NULL},
{":", NULL},
5},
/* ── exception subclass ─────────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class AppException : Exception { public AppException(string msg) : base(msg) {} }",
"AppException",
{"Exception", NULL},
{":", NULL},
1},
/* ── nested class with base ──────────────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Outer { public class Inner : Outer { } }",
"Inner",
{"Outer", NULL},
{":", NULL},
1},
/* ── class in namespace with base ───────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"namespace MyApp.Services { public class OrderSvc : BaseOrderSvc, IOrderSvc { } }",
"OrderSvc",
{"BaseOrderSvc", "IOrderSvc", NULL},
{":", NULL},
2},
/* ── interface with single parent ───────────────────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public interface IAdvancedService : IBasicService { void AdvancedOp(); }",
"IAdvancedService",
{"IBasicService", NULL},
{":", NULL},
1},
/* ── generic class implements generic interface ───────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Repo<T> : BaseRepo<T>, IRepo<T> where T : class { }",
"Repo",
{"BaseRepo", "IRepo", NULL},
{":", "where", NULL},
2},
/* ── class with IDisposable + IAsyncDisposable ───────────────── */
{CBM_LANG_CSHARP,
"Svc.cs",
"public class Resource : IDisposable, IAsyncDisposable { "
"public void Dispose() {} "
"public System.Threading.Tasks.ValueTask DisposeAsync() => default; }",
"Resource",
{"IDisposable", "IAsyncDisposable", NULL},
{":", NULL},
2},
};
TEST(inherit_csharp) {
RUN_CASES(csharp_cases);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* C++ (expected: GREEN — regression guards)
* ═══════════════════════════════════════════════════════════════════ */
static const inherit_case_t cpp_cases[] = {
/* ── public single base ─────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Dog : public Animal { };",
"Dog",
{"Animal", NULL},
{"public", "private", "protected", ":", NULL},
1},
/* ── private base ────────────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Impl : private Base { };",
"Impl",
{"Base", NULL},
{"public", "private", "protected", ":", NULL},
1},
/* ── multiple inheritance ────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class C : public A, public B { };",
"C",
{"A", "B", NULL},
{"public", "private", "protected", ":", NULL},
2},
/* ── struct with base ────────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"struct Derived : Base { int x; };",
"Derived",
{"Base", NULL},
{":", NULL},
1},
/* ── virtual inheritance ─────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class D : public virtual B1, public virtual B2 { };",
"D",
{"B1", "B2", NULL},
{"virtual", "public", ":", NULL},
2},
/* ── template base ───────────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"template<typename T> class Stack : public std::vector<T> { };",
"Stack",
{"std::vector", NULL},
/* qualified base: `::` is legitimate, so the bare-`:` separator-leak
* guard does not apply here (it would match inside `std::vector`). */
{"public", "template", NULL},
1},
/* ── CRTP pattern ────────────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"template<typename Derived> class Base { };\n"
"class Concrete : public Base<Concrete> { };",
"Concrete",
{"Base", NULL},
{"public", ":", NULL},
1},
/* ── abstract class (pure virtual) with public base ──────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class AbstractLogger : public ILogger { public: virtual void log(const char*) = 0; };",
"AbstractLogger",
{"ILogger", NULL},
{"public", ":", NULL},
1},
/* ── class in namespace ──────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"namespace net { class Socket : public BaseSocket { }; }",
"Socket",
{"BaseSocket", NULL},
{"public", ":", NULL},
1},
/* ── three-way diamond inheritance ──────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class A { }; class B : public A { }; class C : public A { };\n"
"class D : public B, public C { };",
"D",
{"B", "C", NULL},
{"public", ":", NULL},
2},
/* ── fully qualified base name ───────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class MyStream : public std::ostream { public: MyStream() : std::ostream(nullptr) {} };",
"MyStream",
{"std::ostream", NULL},
/* qualified base: bare-`:` guard omitted (matches inside `std::ostream`). */
{"public", NULL},
1},
/* ── protected base ──────────────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Node : protected TreeNode { };",
"Node",
{"TreeNode", NULL},
{"protected", ":", NULL},
1},
/* ── multiple bases with mixed access ────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Widget : public Drawable, private EventHandler, protected Serializable { };",
"Widget",
{"Drawable", "EventHandler", "Serializable", NULL},
{"public", "private", "protected", ":", NULL},
3},
/* ── exception class from std::exception ─────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"#include <stdexcept>\nclass AppError : public std::runtime_error { "
"public: AppError(const char* m) : std::runtime_error(m) {} };",
"AppError",
{"std::runtime_error", NULL},
/* qualified base: bare-`:` guard omitted (matches inside `std::runtime_error`). */
{"public", NULL},
1},
/* ── template class with multiple template base types ────────── */
{CBM_LANG_CPP,
"svc.cpp",
"template<typename K, typename V>\n"
"class LruCache : public Cache<K,V>, public Observable { };",
"LruCache",
{"Cache", "Observable", NULL},
{"public", ":", NULL},
2},
/* ── nested class with base ──────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Outer { public: class Inner : public Base { }; };",
"Inner",
{"Base", NULL},
{"public", ":", NULL},
1},
/* ── class using final specifier ─────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class Leaf final : public Node { };",
"Leaf",
{"Node", NULL},
{"public", "final", ":", NULL},
1},
/* ── struct with scoped base ─────────────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"struct MyVisitor : public boost::static_visitor<int> { int operator()(int x) { return x; } "
"};",
"MyVisitor",
{"boost::static_visitor", NULL},
/* qualified base: bare-`:` guard omitted (matches inside `boost::static_visitor`). */
{"public", NULL},
1},
/* ── policy-based design (two template base policies) ────────── */
{CBM_LANG_CPP,
"svc.cpp",
"template<class StoragePolicy, class LogPolicy>\n"
"class Engine : public StoragePolicy, public LogPolicy { };",
"Engine",
{"StoragePolicy", "LogPolicy", NULL},
{"public", ":", NULL},
2},
/* ── empty base optimization (EBO) ──────────────────────────── */
{CBM_LANG_CPP,
"svc.cpp",
"class EboContainer : private Allocator, public ContainerBase { };",
"EboContainer",
{"Allocator", "ContainerBase", NULL},
{"private", "public", ":", NULL},
2},
};
TEST(inherit_cpp) {
RUN_CASES(cpp_cases);
PASS();
}
/* ═══════════════════════════════════════════════════════════════════
* TYPESCRIPT (expected: RED — keyword-capture bug)
*
* Bug: the TS extractor stores "extends" / "implements" as the base
* name instead of the following type name.
* ═══════════════════════════════════════════════════════════════════ */
static const inherit_case_t typescript_cases[] = {
/* ── extends single class ────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Dog extends Animal { bark(): void {} }",
"Dog",
{"Animal", NULL},
{"extends", "implements", NULL},
1},
/* ── implements single interface ─────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Repo implements IRepository { save(x: any) {} }",
"Repo",
{"IRepository", NULL},
{"extends", "implements", NULL},
1},
/* ── extends + implements ────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Service extends BaseService implements IService { run() {} }",
"Service",
{"BaseService", "IService", NULL},
{"extends", "implements", NULL},
2},
/* ── implements two interfaces ───────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Codec implements Encoder, Decoder { encode(x: any) {} decode(x: any) {} }",
"Codec",
{"Encoder", "Decoder", NULL},
{"extends", "implements", NULL},
2},
/* ── extends + implements three ──────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Mega extends Base implements A, B, C { }",
"Mega",
{"Base", "A", "B", "C", NULL},
{"extends", "implements", NULL},
4},
/* ── generic base ────────────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Stack<T> extends Array<T> implements IStack<T> { push(x: T) { return 0; } }",
"Stack",
{"Array", "IStack", NULL},
{"extends", "implements", NULL},
2},
/* ── interface extends interface ─────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"interface ReadWriteRepo extends ReadRepo, WriteRepo { }",
"ReadWriteRepo",
{"ReadRepo", "WriteRepo", NULL},
{"extends", "implements", NULL},
2},
/* ── abstract class ──────────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"abstract class AbstractSvc extends BaseService { abstract run(): void; }",
"AbstractSvc",
{"BaseService", NULL},
{"extends", "implements", NULL},
1},
/* ── class extends Error ─────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class AppError extends Error { constructor(msg: string) { super(msg); } }",
"AppError",
{"Error", NULL},
{"extends", "implements", NULL},
1},
/* ── class extends EventEmitter ──────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"import { EventEmitter } from 'events';\n"
"class Bus extends EventEmitter { emit(ev: string) { return super.emit(ev); } }",
"Bus",
{"EventEmitter", NULL},
{"extends", "implements", NULL},
1},
/* ── generic class extends generic base ──────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Pair<A, B> extends AbstractPair<A, B> implements Iterable<A> { "
"[Symbol.iterator]() { return this as any; } }",
"Pair",
{"AbstractPair", "Iterable", NULL},
{"extends", "implements", NULL},
2},
/* ── class implements multiple generic interfaces ─────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Handler implements Middleware<Request, Response>, Disposable { "
"handle(req: Request): Response { return null as any; } dispose() {} }",
"Handler",
{"Middleware", "Disposable", NULL},
{"extends", "implements", NULL},
2},
/* ── class in module namespace ───────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"export class OrderService extends BaseOrderService implements IOrderService { }",
"OrderService",
{"BaseOrderService", "IOrderService", NULL},
{"extends", "implements", NULL},
2},
/* ── mixin target class (concrete class consuming a mixin) ──────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class Loggable { log(msg: string) { console.log(msg); } }\n"
"class Logger extends Loggable implements ILogger { info(msg: string) { this.log(msg); } }",
"Logger",
{"Loggable", "ILogger", NULL},
{"extends", "implements", NULL},
2},
/* ── decorator + extends ─────────────────────────────────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"function Injectable() { return (c: any) => c; }\n"
"@Injectable()\nclass UserService extends BaseUserService { }",
"UserService",
{"BaseUserService", NULL},
{"extends", "implements", NULL},
1},
/* ── class implementing multiple inferred generics ───────────── */
{CBM_LANG_TYPESCRIPT,
"svc.ts",
"class BinaryTree<T> extends Tree<T> implements Traversable<T>, Serializable { "