-
Notifications
You must be signed in to change notification settings - Fork 3
/
pyjamas_test.d
239 lines (201 loc) · 6.39 KB
/
pyjamas_test.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
void main()
{
import std.exception;
import bed;
import pyjamas;
describe("should", {
describe("should(v)", {
it("returns an Assertion", {
assert(is(typeof(10.should) == Assertion!int));
});
});
describe("Assertion", {
it("can be instantiated for ranges of structs without `opCmp`", {
struct Test {
int a;
int b;
}
cast(void) [Test(2, 3)].should;
});
describe(".message", {
it("returns the correct message for binary operators", {
auto a = new Assertion!int(10);
a.operator = "equal";
assert(a.message(20) == "expected 10 to equal 20");
});
it("returns the correct message for unary operators", {
auto a = new Assertion!string("function");
a.operator = "throw";
assert(a.message == "expected function to throw");
});
it("returns the correct message for negated operators", {
auto a = new Assertion!int(10);
a.operator = "be";
assert(a.not.message(false) == "expected 10 to not be false");
});
});
describe(".exist", {
it("returns and asserts for existence", {
auto a1 = new Assertion!string;
a1.not.exist;
auto a2 = new Assertion!string(null);
a2.not.exist;
auto a3 = new Assertion!int(10);
a3.exist;
});
});
describe(".True", {
it("returns and asserts for true", {
auto a = new Assertion!bool(true);
assert(a.be.True);
});
it("throws for false", {
auto a = new Assertion!bool(false);
assertThrown!Exception(a.be.True);
});
});
describe(".False", {
it("returns and asserts for false", {
auto a = new Assertion!bool(false);
assert(!a.be.False);
});
it("throws for true", {
auto a = new Assertion!bool(true);
assertThrown!Exception(a.be.False);
});
});
describe(".equal", {
it("asserts whether two values are equal", {
auto a = new Assertion!int(10);
a.equal(10);
a.not.equal(5);
a.not;
assertThrown!Exception(a.equal(2));
});
it("works for ranges", {
auto a1 = new Assertion!(uint[])([1, 2, 3, 4]);
a1.equal([1, 2, 3, 4]);
auto a2 = new Assertion!(char[])([0, 2, 1]);
a2.not.equal([1, 2, 3, 5]);
});
it("works for structs", {
struct Example
{
bool a = false;
string f = "something";
}
auto e = Example(true, "here");
auto a1 = new Assertion!(Example)(e);
a1.equal(Example(true, "here"));
auto a2 = new Assertion!(Example)(e);
a1.not.equal(Example(true, "asdf"));
});
});
describe(".match", {
import std.regex;
it("returns whether a string type matches a Regex", {
auto a = new Assertion!string("something weird");
assert(a.match(`[a-z]+`));
assert(a.match(regex(`[a-z]+`)));
});
it("returns whether a string type matches a StaticRegex", {
auto a = new Assertion!string("something 2 weird");
assert(!a.not.match(ctRegex!`^[a-z]+$`));
});
it("returns whether a string type matches a string regex", {
auto a = new Assertion!string("1234numbers");
assert(a.match(`[0-9]+[a-z]+`));
assert(!a.not.match(`^[a-z]+`));
});
});
describe(".value", {
it("asserts for arrays containing elements", {
auto a = new Assertion!(int[])([1, 2, 3, 4, 5, 6]);
a.include(4);
a.not.include(7);
});
it("asserts for associative arrays containing values", {
auto a = new Assertion!(int[string])(["something": 2, "else": 3]);
a.value(2);
a.not.value(4);
});
it("asserts for stirngs containing characters", {
auto a = new Assertion!string("asdf1");
a.include('a');
a.include("sd");
a.not.include(2);
});
});
describe(".length", {
it("asserts for length equality for strings", {
auto a = new Assertion!string("1234567");
a.have.length(7);
});
it("asserts for length equality for arrays", {
auto a = new Assertion!(int[])([1, 2, 3, 4, 5, 6]);
a.have.length(6);
});
it("asserts for length equality for associative arrays", {
auto a = new Assertion!(string[string])([
"something": "here",
"what": "is",
"this": "stuff",
"we're": "doing"
]);
a.have.length(4);
});
});
describe(".Throw", {
it("asserts whether an expressions throws", {
void throwing()
{
throw new Exception("I throw with 0!");
}
should(&throwing).Throw!Exception;
void notThrowing()
{
return;
}
should(¬Throwing).not.Throw;
});
});
describe(".key", {
it("asserts for `key` existence in types with `opIndex` defined", {
auto aArr = [
"something": "here",
];
aArr.should.have.key("something");
aArr.should.not.have.key("else");
});
});
describe(".sorted", {
it("asserts whether a range is sorted", {
auto unsorted = [4, 3, 2, 1];
assert(!new Assertion!(int[])(unsorted).not.sorted);
auto sorted = [1, 2, 3, 4, 8];
assert(new Assertion!(int[])(sorted).sorted);
});
});
describe(".biggerThan", {
it("asserts whether a value is bigger than other", {
auto a1 = new Assertion!bool(true);
a1.biggerThan(0);
a1.biggerThan(false);
auto a2 = new Assertion!string("aab");
a2.biggerThan("aaa");
a2.not.biggerThan("zz");
});
});
describe(".smallerThan", {
it("asserts whether a value is smaller than other", {
auto a1 = new Assertion!bool(false);
a1.smallerThan(1);
a1.smallerThan(true);
auto a2 = new Assertion!int(1000);
a2.smallerThan(2000);
a2.not.smallerThan(99);
});
});
});
});
}