git.delta.rocks / jrsonnet / refs/commits / 3490ff65c93a

difftreelog

source

tests/tests/common.rs1.5 KiBsourcehistory
1use jrsonnet_evaluator::{2	error::Result,3	function::{builtin, FuncVal},4	throw, ObjValueBuilder, State, Thunk, Val,5};6use jrsonnet_stdlib::StateExt;78#[macro_export]9macro_rules! ensure_eq {10	($a:expr, $b:expr $(,)?) => {{11		let a = &$a;12		let b = &$b;13		if a != b {14			::jrsonnet_evaluator::throw!("assertion failed: a != b\na={:#?}\nb={:#?}", a, b)15		}16	}};17}1819#[macro_export]20macro_rules! ensure {21	($v:expr $(,)?) => {22		if !$v {23			::jrsonnet_evaluator::throw!("assertion failed: {}", stringify!($v))24		}25	};26}2728#[macro_export]29macro_rules! ensure_val_eq {30	($a:expr, $b:expr) => {{31		if !::jrsonnet_evaluator::val::equals(&$a.clone(), &$b.clone())? {32			::jrsonnet_evaluator::throw!(33				"assertion failed: a != b\na={:#?}\nb={:#?}",34				$a.to_json(35					2,36					#[cfg(feature = "exp-preserve-order")]37					false38				)?,39				$b.to_json(40					2,41					#[cfg(feature = "exp-preserve-order")]42					false43				)?,44			)45		}46	}};47}4849#[builtin]50fn assert_throw(lazy: Thunk<Val>, message: String) -> Result<bool> {51	match lazy.evaluate() {52		Ok(_) => {53			throw!("expected argument to throw on evaluation, but it returned instead")54		}55		Err(e) => {56			let error = format!("{}", e.error());57			ensure_eq!(message, error);58		}59	}60	Ok(true)61}6263#[allow(dead_code)]64pub fn with_test(s: &State) {65	let mut bobj = ObjValueBuilder::new();66	bobj.member("assertThrow".into())67		.hide()68		.value(Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)))69		.expect("no error");7071	s.add_global("test".into(), Thunk::evaluated(Val::Obj(bobj.build())))72}