1 module parse.cooked;
2 
3 
4 import test.infra;
5 import clang;
6 
7 
8 @("visitChildren C++ file with one simple struct")
9 @safe unittest {
10     with(NewTranslationUnit("foo.cpp",
11                             q{ struct Struct { int int_; double double_; }; }))
12     {
13         translUnit.cursor.visitChildren(
14             (cursor, parent) {
15 
16                 import clang: Cursor;
17 
18                 static int cursorIndex;
19 
20                 switch(cursorIndex) {
21 
22                 default:
23                     assert(false);
24 
25                 case 0:
26                     cursor.kind.shouldEqual(Cursor.Kind.StructDecl);
27                     parent.kind.shouldEqual(Cursor.Kind.TranslationUnit);
28                     break;
29 
30                 case 1:
31                     cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
32                     parent.kind.shouldEqual(Cursor.Kind.StructDecl);
33                     break;
34 
35                 case 2:
36                     cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
37                     parent.kind.shouldEqual(Cursor.Kind.StructDecl);
38                     break;
39                 }
40 
41                 ++cursorIndex;
42 
43 
44                 return ChildVisitResult.Recurse;
45             }
46         );
47     }
48 }
49 
50 
51 @("foreach(cursor, parent) C++ file with one simple struct")
52 @safe unittest {
53     with(NewTranslationUnit("foo.cpp",
54                             q{ struct Struct { int int_; double double_; }; }))
55     {
56         foreach(cursor, parent; translUnit.cursor) {
57 
58             import clang: Cursor;
59 
60             static int cursorIndex;
61 
62             switch(cursorIndex) {
63 
64             default:
65                 assert(false);
66 
67             case 0:
68                 cursor.kind.shouldEqual(Cursor.Kind.StructDecl);
69                 parent.kind.shouldEqual(Cursor.Kind.TranslationUnit);
70                 break;
71 
72             case 1:
73                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
74                 parent.kind.shouldEqual(Cursor.Kind.StructDecl);
75                 break;
76 
77             case 2:
78                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
79                 parent.kind.shouldEqual(Cursor.Kind.StructDecl);
80                 break;
81             }
82 
83             ++cursorIndex;
84         }
85     }
86 }
87 
88 
89 @("foreach(cursor) C++ file with one simple struct")
90 @safe unittest {
91     with(NewTranslationUnit("foo.cpp",
92                             q{ struct Struct { int int_; double double_; }; }))
93     {
94         foreach(cursor; translUnit.cursor) {
95 
96             import clang: Cursor;
97 
98             static int cursorIndex;
99 
100             switch(cursorIndex) {
101 
102             default:
103                 assert(false);
104 
105             case 0:
106                 cursor.kind.shouldEqual(Cursor.Kind.StructDecl);
107                 break;
108 
109             case 1:
110                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
111                 break;
112 
113             case 2:
114                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
115                 break;
116             }
117 
118             ++cursorIndex;
119         }
120     }
121 }
122 
123 
124 @("cursor.children C++ file with one simple struct")
125 @safe unittest {
126     import std.algorithm: map;
127 
128     with(NewTranslationUnit("foo.cpp",
129                             q{ struct Struct { int int_; double double_; }; }))
130     {
131         import clang: Cursor;
132 
133         const cursor = translUnit.cursor;
134         with(Cursor.Kind) {
135             cursor.children.map!(a => a.kind).shouldEqual([StructDecl]);
136             cursor.children[0].children.map!(a => a.kind).shouldEqual(
137                 [FieldDecl, FieldDecl]
138             );
139         }
140     }
141 }
142 
143 
144 @("Function return type should have valid cx")
145 @safe unittest {
146     import clang.c.index: CXType_Pointer;
147     with(NewTranslationUnit("foo.cpp",
148                             q{
149                                 const char* newString();
150                             }))
151     {
152         import clang: Cursor;
153 
154         const cursor = translUnit.cursor;
155         cursor.children.length.shouldEqual(1);
156         const function_ = cursor.children[0];
157         function_.spelling.should == "newString";
158         function_.kind.shouldEqual(Cursor.Kind.FunctionDecl);
159         function_.returnType.kind.shouldEqual(Type.Kind.Pointer);
160         function_.returnType.cx.kind.shouldEqual(CXType_Pointer);
161     }
162 }