1 // PERMUTE_ARGS: -unittest
2 // REQUIRED_ARGS: -D -w -o- -Dd${RESULTS_DIR}/compilable -o-
3 // POST_SCRIPT: compilable/extra-files/ddocAny-postscript.sh
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 fooSelectiveImport() {}
193 import core.stdc.ctype : isalpha;
194 /// test
195 unittest { fooSelectiveImport(); }
196 
197 ///
198 void fooRenamedImport() {}
199 import io = core.stdc.stdio;
200 /// test
201 unittest { fooRenamedImport(); }
202 
203 /// This is a public import
204 public import core.stdc..string;
205 
206 /// This is a mutiple public import
207 public import core.stdc.stdarg, core.stdc.stdlib;
208 
209 /// This is a public selective import
210 public import core.stdc..string : memcpy;
211 
212 /// This is a public selective renamed import
213 public import core.stdc..string : copy = memcpy;
214 
215 /// This is a public multiple selective import
216 public import core.stdc..string : memcpy, memcmp;
217 
218 /// This is a public multiple selective renamed import
219 public import core.stdc..string : copy = memcpy, compare = memcmp;
220 
221 /// This is a public renamed import
222 public import str = core.stdc..string;
223 
224 // This is a public import without a DDoc comment.
225 // It should not be emitted to the documentation file.
226 public import core.stdc.stdlib;
227 
228 
229 // ------------------------------------
230 // documented unittest after conditional declarations
231 
232 static if (true)
233   void fooConditionalDecl1a() {} /** */
234 unittest { int x1a; }   ///
235 
236 static if (true)
237 { void fooConditionalDecl1b() {} /** */ }
238 unittest { int x1b; }   ///
239 
240 static if (false)
241   void fooConditionalDecl2a() {} /** */
242 unittest { int x2a; }   ///
243 
244 static if (false)
245 { void fooConditionalDecl2b() {} /** */ }
246 unittest { int x2b; }   ///
247 
248 static if (true)
249 { void fooConditionalDecl3a() {} /** */ }
250 else
251 { void barConditionalDecl3a() {} /** */ }
252 unittest { int x3a; }   ///
253 
254 static if (true)
255 { void fooConditionalDecl3b() {} /** */ }
256 else
257 { void barConditionalDecl3b() {} /** */ }
258 unittest { int x3b; }   ///
259 
260 static if (false)
261   void fooConditionalDecl4a() {} /** */
262 else
263   void barConditionalDecl4a() {} /** */
264 unittest { int x4a; }   ///
265 
266 static if (false)
267 { void fooConditionalDecl4b() {} /** */ }
268 else
269 { void barConditionalDecl4b() {} /** */ }
270 unittest { int x4b; }   ///
271 
272 static if (true)
273 {}
274 else
275   void barConditionalDecl5a() {} /** */
276 unittest { int x5a; }   ///
277 
278 static if (true)
279 {}
280 else
281 { void barConditionalDecl5b() {} /** */ }
282 unittest { int x5b; }   ///
283 
284 static if (false)
285 {}
286 else
287   void barConditionalDecl6a() {} /** */
288 ///
289 unittest { int x6a; }
290 
291 static if (false)
292 {}
293 else
294 { void barConditionalDecl6b() {} /** */ }
295 ///
296 unittest { int x6b; }
297 
298 // ------------------------------------
299 // https://issues.dlang.org/show_bug.cgi?id=9474
300 
301 ///
302 void foo9474() { }
303 
304 version(none)
305 unittest { }
306 
307 /// Example
308 unittest { foo9474(); }
309 
310 /// doc
311 void bar9474() { }
312 
313 version(none)
314 unittest { }
315 
316 /// Example
317 unittest { bar9474(); }
318 
319 ///
320 struct S9474
321 {
322 }
323 ///
324 unittest { S9474 s; }
325 
326 ///
327 auto autovar9474 = 1;
328 ///
329 unittest { int v = autovar9474; }
330 
331 ///
332 auto autofun9474() { return 1; }
333 ///
334     unittest { int n = autofun9474(); }
335 
336 ///
337 template Template9474()
338 {
339     /// Shouldn't link following unittest to here
340     void foo() {}
341 }
342 ///
343 unittest { alias Template9474!() T; }
344 
345 // ------------------------------------
346 // https://issues.dlang.org/show_bug.cgi?id=9713
347 
348 ///
349 void fooNoDescription() {}
350 ///
351 unittest { fooNoDescription(); }
352 ///
353 unittest { if (true) {fooNoDescription(); } /* comment */ }
354 
355 // ------------------------------------
356 
357 /// test for https://issues.dlang.org/show_bug.cgi?id=9757
358 void foo9757() {}
359 /// ditto
360 void bar9757() {}
361 /// ditto
362 void baz9757() {}
363 ///
364 unittest { foo9757(); bar9757(); }
365 ///
366 unittest { bar9757(); foo9757(); }
367 
368 /// with template functions
369 auto redBlackTree(E)(E[] elems...)
370 {
371     return 1;
372 }
373 /// ditto
374 auto redBlackTree(bool allowDuplicates, E)(E[] elems...)
375 {
376     return 2;
377 }
378 /// ditto
379 auto redBlackTree(alias less, E)(E[] elems...)
380 if (__traits(compiles, (E a, E b) => mixin(less)))
381 {
382     return 3;
383 }
384 ///
385 unittest
386 {
387     auto rbt1 = redBlackTree(0, 1, 5, 7);
388     auto rbt2 = redBlackTree!string("hello", "world");
389     auto rbt3 = redBlackTree!true(0, 1, 5, 7, 5);
390     auto rbt4 = redBlackTree!"a > b"(0, 1, 5, 7);
391 }
392 
393 // ------------------------------------
394 // https://issues.dlang.org/show_bug.cgi?id=9758
395 
396 /// test
397 void foo(){}
398 
399 ///
400 unittest {  }
401 
402 // ------------------------------------
403 // https://issues.dlang.org/show_bug.cgi?id=10519
404 
405 ///
406 bool balancedParens10519(string, char, char) { return true; }
407 ///
408 unittest
409 {
410     auto s = "1 + (2 * (3 + 1 / 2)";
411     assert(!balancedParens10519(s, '(', ')'));
412 }
413 
414 // ------------------------------------
415 // https://issues.dlang.org/show_bug.cgi?id=12097
416 
417 /// declaration
418 struct S12097
419 {
420     /// method
421     void foo() {}
422 }
423 
424 /// ditto
425 void f12097() {}
426 
427 /// ddoc code 1
428 unittest
429 {
430     int a = 1;
431 }
432 
433 /// ditto
434 struct T12097(T) {}
435 
436 /// ddoc code 2
437 unittest
438 {
439     int[] arr;
440 }
441 
442 // ------------------------------------
443 // https://issues.dlang.org/show_bug.cgi?id=14594
444 
445 /*******************
446  * testA
447  */
448 void fun14594a()() {}
449 ///
450 unittest { fun14594a(); }
451 
452 /*******************
453  * testB
454  */
455 void fun14594b()() {}
456 /// ditto
457 void fun14594b(T)(T) {}
458 ///
459 unittest { fun14594b(); fun14594b(1); }
460 
461 /*******************
462  * testC
463  */
464 void fun14594c()() {}
465 ///
466 unittest { fun14594c(); fun14594c(1); }
467 /// ditto
468 void fun14594c(T)(T) {}
469 
470 /*******************
471  * testD
472  */
473 void fun14594d()() {}
474 ///
475 unittest { fun14594d(); }
476 /// ditto
477 void fun14594d(T)(T) {}
478 ///
479 unittest { fun14594d(1); }
480 
481 /*******************
482  * testE
483  */
484 template fun14594e()
485 {
486     /// concatenated doc-comment fun14594e
487     void fun14594e() {}
488     /// ignored-unittest fun14594e
489     unittest { fun14594e(); }
490 }
491 /// doc-unittest fun14594e
492 unittest { fun14594e(); }
493 
494 /*******************
495  * testF
496  */
497 template fun14594f()
498 {
499     /// concatenated doc-comment fun14594f
500     void fun14594f() {}
501     /// ignored-unittest fun14594f
502     unittest { fun14594f(); }
503 }
504 /// ditto
505 template fun14594f(T)
506 {
507     /// ignored doc-comment fun14594f
508     void fun14594f(T) {}
509     /// ignored-unittest fun14594f
510     unittest { fun14594f(1); }
511 }
512 /// doc-unittest fun14594f
513 unittest { fun14594f(); }
514 
515 // ------------------------------------
516 
517 void main() { }