rhai/tests/print.rs

118 lines
3.0 KiB
Rust
Raw Normal View History

use rhai::{Engine, EvalAltResult, Scope, INT};
use std::sync::{Arc, RwLock};
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[test]
fn test_to_string() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
let mut scope = Scope::new();
scope.push("x", 42_u8);
scope.push("y", 42_i32);
scope.push("z", 42_i16);
assert_eq!(
2021-02-23 12:31:43 +01:00
engine.eval_with_scope::<String>(&mut scope, "to_string(x)")?,
"42"
);
assert_eq!(
2021-02-23 12:31:43 +01:00
engine.eval_with_scope::<String>(&mut scope, "to_string(x)")?,
"42"
);
assert_eq!(
2021-02-23 12:31:43 +01:00
engine.eval_with_scope::<String>(&mut scope, "to_string(x)")?,
"42"
);
Ok(())
}
#[test]
2020-12-12 04:47:18 +01:00
fn test_print_debug() -> Result<(), Box<EvalAltResult>> {
let logbook = Arc::new(RwLock::new(Vec::<String>::new()));
// Redirect print/debug output to 'log'
2020-07-12 05:46:53 +02:00
let log1 = logbook.clone();
let log2 = logbook.clone();
let mut engine = Engine::new();
2020-07-12 05:46:53 +02:00
engine
.on_print(move |s| log1.write().unwrap().push(format!("entry: {}", s)))
2020-12-21 15:04:46 +01:00
.on_debug(move |s, src, pos| {
log2.write().unwrap().push(format!(
"DEBUG of {} at {:?}: {}",
src.unwrap_or("unknown"),
pos,
s
))
2020-12-12 04:47:18 +01:00
});
// Evaluate script
2020-12-21 15:04:46 +01:00
engine.consume("print(40 + 2)")?;
let mut ast = engine.compile(r#"let x = "hello!"; debug(x)"#)?;
2021-01-09 09:57:21 +01:00
ast.set_source("world");
2020-12-21 15:04:46 +01:00
engine.consume_ast(&ast)?;
// 'logbook' captures all the 'print' and 'debug' output
assert_eq!(logbook.read().unwrap().len(), 2);
assert_eq!(logbook.read().unwrap()[0], "entry: 42");
2020-12-21 15:04:46 +01:00
assert_eq!(
logbook.read().unwrap()[1],
r#"DEBUG of world at 1:19: "hello!""#
);
for entry in logbook.read().unwrap().iter() {
println!("{}", entry);
}
Ok(())
}
2020-11-30 04:20:51 +01:00
#[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)
}
}
2020-11-30 14:16:59 +01:00
#[cfg(not(feature = "no_object"))]
2020-11-30 04:20:51 +01:00
#[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]"#
);
assert!(engine
.eval::<String>(
r#"
let x = #{ a:123, b:true, c:(), d:"world", e:new_ts() };
x.to_string()
"#
)?
2020-11-30 15:02:32 +01:00
.contains(r#""e": hello: 42"#));
2020-11-30 04:20:51 +01:00
Ok(())
}