1 module mocked.tests.stub;
2 
3 import dshould;
4 import mocked;
5 import unit_threaded.attrs : ShouldFail;
6 
7 @("class can be stubbed")
8 unittest
9 {
10     static class Dependency
11     {
12         bool isEven(int number)
13         {
14             return (number & 1) == 0;
15         }
16     }
17     Mocker mocker;
18     auto stubbed = mocker.stub!Dependency;
19 
20     stubbed.stub.isEven(6).returns(false);
21     stubbed.stub.isEven(5).returns(true);
22 
23     Dependency dependency = stubbed.get;
24 
25     dependency.isEven(5).should.equal(true);
26     dependency.isEven(6).should.equal(false);
27 }
28 
29 @("stubs classes with an constructor")
30 unittest
31 {
32     static class Dependency
33     {
34         string phrase;
35 
36         public this(string phrase)
37         {
38             this.phrase = phrase;
39         }
40 
41         public string saySomething()
42         {
43             return this.phrase;
44         }
45     }
46     auto dependency = Mocker().stub!Dependency("Alea iacta est.");
47 
48     dependency.stub.saySomething().passThrough;
49 
50     dependency.saySomething().should.equal("Alea iacta est.");
51 }
52 
53 @("can use custom comparator")
54 unittest
55 {
56     import std.math : fabs;
57 
58     static class Dependency
59     {
60         public bool call(float)
61         {
62             return false;
63         }
64     }
65 
66     alias approxComparator = (float a, float b) {
67         return fabs(a - b) <= 0.1;
68     };
69     auto mocker = configure!(Comparator!approxComparator);
70     auto builder = mocker.stub!Dependency;
71 
72     builder.stub.call(1.01).returns(true);
73 
74     auto stub = builder.get;
75 
76     stub.call(1.02).should.be(true);
77 }
78 
79 @("stubs const methods")
80 unittest
81 {
82     interface I
83     {
84         public bool isI(string) const;
85     }
86     Mocker mocker;
87 
88     static assert(is(typeof(mocker.stub!I())));
89 }
90 
91 @("overrides returned value")
92 unittest
93 {
94     enum string leibniz = "Unsere Welt ist die beste aller möglichen Welten";
95     enum string schopenhauer =
96         "Unsere Welt ist die schlechteste aller möglichen Welten";
97     static class X
98     {
99         string phrase()
100         {
101             return null;
102         }
103     }
104     Mocker mocker;
105     auto builder = mocker.stub!X;
106 
107     builder.stub.phrase.returns(leibniz);
108     builder.stub.phrase.returns(schopenhauer);
109 
110     builder.get.phrase.should.equal(schopenhauer);
111 }
112 
113 @("overrides default stub")
114 unittest
115 {
116     static class Dependency
117     {
118         string translate(string)
119         {
120             assert(false);
121         }
122     }
123     Mocker mocker;
124     auto builder = mocker.stub!Dependency;
125 
126     builder.stub.translate!string.returns("im Sinne");
127     builder.stub.translate!string.returns("in mente");
128 
129     builder.get.translate("latin").should.equal("in mente");
130 }
131 
132 @("checks whether arguments were set when overriding")
133 unittest
134 {
135     static class Dependency
136     {
137         string translate(string language)
138         {
139             return language == "latin" ? "in mente" : null;
140         }
141     }
142     Mocker mocker;
143     auto builder = mocker.stub!Dependency;
144 
145     builder.stub.translate!string.returns("im Sinne");
146     builder.stub.translate("latin").returns("in mente");
147 
148     builder.get.translate("latin").should.equal("in mente");
149 }
150 
151 @ShouldFail("throws if nothing matches")
152 unittest
153 {
154     static class Dependency
155     {
156         string translate()
157         {
158             return "quinta essentia";
159         }
160     }
161     Mocker mocker;
162     auto builder = mocker.stub!Dependency;
163 
164     builder.get.translate.should.be(null);
165 }