1 module unit_threaded.attrs;
2 
3 enum UnitTest; //opt-in to registration
4 enum DontTest; //opt-out of registration
5 enum Serial; //run tests in the module in one thread / serially
6 
7 alias SingleThreaded = Serial;
8 
9 ///Hide test. Not run by default but can be run.
10 struct HiddenTest {
11     string reason;
12 }
13 
14 /// The suite fails if the test passes.
15 struct ShouldFail {
16     string reason;
17 }
18 
19 /// The suite fails unless the test throws T
20 struct ShouldFailWith(T: Throwable) {
21     alias Type = T;
22     string reason;
23 }
24 
25 /// Associate a name with a unittest block.
26 struct Name {
27     string value;
28 }
29 
30 /// Associates one or more tags with the test
31 struct Tags {
32     this(string[] values...) { this.values = values;}
33     this(string[] values) { this.values =  values; }
34     this(string value)    { this.values = [value]; }
35     string[] values;
36 }
37 
38 /** Automatically assign @Tags for each parameterized test
39  e.g.
40 ---------------
41 @Values("foo", "bar") @AutoTags unittest { ... }
42 // there are now two unit tests, one for "foo" with tag "foo"
43 // and one for "bar" with tag "bar"
44 ---------------
45  */
46 enum AutoTags;
47 
48 /** Attachs these types to the a parametrized unit test.
49     The attached template function will be instantiated with
50     each type listed, e.g.
51 
52     ----------------
53     @Types!(int, byte) void testInit(T)() { T.init.shouldEqual(0); }
54     ----------------
55 
56     These would mean two testInit test runs.
57 
58     Normally this would be a template but I don't know how to write
59  *  the UDA code to filter a template out
60  */
61 struct Types(T...) {}
62 
63 
64 /**
65  Used as a UDA for built-in unittests to enable value-parametrized tests.
66  Example:
67  -------
68  @Values(1, 2, 3) unittest { assert(getValue!int % 2 == 0); }
69  -------
70  The example above results in unit_threaded running the unit tests 3 times,
71  once for each value declared.
72 
73  See `getValue`.
74  */
75 auto Values(T)(T[] values...) {
76     return ValuesImpl!T(values.dup);
77 }
78 
79 auto Values(R)(R values) if(isInputRange!R) {
80     import std.array: array;
81     return ValuesImpl!(ElementType!R)(values.array);
82 }
83 
84 
85 struct ValuesImpl(T) {
86     T[] values;
87 }
88 
89 /**
90  Retrieves the current test value of type T in a built-in unittest.
91  See `Values`.
92  */
93 T getValue(T, int index = 0)() {
94     return ValueHolder!T.values[index];
95 }
96 
97 package struct ValueHolder(T) {
98     static T[10] values;
99 }
100 
101 
102 enum Setup;
103 enum Shutdown;