Skip to content

Commit

Permalink
test: add multi-file hot reloading test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed May 14, 2023
1 parent 4829352 commit 9599f51
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 52 deletions.
2 changes: 2 additions & 0 deletions crates/mun_memory/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
use std::collections::{HashMap, HashSet};

/// The type mapping needed to convert an old into a new set of unique and ordered values.
#[derive(Debug)]
pub struct Mapping {
/// The types that were deleted
pub deletions: HashSet<Type>,
Expand All @@ -19,6 +20,7 @@ pub struct Mapping {
}

/// The struct mapping needed to convert an old into a new struct of unique and ordered fields.
#[derive(Debug)]
pub struct StructMapping {
/// The field mappings for each original struct field
pub field_mapping: Vec<FieldMapping>,
Expand Down
122 changes: 115 additions & 7 deletions crates/mun_runtime/tests/hot_reloading.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#[macro_use]
mod util;

use mun_runtime::StructRef;
use mun_test::CompileAndRunTestDriver;

#[test]
fn hotreloadable() {
fn reloadable_function_single_file() {
let mut driver = CompileAndRunTestDriver::new(
r"
pub fn main() -> i32 { 5 }
Expand All @@ -14,7 +15,7 @@ fn hotreloadable() {
.expect("Failed to build test driver");
assert_invoke_eq!(i32, 5, driver, "main");

driver.update(
driver.update_file(
"mod.mun",
r"
pub fn main() -> i32 { 10 }
Expand All @@ -24,7 +25,37 @@ fn hotreloadable() {
}

#[test]
fn hotreload_struct_decl() {
fn reloadable_function_multi_file() {
let mut driver = CompileAndRunTestDriver::from_fixture(
r#"
//- /mun.toml
[package]
name="foo"
version="0.0.0"
//- /src/mod.mun
use package::foo::bar;
pub fn main() -> i32 { bar() }
//- /src/foo.mun
pub fn bar() -> i32 { 5 }
"#,
|builder| builder,
)
.expect("Failed to build test driver");
assert_invoke_eq!(i32, 5, driver, "main");

driver.update_file(
"foo.mun",
r#"
pub fn bar() -> i32 { 10 }
"#,
);
assert_invoke_eq!(i32, 10, driver, "main");
}

#[test]
fn reloadable_struct_decl_single_file() {
let mut driver = CompileAndRunTestDriver::new(
r#"
pub struct(gc) Args {
Expand All @@ -33,18 +64,28 @@ fn hotreload_struct_decl() {
}
struct(gc) Bar {
m: f64,
m: i32,
}
pub fn args() -> Args {
Args { n: 3, foo: Bar { m: 1.0 }, }
Args { n: 3, foo: Bar { m: 1 }, }
}
"#,
|builder| builder,
)
.expect("Failed to build test driver");

driver.update(
let args: StructRef = driver
.runtime
.invoke("args", ())
.expect("Failed to call function");

let foo: StructRef = args.get("foo").expect("Failed to get struct field");
assert_eq!(foo.get::<i32>("m").expect("Failed to get struct field"), 1);

let foo = foo.root();

driver.update_file(
"mod.mun",
r#"
pub struct(gc) Args {
Expand All @@ -53,12 +94,79 @@ fn hotreload_struct_decl() {
}
struct(gc) Bar {
m: i32,
m: i64,
}
pub fn args() -> Args {
Args { n: 3, foo: Bar { m: 1 }, }
}
"#,
);

let foo = foo.as_ref(&driver.runtime);
assert_eq!(foo.get::<i64>("m").expect("Failed to get struct field"), 1);
}

#[test]
fn reloadable_struct_decl_multi_file() {
let mut driver = CompileAndRunTestDriver::from_fixture(
r#"
//- /mun.toml
[package]
name="foo"
version="0.0.0"
//- /src/mod.mun
use package::foo::Bar;
pub struct(gc) Args {
n: i32,
foo: Bar,
}
pub fn args() -> Args {
Args { n: 3, foo: Bar { m: 1 }, }
}
//- /src/foo.mun
struct(gc) Bar {
m: i64,
}
"#,
|builder| builder,
)
.expect("Failed to build test driver");

let args: StructRef = driver
.runtime
.invoke("args", ())
.expect("Failed to call function");

assert_eq!(args.get::<i32>("n").expect("Failed to get struct field"), 3);

let foo: StructRef = args.get("foo").expect("Failed to get struct field");
assert_eq!(foo.get::<i64>("m").expect("Failed to get struct field"), 1);

let args = args.root();
let foo = foo.root();

driver.update_file(
"mod.mun",
r#"
use package::foo::Bar;
pub struct(gc) Args {
n: i64,
foo: Bar,
}
pub fn args() -> Args {
Args { n: 3, foo: Bar { m: 1 }, }
}
"#,
);

let args = args.as_ref(&driver.runtime);
assert_eq!(args.get::<i64>("n").expect("Failed to get struct field"), 3);

let foo = foo.as_ref(&driver.runtime);
assert_eq!(foo.get::<i64>("m").expect("Failed to get struct field"), 1);
}
Loading

0 comments on commit 9599f51

Please sign in to comment.