Skip to content

Commit 1c6fdb8

Browse files
committed
Make our own assertThrows()
1 parent 9309f12 commit 1c6fdb8

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/test/java/org/truffleruby/ArrayStoreTest.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertFalse;
1414
import static org.junit.Assert.assertTrue;
15-
import static org.junit.Assert.fail;
15+
import static org.truffleruby.RubyTest.assertThrows;
1616

1717
import org.junit.Test;
1818
import org.truffleruby.core.array.library.ArrayStoreLibrary;
@@ -32,12 +32,9 @@ public void delegatedArrayLibraryOnlyAcceptsSpecificStore() {
3232
assertFalse(library.accepts(longDelegated));
3333

3434
assertEquals(42, library.read(intDelegated, 0));
35-
try {
36-
library.read(longDelegated, 0);
37-
fail();
38-
} catch (AssertionError e) {
39-
assertTrue(e.getMessage().contains("Library does not accept given receiver"));
40-
}
35+
36+
var e = assertThrows(() -> library.read(longDelegated, 0), AssertionError.class);
37+
assertTrue(e.getMessage().contains("Library does not accept given receiver"));
4138
}
4239

4340
@Test
@@ -51,12 +48,9 @@ public void sharedArrayLibraryOnlyAcceptsSpecificStore() {
5148
assertFalse(library.accepts(longShared));
5249

5350
assertEquals(42, library.read(intShared, 0));
54-
try {
55-
library.read(longShared, 0);
56-
fail();
57-
} catch (AssertionError e) {
58-
assertTrue(e.getMessage().contains("Library does not accept given receiver"));
59-
}
51+
52+
var e = assertThrows(() -> library.read(longShared, 0), AssertionError.class);
53+
assertTrue(e.getMessage().contains("Library does not accept given receiver"));
6054
}
6155

6256
@Test

src/test/java/org/truffleruby/RubyTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@
3434
public abstract class RubyTest {
3535

3636
public static void assertThrows(Runnable test, Consumer<PolyglotException> exceptionVerifier) {
37+
PolyglotException e = assertThrows(test, PolyglotException.class);
38+
assertTrue(e.isGuestException());
39+
exceptionVerifier.accept(e);
40+
}
41+
42+
public static <E> E assertThrows(Runnable test, Class<E> exceptionClass) {
3743
try {
3844
test.run();
39-
fail("should have thrown");
40-
} catch (PolyglotException e) {
41-
assertTrue(e.isGuestException());
42-
exceptionVerifier.accept(e);
45+
} catch (Throwable e) {
46+
if (!exceptionClass.isInstance(e)) {
47+
throw e;
48+
}
49+
return exceptionClass.cast(e);
4350
}
51+
52+
fail("should have thrown");
53+
throw new Error("unreachable");
4454
}
4555

4656
public static <T extends Node> T adopt(T node) {

0 commit comments

Comments
 (0)