-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
ShouldContainOnly_create_Test.java
485 lines (457 loc) · 23.1 KB
/
ShouldContainOnly_create_Test.java
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2022 the original author or authors.
*/
package org.assertj.core.error;
import static java.lang.String.format;
import static java.util.Collections.emptySet;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.data.MapEntry.entry;
import static org.assertj.core.error.ShouldContainOnly.shouldContainOnly;
import static org.assertj.core.test.Maps.mapOf;
import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.Lists.list;
import static org.assertj.core.util.Sets.newLinkedHashSet;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import org.assertj.core.data.MapEntry;
import org.assertj.core.description.TextDescription;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.presentation.StandardRepresentation;
import org.assertj.core.test.CaseInsensitiveStringComparator;
import org.assertj.core.test.Jedi;
import org.junit.jupiter.api.Test;
/**
* Tests for
* <code>{@link ShouldContainOnly#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>
* .
*
* @author Alex Ruiz
* @author Yvonne Wang
* @author Joel Costigliola
*/
class ShouldContainOnly_create_Test {
private static final ComparatorBasedComparisonStrategy CASE_INSENSITIVE_COMPARISON_STRATEGY = new ComparatorBasedComparisonStrategy(CaseInsensitiveStringComparator.instance);
@Test
void should_create_error_message() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda", "Han"), list("Luke", "Yoda"),
newLinkedHashSet("Luke"), newLinkedHashSet("Han"));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\", \"Han\"]%n"
+ "to contain only:%n"
+ " [\"Luke\", \"Yoda\"]%n"
+ "element(s) not found:%n"
+ " [\"Luke\"]%n"
+ "and element(s) not expected:%n"
+ " [\"Han\"]%n"));
}
@Test
void should_create_error_message_with_custom_comparison_strategy() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda", "Han"),
list("Luke", "Yoda"),
newLinkedHashSet("Luke"),
newLinkedHashSet("Han"),
CASE_INSENSITIVE_COMPARISON_STRATEGY);
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\", \"Han\"]%n"
+ "to contain only:%n"
+ " [\"Luke\", \"Yoda\"]%n"
+ "element(s) not found:%n"
+ " [\"Luke\"]%n"
+ "and element(s) not expected:%n"
+ " [\"Han\"]%n"
+ "when comparing values using CaseInsensitiveStringComparator"));
}
@Test
void should_not_display_unexpected_elements_when_there_are_none() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda"),
list("Luke", "Yoda"),
newLinkedHashSet("Luke"),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\"]%n"
+ "to contain only:%n"
+ " [\"Luke\", \"Yoda\"]%n"
+ "but could not find the following element(s):%n"
+ " [\"Luke\"]%n"));
}
@Test
void should_not_display_unexpected_elements_when_there_are_none_with_custom_comparison_strategy() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda"),
list("Luke", "Yoda"),
newLinkedHashSet("Luke"),
Collections.emptySet(),
CASE_INSENSITIVE_COMPARISON_STRATEGY);
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\"]%n"
+ "to contain only:%n"
+ " [\"Luke\", \"Yoda\"]%n"
+ "but could not find the following element(s):%n"
+ " [\"Luke\"]%n"
+ "when comparing values using CaseInsensitiveStringComparator"));
}
@Test
void should_not_display_elements_not_found_when_there_are_none() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda", "Leia"),
list("Yoda"),
emptySet(),
newLinkedHashSet("Leia"));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\", \"Leia\"]%n"
+ "to contain only:%n"
+ " [\"Yoda\"]%n"
+ "but the following element(s) were unexpected:%n"
+ " [\"Leia\"]%n"));
}
@Test
void should_not_display_elements_not_found_when_there_are_none_with_custom_comparison_strategy() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(list("Yoda", "Leia"),
list("Yoda"),
emptySet(),
newLinkedHashSet("Leia"),
CASE_INSENSITIVE_COMPARISON_STRATEGY);
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting ArrayList:%n"
+ " [\"Yoda\", \"Leia\"]%n"
+ "to contain only:%n"
+ " [\"Yoda\"]%n"
+ "but the following element(s) were unexpected:%n"
+ " [\"Leia\"]%n"
+ "when comparing values using CaseInsensitiveStringComparator"));
}
@Test
void should_create_error_message_unexpected_for_map() {
// GIVEN
Map<String, String> map = mapOf(entry("name", "Yoda"), entry("color", "green"));
MapEntry<String, String>[] expected = array(entry("name", "Yoda"));
ErrorMessageFactory factory = shouldContainOnly(map,
expected,
emptySet(),
set(entry("color", "green")));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting map:%n"
+ " {\"color\"=\"green\", \"name\"=\"Yoda\"}%n"
+ "to contain only:%n"
+ " [\"name\"=\"Yoda\"]%n"
+ "but the following map entries were unexpected:%n"
+ " [\"color\"=\"green\"]%n"));
}
@Test
void should_create_error_message_not_found_for_map() {
// GIVEN
Map<String, String> map = mapOf(entry("name", "Yoda"));
MapEntry<String, String>[] expected = array(entry("name", "Yoda"), entry("color", "green"));
ErrorMessageFactory factory = shouldContainOnly(map,
expected,
set(entry("color", "green")),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting map:%n"
+ " {\"name\"=\"Yoda\"}%n"
+ "to contain only:%n"
+ " [\"name\"=\"Yoda\", \"color\"=\"green\"]%n"
+ "but could not find the following map entries:%n"
+ " [\"color\"=\"green\"]%n"));
}
@Test
void should_create_error_message_not_found_and_unexpected_for_map() {
// GIVEN
Map<String, String> map = mapOf(entry("name", "Yoda"));
MapEntry<String, String>[] expected = array(entry("color", "green"));
ErrorMessageFactory factory = shouldContainOnly(map,
expected,
set(entry("color", "green")),
set(entry("name", "Yoda")));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting map:%n"
+ " {\"name\"=\"Yoda\"}%n"
+ "to contain only:%n"
+ " [\"color\"=\"green\"]%n"
+ "map entries not found:%n"
+ " [\"color\"=\"green\"]%n"
+ "and map entries not expected:%n"
+ " [\"name\"=\"Yoda\"]%n"));
}
@Test
void should_create_error_message_unexpected_for_float_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new float[] { 6f, 8f, 7f },
new float[] { 6f, 8f },
emptySet(),
newLinkedHashSet(7f));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting float[]:%n"
+ " [6.0f, 8.0f, 7.0f]%n"
+ "to contain only:%n"
+ " [6.0f, 8.0f]%n"
+ "but the following float(s) were unexpected:%n"
+ " [7.0f]%n"));
}
@Test
void should_create_error_message_not_found_for_char_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new char[] { 'a' },
new char[] { 'a', 'b' },
newLinkedHashSet('b'),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting char[]:%n"
+ " ['a']%n"
+ "to contain only:%n"
+ " ['a', 'b']%n"
+ "but could not find the following char(s):%n"
+ " ['b']%n"));
}
@Test
void should_create_error_message_not_found_and_unexpected_for_long_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new long[] { 5L, 6L },
new long[] { 3L, 6L },
newLinkedHashSet(3L),
newLinkedHashSet(5L));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting long[]:%n"
+ " [5L, 6L]%n"
+ "to contain only:%n"
+ " [3L, 6L]%n"
+ "long(s) not found:%n"
+ " [3L]%n"
+ "and long(s) not expected:%n"
+ " [5L]%n"));
}
@Test
void should_create_error_message_unexpected_for_boolean_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new boolean[] { true, false },
new boolean[] { false },
emptySet(),
newLinkedHashSet(true));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting boolean[]:%n"
+ " [true, false]%n"
+ "to contain only:%n"
+ " [false]%n"
+ "but the following boolean(s) were unexpected:%n"
+ " [true]%n"));
}
@Test
void should_create_error_message_not_found_for_double_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new double[] { 1.0 },
new double[] { 1.0, 2.0 },
newLinkedHashSet(2.0),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting double[]:%n"
+ " [1.0]%n"
+ "to contain only:%n"
+ " [1.0, 2.0]%n"
+ "but could not find the following double(s):%n"
+ " [2.0]%n"));
}
@Test
void should_create_error_message_not_found_and_unexpected_for_short_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new short[] { 5, 6 },
new short[] { 3, 6 },
newLinkedHashSet((short) 3),
newLinkedHashSet((short) 5));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting short[]:%n"
+ " [5, 6]%n"
+ "to contain only:%n"
+ " [3, 6]%n"
+ "short(s) not found:%n"
+ " [3]%n"
+ "and short(s) not expected:%n"
+ " [5]%n"));
}
@Test
void should_create_error_message_unexpected_for_int_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new int[] { 1, 2 },
new int[] { 2 },
emptySet(),
newLinkedHashSet(1));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting int[]:%n"
+ " [1, 2]%n"
+ "to contain only:%n"
+ " [2]%n"
+ "but the following int(s) were unexpected:%n"
+ " [1]%n"));
}
@Test
void should_create_error_message_not_found_for_byte_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new byte[] { 1 },
new byte[] { 1, 2 },
newLinkedHashSet((byte) 2),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting byte[]:%n"
+ " [1]%n"
+ "to contain only:%n"
+ " [1, 2]%n"
+ "but could not find the following byte(s):%n"
+ " [2]%n"));
}
@Test
void should_create_error_message_unexpected_for_String_array() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(new String[] { "1", "2" },
new String[] { "2" },
emptySet(),
newLinkedHashSet("1"));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting String[]:%n"
+ " [\"1\", \"2\"]%n"
+ "to contain only:%n"
+ " [\"2\"]%n"
+ "but the following string(s) were unexpected:%n"
+ " [\"1\"]%n"));
}
@Test
void should_create_error_message_not_found_for_set() {
// GIVEN
ErrorMessageFactory factory = shouldContainOnly(newLinkedHashSet(1),
newLinkedHashSet(1, 2),
newLinkedHashSet(2),
emptySet());
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting LinkedHashSet:%n"
+ " [1]%n"
+ "to contain only:%n"
+ " [1, 2]%n"
+ "but could not find the following element(s):%n"
+ " [2]%n"));
}
@Test
void should_create_error_message_not_found_and_unexpected_for_custom_object() {
// GIVEN
Jedi actual = new Jedi("Yoda", "green");
Jedi expected = new Jedi("Luke", "blue");
ErrorMessageFactory factory = shouldContainOnly(array(actual),
array(expected),
newLinkedHashSet(expected),
newLinkedHashSet(actual));
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting Jedi[]:%n"
+ " [Yoda the Jedi]%n"
+ "to contain only:%n"
+ " [Luke the Jedi]%n"
+ "jedi(s) not found:%n"
+ " [Luke the Jedi]%n"
+ "and jedi(s) not expected:%n"
+ " [Yoda the Jedi]%n"));
}
@Test
void should_create_error_message_not_found_and_unexpected_with_given_group_description() {
// GIVEN
Jedi actual = new Jedi("Yoda", "green");
Jedi expected = new Jedi("Luke", "blue");
GroupTypeDescription jedisGroupTypeDescription = new GroupTypeDescription("jedis", "some jedis were");
ErrorMessageFactory factory = shouldContainOnly(array(actual),
array(expected),
newLinkedHashSet(expected),
newLinkedHashSet(actual),
jedisGroupTypeDescription);
// WHEN
String message = factory.create(new TextDescription("Test"), new StandardRepresentation());
// THEN
then(message).isEqualTo(format("[Test] %n"
+ "Expecting jedis:%n"
+ " [Yoda the Jedi]%n"
+ "to contain only:%n"
+ " [Luke the Jedi]%n"
+ "some jedis were not found:%n"
+ " [Luke the Jedi]%n"
+ "and some jedis were not expected:%n"
+ " [Yoda the Jedi]%n"));
}
private static <K, V> HashSet<MapEntry<K, V>> set(MapEntry<K, V> entry) {
HashSet<MapEntry<K, V>> set = new HashSet<>();
set.add(entry);
return set;
}
}