-
-
Notifications
You must be signed in to change notification settings - Fork 683
/
IterableAssert_extractingResultOf_with_SortedSet_Test.java
154 lines (139 loc) · 8.82 KB
/
IterableAssert_extractingResultOf_with_SortedSet_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
/*
* 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.api.iterable;
import static java.util.Comparator.comparing;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.GroupAssertTestHelper.comparatorForElementFieldsWithNamesOf;
import static org.assertj.core.api.GroupAssertTestHelper.comparatorForElementFieldsWithTypeOf;
import static org.assertj.core.api.GroupAssertTestHelper.comparatorsByTypeOf;
import static org.assertj.core.presentation.UnicodeRepresentation.UNICODE_REPRESENTATION;
import static org.assertj.core.test.AlwaysEqualComparator.ALWAYS_EQUALS_STRING;
import static org.assertj.core.test.AlwaysEqualComparator.ALWAYS_EQUALS_TIMESTAMP;
import java.sql.Timestamp;
import java.util.SortedSet;
import java.util.TreeSet;
import org.assertj.core.api.AbstractIterableAssert;
import org.assertj.core.api.AbstractListAssert;
import org.assertj.core.test.CaseInsensitiveStringComparator;
import org.assertj.core.test.FluentJedi;
import org.assertj.core.test.Name;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* Tests for:
* <ul>
* <li><code>{@link AbstractIterableAssert#extractingResultOf(String)}</code>,
* <li><code>{@link AbstractIterableAssert#extractingResultOf(String, Class)}</code>.
* </ul>
*
* @author Michał Piotrkowski
*/
class IterableAssert_extractingResultOf_with_SortedSet_Test {
private static FluentJedi yoda;
private static FluentJedi vader;
private static Iterable<FluentJedi> jedis;
@BeforeAll
static void setUpOnce() {
yoda = new FluentJedi(new Name("Yoda"), 800, false);
vader = new FluentJedi(new Name("Darth Vader"), 50, true);
jedis = newSortedSet(yoda, vader);
}
@Test
void should_allow_assertions_on_method_invocation_result_extracted_from_given_iterable() {
// extract method result
assertThat(jedis).extractingResultOf("age").containsOnly(800, 50);
// extract if method result is primitive
assertThat(jedis).extractingResultOf("darkSide").containsOnly(false, true);
// extract if method result is also a property
assertThat(jedis).extractingResultOf("name").containsOnly(new Name("Yoda"), new Name("Darth Vader"));
// extract toString method result
assertThat(jedis).extractingResultOf("toString").containsOnly("Yoda", "Darth Vader");
}
@Test
void should_allow_assertions_on_method_invocation_result_extracted_from_given_iterable_with_enforcing_return_type() {
assertThat(jedis).extractingResultOf("name", Name.class).containsOnly(new Name("Yoda"), new Name("Darth Vader"));
}
@Test
void should_throw_error_if_no_method_with_given_name_can_be_extracted() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThat(jedis).extractingResultOf("unknown"))
.withMessage("Can't find method 'unknown' in class FluentJedi.class. Make sure public method exists and accepts no arguments!");
}
@Test
void should_use_method_name_as_description_when_extracting_result_of_method_list() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(jedis).extractingResultOf("age")
.isEmpty())
.withMessageContaining("[Extracted: result of age()]");
}
@Test
void should_use_method_name_as_description_when_extracting_typed_result_of_method_list() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(jedis).extractingResultOf("age",
Integer.class)
.isEmpty())
.withMessageContaining("[Extracted: result of age()]");
}
@Test
void extractingResultOf_should_keep_assertion_state() {
// WHEN
// not all comparators are used but we want to test that they are passed correctly after extracting
AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")
.withFailMessage("error message")
.withRepresentation(UNICODE_REPRESENTATION)
.usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,
"foo")
.usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,
Timestamp.class)
.extractingResultOf("toString")
.usingComparatorForType(CaseInsensitiveStringComparator.instance,
String.class)
.containsOnly("YODA", "darth vader");
// THEN
assertThat(assertion.descriptionText()).isEqualTo("test description");
assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);
assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");
assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(CaseInsensitiveStringComparator.instance);
assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);
assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);
}
@Test
void strongly_typed_extractingResultOf_should_keep_assertion_state() {
// WHEN
// not all comparators are used but we want to test that they are passed correctly after extracting
AbstractListAssert<?, ?, ?, ?> assertion = assertThat(jedis).as("test description")
.withFailMessage("error message")
.withRepresentation(UNICODE_REPRESENTATION)
.usingComparatorForElementFieldsWithNames(ALWAYS_EQUALS_STRING,
"foo")
.usingComparatorForElementFieldsWithType(ALWAYS_EQUALS_TIMESTAMP,
Timestamp.class)
.extractingResultOf("toString", String.class)
.usingComparatorForType(CaseInsensitiveStringComparator.instance,
String.class)
.containsOnly("YODA", "darth vader");
// THEN
assertThat(assertion.descriptionText()).isEqualTo("test description");
assertThat(assertion.info.representation()).isEqualTo(UNICODE_REPRESENTATION);
assertThat(assertion.info.overridingErrorMessage()).isEqualTo("error message");
assertThat(comparatorsByTypeOf(assertion).getComparatorForType(String.class)).isSameAs(CaseInsensitiveStringComparator.instance);
assertThat(comparatorForElementFieldsWithTypeOf(assertion).getComparatorForType(Timestamp.class)).isSameAs(ALWAYS_EQUALS_TIMESTAMP);
assertThat(comparatorForElementFieldsWithNamesOf(assertion).get("foo")).isSameAs(ALWAYS_EQUALS_STRING);
}
private static SortedSet<FluentJedi> newSortedSet(FluentJedi... jedis) {
TreeSet<FluentJedi> jediSortedSet = new TreeSet<>(comparing(FluentJedi::age));
for (FluentJedi cartoonCharacter : jedis) {
jediSortedSet.add(cartoonCharacter);
}
return jediSortedSet;
}
}