Skip to content

Commit

Permalink
Cleaned up dynamic_field tests and feature flagged the tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
lupan committed Oct 31, 2023
1 parent 49db256 commit a0031d3
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 163 deletions.
4 changes: 3 additions & 1 deletion crates/rune/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ mod core_macros;
mod custom_macros;
mod derive_from_to_value;
mod destructuring;
mod dynamic_fields;
mod esoteric_impls;
mod external_constructor;
mod external_generic;
Expand Down Expand Up @@ -462,3 +461,6 @@ mod vm_tuples;
mod vm_typed_tuple;
mod vm_types;
mod wildcard_imports;

#[cfg(feature = "dynamic_fields")]
mod dynamic_fields;
275 changes: 113 additions & 162 deletions crates/rune/src/tests/dynamic_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,94 @@ use std::collections::HashMap;

prelude!();

#[test]
fn dynamic_fields_never() -> Result<()> {
#[derive(Any, Clone)]
struct TestClass {
#[rune(get, set)]
a: i64,
}
let mut context = Context::with_default_modules()?;
let mut module = Module::new();
module.ty::<TestClass>()?;
context.install(module)?;

let runtime = Arc::new(context.runtime()?);

let mut sources = Sources::new();
sources.insert(Source::new(
"script",
r#"
const TEST_SCRIPT: &str = r#"
pub fn get_meta_field(value) {
value.b
}
pub fn get_field(value) {
value.a
}
"#,
)?)?;
let result = prepare(&mut sources).with_context(&context).build();
pub fn set_meta_field(value, into) {
value.b = into;
value.b
}
"#;

macro_rules! set_up_vm {
() => {{
let mut context = Context::with_default_modules()?;
let mut module = Module::new();
module.ty::<TestClass>()?;
module.function_meta(TestClass::get_meta_field)?;
module.function_meta(TestClass::set_meta_field)?;
context.install(module)?;

let unit = result?;
let mut vm = Vm::new(runtime, Arc::new(unit));
let runtime = Arc::new(context.runtime()?);

vm.set_dynamic_fields(true);
let mut sources = Sources::new();
sources.insert(Source::new("script", TEST_SCRIPT)?)?;

let input = TestClass { a: 42 };
let result = prepare(&mut sources).with_context(&context).build();

let unit = result?;
let mut vm = Vm::new(runtime, Arc::new(unit));
vm.set_dynamic_fields(true);
vm
}};
}

macro_rules! register_type {
($mode:ident) => {
#[derive(Any, Clone)]
#[rune(meta_fields = $mode)]
struct TestClass {
#[rune(get, set)]
a: i64,
values: HashMap<String, i64>,
}
impl TestClass {
#[rune::function(instance, protocol = DYNAMIC_FIELD_GET)]
fn get_meta_field(&self, key: &str) -> Option<i64> {
self.values.get(key).copied()
}
#[rune::function(instance, protocol = DYNAMIC_FIELD_SET)]
fn set_meta_field(&mut self, key: String, val: i64) {
use std::collections::hash_map::Entry;
match self.values.entry(key) {
Entry::Occupied(entry) => {
*entry.into_mut() = val;
}
Entry::Vacant(entry) => {
entry.insert(val);
}
};
}
}
};
}

#[test]
fn dynamic_fields_never() -> Result<()> {
register_type!(never);
let mut vm = set_up_vm!();

let input = TestClass {
a: 42,
values: {
let mut map = HashMap::with_capacity(1);
map.insert("b".into(), 69);
map
},
};

let value = i64::from_value(vm.call(["get_field"], (input.clone(),))?).into_result()?;
assert_eq!(value, 42);

let value = vm.call(["set_meta_field"], (input.clone(), 1337));
assert!(value.is_err());

let value = vm.call(["get_meta_field"], (input,));
assert!(value.is_err());

Expand All @@ -50,49 +98,8 @@ fn dynamic_fields_never() -> Result<()> {

#[test]
fn dynamic_fields_first() -> Result<()> {
#[derive(Any, Clone)]
#[rune(meta_fields = first)]
struct TestClass {
#[rune(get, set)]
a: i64,
values: HashMap<String, i64>,
}
let mut context = Context::with_default_modules()?;
let mut module = Module::new();
module.ty::<TestClass>()?;
module.inst_fn(
Protocol::DYNAMIC_FIELD_GET,
|this: &TestClass, val: &str| this.values.get(val).copied(),
)?;
context.install(module)?;

let runtime = Arc::new(context.runtime()?);

let mut sources = Sources::new();
sources.insert(Source::new(
"script",
r#"
pub fn get_meta_field(value) {
value.b
}
pub fn get_field(value) {
value.a
}
pub fn set_meta_field(value, into) {
value.b = into;
value.b
}
"#,
)?)?;

let result = prepare(&mut sources).with_context(&context).build();

let unit = result?;
let mut vm = Vm::new(runtime, Arc::new(unit));
vm.set_dynamic_fields(true);

register_type!(first);
let mut vm = set_up_vm!();
let input = TestClass {
a: 69,
values: {
Expand All @@ -105,62 +112,27 @@ fn dynamic_fields_first() -> Result<()> {
let value = i64::from_value(vm.call(["get_meta_field"], (input.clone(),))?).into_result()?;
assert_eq!(value, 42);

let value = i64::from_value(vm.call(["get_field"], (input,))?).into_result()?;
let value =
i64::from_value(vm.call(["set_meta_field"], (input.clone(), 1337))?).into_result()?;
assert_eq!(value, 1337);

let value = i64::from_value(vm.call(["get_field"], (input.clone(),))?).into_result()?;
assert_eq!(value, 69);

vm.set_dynamic_fields(false);
let value = vm.call(["get_meta_field"], (input.clone(),));
assert!(value.is_err());

let value = vm.call(["set_meta_field"], (input, 1337));
assert!(value.is_err());

Ok(())
}

#[test]
fn dynamic_fields_last() -> Result<()> {
#[derive(Any, Clone)]
#[rune(meta_fields = last)]
struct TestClass {
#[rune(get, set)]
a: i64,
values: HashMap<String, i64>,
}
let mut context = Context::with_default_modules()?;
let mut module = Module::new();
module.ty::<TestClass>()?;
module.inst_fn(
Protocol::DYNAMIC_FIELD_GET,
|this: &TestClass, val: &str| this.values.get(val).copied(),
)?;
module.inst_fn(
Protocol::DYNAMIC_FIELD_SET,
|this: &mut TestClass, key: &str, value: i64| {
this.values.insert(key.into(), value);
Some(())
},
)?;
context.install(module)?;

let runtime = Arc::new(context.runtime()?);

let mut sources = Sources::new();
sources.insert(Source::new(
"script",
r#"
pub fn get_meta_field(value) {
value.b
}
pub fn get_field(value) {
value.a
}
pub fn set_meta_field(value, into) {
value.b = into;
value.b
}
"#,
)?)?;

let result = prepare(&mut sources).with_context(&context).build();

let unit = result?;
let mut vm = Vm::new(runtime, Arc::new(unit));
vm.set_dynamic_fields(true);
register_type!(last);
let mut vm = set_up_vm!();

let input = TestClass {
a: 69,
Expand All @@ -178,55 +150,23 @@ fn dynamic_fields_last() -> Result<()> {
i64::from_value(vm.call(["set_meta_field"], (input.clone(), 1337))?).into_result()?;
assert_eq!(value, 1337);

let value = i64::from_value(vm.call(["get_field"], (input,))?).into_result()?;
let value = i64::from_value(vm.call(["get_field"], (input.clone(),))?).into_result()?;
assert_eq!(value, 69);

vm.set_dynamic_fields(false);
let value = vm.call(["get_meta_field"], (input.clone(),));
assert!(value.is_err());

let value = vm.call(["set_meta_field"], (input, 1337));
assert!(value.is_err());

Ok(())
}

#[test]
fn dynamic_fields_only() -> Result<()> {
#[derive(Any, Clone)]
#[rune(meta_fields = only)]
struct TestClass {
#[rune(get, set)]
a: i64,
values: HashMap<String, i64>,
}
let mut context = Context::with_default_modules()?;
let mut module = Module::new();
module.ty::<TestClass>()?;
module.inst_fn(
Protocol::DYNAMIC_FIELD_GET,
|this: &TestClass, val: &str| this.values.get(val).copied(),
)?;
context.install(module)?;

let runtime = Arc::new(context.runtime()?);

let mut sources = Sources::new();
sources.insert(Source::new(
"script",
r#"
pub fn get_meta_field(value) {
value.b
}
pub fn get_field(value) {
value.a
}
pub fn set_meta_field(value, into) {
value.b = into;
value.b
}
"#,
)?)?;

let result = prepare(&mut sources).with_context(&context).build();

let unit = result?;
let mut vm = Vm::new(runtime, Arc::new(unit));
vm.set_dynamic_fields(true);
register_type!(only);
let mut vm = set_up_vm!();

let input = TestClass {
a: 69,
Expand All @@ -240,7 +180,18 @@ fn dynamic_fields_only() -> Result<()> {
let value = i64::from_value(vm.call(["get_meta_field"], (input.clone(),))?).into_result()?;
assert_eq!(value, 42);

let value = vm.call(["get_field"], (input,));
let value = vm.call(["get_field"], (input.clone(),));
assert!(value.is_err());

vm.set_dynamic_fields(false);
let value = vm.call(["get_meta_field"], (input.clone(),));
assert!(value.is_err());

let value = vm.call(["set_meta_field"], (input.clone(), 1337));
assert!(value.is_err());

let value = i64::from_value(vm.call(["get_field"], (input,))?).into_result()?;
assert_eq!(value, 69);

Ok(())
}

0 comments on commit a0031d3

Please sign in to comment.