Skip to content

Commit 6e85fa8

Browse files
committed
test(struct): add test for marshalling structs
1 parent caef098 commit 6e85fa8

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

crates/mun_runtime/src/test.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Runtime, RuntimeBuilder};
1+
use crate::{Runtime, RuntimeBuilder, Struct};
22
use mun_compiler::{ColorChoice, Config, Driver, FileId, PathOrInline, RelativePathBuf};
33
use std::path::PathBuf;
44
use std::thread::sleep;
@@ -420,3 +420,50 @@ fn field_crash() {
420420
);
421421
assert_invoke_eq!(i64, 15, driver, "main", 10);
422422
}
423+
424+
#[test]
425+
fn marshal_struct() {
426+
let mut driver = TestDriver::new(
427+
r#"
428+
struct(gc) Foo { a: int, b: bool, c: float, };
429+
430+
fn foo_new(a: int, b: bool, c: float): Foo {
431+
Foo { a, b, c, }
432+
}
433+
fn foo_a(foo: Foo):int { foo.a }
434+
fn foo_b(foo: Foo):bool { foo.b }
435+
fn foo_c(foo: Foo):float { foo.c }
436+
"#,
437+
);
438+
439+
let a = 3i64;
440+
let b = true;
441+
let c = 1.23f64;
442+
let mut foo: Struct = invoke_fn!(driver.runtime, "foo_new", a, b, c).unwrap();
443+
assert_eq!(Ok(&a), foo.get::<i64>("a"));
444+
assert_eq!(Ok(&b), foo.get::<bool>("b"));
445+
assert_eq!(Ok(&c), foo.get::<f64>("c"));
446+
447+
let d = 6i64;
448+
let e = false;
449+
let f = 4.56f64;
450+
foo.set("a", d).unwrap();
451+
foo.set("b", e).unwrap();
452+
foo.set("c", f).unwrap();
453+
454+
assert_eq!(Ok(&d), foo.get::<i64>("a"));
455+
assert_eq!(Ok(&e), foo.get::<bool>("b"));
456+
assert_eq!(Ok(&f), foo.get::<f64>("c"));
457+
458+
assert_eq!(Ok(d), foo.replace("a", a));
459+
assert_eq!(Ok(e), foo.replace("b", b));
460+
assert_eq!(Ok(f), foo.replace("c", c));
461+
462+
assert_eq!(Ok(&a), foo.get::<i64>("a"));
463+
assert_eq!(Ok(&b), foo.get::<bool>("b"));
464+
assert_eq!(Ok(&c), foo.get::<f64>("c"));
465+
466+
assert_invoke_eq!(i64, a, driver, "foo_a", foo.clone());
467+
assert_invoke_eq!(bool, b, driver, "foo_b", foo.clone());
468+
assert_invoke_eq!(f64, c, driver, "foo_c", foo);
469+
}

0 commit comments

Comments
 (0)