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 @("foreach(cursor) C++ file with one simple struct")
89 @safe unittest {
90     with(NewTranslationUnit("foo.cpp",
91                             q{ struct Struct { int int_; double double_; }; }))
92     {
93         foreach(cursor; translUnit.cursor) {
94 
95             import clang: Cursor;
96 
97             static int cursorIndex;
98 
99             switch(cursorIndex) {
100 
101             default:
102                 assert(false);
103 
104             case 0:
105                 cursor.kind.shouldEqual(Cursor.Kind.StructDecl);
106                 break;
107 
108             case 1:
109                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
110                 break;
111 
112             case 2:
113                 cursor.kind.shouldEqual(Cursor.Kind.FieldDecl);
114                 break;
115             }
116 
117             ++cursorIndex;
118         }
119     }
120 }
121 
122 @("cursor.children C++ file with one simple struct")
123 @safe unittest {
124     import std.algorithm: map;
125 
126     with(NewTranslationUnit("foo.cpp",
127                             q{ struct Struct { int int_; double double_; }; }))
128     {
129         import clang: Cursor;
130 
131         const cursor = translUnit.cursor;
132         with(Cursor.Kind) {
133             cursor.children.map!(a => a.kind).shouldEqual([StructDecl]);
134             cursor.children[0].children.map!(a => a.kind).shouldEqual(
135                 [FieldDecl, FieldDecl]
136             );
137         }
138     }
139 }
140 
141 @("Function return type should have valid cx")
142 @safe unittest {
143     import clang.c.index: CXType_Pointer;
144     with(NewTranslationUnit("foo.cpp",
145                             q{
146                                 const char* newString();
147                             }))
148     {
149         import clang: Cursor;
150 
151         const cursor = translUnit.cursor;
152         cursor.children.length.shouldEqual(1);
153         const function_ = cursor.children[0];
154         function_.kind.shouldEqual(Cursor.Kind.FunctionDecl);
155         function_.returnType.kind.shouldEqual(Type.Kind.Pointer);
156         function_.returnType.cx.kind.shouldEqual(CXType_Pointer);
157     }
158 
159 }