1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package ch.powerunit.comparator.impl;
21
22 import java.util.Comparator;
23 import java.util.function.Supplier;
24 import java.util.stream.Stream;
25 import java.util.stream.Stream.Builder;
26
27 import org.hamcrest.Matcher;
28
29 import ch.powerunit.Parameter;
30 import ch.powerunit.Parameters;
31 import ch.powerunit.TestDelegate;
32 import ch.powerunit.TestDelegator;
33 import ch.powerunit.TestSuite;
34 import ch.powerunit.bifunction.BiFunctionTester;
35 import ch.powerunit.comparator.ComparatorTester;
36
37
38
39
40
41 @TestDelegator
42 public class ComparatorTesterImpl<O, C extends Comparator<O>> implements
43 TestSuite {
44 @Parameters
45 public static <O, C extends Comparator<O>> Stream<Object[]> getParameters(
46 ComparatorTester<O, C> input) {
47 Builder<Object[]> b = Stream.builder();
48 O less[] = input.getLessSamples().get();
49 O equal[] = input.getEqualSamples().get();
50 O greater[] = input.getGreaterSamples().get();
51 Comparator<O> underTest = input.getUnderTest().get();
52 int i = 0;
53 for (O l : less) {
54 b.accept(new Object[] {
55 input.getComparatorClass(),
56 underTest,
57 l,
58 l,
59 TestSuite.DSL.equalTo(0),
60 "Having `"
61 + l
62 + "` = `"
63 + l
64 + "` Then result should be 0 for the Comparator class "
65 + input.getComparatorClass() + " (with instance "
66 + underTest + ")" });
67 for (O e : equal) {
68 b.accept(new Object[] {
69 input.getComparatorClass(),
70 underTest,
71 l,
72 e,
73 TestSuite.DSL.lessThan(0),
74 "Having `"
75 + l
76 + "` < `"
77 + e
78 + "` Then result should be <0 for the Comparator class "
79 + input.getComparatorClass()
80 + " (with instance " + underTest + ")" });
81 b.accept(new Object[] {
82 input.getComparatorClass(),
83 underTest,
84 e,
85 l,
86 TestSuite.DSL.greaterThan(0),
87 "Having `"
88 + e
89 + "` > `"
90 + l
91 + "` Then result should be >0 for the Comparator class "
92 + input.getComparatorClass()
93 + " (with instance " + underTest + ")" });
94 }
95 for (O g : greater) {
96 b.accept(new Object[] {
97 input.getComparatorClass(),
98 underTest,
99 l,
100 g,
101 TestSuite.DSL.lessThan(0),
102 "Having `"
103 + l
104 + "` < `"
105 + g
106 + "` Then result should be <0 for the Comparator class "
107 + input.getComparatorClass()
108 + " (with instance " + underTest + ")" });
109 b.accept(new Object[] {
110 input.getComparatorClass(),
111 underTest,
112 g,
113 l,
114 TestSuite.DSL.greaterThan(0),
115 "Having `"
116 + g
117 + "` > `"
118 + l
119 + "` Then result should be >0 for the Comparator class "
120 + input.getComparatorClass()
121 + " (with instance " + underTest + ")" });
122 }
123 int j = 0;
124 for (O l2 : less) {
125 if (j++ <= i) {
126 continue;
127 }
128 b.accept(new Object[] {
129 input.getComparatorClass(),
130 underTest,
131 l,
132 l2,
133 TestSuite.DSL.lessThan(0),
134 "Having `"
135 + l
136 + "` < `"
137 + l2
138 + "` Then result should be <0 for the Comparator class "
139 + input.getComparatorClass()
140 + " (with instance " + underTest + ")" });
141 b.accept(new Object[] {
142 input.getComparatorClass(),
143 underTest,
144 l2,
145 l,
146 TestSuite.DSL.greaterThan(0),
147 "Having `"
148 + l2
149 + "` > `"
150 + l
151 + "` Then result should be >0 for the Comparator class "
152 + input.getComparatorClass()
153 + " (with instance " + underTest + ")" });
154 }
155 i++;
156 }
157 i = 0;
158 for (O e : equal) {
159 for (O g : greater) {
160 b.accept(new Object[] {
161 input.getComparatorClass(),
162 underTest,
163 e,
164 g,
165 TestSuite.DSL.lessThan(0),
166 "Having `"
167 + e
168 + "` < `"
169 + g
170 + "` Then result should be <0 for the Comparator class "
171 + input.getComparatorClass()
172 + " (with instance " + underTest + ")" });
173 b.accept(new Object[] {
174 input.getComparatorClass(),
175 underTest,
176 g,
177 e,
178 TestSuite.DSL.greaterThan(0),
179 "Having `"
180 + g
181 + "` > `"
182 + e
183 + "` Then result should be >0 for the Comparator class "
184 + input.getComparatorClass()
185 + " (with instance " + underTest + ")" });
186 }
187 for (O e2 : equal) {
188 b.accept(new Object[] {
189 input.getComparatorClass(),
190 underTest,
191 e,
192 e2,
193 TestSuite.DSL.equalTo(0),
194 "Having `"
195 + e
196 + "` = `"
197 + e2
198 + "` Then result should be 0 for the Comparator class "
199 + input.getComparatorClass()
200 + " (with instance " + underTest + ")" });
201 b.accept(new Object[] {
202 input.getComparatorClass(),
203 underTest,
204 e2,
205 e,
206 TestSuite.DSL.equalTo(0),
207 "Having `"
208 + e2
209 + "` = `"
210 + e
211 + "` Then result should be 0 for the Comparator class "
212 + input.getComparatorClass()
213 + " (with instance " + underTest + ")" });
214 }
215 }
216 i = 0;
217 for (O g : greater) {
218 int j = 0;
219 for (O g2 : greater) {
220 if (j++ <= i) {
221 continue;
222 }
223 b.accept(new Object[] {
224 input.getComparatorClass(),
225 underTest,
226 g,
227 g2,
228 TestSuite.DSL.lessThan(0),
229 "Having `"
230 + g
231 + "` < `"
232 + g2
233 + "` Then result should be <0 for the Comparator class "
234 + input.getComparatorClass()
235 + " (with instance " + underTest + ")" });
236 b.accept(new Object[] {
237 input.getComparatorClass(),
238 underTest,
239 g2,
240 g,
241 TestSuite.DSL.greaterThan(0),
242 "Having `"
243 + g2
244 + "` > `"
245 + g
246 + "` Then result should be >0 for the Comparator class "
247 + input.getComparatorClass()
248 + " (with instance " + underTest + ")" });
249 }
250 i++;
251 }
252
253
254 return b.build();
255 }
256
257 @Parameter(0)
258 public Class<C> target;
259
260 @Parameter(1)
261 public Comparator<O> instance;
262
263 @Parameter(2)
264 public O obj1;
265
266 @Parameter(3)
267 public O obj2;
268
269 @Parameter(4)
270 public Matcher<Integer> expectedResult;
271
272 @Parameter(5)
273 public String name;
274
275 @TestDelegate
276 public final Supplier<BiFunctionTester<O, O, Integer>> tester = () -> testerOfBiFunction(
277 instance::compare).passingAsParameter(obj1, obj2)
278 .thenExpectingResultThat(expectedResult).testNamed(name).build();
279
280 }