Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add multi-file hot reloading test #522

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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