1 module unit_threaded.ut.issues;
2 
3 import unit_threaded;
4 import unit_threaded.asserts;
5 
6 
7 interface ICalcView {
8    @property string total() @safe pure;
9    @property void total(string t) @safe pure;
10 }
11 
12 class CalcController {
13     private ICalcView view;
14 
15     this(ICalcView view) @safe pure {
16         this.view = view;
17     }
18 
19     void onClick(int number) @safe pure {
20         import std.conv: to;
21         view.total = number.to!string;
22     }
23 }
24 
25 
26 @("54")
27 @safe pure unittest {
28    auto m = mock!ICalcView;
29    m.expect!"total"("42");
30 
31    auto ctrl = new CalcController(m);
32    ctrl.onClick(42);
33 
34    m.verify;
35 }
36 
37 
38 @("82")
39 @safe unittest {
40 
41     import std.exception: assertThrown;
42 
43     static class A {
44         string x;
45 
46         override string toString() const {
47             return x;
48         }
49     }
50 
51     class B : A {}
52 
53     auto actual = new B;
54     auto expected = new B;
55 
56     actual.x = "foo";
57     expected.x = "bar";
58 
59     assertThrown(actual.shouldEqual(expected));
60 }
61 
62 
63 @("124")
64 @safe pure unittest {
65     static struct Array {
66 
67         alias opSlice this;
68 
69         private int[] ints;
70 
71         bool opCast(T)() const if(is(T == bool)) {
72             return ints.length != 0;
73         }
74 
75         inout(int)[] opSlice() inout {
76             return ints;
77         }
78     }
79 
80     {
81         auto arr = Array([1, 2, 3]);
82         arr.shouldEqual([1, 2, 3]);
83     }
84 
85     {
86         const arr = Array([1, 2, 3]);
87         arr.shouldEqual([1, 2, 3]);
88     }
89 
90 }
91 
92 
93 @("138")
94 @safe unittest {
95     import std.exception: assertThrown;
96 
97     static class Class {
98         private string foo;
99         override bool opEquals(scope Object b) @safe @nogc pure nothrow scope const {
100             return foo == (cast(Class)b).foo;
101         }
102     }
103 
104     auto a = new Class;
105     auto b = new Class;
106 
107     a.foo = "1";
108     b.foo = "2";
109 
110     // Object.opEquals isn't scope and therefore not @safe
111     assertThrown(() @trusted { a.should == b; }());
112 }
113 
114 
115 @("146")
116 @safe unittest {
117     version(unitThreadedLight) {}
118     else {
119         assertExceptionMsg(shouldApproxEqual(42000.301, 42000.302, 1e-9),
120                            `    tests/unit_threaded/ut/issues.d:123 - Expected approx: 42000.302000` ~ "\n" ~
121                            `    tests/unit_threaded/ut/issues.d:123 -      Got       : 42000.301000`);
122     }
123 }