Skip to content

Commit 842b382

Browse files
committed
Add a test case
1 parent 062a06a commit 842b382

File tree

11 files changed

+102
-0
lines changed

11 files changed

+102
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Very basic Rust/Cargo test.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ivyLoggingLevel := UpdateLogging.Quiet
2+
3+
lazy val root = (project in file(".")).aggregate(core, native)
4+
5+
lazy val core = project
6+
.settings(
7+
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % Test,
8+
sbtJniCoreScope := Compile
9+
)
10+
.dependsOn(native % Runtime)
11+
12+
lazy val native = project
13+
.settings(nativeCompile / sourceDirectory := baseDirectory.value) // `baseDirectory`, not `sourceDirectory`
14+
.enablePlugins(JniNative)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package simplecargo
2+
3+
import com.github.sbt.jni.syntax.NativeLoader
4+
5+
class Adder(val base: Int) extends NativeLoader("adder") {
6+
@native def plus(term: Int): Int // implemented in libadder.so
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package simplecargo
2+
3+
object Main {
4+
5+
def main(args: Array[String]): Unit = {
6+
println("hello")
7+
val adder = new Adder(1)
8+
val sum = adder.plus(2)
9+
println(s"sum: $sum")
10+
}
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package simplecargo
2+
3+
import org.scalatest.flatspec._
4+
5+
class SimpleSpec extends AnyFlatSpec {
6+
7+
"Calling native methods in tests" should "work" in {
8+
assert(new Adder(12).plus(34) == 46)
9+
}
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "adder"
3+
version = "0.1.0"
4+
authors = ["John Doe <[email protected]>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
jni = "0.19"
9+
10+
[lib]
11+
crate_type = ["cdylib"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This is the interface to the JVM that we'll call the majority of our
2+
// methods on.
3+
use jni::JNIEnv;
4+
5+
// These objects are what you should use as arguments to your native
6+
// function. They carry extra lifetime information to prevent them escaping
7+
// this context and getting used after being GC'd.
8+
use jni::objects::JObject;
9+
10+
use jni::sys::jint;
11+
12+
// This keeps Rust from "mangling" the name and making it unique for this
13+
// crate.
14+
#[no_mangle]
15+
pub extern "system" fn Java_simplecargo_Adder_plus(
16+
env: JNIEnv,
17+
object: JObject,
18+
term: jint,
19+
) -> jint {
20+
let base = env.get_field(object, "base", "I").unwrap().i().unwrap();
21+
println!("Printing from rust library. base: {}", base);
22+
println!("Printing from rust library. term: {}", term);
23+
base + term
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sbt._
2+
import sbt.Keys._
3+
4+
object ScriptedHelper extends AutoPlugin {
5+
6+
override def requires = empty
7+
override def trigger = allRequirements
8+
9+
override def projectSettings = Seq(
10+
scalacOptions ++= Seq("-feature", "-deprecation"),
11+
crossScalaVersions := Seq("2.13.12", "2.12.18"),
12+
scalaVersion := crossScalaVersions.value.head
13+
)
14+
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.9.6
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ivyLoggingLevel := UpdateLogging.Quiet
2+
3+
addSbtPlugin("com.github.sbt" % "sbt-jni" % System.getProperty("plugin.version"))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
> nativeInit cargo adder
2+
> +test
3+
> +core/run

0 commit comments

Comments
 (0)