Print arrays and maps with to_debug.

This commit is contained in:
Stephen Chung
2020-11-30 11:20:51 +08:00
parent 65a4ceb3be
commit 1004bca5b5
9 changed files with 113 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult};
use rhai::{Engine, EvalAltResult, RegisterFn, INT};
use std::sync::{Arc, RwLock};
#[test]
@@ -30,3 +30,49 @@ fn test_print() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
struct MyStruct {
field: INT,
}
impl std::fmt::Display for MyStruct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "hello: {}", self.field)
}
}
#[test]
fn test_print_custom_type() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine
.register_type_with_name::<MyStruct>("MyStruct")
.register_fn("to_debug", |x: &mut MyStruct| x.to_string())
.register_fn("debug", |x: &mut MyStruct| x.to_string())
.register_fn("new_ts", || MyStruct { field: 42 });
engine.consume("let x = new_ts(); debug(x);")?;
#[cfg(not(feature = "no_index"))]
assert_eq!(
engine.eval::<String>(
r#"
let x = [ 123, true, (), "world", new_ts() ];
x.to_string()
"#
)?,
r#"[123, true, (), "world", hello: 42]"#
);
#[cfg(not(feature = "no_object"))]
assert!(engine
.eval::<String>(
r#"
let x = #{ a:123, b:true, c:(), d:"world", e:new_ts() };
x.to_string()
"#
)?
.contains("e: hello: 42"));
Ok(())
}