1 module mocked.tests.readme;
2 
3 import mocked;
4 
5 unittest
6 {
7     static class Dependency
8     {
9         string authorOf(string phrase)
10         {
11             return null;
12         }
13     }
14 
15     enum string phrase = "[T]he meaning of a word is its use in the language.";
16     enum string expected = "L. Wittgenstein";
17 
18     Mocker mocker;
19     auto builder = mocker.mock!Dependency;
20 
21     builder.expect.authorOf(phrase).returns(expected);
22 
23     auto dependency = builder.get;
24 
25     assert(dependency.authorOf(phrase) == expected);
26 }
27 
28 unittest
29 {
30     import std.math : fabs;
31 
32     static class Dependency
33     {
34         public void call(float)
35         {
36         }
37     }
38 
39     // This function is used to compare two floating point numbers that don't
40     // match exactly.
41     alias approxComparator = (float a, float b) {
42         return fabs(a - b) <= 0.1;
43     };
44     auto mocker = configure!(Comparator!approxComparator);
45     auto builder = mocker.mock!Dependency;
46 
47     builder.expect.call(1.01);
48 
49     auto mock = builder.get;
50 
51     mock.call(1.02);
52 }
53 
54 unittest
55 {
56     static class Dependency
57     {
58         bool isTrue()
59         {
60             return true;
61         }
62     }
63     Mocker mocker;
64     auto mock = mocker.mock!Dependency;
65     mock.expect.isTrue.passThrough;
66 
67     assert(mock.get.isTrue);
68 }
69 
70 unittest
71 {
72     Mocker mocker;
73     auto mock = mocker.mock!Object;
74     mock.expect.toString.returns("in abstracto");
75 
76     assert(mock.get.toString == "in abstracto");
77 }
78 
79 unittest
80 {
81     import std.exception : assertThrown;
82 
83     Mocker mocker;
84     auto mock = mocker.mock!Object;
85     mock.expect.toString.throws!Exception("");
86 
87     assertThrown!Exception(mock.get.toString);
88 }
89 
90 unittest
91 {
92     static bool flag = false;
93 
94     static class Dependency
95     {
96         void setFlag(bool flag)
97         {
98         }
99     }
100     Mocker mocker;
101     auto mock = mocker.mock!Dependency;
102     mock.expect.setFlag.action((value) { flag = value; });
103 
104     mock.get.setFlag(true);
105 
106     assert(flag);
107 }
108 
109 unittest
110 {
111     enum string expected = "Three times you must say it, then.";
112     Mocker mocker;
113 
114     auto builder = mocker.mock!Object;
115     builder.expect.toString.returns(expected).repeat(3);
116     // Or: builder.expect.toString.returns(expected).repeatAny;
117 
118     auto mock = builder.get;
119 
120     assert(mock.toString() == expected);
121     assert(mock.toString() == expected);
122     assert(mock.toString() == expected);
123 }
124 
125 unittest
126 {
127     static class Dependency
128     {
129         void say(string)
130         {
131         }
132     }
133     Mocker mocker;
134     auto mock = mocker.mock!Dependency;
135     mock.expect.say("Naturam expelles furca, tamen usque recurret. (Horace)");
136     // or mock.expect.say(); to ignore the arguments
137 
138     mock.get.say("Naturam expelles furca, tamen usque recurret. (Horace)");
139 }
140 
141 unittest
142 {
143     enum string vergil = "tu ne cede malis, sed contra audentior ito.";
144     enum string plotinus = "neque est alter hijus universi locus, quam anima";
145     static class Dependency
146     {
147         string authorOf(string)
148         {
149             return null;
150         }
151     }
152     Mocker mocker;
153     auto stub = mocker.stub!Dependency;
154     stub.stub.authorOf(vergil).returns("Vergil");
155     stub.stub.authorOf(plotinus).returns("Plotinus");
156 
157     assert(stub.get.authorOf(vergil) == "Vergil");
158     assert(stub.get.authorOf(plotinus) == "Plotinus");
159 }
160 
161 unittest
162 {
163     static class Dependency
164     {
165         void callFirst()
166         {
167         }
168 
169         void callSecond()
170         {
171         }
172     }
173     Mocker mocker;
174     auto mock = mocker.mock!Dependency.unordered;
175 
176     mock.expect.callFirst;
177     mock.expect.callSecond;
178 
179     mock.get.callSecond;
180     mock.get.callFirst;
181 }