Skip to content

Commit

Permalink
cleanup: update rust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Mar 22, 2024
1 parent 2cea9bc commit 5309d64
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 30 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ jobs:
crate: mdbook

- name: Build
run: opam exec -- cargo build --tests --features=link
run: opam exec -- cargo build --example rust --features=link

- name: Build build
run: opam exec -- cargo build --package ocaml-build --features=dune

- name: Run Rust tests
run: opam exec -- cargo test --features=link -- --test-threads=1
run: opam exec -- cargo run --example rust --features=link

- name: Check mdbook
run: mdbook test doc -L ./target/debug/deps

- name: Test `no_std`
run: opam exec -- cargo build --tests --features=no-std
run: opam exec -- cargo build --example rust --features=no-std,link
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ members = [

[dev-dependencies]
serial_test = "3.0.0"

[[example]]
name = "rust"
required-features = ["link"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test: test-rust test-ocaml

test-rust:
@cargo test --features=link -- --test-threads=1
@cargo run --example rust --features=link

test-ocaml:
@dune clean --root=test
Expand Down
32 changes: 10 additions & 22 deletions src/tests/mod.rs → examples/rust.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
use crate as ocaml;
use ocaml::{Error, FromValue, ToValue, Value};

use crate::{Error, FromValue, ToValue, Value};

#[cfg(test)]
use serial_test::serial;

#[test]
#[serial]
fn test_basic_array() -> Result<(), Error> {
ocaml::runtime::init_persistent();
ocaml::runtime::init();
let gc = unsafe { ocaml::Runtime::recover_handle() };
unsafe {
let mut a: ocaml::Array<&str> = ocaml::Array::alloc(2);
a.set(gc, 0, &"testing")?;
a.set(gc, 1, &"123")?;
let b: Vec<&str> = FromValue::from_value(a.to_value(gc));
assert!(b.as_slice() == &["testing", "123"]);
unsafe {
ocaml::sys::caml_enter_blocking_section();
}
Ok(())
}
}

#[test]
#[serial]
fn test_basic_list() {
ocaml::runtime::init_persistent();
ocaml::runtime::init();
let gc = unsafe { ocaml::Runtime::recover_handle() };
let mut list = ocaml::List::empty();
let a = 3i64.to_value(gc);
Expand All @@ -50,8 +38,6 @@ fn test_basic_list() {
}
}

#[test]
#[serial]
fn test_int() {
ocaml::runtime::init_persistent();
let gc = unsafe { ocaml::Runtime::recover_handle() };
Expand All @@ -78,13 +64,8 @@ pub fn make_tuple(a: Value, b: Value) -> (Value, Value) {
(a, b)
}

#[test]
#[serial]
fn test_tuple_of_tuples() {
ocaml::runtime::init_persistent();
unsafe {
ocaml::sys::caml_leave_blocking_section();
}
let gc = unsafe { ocaml::Runtime::recover_handle() };

let x = (1f64, 2f64, 3f64, 4f64, 5f64, 6f64, 7f64, 8f64, 9f64).to_value(gc);
Expand All @@ -105,3 +86,10 @@ fn test_tuple_of_tuples() {
assert!(h == k);
assert!(i == j);
}

fn main() {
test_basic_array().unwrap();
test_basic_list();
test_int();
test_tuple_of_tuples();
}
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ pub fn ocamlopt() -> std::process::Command {
std::process::Command::new(sys::COMPILER)
}

#[cfg(feature = "link")]
#[cfg(test)]
mod tests;

/// OCaml runtime handle
pub struct Runtime {
_private: (),
Expand Down

0 comments on commit 5309d64

Please sign in to comment.