Skip to content

Commit 47bb45a

Browse files
committed
test: nullptr class, nullable returns
1 parent 358586c commit 47bb45a

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/test/java/toolbox/Main.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.Test;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
56
import static org.junit.jupiter.api.Assertions.assertThrows;
67

78

@@ -14,6 +15,7 @@ public class Main {
1415
static native String concat(String a, String b);
1516
static native String[] to_vec(String a, String b, String c);
1617
static native boolean maybe(String optional);
18+
static native String optional(boolean present);
1719
static native String raw();
1820

1921
@Test
@@ -29,9 +31,9 @@ public void argumentsByReference() {
2931
@Test
3032
public void checksForNull() {
3133
// TODO maybe these should throw NullPtrException
32-
assertThrows(RuntimeException.class, () -> Main.concat("a", null));
33-
assertThrows(RuntimeException.class, () -> Main.concat(null, "a"));
34-
assertThrows(RuntimeException.class, () -> Main.concat(null, null));
34+
assertThrows(NullPointerException.class, () -> Main.concat("a", null));
35+
assertThrows(NullPointerException.class, () -> Main.concat(null, "a"));
36+
assertThrows(NullPointerException.class, () -> Main.concat(null, null));
3537
}
3638

3739
@Test
@@ -53,5 +55,11 @@ public void optional() {
5355
public void passEnv() {
5456
assertEquals(Main.raw(), "hello world!");
5557
}
58+
59+
@Test
60+
public void nullableReturn() {
61+
assertNull(Main.optional(false));
62+
assertEquals(Main.optional(true), "hello world!");
63+
}
5664

5765
}

src/test/test.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ fn maybe(idk: Option<String>) -> bool {
2020
idk.is_some()
2121
}
2222

23+
#[jni(package = "toolbox", class = "Main")]
24+
fn optional(present: bool) -> Option<String> {
25+
if present {
26+
Some("hello world!".into())
27+
} else {
28+
None
29+
}
30+
}
31+
2332
#[jni(package = "toolbox", class = "Main")]
2433
fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> {
2534
env.new_string("hello world!")

0 commit comments

Comments
 (0)