From 59e1c605deb80f8ebb6ddafde6c5e5d37173e638 Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Wed, 6 Nov 2024 06:53:30 +0100 Subject: [PATCH] rune: Clean tests a bit more --- crates/rune/src/tests.rs | 20 ++++++++---------- crates/rune/src/tests/matching.rs | 35 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 crates/rune/src/tests/matching.rs diff --git a/crates/rune/src/tests.rs b/crates/rune/src/tests.rs index 792b27d5c..9a4ef3d85 100644 --- a/crates/rune/src/tests.rs +++ b/crates/rune/src/tests.rs @@ -25,7 +25,7 @@ pub(crate) mod prelude { pub(crate) use crate::tests::{eval, run}; pub(crate) use crate::{ from_value, prepare, sources, span, vm_try, Any, Context, ContextError, Diagnostics, - FromValue, Hash, Item, ItemBuf, Module, Options, Source, Sources, Value, Vm, + FromValue, Hash, Item, ItemBuf, Module, Source, Sources, Value, Vm, }; pub(crate) use futures_executor::block_on; @@ -43,7 +43,7 @@ use ::rust_alloc::sync::Arc; use anyhow::{Context as _, Error, Result}; -use crate::runtime::{Args, VmError}; +use crate::runtime::{GuardedArgs, VmError}; use crate::{ alloc, termcolor, BuildError, Context, Diagnostics, FromValue, Hash, Options, Source, Sources, Unit, Vm, @@ -149,7 +149,7 @@ pub fn run_helper( context: &Context, sources: &mut Sources, diagnostics: &mut Diagnostics, - args: impl Args, + args: impl GuardedArgs, script: bool, ) -> Result where @@ -157,16 +157,13 @@ where { let mut vm = vm(context, sources, diagnostics, script)?; - let mut execute = if script { - vm.execute(Hash::EMPTY, args).map_err(TestError::VmError)? + let result = if script { + ::futures_executor::block_on(vm.async_call(Hash::EMPTY, args)) } else { - vm.execute(["main"], args).map_err(TestError::VmError)? + ::futures_executor::block_on(vm.async_call(["main"], args)) }; - let output = ::futures_executor::block_on(execute.async_complete()) - .into_result() - .map_err(TestError::VmError)?; - + let output = result.map_err(TestError::VmError)?; crate::from_value(output).map_err(|error| TestError::VmError(error.into())) } @@ -180,7 +177,7 @@ pub fn sources(source: &str) -> Sources { } /// Run the given source with diagnostics being printed to stderr. -pub fn run(context: &Context, source: &str, args: impl Args, script: bool) -> Result +pub fn run(context: &Context, source: &str, args: impl GuardedArgs, script: bool) -> Result where T: FromValue, { @@ -487,6 +484,7 @@ mod getter_setter; mod iterator; #[cfg(not(miri))] mod macros; +mod matching; #[cfg(not(miri))] mod moved; #[cfg(not(miri))] diff --git a/crates/rune/src/tests/matching.rs b/crates/rune/src/tests/matching.rs new file mode 100644 index 000000000..2b484a6e9 --- /dev/null +++ b/crates/rune/src/tests/matching.rs @@ -0,0 +1,35 @@ +prelude!(); + +#[derive(Debug, Any)] +struct External { + #[rune(get, set)] + value: u32, +} + +pub fn module() -> Result { + let mut module = Module::new(); + module.ty::()?; + Ok(module) +} + +#[test] +#[ignore = "fix this"] +fn try_matching() -> crate::support::Result<()> { + let m = module()?; + + let external = External { value: 1337 }; + + let b: bool = rune_n! { + mod m, + (&external,), + pub fn main(external) { + match external { + External { value: 1337 } => true, + _ => false, + } + } + }; + + assert!(b); + Ok(()) +}