1 module dud.resolve.conftests;
2 
3 @safe pure:
4 import std.format : format;
5 import std.algorithm.iteration : map, joiner;
6 import std.exception : enforce;
7 import core.exception : AssertError;
8 
9 import dud.resolve.conf;
10 import dud.resolve.confs;
11 import dud.resolve.positive;
12 import dud.semver.versionrange;
13 
14 private:
15 
16 const c1 = Conf("foo", IsPositive.yes);
17 const c2 = Conf("foo", IsPositive.no);
18 const c3 = Conf("bar", IsPositive.yes);
19 const c4 = Conf("bar", IsPositive.no);
20 const c5 = Conf("", IsPositive.yes);
21 const c6 = Conf("", IsPositive.no);
22 
23 void testImpl(const(Conf) a, const(Conf) b, const(Confs) exp, const(Confs) rslt,
24 		int line)
25 {
26 	enforce!AssertError(rslt == exp,
27 		format("\na: %s\nb: %s\nexp: %s\nrsl: %s", a, b, exp, rslt),
28 		__FILE__, line);
29 }
30 
31 void testIntersection(const(Conf) a, const(Conf) b, const(Confs) exp,
32 		int line = __LINE__)
33 {
34 	const(Confs) rslt = intersectionOf(a, b);
35 	testImpl(a, b, exp, rslt, line);
36 }
37 
38 void testDifference(const(Conf) a, const(Conf) b, const(Confs) exp,
39 		int line = __LINE__)
40 {
41 	const(Confs) rslt = differenceOf(a, b);
42 	testImpl(a, b, exp, rslt, line);
43 }
44 
45 
46 //
47 // differenceOf
48 //
49 
50 unittest {
51 	testDifference(c1, c1, Confs([]));
52 	testDifference(c1, c2, Confs([c1]));
53 	testDifference(c1, c3, Confs([c1]));
54 	testDifference(c1, c4, Confs([c1]));
55 	testDifference(c1, c5, Confs([c1]));
56 	testDifference(c1, c6, Confs([c1]));
57 
58 	testDifference(c2, c1, Confs([c2]));
59 	testDifference(c2, c2, Confs([]));
60 	testDifference(c2, c3, Confs([c2]));
61 	testDifference(c2, c4, Confs([c2]));
62 	testDifference(c2, c5, Confs([c2]));
63 	testDifference(c2, c6, Confs([c2]));
64 }
65 
66 //
67 // opCmp
68 //
69 
70 unittest {
71 	assert(c6 < c1);
72 	assert(c6 < c2);
73 	assert(c6 < c3);
74 	assert(c6 < c4);
75 	assert(c6 < c5);
76 	assert(c6 >= c6);
77 	assert(c6 <= c6);
78 
79 	assert(c5 < c1);
80 	assert(c5 < c2);
81 	assert(c5 < c3);
82 	assert(c5 < c4);
83 	assert(c5 <= c5);
84 	assert(c5 >= c5);
85 	assert(c5 > c6);
86 
87 	assert(c4 < c1);
88 	assert(c4 < c2);
89 	assert(c4 < c3);
90 	assert(c4 >= c4);
91 	assert(c4 <= c4);
92 	assert(c4 > c5);
93 	assert(c4 > c6);
94 
95 	assert(c3 < c1);
96 	assert(c3 < c2);
97 	assert(c3 >= c3);
98 	assert(c3 <= c3);
99 	assert(c3 > c4);
100 	assert(c3 > c5);
101 	assert(c3 > c6);
102 
103 	assert(c2 < c1);
104 	assert(c2 <= c2);
105 	assert(c2 >= c2);
106 	assert(c2 > c3);
107 	assert(c2 > c4);
108 	assert(c2 > c5);
109 	assert(c2 > c6);
110 
111 	assert(c1 <= c1);
112 	assert(c1 >= c1);
113 	assert(c1 > c2);
114 	assert(c1 > c3);
115 	assert(c1 > c4);
116 	assert(c1 > c5);
117 	assert(c1 > c6);
118 }
119 
120 //
121 // intersectionOf { x : x in A and x in B }
122 //
123 
124 unittest {
125 	testIntersection(c6, c1, Confs([c6]));
126 	testIntersection(c6, c2, Confs([c6]));
127 	testIntersection(c6, c3, Confs([c6]));
128 	testIntersection(c6, c4, Confs([c6]));
129 	testIntersection(c6, c5, Confs([c6]));
130 	testIntersection(c6, c6, Confs([c6]));
131 
132 	testIntersection(c5, c1, Confs([c1]));
133 	testIntersection(c5, c2, Confs([c2, c5]));
134 	testIntersection(c5, c3, Confs([c3]));
135 	testIntersection(c5, c4, Confs([c4, c5]));
136 	testIntersection(c5, c5, Confs([c5]));
137 	testIntersection(c5, c6, Confs([c6]));
138 
139 	testIntersection(c4, c1, Confs([]));
140 	testIntersection(c4, c2, Confs([]));
141 	testIntersection(c4, c3, Confs([]));
142 	testIntersection(c4, c4, Confs([c4]));
143 	testIntersection(c4, c5, Confs([c4, c5]));
144 	testIntersection(c4, c6, Confs([c6]));
145 
146 	testIntersection(c3, c1, Confs([]));
147 	testIntersection(c3, c2, Confs([]));
148 	testIntersection(c3, c3, Confs([c3]));
149 	testIntersection(c3, c4, Confs([]));
150 	testIntersection(c3, c5, Confs([c3]));
151 	testIntersection(c3, c6, Confs([c6]));
152 
153 	testIntersection(c2, c1, Confs([]));
154 	testIntersection(c2, c2, Confs([c2]));
155 	testIntersection(c2, c3, Confs([]));
156 	testIntersection(c2, c4, Confs([]));
157 	testIntersection(c2, c5, Confs([c2, c5]));
158 	testIntersection(c2, c6, Confs([c6]));
159 
160 	testIntersection(c1, c1, Confs([c1]));
161 	testIntersection(c1, c2, Confs([]));
162 	testIntersection(c1, c3, Confs([]));
163 	testIntersection(c1, c4, Confs([]));
164 	testIntersection(c1, c5, Confs([c1]));
165 	testIntersection(c1, c6, Confs([c6]));
166 }
167 
168 //
169 // invert
170 //
171 
172 void testInvert(const(Conf) inp, const(Conf) exp, const int line = __LINE__) {
173 	import std.exception : enforce;
174 
175 	const(Conf) inv = invert(inp);
176 	enforce!AssertError(inv == exp
177 			, format("\ninp %s\ninv %s\nexp %s", inp, inv, exp), __FILE__, line);
178 }
179 
180 unittest {
181 	testInvert(c1, c2);
182 	testInvert(c2, c1);
183 	testInvert(c3, c4);
184 	testInvert(c4, c3);
185 	testInvert(c5, c6);
186 	testInvert(c6, c5);
187 }
188 
189 //
190 // allowsAny
191 //
192 
193 unittest {
194 	assert( allowsAny(c1, c1));
195 	assert(!allowsAny(c1, c2));
196 	assert(!allowsAny(c1, c3));
197 	assert(!allowsAny(c1, c4));
198 	assert( allowsAny(c1, c5));
199 	assert(!allowsAny(c1, c6));
200 
201 	assert(!allowsAny(c2, c1));
202 	assert(!allowsAny(c2, c2));
203 	assert(!allowsAny(c2, c3));
204 	assert(!allowsAny(c2, c4));
205 	assert( allowsAny(c2, c5));
206 	assert(!allowsAny(c2, c6));
207 
208 	assert(!allowsAny(c3, c1));
209 	assert(!allowsAny(c3, c2));
210 	assert( allowsAny(c3, c3));
211 	assert(!allowsAny(c3, c4));
212 	assert( allowsAny(c3, c5));
213 	assert(!allowsAny(c3, c6));
214 
215 	assert(!allowsAny(c4, c1));
216 	assert(!allowsAny(c4, c2));
217 	assert(!allowsAny(c4, c3));
218 	assert(!allowsAny(c4, c4));
219 	assert( allowsAny(c4, c5));
220 	assert(!allowsAny(c4, c6));
221 
222 	assert( allowsAny(c5, c1));
223 	assert( allowsAny(c5, c2));
224 	assert( allowsAny(c5, c3));
225 	assert( allowsAny(c5, c4));
226 	assert( allowsAny(c5, c5));
227 	assert(!allowsAny(c5, c6));
228 
229 	assert(!allowsAny(c6, c1));
230 	assert(!allowsAny(c6, c2));
231 	assert(!allowsAny(c6, c3));
232 	assert(!allowsAny(c6, c4));
233 	assert(!allowsAny(c6, c5));
234 	assert(!allowsAny(c6, c6));
235 }
236 
237 //
238 // allowsAll
239 //
240 
241 unittest {
242 	assert( allowsAll(c1, c1));
243 	assert(!allowsAll(c1, c2));
244 	assert(!allowsAll(c1, c3));
245 	assert(!allowsAll(c1, c4));
246 	assert(!allowsAll(c1, c5));
247 	assert(!allowsAll(c1, c6));
248 
249 	assert(!allowsAll(c2, c1));
250 	assert(!allowsAll(c2, c2));
251 	assert(!allowsAll(c2, c3));
252 	assert(!allowsAll(c2, c4));
253 	assert(!allowsAll(c2, c5));
254 	assert(!allowsAll(c2, c6));
255 
256 	assert(!allowsAll(c3, c1));
257 	assert(!allowsAll(c3, c2));
258 	assert( allowsAll(c3, c3));
259 	assert(!allowsAll(c3, c4));
260 	assert(!allowsAll(c3, c5));
261 	assert(!allowsAll(c3, c6));
262 
263 	assert(!allowsAll(c4, c1));
264 	assert(!allowsAll(c4, c2));
265 	assert(!allowsAll(c4, c3));
266 	assert(!allowsAll(c4, c4));
267 	assert(!allowsAll(c4, c5));
268 	assert(!allowsAll(c4, c6));
269 
270 	assert( allowsAll(c5, c1));
271 	assert( allowsAll(c5, c2));
272 	assert( allowsAll(c5, c3));
273 	assert( allowsAll(c5, c4));
274 	assert( allowsAll(c5, c5));
275 	assert( allowsAll(c5, c6));
276 
277 	assert(!allowsAll(c6, c1));
278 	assert(!allowsAll(c6, c2));
279 	assert(!allowsAll(c6, c3));
280 	assert(!allowsAll(c6, c4));
281 	assert(!allowsAll(c6, c5));
282 	assert(!allowsAll(c6, c6));
283 }
284 
285 //
286 // relation
287 //
288 
289 void testSet(const(Conf) a, const(Conf) b, SetRelation exp,
290 		int line = __LINE__)
291 {
292 	const(SetRelation) rslt = relation(a, b);
293 	enforce!AssertError(rslt == exp,
294 		format("\na: %s\nb: %s\nexp: %s\nrsl: %s", a, b, exp, rslt),
295 		__FILE__, line);
296 }
297 
298 unittest {
299 	Conf nc1 = Conf("", IsPositive.yes);
300 	Conf nc2 = Conf("conf1", IsPositive.yes);
301 	Conf nc3 = Conf("conf1", IsPositive.no);
302 	Conf nc4 = Conf("conf2", IsPositive.yes);
303 	Conf nc5 = Conf("conf2", IsPositive.no);
304 	Conf nc6 = Conf("", IsPositive.no);
305 
306 	testSet(nc1, nc1, SetRelation.subset);
307 	testSet(nc1, nc2, SetRelation.overlapping);
308 	testSet(nc1, nc3, SetRelation.overlapping);
309 	testSet(nc1, nc4, SetRelation.overlapping);
310 	testSet(nc1, nc5, SetRelation.overlapping);
311 	testSet(nc1, nc6, SetRelation.disjoint);
312 	testSet(nc2, nc1, SetRelation.subset);
313 	testSet(nc2, nc2, SetRelation.subset);
314 	testSet(nc2, nc3, SetRelation.disjoint);
315 	testSet(nc2, nc4, SetRelation.disjoint);
316 	testSet(nc2, nc5, SetRelation.disjoint);
317 	testSet(nc2, nc6, SetRelation.disjoint);
318 	testSet(nc3, nc1, SetRelation.subset);
319 	testSet(nc3, nc2, SetRelation.disjoint);
320 	testSet(nc3, nc3, SetRelation.disjoint);
321 	testSet(nc3, nc4, SetRelation.disjoint);
322 	testSet(nc3, nc5, SetRelation.disjoint);
323 	testSet(nc3, nc6, SetRelation.disjoint);
324 	testSet(nc4, nc1, SetRelation.subset);
325 	testSet(nc4, nc2, SetRelation.disjoint);
326 	testSet(nc4, nc3, SetRelation.disjoint);
327 	testSet(nc4, nc4, SetRelation.subset);
328 	testSet(nc4, nc5, SetRelation.disjoint);
329 	testSet(nc4, nc6, SetRelation.disjoint);
330 	testSet(nc5, nc1, SetRelation.subset);
331 	testSet(nc5, nc2, SetRelation.disjoint);
332 	testSet(nc5, nc3, SetRelation.disjoint);
333 	testSet(nc5, nc4, SetRelation.disjoint);
334 	testSet(nc5, nc5, SetRelation.disjoint);
335 	testSet(nc5, nc6, SetRelation.disjoint);
336 	testSet(nc6, nc1, SetRelation.subset);
337 	testSet(nc6, nc2, SetRelation.disjoint);
338 	testSet(nc6, nc3, SetRelation.disjoint);
339 	testSet(nc6, nc4, SetRelation.disjoint);
340 	testSet(nc6, nc5, SetRelation.disjoint);
341 	testSet(nc6, nc6, SetRelation.disjoint);
342 }
343 
344 unittest {
345 	Conf[] tt;
346 	foreach(c1; ["", "conf", "conf2", "conf3"]) {
347 		foreach(c2; [IsPositive.no, IsPositive.yes]) {
348 			tt ~= Conf(c1, c2);
349 		}
350 	}
351 
352 	foreach(it; tt) {
353 		foreach(jt; tt) {
354 			relation(it, jt);
355 		}
356 	}
357 }
358 
359