Skip to content

Commit

Permalink
Use assertThrows even in GWT/J2CL/J2KT-compatible code, `common.pri…
Browse files Browse the repository at this point in the history
…mitives` edition.

(continuing the path started in cl/675634517)

RELNOTES=n/a
PiperOrigin-RevId: 687502286
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 19, 2024
1 parent cdc2254 commit fea7a36
Show file tree
Hide file tree
Showing 36 changed files with 656 additions and 1,134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.primitives;

import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

Expand Down Expand Up @@ -170,17 +171,8 @@ public void testEnsureCapacity() {
}

public void testEnsureCapacity_fail() {
try {
Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Booleans.ensureCapacity(ARRAY_FALSE, -1, 1));
assertThrows(IllegalArgumentException.class, () -> Booleans.ensureCapacity(ARRAY_FALSE, 1, -1));
}

public void testJoin() {
Expand Down Expand Up @@ -512,11 +504,7 @@ public void testToArray_threadSafe() {

public void testToArray_withNull() {
List<@Nullable Boolean> list = Arrays.asList(false, true, null);
try {
Booleans.toArray(list);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> Booleans.toArray(list));
}

@SuppressWarnings({"CollectionIsEmptyTruth", "CollectionIsNotEmptyTruth"})
Expand Down Expand Up @@ -593,16 +581,8 @@ public void testAsListSet() {
List<Boolean> list = Booleans.asList(ARRAY_FALSE);
assertThat(list.set(0, true)).isFalse();
assertThat(list.set(0, false)).isTrue();
try {
list.set(0, null);
fail();
} catch (NullPointerException expected) {
}
try {
list.set(1, true);
fail();
} catch (IndexOutOfBoundsException expected) {
}
assertThrows(NullPointerException.class, () -> list.set(0, null));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(1, true));
}

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

package com.google.common.primitives;

import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -171,17 +172,8 @@ public void testEnsureCapacity() {
}

public void testEnsureCapacity_fail() {
try {
Bytes.ensureCapacity(ARRAY1, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Bytes.ensureCapacity(ARRAY1, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Bytes.ensureCapacity(ARRAY1, -1, 1));
assertThrows(IllegalArgumentException.class, () -> Bytes.ensureCapacity(ARRAY1, 1, -1));
}

public void testToArray() {
Expand Down Expand Up @@ -217,11 +209,7 @@ public void testToArray_threadSafe() {

public void testToArray_withNull() {
List<@Nullable Byte> list = Arrays.asList((byte) 0, (byte) 1, null);
try {
Bytes.toArray(list);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> Bytes.toArray(list));
}

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

import static com.google.common.primitives.Chars.max;
import static com.google.common.primitives.Chars.min;
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

Expand Down Expand Up @@ -169,11 +170,7 @@ public void testLastIndexOf() {
}

public void testMax_noArgs() {
try {
max();
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> max());
}

public void testMax() {
Expand All @@ -184,11 +181,7 @@ public void testMax() {
}

public void testMin_noArgs() {
try {
min();
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> min());
}

public void testMin() {
Expand All @@ -204,11 +197,8 @@ public void testConstrainToRange() {
assertThat(Chars.constrainToRange((char) 1, (char) 3, (char) 5)).isEqualTo((char) 3);
assertThat(Chars.constrainToRange((char) 255, (char) 250, (char) 254)).isEqualTo((char) 254);
assertThat(Chars.constrainToRange((char) 5, (char) 2, (char) 2)).isEqualTo((char) 2);
try {
Chars.constrainToRange((char) 1, (char) 3, (char) 2);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class, () -> Chars.constrainToRange((char) 1, (char) 3, (char) 2));
}

public void testConcat() {
Expand Down Expand Up @@ -263,11 +253,8 @@ public void testFromByteArray() {

@GwtIncompatible // Chars.fromByteArray
public void testFromByteArrayFails() {
try {
Chars.fromByteArray(new byte[Chars.BYTES - 1]);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class, () -> Chars.fromByteArray(new byte[Chars.BYTES - 1]));
}

@GwtIncompatible // Chars.fromBytes
Expand Down Expand Up @@ -305,11 +292,7 @@ public void testByteArrayRoundTrips() {

@GwtIncompatible // Chars.fromByteArray, Chars.toByteArray
public void testByteArrayRoundTripsFails() {
try {
Chars.fromByteArray(new byte[] {0x11});
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Chars.fromByteArray(new byte[] {0x11}));
}

public void testEnsureCapacity() {
Expand All @@ -321,17 +304,8 @@ public void testEnsureCapacity() {
}

public void testEnsureCapacity_fail() {
try {
Chars.ensureCapacity(ARRAY1, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Chars.ensureCapacity(ARRAY1, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Chars.ensureCapacity(ARRAY1, -1, 1));
assertThrows(IllegalArgumentException.class, () -> Chars.ensureCapacity(ARRAY1, 1, -1));
}

public void testJoin() {
Expand Down Expand Up @@ -654,11 +628,7 @@ public void testToArray_threadSafe() {

public void testToArray_withNull() {
List<@Nullable Character> list = Arrays.asList((char) 0, (char) 1, null);
try {
Chars.toArray(list);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> Chars.toArray(list));
}

@J2ktIncompatible // b/285319375
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.primitives.Doubles.max;
import static com.google.common.primitives.Doubles.min;
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.lang.Double.NaN;
Expand Down Expand Up @@ -211,11 +212,7 @@ public void testLastIndexOf() {

@GwtIncompatible
public void testMax_noArgs() {
try {
max();
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> max());
}

public void testMax() {
Expand All @@ -233,11 +230,7 @@ public void testMax() {

@GwtIncompatible
public void testMin_noArgs() {
try {
min();
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> min());
}

public void testMin() {
Expand All @@ -260,11 +253,9 @@ public void testConstrainToRange() {
assertThat(Doubles.constrainToRange((double) 0, (double) -5, (double) -1))
.isEqualTo((double) -1);
assertThat(Doubles.constrainToRange((double) 5, (double) 2, (double) 2)).isEqualTo((double) 2);
try {
Doubles.constrainToRange((double) 1, (double) 3, (double) 2);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(
IllegalArgumentException.class,
() -> Doubles.constrainToRange((double) 1, (double) 3, (double) 2));
}

public void testConcat() {
Expand Down Expand Up @@ -323,17 +314,8 @@ public void testEnsureCapacity() {
}

public void testEnsureCapacity_fail() {
try {
Doubles.ensureCapacity(ARRAY1, -1, 1);
fail();
} catch (IllegalArgumentException expected) {
}
try {
// notice that this should even fail when no growth was needed
Doubles.ensureCapacity(ARRAY1, 1, -1);
fail();
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> Doubles.ensureCapacity(ARRAY1, -1, 1));
assertThrows(IllegalArgumentException.class, () -> Doubles.ensureCapacity(ARRAY1, 1, -1));
}

@GwtIncompatible // Double.toString returns different value in GWT.
Expand Down Expand Up @@ -580,11 +562,7 @@ public void testToArray_threadSafe() {

public void testToArray_withNull() {
List<@Nullable Double> list = Arrays.asList((double) 0, (double) 1, null);
try {
Doubles.toArray(list);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> Doubles.toArray(list));
}

public void testToArray_withConversion() {
Expand Down Expand Up @@ -791,11 +769,8 @@ public void testStringConverter_convert() {
}

public void testStringConverter_convertError() {
try {
Doubles.stringConverter().convert("notanumber");
fail();
} catch (NumberFormatException expected) {
}
assertThrows(
NumberFormatException.class, () -> Doubles.stringConverter().convert("notanumber"));
}

public void testStringConverter_nullConversions() {
Expand Down Expand Up @@ -823,10 +798,6 @@ public void testStringConverter_nullPointerTester() throws Exception {
@GwtIncompatible
public void testTryParse_withNullNoGwt() {
assertThat(Doubles.tryParse("null")).isNull();
try {
Doubles.tryParse(null);
fail("Expected NPE");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> Doubles.tryParse(null));
}
}
Loading

0 comments on commit fea7a36

Please sign in to comment.