1 module contract.aggregates; 2 3 4 import contract; 5 6 7 @("struct.simple") 8 @safe unittest { 9 const tu = parse( 10 Cpp( 11 q{ 12 struct Struct { int i; }; 13 } 14 ) 15 ); 16 17 tu.children.length.should == 1; 18 const struct_ = tu.children[0]; 19 struct_.kind.should == Cursor.Kind.StructDecl; 20 struct_.spelling.should == "Struct"; 21 struct_.type.kind.should == Type.Kind.Record; 22 struct_.type.spelling.should == "Struct"; 23 24 printChildren(struct_); 25 struct_.children.length.should == 1; 26 const member = struct_.children[0]; 27 member.kind.should == Cursor.Kind.FieldDecl; 28 member.spelling.should == "i"; 29 30 member.type.kind.should == Type.Kind.Int; 31 member.type.spelling.should == "int"; 32 } 33 34 35 @("struct.nested") 36 @safe unittest { 37 const tu = parse( 38 Cpp( 39 q{ 40 struct Outer { 41 int integer; 42 struct Inner { 43 int x; 44 } inner; 45 }; 46 } 47 ) 48 ); 49 50 const outer = tu.children[0]; 51 printChildren(outer); 52 outer.children.length.should == 3; 53 54 55 const integer = outer.children[0]; 56 integer.kind.should == Cursor.Kind.FieldDecl; 57 integer.spelling.should == "integer"; 58 integer.type.kind.should == Type.Kind.Int; 59 integer.type.spelling.should == "int"; 60 61 62 const innerStruct = outer.children[1]; 63 innerStruct.kind.should == Cursor.Kind.StructDecl; 64 innerStruct.spelling.should == "Inner"; 65 printChildren(innerStruct); 66 innerStruct.children.length.should == 1; // the `x` field 67 68 innerStruct.type.kind.should == Type.Kind.Record; 69 innerStruct.type.spelling.should == "Outer::Inner"; 70 innerStruct.type.canonical.kind.should == Type.Kind.Record; 71 innerStruct.type.canonical.spelling.should == "Outer::Inner"; 72 73 const xfield = innerStruct.children[0]; 74 xfield.kind.should == Cursor.Kind.FieldDecl; 75 xfield.spelling.should == "x"; 76 xfield.type.kind.should == Type.Kind.Int; 77 78 79 const innerField = outer.children[2]; 80 innerField.kind.should == Cursor.Kind.FieldDecl; 81 innerField.spelling.should == "inner"; 82 printChildren(innerField); 83 innerField.children.length.should == 1; // the Inner StructDecl 84 85 innerField.type.kind.should == Type.Kind.Elaborated; 86 innerField.type.spelling.should == "struct Inner"; 87 innerField.type.canonical.kind.should == Type.Kind.Record; 88 innerField.type.canonical.spelling.should == "Outer::Inner"; 89 90 innerField.children[0].should == innerStruct; 91 }