Skip to content

Commit

Permalink
test(struct): add test for marshalling structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Jan 25, 2020
1 parent caef098 commit 6e85fa8
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion crates/mun_runtime/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Runtime, RuntimeBuilder};
use crate::{Runtime, RuntimeBuilder, Struct};
use mun_compiler::{ColorChoice, Config, Driver, FileId, PathOrInline, RelativePathBuf};
use std::path::PathBuf;
use std::thread::sleep;
Expand Down Expand Up @@ -420,3 +420,50 @@ fn field_crash() {
);
assert_invoke_eq!(i64, 15, driver, "main", 10);
}

#[test]
fn marshal_struct() {
let mut driver = TestDriver::new(
r#"
struct(gc) Foo { a: int, b: bool, c: float, };
fn foo_new(a: int, b: bool, c: float): Foo {
Foo { a, b, c, }
}
fn foo_a(foo: Foo):int { foo.a }
fn foo_b(foo: Foo):bool { foo.b }
fn foo_c(foo: Foo):float { foo.c }
"#,
);

let a = 3i64;
let b = true;
let c = 1.23f64;
let mut foo: Struct = invoke_fn!(driver.runtime, "foo_new", a, b, c).unwrap();
assert_eq!(Ok(&a), foo.get::<i64>("a"));
assert_eq!(Ok(&b), foo.get::<bool>("b"));
assert_eq!(Ok(&c), foo.get::<f64>("c"));

let d = 6i64;
let e = false;
let f = 4.56f64;
foo.set("a", d).unwrap();
foo.set("b", e).unwrap();
foo.set("c", f).unwrap();

assert_eq!(Ok(&d), foo.get::<i64>("a"));
assert_eq!(Ok(&e), foo.get::<bool>("b"));
assert_eq!(Ok(&f), foo.get::<f64>("c"));

assert_eq!(Ok(d), foo.replace("a", a));
assert_eq!(Ok(e), foo.replace("b", b));
assert_eq!(Ok(f), foo.replace("c", c));

assert_eq!(Ok(&a), foo.get::<i64>("a"));
assert_eq!(Ok(&b), foo.get::<bool>("b"));
assert_eq!(Ok(&c), foo.get::<f64>("c"));

assert_invoke_eq!(i64, a, driver, "foo_a", foo.clone());
assert_invoke_eq!(bool, b, driver, "foo_b", foo.clone());
assert_invoke_eq!(f64, c, driver, "foo_c", foo);
}

0 comments on commit 6e85fa8

Please sign in to comment.