aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java')
-rw-r--r--core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java319
1 files changed, 180 insertions, 139 deletions
diff --git a/core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java b/core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java
index bb075e2e..f9e3986d 100644
--- a/core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java
+++ b/core/src/test/java/com/google/common/truth/IterableSubjectCorrespondenceTest.java
@@ -32,9 +32,10 @@ import static com.google.common.truth.TestCorrespondences.STRING_PARSES_TO_INTEG
import static com.google.common.truth.TestCorrespondences.WITHIN_10_OF;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.asList;
+import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
-import com.google.common.truth.TestCorrespondences.Record;
+import com.google.common.truth.TestCorrespondences.MyRecord;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
@@ -55,6 +56,41 @@ import org.junit.runners.JUnit4;
public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
+ // test of a mistaken call
+ @SuppressWarnings({"EqualsIncompatibleType", "DoNotCall", "deprecation"})
+ public void equalsThrowsUSOE() {
+ try {
+ boolean unused =
+ assertThat(ImmutableList.of(42.0))
+ .comparingElementsUsing(tolerance(10e-5))
+ .equals(ImmutableList.of(0.0));
+ } catch (UnsupportedOperationException expected) {
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo(
+ "UsingCorrespondence.equals() is not supported. Did you mean to call"
+ + " containsExactlyElementsIn(expected) instead of equals(expected)?");
+ return;
+ }
+ fail("Should have thrown.");
+ }
+
+ @Test
+ @SuppressWarnings({"DoNotCall", "deprecation"}) // test of a mistaken call
+ public void hashCodeThrowsUSOE() {
+ try {
+ int unused =
+ assertThat(ImmutableList.of(42.0)).comparingElementsUsing(tolerance(10e-5)).hashCode();
+ } catch (UnsupportedOperationException expected) {
+ assertThat(expected)
+ .hasMessageThat()
+ .isEqualTo("UsingCorrespondence.hashCode() is not supported.");
+ return;
+ }
+ fail("Should have thrown.");
+ }
+
+ @Test
public void contains_success() {
ImmutableList<String> actual = ImmutableList.of("not a number", "+123", "+456", "+789");
assertThat(actual)
@@ -123,14 +159,14 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_contains() {
- Record expected = Record.create(2, 200);
- ImmutableList<Record> actual =
+ MyRecord expected = MyRecord.create(2, 200);
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(4, 400),
- Record.create(2, 189),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(4, 400),
+ MyRecord.create(2, 189),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -156,14 +192,14 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_contains_noDiff() {
- Record expected = Record.create(2, 200);
- ImmutableList<Record> actual =
+ MyRecord expected = MyRecord.create(2, 200);
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(4, 400),
- Record.create(2, 189),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(4, 400),
+ MyRecord.create(2, 189),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -182,8 +218,8 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_contains_handlesActualKeyerExceptions() {
- Record expected = Record.create(0, 999);
- List<Record> actual = asList(Record.create(1, 100), null, Record.create(4, 400));
+ MyRecord expected = MyRecord.create(0, 999);
+ List<MyRecord> actual = asList(MyRecord.create(1, 100), null, MyRecord.create(4, 400));
expectFailure
.whenTesting()
.that(actual)
@@ -203,8 +239,8 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_contains_handlesExpectedKeyerExceptions() {
- List<Record> actual =
- asList(Record.create(1, 100), Record.create(2, 200), Record.create(4, 400));
+ List<MyRecord> actual =
+ asList(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(4, 400));
expectFailure
.whenTesting()
.that(actual)
@@ -224,8 +260,8 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_contains_handlesFormatDiffExceptions() {
- Record expected = Record.create(0, 999);
- List<Record> actual = asList(Record.create(1, 100), null, Record.create(4, 400));
+ MyRecord expected = MyRecord.create(0, 999);
+ List<MyRecord> actual = asList(MyRecord.create(1, 100), null, MyRecord.create(4, 400));
expectFailure
.whenTesting()
.that(actual)
@@ -622,18 +658,18 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_1arg_containsExactlyElementsIn() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(900));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(900));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(4, 400),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(4, 400),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -666,12 +702,12 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_2arg_containsExactlyElementsIn() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(900));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(900));
ImmutableList<String> actual = ImmutableList.of("1/100", "2/211", "4/400", "none/999");
expectFailure
.whenTesting()
@@ -705,18 +741,18 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_onlyKeyed() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(999));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(3, 303),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(3, 303),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -741,18 +777,18 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_noKeyed() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(900));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(900));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 201),
- Record.create(4, 400),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 201),
+ MyRecord.create(4, 400),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -773,18 +809,18 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_noDiffs() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(999));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(3, 303),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(3, 303),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -816,19 +852,19 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
public void displayingDiffsPairedBy_containsExactlyElementsIn_notUnique() {
// The missing elements here are not uniquely keyed by the key function, so the key function
// should be ignored, but a warning about this should be appended to the failure message.
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.create(3, 301),
- Record.createWithoutId(900));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.create(3, 301),
+ MyRecord.createWithoutId(900));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 211),
- Record.create(4, 400),
- Record.createWithoutId(999));
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 211),
+ MyRecord.create(4, 400),
+ MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -850,9 +886,9 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_handlesActualKeyerExceptions() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.create(4, 400));
- List<Record> actual = asList(Record.create(1, 101), Record.create(2, 211), null);
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(4, 400));
+ List<MyRecord> actual = asList(MyRecord.create(1, 101), MyRecord.create(2, 211), null);
expectFailure
.whenTesting()
.that(actual)
@@ -882,9 +918,9 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_handlesExpectedKeyerExceptions() {
- List<Record> expected = asList(Record.create(1, 100), Record.create(2, 200), null);
- List<Record> actual =
- asList(Record.create(1, 101), Record.create(2, 211), Record.create(4, 400));
+ List<MyRecord> expected = asList(MyRecord.create(1, 100), MyRecord.create(2, 200), null);
+ List<MyRecord> actual =
+ asList(MyRecord.create(1, 101), MyRecord.create(2, 211), MyRecord.create(4, 400));
expectFailure
.whenTesting()
.that(actual)
@@ -914,9 +950,9 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsExactlyElementsIn_handlesFormatDiffExceptions() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.create(0, 999));
- List<Record> actual = asList(Record.create(1, 101), Record.create(2, 211), null);
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(0, 999));
+ List<MyRecord> actual = asList(MyRecord.create(1, 101), MyRecord.create(2, 211), null);
expectFailure
.whenTesting()
.that(actual)
@@ -1275,15 +1311,16 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingElementsPairedBy_containsAtLeastElementsIn() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.createWithoutId(999));
- ImmutableList<Record> actual =
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(
+ MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 101),
- Record.create(2, 211),
- Record.create(2, 222),
- Record.create(3, 303),
- Record.createWithoutId(888));
+ MyRecord.create(1, 101),
+ MyRecord.create(2, 211),
+ MyRecord.create(2, 222),
+ MyRecord.create(3, 303),
+ MyRecord.createWithoutId(888));
expectFailure
.whenTesting()
.that(actual)
@@ -1318,14 +1355,15 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingElementsPairedBy_containsAtLeastElementsIn_notUnique() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(2, 201),
+ MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(2, 201),
- Record.createWithoutId(999));
- ImmutableList<Record> actual =
- ImmutableList.of(Record.create(1, 101), Record.create(3, 303), Record.createWithoutId(999));
+ MyRecord.create(1, 101), MyRecord.create(3, 303), MyRecord.createWithoutId(999));
expectFailure
.whenTesting()
.that(actual)
@@ -1345,10 +1383,10 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingElementsPairedBy_containsAtLeastElementsIn_handlesFormatDiffExceptions() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.create(0, 999));
- List<Record> actual =
- asList(Record.create(1, 101), Record.create(2, 211), Record.create(3, 303), null);
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(0, 999));
+ List<MyRecord> actual =
+ asList(MyRecord.create(1, 101), MyRecord.create(2, 211), MyRecord.create(3, 303), null);
expectFailure
.whenTesting()
.that(actual)
@@ -1647,19 +1685,19 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsAnyIn_withKeyMatches() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(3, 300),
- Record.createWithoutId(999));
- ImmutableList<Record> actual =
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(3, 300),
+ MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(3, 311),
- Record.create(2, 211),
- Record.create(2, 222),
- Record.create(4, 404),
- Record.createWithoutId(888));
+ MyRecord.create(3, 311),
+ MyRecord.create(2, 211),
+ MyRecord.create(2, 222),
+ MyRecord.create(4, 404),
+ MyRecord.createWithoutId(888));
expectFailure
.whenTesting()
.that(actual)
@@ -1700,10 +1738,12 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsAnyIn_withoutKeyMatches() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.createWithoutId(999));
- ImmutableList<Record> actual =
- ImmutableList.of(Record.create(3, 300), Record.create(4, 411), Record.createWithoutId(888));
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(
+ MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
+ ImmutableList.of(
+ MyRecord.create(3, 300), MyRecord.create(4, 411), MyRecord.createWithoutId(888));
expectFailure
.whenTesting()
.that(actual)
@@ -1719,14 +1759,15 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsAnyIn_notUnique() {
- ImmutableList<Record> expected =
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 200),
+ MyRecord.create(2, 250),
+ MyRecord.createWithoutId(999));
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(1, 100),
- Record.create(2, 200),
- Record.create(2, 250),
- Record.createWithoutId(999));
- ImmutableList<Record> actual =
- ImmutableList.of(Record.create(3, 300), Record.create(2, 211), Record.createWithoutId(888));
+ MyRecord.create(3, 300), MyRecord.create(2, 211), MyRecord.createWithoutId(888));
expectFailure
.whenTesting()
.that(actual)
@@ -1743,9 +1784,9 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void displayingDiffsPairedBy_containsAnyIn_handlesFormatDiffExceptions() {
- ImmutableList<Record> expected =
- ImmutableList.of(Record.create(1, 100), Record.create(2, 200), Record.create(0, 999));
- List<Record> actual = asList(Record.create(3, 311), Record.create(4, 404), null);
+ ImmutableList<MyRecord> expected =
+ ImmutableList.of(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(0, 999));
+ List<MyRecord> actual = asList(MyRecord.create(3, 311), MyRecord.create(4, 404), null);
expectFailure
.whenTesting()
.that(actual)
@@ -1993,28 +2034,28 @@ public class IterableSubjectCorrespondenceTest extends BaseSubjectTestCase {
@Test
public void formattingDiffsUsing_success() {
- ImmutableList<Record> actual =
- ImmutableList.of(Record.create(3, 300), Record.create(2, 200), Record.create(1, 100));
+ ImmutableList<MyRecord> actual =
+ ImmutableList.of(MyRecord.create(3, 300), MyRecord.create(2, 200), MyRecord.create(1, 100));
assertThat(actual)
.formattingDiffsUsing(RECORD_DIFF_FORMATTER)
.displayingDiffsPairedBy(RECORD_ID)
- .containsExactly(Record.create(1, 100), Record.create(2, 200), Record.create(3, 300));
+ .containsExactly(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(3, 300));
}
@Test
public void formattingDiffsUsing_failure() {
- ImmutableList<Record> actual =
+ ImmutableList<MyRecord> actual =
ImmutableList.of(
- Record.create(3, 300),
- Record.create(2, 201),
- Record.create(1, 100),
- Record.create(2, 199));
+ MyRecord.create(3, 300),
+ MyRecord.create(2, 201),
+ MyRecord.create(1, 100),
+ MyRecord.create(2, 199));
expectFailure
.whenTesting()
.that(actual)
.formattingDiffsUsing(RECORD_DIFF_FORMATTER)
.displayingDiffsPairedBy(RECORD_ID)
- .containsExactly(Record.create(1, 100), Record.create(2, 200), Record.create(3, 300));
+ .containsExactly(MyRecord.create(1, 100), MyRecord.create(2, 200), MyRecord.create(3, 300));
assertFailureKeys(
"for key",
"missing",