test: nullptr class, nullable returns

This commit is contained in:
əlemi 2024-09-24 05:02:11 +02:00
parent 358586c513
commit 47bb45a387
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 20 additions and 3 deletions

View file

@ -2,6 +2,7 @@ package toolbox;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
@ -14,6 +15,7 @@ public class Main {
static native String concat(String a, String b); static native String concat(String a, String b);
static native String[] to_vec(String a, String b, String c); static native String[] to_vec(String a, String b, String c);
static native boolean maybe(String optional); static native boolean maybe(String optional);
static native String optional(boolean present);
static native String raw(); static native String raw();
@Test @Test
@ -29,9 +31,9 @@ public class Main {
@Test @Test
public void checksForNull() { public void checksForNull() {
// TODO maybe these should throw NullPtrException // TODO maybe these should throw NullPtrException
assertThrows(RuntimeException.class, () -> Main.concat("a", null)); assertThrows(NullPointerException.class, () -> Main.concat("a", null));
assertThrows(RuntimeException.class, () -> Main.concat(null, "a")); assertThrows(NullPointerException.class, () -> Main.concat(null, "a"));
assertThrows(RuntimeException.class, () -> Main.concat(null, null)); assertThrows(NullPointerException.class, () -> Main.concat(null, null));
} }
@Test @Test
@ -53,5 +55,11 @@ public class Main {
public void passEnv() { public void passEnv() {
assertEquals(Main.raw(), "hello world!"); assertEquals(Main.raw(), "hello world!");
} }
@Test
public void nullableReturn() {
assertNull(Main.optional(false));
assertEquals(Main.optional(true), "hello world!");
}
} }

View file

@ -20,6 +20,15 @@ fn maybe(idk: Option<String>) -> bool {
idk.is_some() idk.is_some()
} }
#[jni(package = "toolbox", class = "Main")]
fn optional(present: bool) -> Option<String> {
if present {
Some("hello world!".into())
} else {
None
}
}
#[jni(package = "toolbox", class = "Main")] #[jni(package = "toolbox", class = "Main")]
fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> { fn raw<'local>(env: &mut jni::JNIEnv<'local>) -> Result<jni::objects::JString<'local>, jni::errors::Error> {
env.new_string("hello world!") env.new_string("hello world!")