1 module contract.inheritance;
2 
3 
4 import contract;
5 
6 
7 @Tags("contract")
8 @("single")
9 @safe unittest {
10     const tu = parse(
11         Cpp(
12             q{
13                 struct Base {
14                     int i;
15                 };
16 
17                 struct Derived: public Base {
18                     double d;
19                 };
20             }
21         )
22     );
23 
24     tu.children.length.should == 2;
25 
26     const base = tu.child(0);
27     base.kind.should == Cursor.Kind.StructDecl;
28     base.spelling.should == "Base";
29     base.type.kind.should == Type.Kind.Record;
30     base.type.spelling.should == "Base";
31 
32     printChildren(base);
33     base.children.length.should == 1;
34 
35     const derived = tu.child(1);
36     derived.kind.should == Cursor.Kind.StructDecl;
37     derived.spelling.should == "Derived";
38     derived.type.kind.should == Type.Kind.Record;
39     derived.type.spelling.should == "Derived";
40 
41     printChildren(derived);
42     derived.children.length.should == 2;
43 
44     const j = derived.child(1);
45     j.kind.should == Cursor.Kind.FieldDecl;
46 
47     const baseSpec = derived.child(0);
48     baseSpec.kind.should == Cursor.Kind.CXXBaseSpecifier;
49     baseSpec.spelling.should == "struct Base";
50     baseSpec.type.kind.should == Type.Kind.Record;
51     baseSpec.type.spelling.should == "Base";
52 
53     printChildren(baseSpec);
54     baseSpec.children.length.should == 1;
55 
56     const typeRef = baseSpec.child(0);
57     typeRef.kind.should == Cursor.Kind.TypeRef;
58     typeRef.spelling.should == "struct Base";
59     typeRef.type.kind.should == Type.Kind.Record;
60     typeRef.type.spelling.should == "Base";
61 
62     printChildren(typeRef);
63     typeRef.children.length.should == 0;
64 }
65 
66 
67 @Tags("contract")
68 @("multiple")
69 @safe unittest {
70     const tu = parse(
71         Cpp(
72             q{
73                 struct Base0 {
74                     int i;
75                 };
76 
77                 struct Base1 {
78                     int j;
79                 };
80 
81                 struct Derived: public Base0, Base1 {
82                     double d;
83                 };
84             }
85         )
86     );
87 
88     tu.children.length.should == 3;
89 
90     const derived = tu.child(2);
91     derived.shouldMatch(Cursor.Kind.StructDecl, "Derived");
92     derived.type.shouldMatch(Type.Kind.Record, "Derived");
93 
94     printChildren(derived);
95     derived.children.length.should == 3;
96 
97     const baseSpec0 = derived.child(0);
98     baseSpec0.shouldMatch(Cursor.Kind.CXXBaseSpecifier, "struct Base0");
99     baseSpec0.type.shouldMatch(Type.Kind.Record, "Base0");
100     printChildren(baseSpec0);
101     baseSpec0.children.length.should == 1;
102 
103     const typeRef0 = baseSpec0.child(0);
104     typeRef0.shouldMatch(Cursor.Kind.TypeRef, "struct Base0");
105     typeRef0.type.shouldMatch(Type.Kind.Record, "Base0");
106     typeRef0.children.length.should == 0;
107 
108     const baseSpec1 = derived.child(1);
109     baseSpec1.shouldMatch(Cursor.Kind.CXXBaseSpecifier, "struct Base1");
110     baseSpec1.type.shouldMatch(Type.Kind.Record, "Base1");
111     printChildren(baseSpec1);
112     baseSpec1.children.length.should == 1;
113 
114     const typeRef1 = baseSpec1.child(0);
115     typeRef1.shouldMatch(Cursor.Kind.TypeRef, "struct Base1");
116     typeRef1.type.shouldMatch(Type.Kind.Record, "Base1");
117     typeRef1.children.length.should == 0;
118 }