test: vecs, options, nulls, also build with gradle

This commit is contained in:
əlemi 2024-09-24 02:29:31 +02:00
parent 218a816c2d
commit 3d3bc8b6d5
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 43 additions and 1 deletions

View file

@ -27,5 +27,4 @@ jobs:
- run: cargo build --verbose - run: cargo build --verbose
- run: cargo test --verbose - run: cargo test --verbose
- uses: gradle/actions/setup-gradle@v4 - uses: gradle/actions/setup-gradle@v4
- run: cargo build -p jni-toolbox-test
- run: gradle test - run: gradle test

View file

@ -11,7 +11,14 @@ repositories {
mavenCentral() mavenCentral()
} }
task cargoBuild(type: Exec) {
workingDir '.'
commandLine 'cargo', 'build', '-p', 'jni-toolbox-test'
}
test { test {
dependsOn cargoBuild
outputs.upToDateWhen { false }
useJUnitPlatform() useJUnitPlatform()
systemProperty 'java.library.path','target/debug' systemProperty 'java.library.path','target/debug'
} }

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.assertThrows;
public class Main { public class Main {
@ -11,6 +12,8 @@ public class Main {
static native int sum(int a, int b); static native int sum(int a, int b);
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 boolean maybe(String optional);
@Test @Test
public void argumentsByValue() { public void argumentsByValue() {
@ -21,5 +24,28 @@ public class Main {
public void argumentsByReference() { public void argumentsByReference() {
assertEquals(Main.concat("hello", "world"), "hello -- world"); assertEquals(Main.concat("hello", "world"), "hello -- world");
} }
@Test
public void checksForNull() {
// TODO maybe these should throw NullPtrException
assertThrows(RuntimeException.class, () -> Main.concat("a", null));
assertThrows(RuntimeException.class, () -> Main.concat(null, "a"));
assertThrows(RuntimeException.class, () -> Main.concat(null, null));
}
@Test
public void returnVec() {
String[] actual = new String[]{"a", "b", "c"};
String[] from_rust = Main.to_vec("a", "b", "c");
for (int i = 0; i < 3; i++) {
assertEquals(actual[i], from_rust[i]);
}
}
@Test
public void optional() {
assertEquals(Main.maybe(null), false);
assertEquals(Main.maybe("aa"), true);
}
} }

View file

@ -9,3 +9,13 @@ fn sum(a: i32, b: i32) -> i32 {
fn concat(a: String, b: String) -> String { fn concat(a: String, b: String) -> String {
format!("{a} -- {b}") format!("{a} -- {b}")
} }
#[jni(package = "toolbox", class = "Main")]
fn to_vec(a: String, b: String, c: String) -> Vec<String> {
vec![a, b, c]
}
#[jni(package = "toolbox", class = "Main")]
fn maybe(idk: Option<String>) -> bool {
idk.is_some()
}