|
20 | 20 | import static java.util.Arrays.asList;
|
21 | 21 | import static org.junit.Assert.fail;
|
22 | 22 |
|
| 23 | +import com.google.common.base.Function; |
23 | 24 | import com.google.common.collect.ImmutableList;
|
| 25 | +import org.checkerframework.checker.nullness.compatqual.NullableDecl; |
24 | 26 | import org.junit.Test;
|
25 | 27 | import org.junit.runner.RunWith;
|
26 | 28 | import org.junit.runners.JUnit4;
|
@@ -135,6 +137,97 @@ public void testFrom_viaIterableSubjectContainsExactly_null() {
|
135 | 137 | .startsWith("compare(null, foot) threw java.lang.NullPointerException");
|
136 | 138 | }
|
137 | 139 |
|
| 140 | + // Tests of the 'transform' factory methods. |
| 141 | + |
| 142 | + private static final Correspondence<String, Integer> LENGTHS = |
| 143 | + Correspondence.transforming( |
| 144 | + new Function<String, Integer>() { |
| 145 | + @Override |
| 146 | + @NullableDecl |
| 147 | + public Integer apply(String str) { |
| 148 | + return str.length(); |
| 149 | + } |
| 150 | + }, |
| 151 | + "has a length of"); |
| 152 | + |
| 153 | + private static final Correspondence<String, Integer> HYPHEN_INDEXES = |
| 154 | + Correspondence.transforming( |
| 155 | + new Function<String, Integer>() { |
| 156 | + @Override |
| 157 | + @NullableDecl |
| 158 | + public Integer apply(String str) { |
| 159 | + int index = str.indexOf('-'); |
| 160 | + return (index >= 0) ? index : null; |
| 161 | + } |
| 162 | + }, |
| 163 | + "has a hyphen at an index of"); |
| 164 | + |
| 165 | + @Test |
| 166 | + public void testTransforming_actual_compare() { |
| 167 | + assertThat(LENGTHS.compare("foo", 3)).isTrue(); |
| 168 | + assertThat(LENGTHS.compare("foot", 4)).isTrue(); |
| 169 | + assertThat(LENGTHS.compare("foo", 4)).isFalse(); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void testTransforming_actual_formatDiff() { |
| 174 | + assertThat(LENGTHS.formatDiff("foo", 4)).isNull(); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testTransforming_actual_toString() { |
| 179 | + assertThat(LENGTHS.toString()).isEqualTo("has a length of"); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testTransforming_actual_viaIterableSubjectContainsExactly_success() { |
| 184 | + assertThat(ImmutableList.of("feet", "barns", "gallons")) |
| 185 | + .comparingElementsUsing(LENGTHS) |
| 186 | + .containsExactly(4, 5, 7) |
| 187 | + .inOrder(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void testTransforming_actual_viaIterableSubjectContainsExactly_failure() { |
| 192 | + expectFailure |
| 193 | + .whenTesting() |
| 194 | + .that(ImmutableList.of("feet", "barns", "gallons")) |
| 195 | + .comparingElementsUsing(LENGTHS) |
| 196 | + .containsExactly(4, 5); |
| 197 | + assertThat(expectFailure.getFailure()) |
| 198 | + .hasMessageThat() |
| 199 | + .isEqualTo( |
| 200 | + "Not true that <[feet, barns, gallons]> contains exactly one element that has a length " |
| 201 | + + "of each element of <[4, 5]>. It has unexpected elements <[gallons]>"); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void testTransforming_actual_viaIterableSubjectContainsExactly_nullActual() { |
| 206 | + expectFailure |
| 207 | + .whenTesting() |
| 208 | + .that(asList("feet", "barns", null)) |
| 209 | + .comparingElementsUsing(LENGTHS) |
| 210 | + .containsExactly(4, 5); |
| 211 | + assertFailureKeys( |
| 212 | + "Not true that <[feet, barns, null]> contains exactly one element that has a length of " |
| 213 | + + "each element of <[4, 5]>. It has unexpected elements <[null]>", |
| 214 | + "additionally, one or more exceptions were thrown while comparing elements", |
| 215 | + "first exception"); |
| 216 | + assertThatFailure() |
| 217 | + .factValue("first exception") |
| 218 | + .startsWith("compare(null, 4) threw java.lang.NullPointerException"); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + public void testTransforming_actual_viaIterableSubjectContainsExactly_nullTransformed() { |
| 223 | + // "mailing-list" and "chat-room" have hyphens at index 7 and 4 respectively. |
| 224 | + // "forum" contains no hyphen so the Function in HYPHEN_INDEXES transforms it to null. |
| 225 | + assertThat(ImmutableList.of("mailing-list", "chat-room", "forum")) |
| 226 | + .comparingElementsUsing(HYPHEN_INDEXES) |
| 227 | + .containsExactly(7, 4, null) |
| 228 | + .inOrder(); |
| 229 | + } |
| 230 | + |
138 | 231 | // Tests of the 'tolerance' factory method. Includes both direct tests of the compare method and
|
139 | 232 | // indirect tests using it in a basic call chain.
|
140 | 233 |
|
|
0 commit comments