1use jrsonnet_evaluator::{2 error::Result,3 function::{builtin, FuncVal},4 throw_runtime, ObjValueBuilder, State, Thunk, Val,5};67#[macro_export]8macro_rules! ensure_eq {9 ($a:expr, $b:expr $(,)?) => {{10 let a = &$a;11 let b = &$b;12 if a != b {13 ::jrsonnet_evaluator::throw_runtime!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)14 }15 }};16}1718#[macro_export]19macro_rules! ensure {20 ($v:expr $(,)?) => {21 if !$v {22 ::jrsonnet_evaluator::throw_runtime!("assertion failed: {}", stringify!($v))23 }24 };25}2627#[macro_export]28macro_rules! ensure_val_eq {29 ($s:expr, $a:expr, $b:expr) => {{30 if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {31 ::jrsonnet_evaluator::throw_runtime!(32 "assertion failed: a != b\na={:#?}\nb={:#?}",33 $a.to_json(34 $s.clone(),35 2,36 #[cfg(feature = "exp-preserve-order")]37 false38 )?,39 $b.to_json(40 $s.clone(),41 2,42 #[cfg(feature = "exp-preserve-order")]43 false44 )?,45 )46 }47 }};48}4950#[builtin]51fn assert_throw(s: State, lazy: Thunk<Val>, message: String) -> Result<bool> {52 match lazy.evaluate(s) {53 Ok(_) => {54 throw_runtime!("expected argument to throw on evaluation, but it returned instead")55 }56 Err(e) => {57 let error = format!("{}", e.error());58 ensure_eq!(message, error);59 }60 }61 Ok(true)62}6364#[allow(dead_code)]65pub fn with_test(s: &State) {66 let mut bobj = ObjValueBuilder::new();67 bobj.member("assertThrow".into())68 .hide()69 .value(70 s.clone(),71 Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)),72 )73 .expect("no error");7475 s.settings_mut()76 .globals77 .insert("test".into(), Val::Obj(bobj.build()));78}