1 module fluentasserts.core.basetype;
2 
3 public import fluentasserts.core.base;
4 import fluentasserts.core.results;
5 
6 import std..string;
7 import std.conv;
8 import std.algorithm;
9 
10 /// When there is a lazy number that throws an it should throw that exception
11 unittest {
12   int someLazyInt() {
13     throw new Exception("This is it.");
14   }
15 
16   ({
17     someLazyInt.should.equal(3);
18   }).should.throwAnyException.withMessage("This is it.");
19 
20   ({
21     someLazyInt.should.be.greaterThan(3);
22   }).should.throwAnyException.withMessage("This is it.");
23 
24   ({
25     someLazyInt.should.be.lessThan(3);
26   }).should.throwAnyException.withMessage("This is it.");
27 
28   ({
29     someLazyInt.should.be.between(3, 4);
30   }).should.throwAnyException.withMessage("This is it.");
31 
32   ({
33     someLazyInt.should.be.approximately(3, 4);
34   }).should.throwAnyException.withMessage("This is it.");
35 }
36 
37 @("numbers equal")
38 unittest {
39   ({
40     5.should.equal(5);
41     5.should.not.equal(6);
42   }).should.not.throwAnyException;
43 
44   auto msg = ({
45     5.should.equal(6);
46   }).should.throwException!TestException.msg;
47 
48   msg.split("\n")[0].should.equal("5 should equal 6. 5 is not equal to 6.");
49 
50   msg = ({
51     5.should.not.equal(5);
52   }).should.throwException!TestException.msg;
53 
54   msg.split("\n")[0].should.equal("5 should not equal 5. 5 is equal to 5.");
55 }
56 
57 @("bools equal")
58 unittest {
59   ({
60     true.should.equal(true);
61     true.should.not.equal(false);
62   }).should.not.throwAnyException;
63 
64   auto msg = ({
65     true.should.equal(false);
66   }).should.throwException!TestException.msg;
67 
68   msg.split("\n")[0].should.equal("true should equal false.");
69   msg.split("\n")[2].strip.should.equal("Expected:false");
70   msg.split("\n")[3].strip.should.equal("Actual:true");
71 
72   msg = ({
73     true.should.not.equal(true);
74   }).should.throwException!TestException.msg;
75 
76   msg.split("\n")[0].should.equal("true should not equal true.");
77   msg.split("\n")[2].strip.should.equal("Expected:not true");
78   msg.split("\n")[3].strip.should.equal("Actual:true");
79 }
80 
81 @("numbers greater than")
82 unittest {
83   ({
84     5.should.be.greaterThan(4);
85     5.should.not.be.greaterThan(6);
86 
87     5.should.be.above(4);
88     5.should.not.be.above(6);
89   }).should.not.throwAnyException;
90 
91   auto msg = ({
92     5.should.be.greaterThan(5);
93     5.should.be.above(5);
94   }).should.throwException!TestException.msg;
95 
96   msg.split("\n")[0].should.equal("5 should be greater than 5. 5 is less than or equal to 5.");
97 
98   msg = ({
99     5.should.not.be.greaterThan(4);
100     5.should.not.be.above(4);
101   }).should.throwException!TestException.msg;
102 
103   msg.split("\n")[0].should.equal("5 should not be greater than 4. 5 is greater than 4.");
104 }
105 
106 @("numbers less than")
107 unittest {
108   ({
109     5.should.be.lessThan(6);
110     5.should.not.be.lessThan(4);
111 
112     5.should.be.below(6);
113     5.should.not.be.below(4);
114   }).should.not.throwAnyException;
115 
116   auto msg = ({
117     5.should.be.lessThan(4);
118     5.should.be.below(4);
119   }).should.throwException!TestException.msg;
120 
121   msg.split("\n")[0].should.equal("5 should be less than 4. 5 is greater than or equal to 4.");
122   msg.split("\n")[2].strip.should.equal("Expected:less than 4");
123   msg.split("\n")[3].strip.should.equal("Actual:5");
124 
125   msg = ({
126     5.should.not.be.lessThan(6);
127     5.should.not.be.below(6);
128   }).should.throwException!TestException.msg;
129 
130   msg.split("\n")[0].should.equal("5 should not be less than 6. 5 is less than 6.");
131 }
132 
133 @("numbers between")
134 unittest {
135   ({
136     5.should.be.between(4, 6);
137     5.should.be.between(6, 4);
138     5.should.not.be.between(5, 6);
139     5.should.not.be.between(4, 5);
140 
141     5.should.be.within(4, 6);
142     5.should.be.within(6, 4);
143     5.should.not.be.within(5, 6);
144     5.should.not.be.within(4, 5);
145   }).should.not.throwAnyException;
146 
147   auto msg = ({
148     5.should.be.between(5, 6);
149     5.should.be.within(5, 6);
150   }).should.throwException!TestException.msg;
151 
152   msg.split("\n")[0].should.equal("5 should be between 5 and 6. 5 is less than or equal to 5.");
153   msg.split("\n")[2].strip.should.equal("Expected:a value inside (5, 6) interval");
154   msg.split("\n")[3].strip.should.equal("Actual:5");
155 
156   msg = ({
157     5.should.be.between(4, 5);
158     5.should.be.within(4, 5);
159   }).should.throwException!TestException.msg;
160 
161   msg.split("\n")[0].should.equal("5 should be between 4 and 5. 5 is greater than or equal to 5.");
162   msg.split("\n")[2].strip.should.equal("Expected:a value inside (4, 5) interval");
163   msg.split("\n")[3].strip.should.equal("Actual:5");
164 
165   msg = ({
166     5.should.not.be.between(4, 6);
167     5.should.not.be.within(4, 6);
168   }).should.throwException!TestException.msg;
169 
170   msg.split("\n")[0].strip.should.equal("5 should not be between 4 and 6.");
171   msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval");
172   msg.split("\n")[3].strip.should.equal("Actual:5");
173 
174   msg = ({
175     5.should.not.be.between(6, 4);
176     5.should.not.be.within(6, 4);
177   }).should.throwException!TestException.msg;
178 
179   msg.split("\n")[0].strip.should.equal("5 should not be between 6 and 4.");
180   msg.split("\n")[2].strip.should.equal("Expected:a value outside (4, 6) interval");
181   msg.split("\n")[3].strip.should.equal("Actual:5");
182 }
183 
184 /// numbers approximately
185 unittest {
186   ({
187     (10f/3f).should.be.approximately(3, 0.34);
188     (10f/3f).should.not.be.approximately(3, 0.1);
189   }).should.not.throwAnyException;
190 
191   auto msg = ({
192     (10f/3f).should.be.approximately(3, 0.1);
193   }).should.throwException!TestException.msg;
194 
195   msg.split("\n")[0].strip.should.contain("(10f/3f) should be approximately 3±0.1.");
196   msg.split("\n")[2].strip.should.contain("Expected:3±0.1");
197   msg.split("\n")[3].strip.should.contain("Actual:3.33333");
198 
199   msg = ({
200     (10f/3f).should.not.be.approximately(3, 0.34);
201   }).should.throwException!TestException.msg;
202 
203   msg.split("\n")[0].strip.should.contain("(10f/3f) should not be approximately 3±0.34.");
204   msg.split("\n")[2].strip.should.contain("Expected:not 3±0.34");
205   msg.split("\n")[3].strip.should.contain("Actual:3.33333");
206 }
207 
208 /// should throw exceptions for delegates that return basic types
209 unittest {
210   int value() {
211     throw new Exception("not implemented value");
212   }
213 
214   void voidValue() {
215     throw new Exception("nothing here");
216   }
217 
218   void noException() { }
219 
220   value().should.throwAnyException.withMessage.equal("not implemented value");
221   voidValue().should.throwAnyException.withMessage.equal("nothing here");
222 
223   bool thrown;
224 
225   try {
226     noException.should.throwAnyException;
227   } catch (TestException e) {
228     e.msg.should.startWith("noException should throw any exception. No exception was thrown.");
229     thrown = true;
230   }
231   thrown.should.equal(true);
232 
233   thrown = false;
234 
235   try {
236     voidValue().should.not.throwAnyException;
237   } catch(TestException e) {
238     thrown = true;
239     e.msg.split("\n")[0].should.equal("voidValue() should not throw any exception. `object.Exception` saying `nothing here` was thrown.");
240   }
241 
242   thrown.should.equal(true);
243 }
244 
245 /// it should compile const comparison
246 unittest {
247   const actual = 42;
248   actual.should.equal(42);
249 }