1 module ut.translation.node.structs;
2 
3 
4 import ut.translation.node;
5 import std.array: join;
6 
7 
8 @("onefield.int")
9 @safe pure unittest {
10     enum node = Node(
11         Struct(
12             "Foo",
13             [
14                 Node(Field(Type(Int()), "i")),
15             ]
16         )
17     );
18 
19     enum translation = translate(node);
20     writelnUt(translation);
21     mixin(`static ` ~ translation.join("\n"));
22 
23     static assert(Foo.tupleof.length == 1);
24     static assert(is(typeof(Foo.i) == int));
25 }
26 
27 
28 @("onefield.double")
29 @safe pure unittest {
30     enum node = Node(
31         Struct(
32             "Bar",
33             [
34                 Node(Field(Type(Double()), "d")),
35             ]
36         )
37     );
38 
39     enum translation = translate(node);
40     writelnUt(translation);
41     mixin(`static ` ~ translation.join("\n"));
42 
43     static assert(Bar.tupleof.length == 1);
44     static assert(is(typeof(Bar.d) == double));
45 }
46 
47 
48 @("twofields")
49 @safe pure unittest {
50     enum node = Node(
51         Struct(
52             "Baz",
53             [
54                 Node(Field(Type(Int()), "i",)),
55                 Node(Field(Type(Double()), "d",)),
56             ]
57         )
58     );
59 
60     enum translation = translate(node);
61     writelnUt(translation);
62     mixin(`static ` ~ translation.join("\n"));
63 
64     static assert(Baz.tupleof.length == 2);
65     static assert(is(typeof(Baz.i) == int));
66     static assert(is(typeof(Baz.d) == double));
67 }
68 
69 
70 @("typedef.name")
71 @safe pure unittest {
72     enum nodes = [
73         Node(Struct("TypeDefd_", [])),
74         Node(Typedef("TypeDefd", Type(UserDefinedType("TypeDefd_")))),
75     ];
76     enum translation = translate(nodes);
77     writelnUt(translation);
78     mixin(translation.join("\n"));
79 
80     static assert(is(TypeDefd));
81     static assert(is(TypeDefd_));
82 }
83 
84 
85 @("typedef.anon")
86 @safe pure unittest {
87     enum nodes = [
88         Node(
89             Struct(
90                 "",
91                 [
92                     Node(Field(Type(Int()), "x")),
93                     Node(Field(Type(Int()), "y")),
94                     Node(Field(Type(Int()), "z")),
95                 ],
96                 "Nameless1", // type spelling
97             ),
98         ),
99         Node(Typedef("Nameless1", Type(UserDefinedType("Nameless1")))),
100         Node(
101             Struct(
102                 "",
103                 [
104                     Node(Field(Type(Double()), "d")),
105                 ],
106                 "Nameless2",  // type spelling
107             ),
108         ),
109         Node(Typedef("Nameless2", Type(UserDefinedType("Nameless2")))),
110     ];
111 
112     enum translation = "\n" ~ translate(nodes).join("\n");
113     writelnUt(translation);
114     mixin(translation);
115 
116     static assert(is(Nameless1), translation);
117     static assert(is(typeof(Nameless1.x) == int), translation);
118     static assert(is(typeof(Nameless1.y) == int), translation);
119     static assert(is(typeof(Nameless1.z) == int), translation);
120 
121     static assert(is(Nameless2), translation);
122     static assert(is(typeof(Nameless2.d) == double), translation);
123 }
124 
125 @("typedef.before")
126 @safe pure unittest {
127 
128     // unfortunately, order matters in unit test blocks
129     enum nodes = [
130         Node(Struct("A", [Node(Field(Type(Int()), "a"))], "struct A")),
131         Node(Typedef("B", Type(UserDefinedType("A")))),
132     ];
133 
134     enum translation = "\n" ~ translate(nodes).join("\n");
135     writelnUt(translation);
136     mixin(translation);
137 
138     static assert(is(A == B));
139     static assert(is(A == struct));
140     static assert(A.tupleof.length == 1);
141     static assert(is(typeof(A.a) == int));
142 }