1 module cogito.tests.statements;
2 
3 import cogito;
4 import std.sumtype;
5 
6 @("if")
7 unittest
8 {
9     auto meter = runOnCode(q{
10 void f()
11 {
12     if (true)
13     {
14         if (false)
15         {
16             if (true)
17             {
18             }
19         }
20     }
21 }
22     });
23 
24     assert(meter.tryMatch!((Source source) => source.score) == 6);
25 }
26 
27 @("while")
28 unittest
29 {
30     auto meter = runOnCode(q{
31 void f()
32 {
33     if (true)
34     {
35         while (false)
36         {
37             if (true)
38             {
39             }
40         }
41     }
42 }
43     });
44 
45     assert(meter.tryMatch!((Source source) => source.score) == 6);
46 }
47 
48 @("else")
49 unittest
50 {
51     auto meter = runOnCode(q{
52 void f()
53 {
54     if (true)
55     {
56     }
57     else
58     {
59     }
60 }
61     });
62 
63     assert(meter.tryMatch!((Source source) => source.score) == 2);
64 }
65 
66 @("Simple else-if")
67 unittest
68 {
69     auto meter = runOnCode(q{
70 void f()
71 {
72     if (true)
73     {
74     }
75     else if (false)
76     {
77     }
78 }
79     });
80 
81     assert(meter.tryMatch!((Source source) => source.score) == 2);
82 }
83 
84 @("Simple for")
85 unittest
86 {
87     auto meter = runOnCode(q{
88 void f()
89 {
90     for (;;)
91     {
92     }
93 }
94     });
95 
96     assert(meter.tryMatch!((Source source) => source.score) == 1);
97 }
98 
99 @("Simple do while")
100 unittest
101 {
102     auto meter = runOnCode(q{
103 void f()
104 {
105     do
106     {
107     }
108     while (true);
109 }
110     });
111 
112     assert(meter.tryMatch!((Source source) => source.score) == 1);
113 }
114 
115 @("Simple foreach")
116 unittest
117 {
118     auto meter = runOnCode(q{
119 void f()
120 {
121     foreach (const x; xs)
122     {
123     }
124 }
125     });
126 
127     assert(meter.tryMatch!((Source source) => source.score) == 1);
128 }
129 
130 @("Simple reverse foreach")
131 unittest
132 {
133     auto meter = runOnCode(q{
134 void f()
135 {
136     foreach_reverse (const x; xs)
137     {
138     }
139 }
140     });
141 
142     assert(meter.tryMatch!((Source source) => source.score) == 1);
143 }
144 
145 @("try-catch")
146 unittest
147 {
148     auto meter = runOnCode(q{
149 void f()
150 {
151     try
152     {
153     }
154     catch (Exception)
155     {
156     }
157 }
158     });
159 
160     assert(meter.tryMatch!((Source source) => source.score) == 1);
161 }
162 
163 @("Nested statement in try")
164 unittest
165 {
166     auto meter = runOnCode(q{
167 void f()
168 {
169     try
170     {
171         if (true)
172         {
173         }
174     }
175     catch (Exception)
176     {
177     }
178 }
179     });
180 
181     assert(meter.tryMatch!((Source source) => source.score) == 2);
182 }
183 
184 @("Nested statement in catch")
185 unittest
186 {
187     auto meter = runOnCode(q{
188 void f()
189 {
190     try
191     {
192     }
193     catch (Exception)
194     {
195         if (true)
196         {
197         }
198     }
199 }
200     });
201 
202     assert(meter.tryMatch!((Source source) => source.score) == 3);
203 }
204 
205 @("Switch statement")
206 unittest
207 {
208     auto meter = runOnCode(q{
209 void f()
210 {
211     char c;
212 
213     switch (c)
214     {
215         case 'a':
216             break;
217         case 'b':
218             break;
219         default:
220             assert(false);
221     }
222 }
223     });
224 
225     assert(meter.tryMatch!((Source source) => source.score) == 1);
226 }
227 
228 @("Break statement")
229 unittest
230 {
231     auto meter = runOnCode(q{
232 void f() {
233   WhileLabel: while (true) {
234     break WhileLabel;
235   }
236 }
237     });
238 
239     assert(meter.tryMatch!((Source source) => source.score) == 2);
240 }
241 
242 @("Continue with a label")
243 unittest
244 {
245     auto meter = runOnCode(q{
246 void f() {
247   WhileLabel: while (true) {
248     continue WhileLabel;
249   }
250 }
251     });
252 
253     assert(meter.tryMatch!((Source source) => source.score) == 2);
254 }
255 
256 @("Continue statement")
257 unittest
258 {
259     auto meter = runOnCode(q{
260 void f() {
261   WhileLabel: while (true) {
262     continue;
263   }
264 }
265     });
266 
267     assert(meter.tryMatch!((Source source) => source.score) == 1);
268 }
269 
270 @("Multiple nested else-if statements")
271 unittest
272 {
273     auto meter = runOnCode(q{
274 void f()
275 {
276     if (true)
277     {
278         if (true)
279         {
280         }
281         else if (false)
282         {
283             if (true)
284             {
285             }
286         }
287     }
288     else
289     {
290     }
291 }
292     });
293 
294     assert(meter.tryMatch!((Source source) => source.score) == 8);
295 }
296 
297 @("goto")
298 unittest
299 {
300     auto meter = runOnCode(q{
301 void f()
302 {
303     goto end;
304 end:
305 }
306     });
307 
308     assert(meter.tryMatch!((Source source) => source.score) == 1);
309 }