1 // PERMUTE_ARGS: -unittest
2 // REQUIRED_ARGS: -D -w -o- -c -Dd${RESULTS_DIR}/compilable -o-
3 // POST_SCRIPT: compilable/extra-files/ddocAny-postscript.sh unittest
4 
5 module ddocunittest;
6 
7 /* Insert test-cases for documented unittests feature here. */
8 
9 /// foo function - 1 example
10 int foo(int a, int b) { return a + b; }
11 
12 ///
13 unittest
14 {
15     assert(foo(1, 1) == 2);
16 }
17 
18 /// bar function - 1 example
19 bool bar() { return true; }
20 
21 ///
22 unittest
23 {
24     // documented
25     assert(bar());
26 }
27 
28 /// placeholder
29 unittest
30 {
31 }
32 
33 /// doo function - no examples
34 void doo() { }
35 
36 ///
37 private unittest
38 {
39     // undocumented
40     doo();
41 }
42 
43 unittest
44 {
45     // undocumented
46     doo();
47 }
48 
49 /**
50 add function - 3 examples
51 
52 Examples:
53 
54 ----
55 assert(add(1, 1) == 2);
56 ----
57 */
58 int add(int a, int b) { return a + b; }
59 
60 ///
61 unittest
62 {
63     // documented
64     assert(add(3, 3) == 6);
65     assert(add(4, 4) == 8);
66 }
67 
68 unittest
69 {
70     // undocumented
71     assert(add(2, 2) + add(2, 2) == 8);
72 }
73 
74 ///
75 unittest
76 {
77     // documented
78     assert(add(5, 5) == 10);
79     assert(add(6, 6) == 12);
80 }
81 
82 /// class Foo
83 immutable pure nothrow class Foo
84 {
85     int x;
86 
87     ///
88     unittest
89     {
90         // another foo example
91         Foo foo = new Foo;
92     }
93 }
94 
95 ///
96 unittest
97 {
98     Foo foo = new Foo;
99 }
100 
101 pure
102 {
103     const
104     {
105         immutable
106         {
107             /// some class - 1 example
108             class SomeClass {}
109         }
110     }
111 }
112 
113 ///
114 unittest
115 {
116     SomeClass sc = new SomeClass;
117 }
118 
119 /// Outer - 1 example
120 class Outer
121 {
122     /// Inner
123     static class Inner
124     {
125     }
126 
127     ///
128     unittest
129     {
130         Inner inner = new Inner;
131     }
132 }
133 
134 ///
135 unittest
136 {
137     Outer outer = new Outer;
138 }
139 
140 /** foobar - no examples */
141 void foobar()
142 {
143 }
144 
145 unittest
146 {
147     foobar();
148 }
149 
150 /**
151 func - 4 examples
152 Examples:
153 ---
154 foo(1);
155 ---
156 
157 Examples:
158 ---
159 foo(2);
160 ---
161 */
162 void foo(int x) {  }
163 
164 ///
165 unittest
166 {
167     foo(2);
168 }
169 
170 ///
171 unittest
172 {
173     foo(4);
174 }
175 
176 // ------------------------------------
177 // insert import declaration between documented function and unittests
178 
179 ///
180 void fooImport() {}
181 import core.stdc.stdio;
182 /// test
183 unittest { fooImport(); }
184 
185 ///
186 void fooStaticImport() {}
187 static import core.stdc.stdlib;
188 /// test
189 unittest { fooStaticImport(); }
190 
191 ///
192 void fooPublicImport() {}
193 public import core.stdc..string;
194 /// test
195 unittest { fooPublicImport(); }
196 
197 ///
198 void fooSelectiveImport() {}
199 import core.stdc.ctype : isalpha;
200 /// test
201 unittest { fooSelectiveImport(); }
202 
203 ///
204 void fooRenamedImport() {}
205 import io = core.stdc.stdio;
206 /// test
207 unittest { fooRenamedImport(); }
208 
209 // ------------------------------------
210 // documented unittest after conditional declarations
211 
212 static if (true)
213   void fooConditionalDecl1a() {} /** */
214 unittest { int x1a; }   ///
215 
216 static if (true)
217 { void fooConditionalDecl1b() {} /** */ }
218 unittest { int x1b; }   ///
219 
220 static if (false)
221   void fooConditionalDecl2a() {} /** */
222 unittest { int x2a; }   ///
223 
224 static if (false)
225 { void fooConditionalDecl2b() {} /** */ }
226 unittest { int x2b; }   ///
227 
228 static if (true)
229 { void fooConditionalDecl3a() {} /** */ }
230 else
231 { void barConditionalDecl3a() {} /** */ }
232 unittest { int x3a; }   ///
233 
234 static if (true)
235 { void fooConditionalDecl3b() {} /** */ }
236 else
237 { void barConditionalDecl3b() {} /** */ }
238 unittest { int x3b; }   ///
239 
240 static if (false)
241   void fooConditionalDecl4a() {} /** */
242 else
243   void barConditionalDecl4a() {} /** */
244 unittest { int x4a; }   ///
245 
246 static if (false)
247 { void fooConditionalDecl4b() {} /** */ }
248 else
249 { void barConditionalDecl4b() {} /** */ }
250 unittest { int x4b; }   ///
251 
252 static if (true)
253 {}
254 else
255   void barConditionalDecl5a() {} /** */
256 unittest { int x5a; }   ///
257 
258 static if (true)
259 {}
260 else
261 { void barConditionalDecl5b() {} /** */ }
262 unittest { int x5b; }   ///
263 
264 static if (false)
265 {}
266 else
267   void barConditionalDecl6a() {} /** */
268 ///
269 unittest { int x6a; }
270 
271 static if (false)
272 {}
273 else
274 { void barConditionalDecl6b() {} /** */ }
275 ///
276 unittest { int x6b; }
277 
278 // ------------------------------------
279 // 9474
280 
281 ///
282 void foo9474() { }
283 
284 version(none)
285 unittest { }
286 
287 /// Example
288 unittest { foo9474(); }
289 
290 /// doc
291 void bar9474() { }
292 
293 version(none)
294 unittest { }
295 
296 /// Example
297 unittest { bar9474(); }
298 
299 ///
300 struct S9474
301 {
302 }
303 ///
304 unittest { S9474 s; }
305 
306 ///
307 auto autovar9474 = 1;
308 ///
309 unittest { int v = autovar9474; }
310 
311 ///
312 auto autofun9474() { return 1; }
313 ///
314     unittest { int n = autofun9474(); }
315 
316 ///
317 template Template9474()
318 {
319     /// Shouldn't link following unittest to here
320     void foo() {}
321 }
322 ///
323 unittest { alias Template9474!() T; }
324 
325 // ------------------------------------
326 // 9713
327 
328 ///
329 void fooNoDescription() {}
330 ///
331 unittest { fooNoDescription(); }
332 ///
333 unittest { if (true) {fooNoDescription(); } /* comment */ }
334 
335 // ------------------------------------
336 
337 /// test for bugzilla 9757
338 void foo9757() {}
339 /// ditto
340 void bar9757() {}
341 /// ditto
342 void baz9757() {}
343 ///
344 unittest { foo9757(); bar9757(); }
345 ///
346 unittest { bar9757(); foo9757(); }
347 
348 /// with template functions
349 auto redBlackTree(E)(E[] elems...)
350 {
351     return 1;
352 }
353 /// ditto
354 auto redBlackTree(bool allowDuplicates, E)(E[] elems...)
355 {
356     return 2;
357 }
358 /// ditto
359 auto redBlackTree(alias less, E)(E[] elems...)
360 {
361     return 3;
362 }
363 ///
364 unittest
365 {
366     auto rbt1 = redBlackTree(0, 1, 5, 7);
367     auto rbt2 = redBlackTree!string("hello", "world");
368     auto rbt3 = redBlackTree!true(0, 1, 5, 7, 5);
369     auto rbt4 = redBlackTree!"a > b"(0, 1, 5, 7);
370 }
371 
372 // ------------------------------------
373 // Issue 9758
374 
375 /// test
376 void foo(){}
377 
378 ///
379 unittest {  }
380 
381 // ------------------------------------
382 // Issue 10519
383 
384 ///
385 bool balancedParens10519(string, char, char) { return true; }
386 ///
387 unittest
388 {
389     auto s = "1 + (2 * (3 + 1 / 2)";
390     assert(!balancedParens10519(s, '(', ')'));
391 }
392 
393 // ------------------------------------
394 // Issue 12097
395 
396 /// declaration
397 struct S12097
398 {
399     /// method
400     void foo() {}
401 }
402 
403 /// ditto
404 void f12097() {}
405 
406 /// ddoc code 1
407 unittest
408 {
409     int a = 1;
410 }
411 
412 /// ditto
413 struct T12097(T) {}
414 
415 /// ddoc code 2
416 unittest
417 {
418     int[] arr;
419 }
420 
421 // ------------------------------------
422 // 14594
423 
424 /*******************
425  * testA
426  */
427 void fun14594a()() {}
428 ///
429 unittest { fun14594a(); }
430 
431 /*******************
432  * testB
433  */
434 void fun14594b()() {}
435 /// ditto
436 void fun14594b(T)(T) {}
437 ///
438 unittest { fun14594b(); fun14594b(1); }
439 
440 /*******************
441  * testC
442  */
443 void fun14594c()() {}
444 ///
445 unittest { fun14594c(); fun14594c(1); }
446 /// ditto
447 void fun14594c(T)(T) {}
448 
449 /*******************
450  * testD
451  */
452 void fun14594d()() {}
453 ///
454 unittest { fun14594d(); }
455 /// ditto
456 void fun14594d(T)(T) {}
457 ///
458 unittest { fun14594d(1); }
459 
460 /*******************
461  * testE
462  */
463 template fun14594e()
464 {
465     /// concatenated doc-comment fun14594e
466     void fun14594e() {}
467     /// ignored-unittest fun14594e
468     unittest { fun14594e(); }
469 }
470 /// doc-unittest fun14594e
471 unittest { fun14594e(); }
472 
473 /*******************
474  * testF
475  */
476 template fun14594f()
477 {
478     /// concatenated doc-comment fun14594f
479     void fun14594f() {}
480     /// ignored-unittest fun14594f
481     unittest { fun14594f(); }
482 }
483 /// ditto
484 template fun14594f(T)
485 {
486     /// ignored doc-comment fun14594f
487     void fun14594f(T) {}
488     /// ignored-unittest fun14594f
489     unittest { fun14594f(1); }
490 }
491 /// doc-unittest fun14594f
492 unittest { fun14594f(); }
493 
494 // ------------------------------------
495 
496 void main() { }