1 /**
2    From clang.Cursor to dpp.node.Node.
3    The tests can't be pure because clang.Cursor.children isn't.
4  */
5 module ut.transform.cursor;
6 
7 
8 import ut.transform;
9 import clang: Cursor, ClangType = Type;
10 import dpp2.transform: toNode;
11 
12 
13 
14 @("struct.onefield.int")
15 @safe unittest {
16     auto intField = Cursor(Cursor.Kind.FieldDecl, "i");
17     intField.type = ClangType(ClangType.Kind.Int, "int");
18     auto struct_ = Cursor(Cursor.Kind.StructDecl, "Foo");
19     struct_.children = [intField];
20 
21     struct_.toNode.should ==
22         Node(
23             Struct(
24                 "Foo",
25                 [
26                     Field(Type(Int()), "i"),
27                 ]
28             )
29         );
30 }
31 
32 
33 @("struct.onefield.double")
34 @safe unittest {
35     auto doubleField = Cursor(Cursor.Kind.FieldDecl, "d");
36     doubleField.type = ClangType(ClangType.Kind.Double, "double");
37     auto struct_ = Cursor(Cursor.Kind.StructDecl, "Bar");
38     struct_.children = [doubleField];
39 
40     struct_.toNode.should ==
41         Node(
42             Struct(
43                 "Bar",
44                 [
45                     Field(Type(Double()), "d"),
46                 ]
47             )
48         );
49 }
50 
51 
52 @("struct.twofields")
53 @safe unittest {
54     auto intField = Cursor(Cursor.Kind.FieldDecl, "i");
55     intField.type = ClangType(ClangType.Kind.Int, "int");
56 
57     auto doubleField = Cursor(Cursor.Kind.FieldDecl, "d");
58     doubleField.type = ClangType(ClangType.Kind.Double, "double");
59 
60     auto struct_ = Cursor(Cursor.Kind.StructDecl, "Baz");
61     struct_.children = [intField, doubleField];
62 
63     struct_.toNode.should ==
64         Node(
65             Struct(
66                 "Baz",
67                 [
68                     Field(Type(Int()), "i"),
69                     Field(Type(Double()), "d"),
70                 ]
71             )
72         );
73 }
74 
75 
76 // FIXME - `Field` should be an option for `Node`
77 @ShouldFail("Equal but not equal")
78 @("struct.nested")
79 @safe unittest {
80     auto xfield = Cursor(Cursor.Kind.FieldDecl, "x");
81     xfield.type = ClangType(ClangType.Kind.Int, "int");
82 
83     auto innerStruct = Cursor(Cursor.Kind.StructDecl, "Inner");
84     innerStruct.type = ClangType(ClangType.Kind.Record, "Outer::Inner");
85     innerStruct.children = [xfield];
86 
87     auto innerField = Cursor(Cursor.Kind.FieldDecl, "inner");
88     innerField.type = ClangType(ClangType.Kind.Elaborated, "struct Inner");
89     innerField.children = [innerStruct];
90 
91     auto outer = Cursor(Cursor.Kind.StructDecl, "Outer");
92     outer.type = ClangType(ClangType.Kind.Record, "Outer");
93     outer.children = [innerStruct, innerField];
94 
95     outer.toNode.should ==
96         Node(
97             Struct(
98                 "Outer",
99                 [
100                     Field(Type(UserDefinedType("Inner")), "inner"),
101                 ],
102                 [
103                     Struct("Inner", [ Field(Type(Int()), "x") ]),
104                 ],
105             )
106         );
107 }