Skip to content

Commit

Permalink
Convert much of Guava to Truth by using Refaster.
Browse files Browse the repository at this point in the history
The motivation was my wish for a better ThrowablesTest error message in #2130 (though I'm pretty confident that the problem there is \n vs. \r\n).
I made a lot of changes, but I left a lot undone. In particular, I avoided most of the collection assertions, since we often want to test specific collection methods. e.g., we don't necessarily want get() to be rewritten to containsKey().
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=101020845
  • Loading branch information
cpovirk committed Aug 19, 2015
1 parent c94fe82 commit 1f2b877
Show file tree
Hide file tree
Showing 74 changed files with 449 additions and 465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.base;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
import com.google.common.testing.SerializableTester;

Expand All @@ -41,27 +43,27 @@ private enum TestEnum {
private enum OtherEnum {}

public void testGetIfPresent() {
assertEquals(Optional.of(TestEnum.CHEETO), Enums.getIfPresent(TestEnum.class, "CHEETO"));
assertEquals(Optional.of(TestEnum.HONDA), Enums.getIfPresent(TestEnum.class, "HONDA"));
assertEquals(Optional.of(TestEnum.POODLE), Enums.getIfPresent(TestEnum.class, "POODLE"));
assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO);
assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA);
assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).hasValue(TestEnum.POODLE);

assertTrue(Enums.getIfPresent(TestEnum.class, "CHEETO").isPresent());
assertTrue(Enums.getIfPresent(TestEnum.class, "HONDA").isPresent());
assertTrue(Enums.getIfPresent(TestEnum.class, "POODLE").isPresent());
assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).isPresent();
assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).isPresent();
assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).isPresent();

assertEquals(TestEnum.CHEETO, Enums.getIfPresent(TestEnum.class, "CHEETO").get());
assertEquals(TestEnum.HONDA, Enums.getIfPresent(TestEnum.class, "HONDA").get());
assertEquals(TestEnum.POODLE, Enums.getIfPresent(TestEnum.class, "POODLE").get());
assertThat(Enums.getIfPresent(TestEnum.class, "CHEETO")).hasValue(TestEnum.CHEETO);
assertThat(Enums.getIfPresent(TestEnum.class, "HONDA")).hasValue(TestEnum.HONDA);
assertThat(Enums.getIfPresent(TestEnum.class, "POODLE")).hasValue(TestEnum.POODLE);
}

public void testGetIfPresent_caseSensitive() {
assertFalse(Enums.getIfPresent(TestEnum.class, "cHEETO").isPresent());
assertFalse(Enums.getIfPresent(TestEnum.class, "Honda").isPresent());
assertFalse(Enums.getIfPresent(TestEnum.class, "poodlE").isPresent());
assertThat(Enums.getIfPresent(TestEnum.class, "cHEETO")).isAbsent();
assertThat(Enums.getIfPresent(TestEnum.class, "Honda")).isAbsent();
assertThat(Enums.getIfPresent(TestEnum.class, "poodlE")).isAbsent();
}

public void testGetIfPresent_whenNoMatchingConstant() {
assertEquals(Optional.absent(), Enums.getIfPresent(TestEnum.class, "WOMBAT"));
assertThat(Enums.getIfPresent(TestEnum.class, "WOMBAT")).isAbsent();
}

// Create a second ClassLoader and use it to get a second version of the TestEnum class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.common.base;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;

import junit.framework.AssertionFailedError;
Expand Down Expand Up @@ -59,7 +61,7 @@ public void testCheckArgument_nullMessage_failure() {
Preconditions.checkArgument(false, null);
fail("no exception thrown");
} catch (IllegalArgumentException expected) {
assertEquals("null", expected.getMessage());
assertThat(expected).hasMessage("null");
}
}

Expand Down Expand Up @@ -106,7 +108,7 @@ public void testCheckState_nullMessage_failure() {
Preconditions.checkState(false, null);
fail("no exception thrown");
} catch (IllegalStateException expected) {
assertEquals("null", expected.getMessage());
assertThat(expected).hasMessage("null");
}
}

Expand Down Expand Up @@ -188,7 +190,7 @@ public void testCheckElementIndex_negative() {
Preconditions.checkElementIndex(-1, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("index (-1) must not be negative", expected.getMessage());
assertThat(expected).hasMessage("index (-1) must not be negative");
}
}

Expand All @@ -197,8 +199,7 @@ public void testCheckElementIndex_tooHigh() {
Preconditions.checkElementIndex(1, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("index (1) must be less than size (1)",
expected.getMessage());
assertThat(expected).hasMessage("index (1) must be less than size (1)");
}
}

Expand All @@ -207,7 +208,7 @@ public void testCheckElementIndex_withDesc_negative() {
Preconditions.checkElementIndex(-1, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("foo (-1) must not be negative", expected.getMessage());
assertThat(expected).hasMessage("foo (-1) must not be negative");
}
}

Expand All @@ -216,8 +217,7 @@ public void testCheckElementIndex_withDesc_tooHigh() {
Preconditions.checkElementIndex(1, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("foo (1) must be less than size (1)",
expected.getMessage());
assertThat(expected).hasMessage("foo (1) must be less than size (1)");
}
}

Expand All @@ -242,7 +242,7 @@ public void testCheckPositionIndex_negative() {
Preconditions.checkPositionIndex(-1, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("index (-1) must not be negative", expected.getMessage());
assertThat(expected).hasMessage("index (-1) must not be negative");
}
}

Expand All @@ -251,8 +251,7 @@ public void testCheckPositionIndex_tooHigh() {
Preconditions.checkPositionIndex(2, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("index (2) must not be greater than size (1)",
expected.getMessage());
assertThat(expected).hasMessage("index (2) must not be greater than size (1)");
}
}

Expand All @@ -261,7 +260,7 @@ public void testCheckPositionIndex_withDesc_negative() {
Preconditions.checkPositionIndex(-1, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("foo (-1) must not be negative", expected.getMessage());
assertThat(expected).hasMessage("foo (-1) must not be negative");
}
}

Expand All @@ -270,8 +269,7 @@ public void testCheckPositionIndex_withDesc_tooHigh() {
Preconditions.checkPositionIndex(2, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("foo (2) must not be greater than size (1)",
expected.getMessage());
assertThat(expected).hasMessage("foo (2) must not be greater than size (1)");
}
}

Expand All @@ -295,8 +293,7 @@ public void testCheckPositionIndex_startNegative() {
Preconditions.checkPositionIndexes(-1, 1, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("start index (-1) must not be negative",
expected.getMessage());
assertThat(expected).hasMessage("start index (-1) must not be negative");
}
}

Expand All @@ -305,8 +302,7 @@ public void testCheckPositionIndexes_endTooHigh() {
Preconditions.checkPositionIndexes(0, 2, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("end index (2) must not be greater than size (1)",
expected.getMessage());
assertThat(expected).hasMessage("end index (2) must not be greater than size (1)");
}
}

Expand All @@ -315,8 +311,7 @@ public void testCheckPositionIndexes_reversed() {
Preconditions.checkPositionIndexes(1, 0, 1);
fail();
} catch (IndexOutOfBoundsException expected) {
assertEquals("end index (0) must not be less than start index (1)",
expected.getMessage());
assertThat(expected).hasMessage("end index (0) must not be less than start index (1)");
}
}

Expand Down Expand Up @@ -356,10 +351,10 @@ private static class Message {
private static final String FORMAT = "I ate %s pies.";

private static void verifySimpleMessage(Exception e) {
assertEquals("A message", e.getMessage());
assertThat(e).hasMessage("A message");
}

private static void verifyComplexMessage(Exception e) {
assertEquals("I ate 5 pies.", e.getMessage());
assertThat(e).hasMessage("I ate 5 pies.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.base;

import static com.google.common.truth.Truth.assertThat;
import static java.lang.Character.MAX_CODE_POINT;
import static java.lang.Character.MAX_HIGH_SURROGATE;
import static java.lang.Character.MAX_LOW_SURROGATE;
Expand Down Expand Up @@ -121,8 +122,7 @@ private static void testEncodedLengthFails(String invalidString,
Utf8.encodedLength(invalidString);
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Unpaired surrogate at index " + invalidCodePointIndex,
expected.getMessage());
assertThat(expected).hasMessage("Unpaired surrogate at index " + invalidCodePointIndex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ public void testPutIllegal() {
table.put("dog", 1, 'd');
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Row dog not in [foo, bar, cat]", expected.getMessage());
assertThat(expected).hasMessage("Row dog not in [foo, bar, cat]");
}
try {
table.put("foo", 4, 'd');
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Column 4 not in [1, 2, 3]", expected.getMessage());
assertThat(expected).hasMessage("Column 4 not in [1, 2, 3]");
}
assertFalse(table.containsValue('d'));
}
Expand Down Expand Up @@ -422,7 +422,7 @@ public void testRowPutIllegal() {
map.put(4, 'd');
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Column 4 not in [1, 2, 3]", expected.getMessage());
assertThat(expected).hasMessage("Column 4 not in [1, 2, 3]");
}
}

Expand All @@ -433,7 +433,7 @@ public void testColumnPutIllegal() {
map.put("dog", 'd');
fail();
} catch (IllegalArgumentException expected) {
assertEquals("Row dog not in [foo, bar, cat]", expected.getMessage());
assertThat(expected).hasMessage("Row dog not in [foo, bar, cat]");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public void testPuttingTheSameKeyTwiceThrowsOnBuild() {
builder.build();
fail();
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().contains("one"));
assertThat(expected.getMessage()).contains("one");
}
}

Expand Down Expand Up @@ -351,7 +351,7 @@ public void testOfWithDuplicateKey() {
ImmutableBiMap.of("one", 1, "one", 1);
fail();
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().contains("one"));
assertThat(expected.getMessage()).contains("one");
}
}

Expand Down Expand Up @@ -425,7 +425,7 @@ public void testDuplicateValues() {
ImmutableBiMap.copyOf(map);
fail();
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().contains("1"));
assertThat(expected.getMessage()).contains("1");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ public void testBuilderOrderKeysBy() {
assertThat(multimap.values()).containsExactly(2, 4, 3, 6, 5, 2).inOrder();
assertThat(multimap.get("a")).containsExactly(5, 2).inOrder();
assertThat(multimap.get("b")).containsExactly(3, 6).inOrder();
assertFalse(multimap.get("a") instanceof ImmutableSortedSet);
assertFalse(multimap.get("x") instanceof ImmutableSortedSet);
assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet);
assertThat(multimap.get("a")).isNotInstanceOf(ImmutableSortedSet.class);
assertThat(multimap.get("x")).isNotInstanceOf(ImmutableSortedSet.class);
assertThat(multimap.asMap().get("a")).isNotInstanceOf(ImmutableSortedSet.class);
}

public void testBuilderOrderKeysByDuplicates() {
Expand All @@ -265,9 +265,9 @@ public int compare(String left, String right) {
assertThat(multimap.values()).containsExactly(2, 5, 2, 3, 6, 4).inOrder();
assertThat(multimap.get("a")).containsExactly(5, 2).inOrder();
assertThat(multimap.get("bb")).containsExactly(3, 6).inOrder();
assertFalse(multimap.get("a") instanceof ImmutableSortedSet);
assertFalse(multimap.get("x") instanceof ImmutableSortedSet);
assertFalse(multimap.asMap().get("a") instanceof ImmutableSortedSet);
assertThat(multimap.get("a")).isNotInstanceOf(ImmutableSortedSet.class);
assertThat(multimap.get("x")).isNotInstanceOf(ImmutableSortedSet.class);
assertThat(multimap.asMap().get("a")).isNotInstanceOf(ImmutableSortedSet.class);
}

public void testBuilderOrderValuesBy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.testing.IteratorTester;
Expand Down Expand Up @@ -234,14 +233,10 @@ public void testFind_withDefault() {

public void testTryFind() {
Iterable<String> list = newArrayList("cool", "pants");
assertEquals(Optional.of("cool"),
Iterables.tryFind(list, Predicates.equalTo("cool")));
assertEquals(Optional.of("pants"),
Iterables.tryFind(list, Predicates.equalTo("pants")));
assertEquals(Optional.of("cool"),
Iterables.tryFind(list, Predicates.alwaysTrue()));
assertEquals(Optional.absent(),
Iterables.tryFind(list, Predicates.alwaysFalse()));
assertThat(Iterables.tryFind(list, Predicates.equalTo("cool"))).hasValue("cool");
assertThat(Iterables.tryFind(list, Predicates.equalTo("pants"))).hasValue("pants");
assertThat(Iterables.tryFind(list, Predicates.alwaysTrue())).hasValue("cool");
assertThat(Iterables.tryFind(list, Predicates.alwaysFalse())).isAbsent();
assertCanIterateAgain(list);
}

Expand Down
Loading

0 comments on commit 1f2b877

Please sign in to comment.