1use jrsonnet_evaluator::{2 error::Result, function::builtin, throw_runtime, val::FuncVal, LazyVal, ObjValueBuilder, State,3 Val,4};56#[macro_export]7macro_rules! ensure_eq {8 ($a:expr, $b:expr $(,)?) => {{9 if $a != $b {10 ::jrsonnet_evaluator::throw_runtime!(11 "assertion failed: a != b\na={:#?}\nb={:#?}",12 $a,13 $b,14 )15 }16 }};17}1819#[macro_export]20macro_rules! ensure {21 ($v:expr $(,)?) => {22 if !$v {23 ::jrsonnet_evaluator::throw_runtime!("assertion failed: {}", stringify!($v))24 }25 };26}2728#[macro_export]29macro_rules! ensure_val_eq {30 ($s:expr, $a:expr, $b:expr) => {{31 if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {32 ::jrsonnet_evaluator::throw_runtime!(33 "assertion failed: a != b\na={:#?}\nb={:#?}",34 $a.to_json($s.clone(), 2)?,35 $b.to_json($s.clone(), 2)?,36 )37 }38 }};39}4041#[builtin]42fn assert_throw(s: State, lazy: LazyVal, message: String) -> Result<bool> {43 match lazy.evaluate(s) {44 Ok(_) => {45 throw_runtime!("expected argument to throw on evaluation, but it returned instead")46 }47 Err(e) => {48 let error = format!("{}", e.error());49 ensure_eq!(message, error);50 }51 }52 Ok(true)53}5455#[allow(dead_code)]56pub fn with_test(s: &State) {57 let mut bobj = ObjValueBuilder::new();58 bobj.member("assertThrow".into())59 .hide()60 .value(61 s.clone(),62 Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)),63 )64 .expect("no error");6566 s.settings_mut()67 .globals68 .insert("test".into(), Val::Obj(bobj.build()));69}