use crate::rhai_engine::{RhaiEngine, RhaiEngineConfig};
use serde_json::json;
fn engine() -> RhaiEngine {
RhaiEngine::with_config(RhaiEngineConfig::default().with_use_holochain_time(false)).unwrap()
}
#[test]
fn to_num_dispatches_on_both_authored_shapes() {
let script = rmp_serde::to_vec(
&r#"
let total = to_num(inputs.rate.data) * to_num(inputs.count.data);
if total != 3.0 { throw "wrong total: " + total.to_string() }
#{ "output": #{ "unyt_allocation": [] } }
"#,
)
.unwrap();
let input = json!({ "inputs": { "rate": { "data": "0.5" }, "count": { "data": 6 } } });
engine()
.execute(&input, script, None)
.expect("both authored shapes must dispatch through to_num");
}
#[test]
fn to_num_garbage_fails_the_run_naming_the_value() {
let script =
rmp_serde::to_vec(&r#" #{ "output": #{ "x": to_num(inputs.rate.data) } } "#).unwrap();
let input = json!({ "inputs": { "rate": { "data": "O.5" } } });
let err = format!(
"{:?}",
engine()
.execute(&input, script, None)
.expect_err("a typo'd rate must refuse the run, not price it at 0")
);
assert!(err.contains("O.5"), "the error must name the value: {err}");
}