1 #!/usr/bin/env dub
2 /+ dub.sdl:
3 name "example"
4 dependency "d-unit" version=">=0.8.0"
5 +/
6 
7 //          Copyright Juan Manuel Cabo 2012.
8 //          Copyright Mario Kröplin 2017.
9 // Distributed under the Boost Software License, Version 1.0.
10 //    (See accompanying file LICENSE_1_0.txt or copy at
11 //          http://www.boost.org/LICENSE_1_0.txt)
12 
13 module example;
14 
15 import dunit;
16 import core.thread;
17 import core.time;
18 import std.range;
19 import std.stdio;
20 
21 /**
22  * This example demonstrates the reporting of test failures.
23  */
24 class Test
25 {
26     mixin UnitTest;
27 
28     @Test
29     public void assertEqualsFailure() @safe pure
30     {
31         string expected = "bar";
32         string actual = "baz";
33 
34         assertEquals(expected, actual);
35     }
36 
37     @Test
38     public void assertAssocArrayEqualsFailure() pure
39     {
40         string[int] expected = [1: "foo", 2: "bar"];
41         string[int] actual = [1: "foo", 2: "baz"];
42 
43         assertArrayEquals(expected, actual);
44     }
45 
46     @Test
47     public void assertRangeEqualsFailure() @safe pure
48     {
49         int[] expected = [0, 1, 1, 2];
50         auto actual = iota(0, 3);
51 
52         assertRangeEquals(expected, actual);
53     }
54 
55     @Test
56     public void assertAllFailure() @safe
57     {
58         assertAll(
59             assertLessThan(6 * 7, 42),
60             assertGreaterThan(6 * 7, 42),
61         );
62     }
63 }
64 
65 /**
66  * This example demonstrates the order in which the fixture functions run.
67  * The functions 'setUp' and 'tearDown' run before and after each test.
68  * The functions 'setUpAll' and 'tearDownAll' run once before and after
69  * all tests in the class.
70  */
71 class TestFixture
72 {
73     mixin UnitTest;
74 
75     public this()
76     {
77         debug writeln("@this()");
78     }
79 
80     @BeforeAll
81     public static void setUpAll()
82     {
83         debug writeln("@BeforeAll");
84     }
85 
86     @AfterAll
87     public static void tearDownAll()
88     {
89         debug writeln("@AfterAll");
90     }
91 
92     @BeforeEach
93     public void setUp()
94     {
95         debug writeln("@BeforeEach");
96     }
97 
98     @AfterEach
99     public void tearDown()
100     {
101         debug writeln("@AfterEach");
102     }
103 
104     @Test
105     public void test1() @safe pure
106     {
107         debug writeln("@test1()");
108     }
109 
110     @Test
111     public void test2() @safe pure
112     {
113         debug writeln("@test2()");
114     }
115 }
116 
117 /**
118  * This example demonstrates how to reuse tests and a test fixture.
119  */
120 class TestReuse : TestFixture
121 {
122     mixin UnitTest;
123 
124     @BeforeEach
125     public override void setUp()
126     {
127         debug writeln("@BeforeEach override");
128     }
129 }
130 
131 /**
132  * This example demonstrates various things to know about the test framework.
133  */
134 class TestingThisAndThat
135 {
136     mixin UnitTest;
137 
138     // test function can have default arguments
139     @Test
140     public void testResult(bool actual = true) @safe pure
141     {
142         assertTrue(actual);
143     }
144 
145     // test function can even be private
146     // tagged test functions can be selected to be included or excluded
147     @Test
148     @Tag("fast")
149     @Tag("smoke")
150     private void success() @safe pure
151     {
152         testResult(true);
153     }
154 
155     // disabled test function
156     @Test
157     @Disabled("not ready yet")
158     public void failure() @safe pure
159     {
160         testResult(false);
161     }
162 
163     // failed contracts are errors, not failures
164     @Test
165     public void error() @safe pure
166     {
167         assert(false);
168     }
169 
170     // expected exception can be further verified
171     @Test
172     public void testException() @safe pure
173     {
174         import std.exception : enforce;
175 
176         auto exception = expectThrows(enforce(false));
177 
178         assertEquals("Enforcement failed", exception.msg);
179     }
180 }
181 
182 /**
183  * This example demonstrates how to test asynchronous code.
184  */
185 class TestingAsynchronousCode
186 {
187     mixin UnitTest;
188 
189     private Thread thread;
190 
191     private bool done;
192 
193     @BeforeEach
194     public void setUp()
195     {
196         done = false;
197         thread = new Thread(&threadFunction);
198     }
199 
200     @AfterEach
201     public void tearDown()
202     {
203         thread.join();
204     }
205 
206     private void threadFunction()
207     {
208         Thread.sleep(100.msecs);
209         done = true;
210     }
211 
212     @Test
213     @Tag("slow")
214     public void test()
215     {
216         assertFalse(done);
217 
218         thread.start();
219 
220         assertEventually({ return done; });
221     }
222 }
223 
224 // either use the 'Main' mixin or call 'dunit_main(args)'
225 mixin Main;