1 module contract.methods; 2 3 4 import contract; 5 6 7 @("header") 8 @safe unittest { 9 10 const tu = parse( 11 Cpp( 12 q{ 13 struct Foo { 14 double fun(int i); 15 }; 16 17 double Foo::fun(int i) { 18 return i * 2; 19 } 20 } 21 ), 22 ); 23 24 tu.children.length.should == 2; 25 26 const foo = tu.child(0); 27 foo.shouldMatch(Cursor.Kind.StructDecl, "Foo"); 28 29 const funDef = tu.child(1); 30 funDef.shouldMatch(Cursor.Kind.CXXMethod, "fun"); 31 funDef.type.shouldMatch(Type.Kind.FunctionProto, "double (int)"); 32 printChildren(funDef); 33 funDef.children.length.should == 3; 34 writelnUt(funDef.tokens); 35 36 const typeRef = funDef.child(0); 37 typeRef.shouldMatch(Cursor.Kind.TypeRef, "struct Foo"); 38 39 const param = funDef.child(1); 40 param.shouldMatch(Cursor.Kind.ParmDecl, "i"); 41 42 // this is where the magic happens 43 const body_ = funDef.child(2); 44 body_.shouldMatch(Cursor.Kind.CompoundStmt, ""); 45 body_.type.shouldMatch(Type.Kind.Invalid, ""); 46 printChildren(body_); 47 body_.children.length.should == 1; 48 49 const return_ = body_.child(0); 50 return_.shouldMatch(Cursor.Kind.ReturnStmt, ""); 51 return_.type.shouldMatch(Type.Kind.Invalid, ""); 52 printChildren(return_); 53 return_.children.length.should == 1; 54 55 const doubleExpr = return_.child(0); 56 doubleExpr.shouldMatch(Cursor.Kind.FirstExpr, ""); 57 doubleExpr.type.shouldMatch(Type.Kind.Double, "double"); 58 printChildren(doubleExpr); 59 60 // it keeps going after this 61 }