git.delta.rocks / jrsonnet / refs/commits / 88a0ba11fe45

difftreelog

source

crates/jrsonnet-evaluator/tests/common.rs1.5 KiBsourcehistory
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($s.clone(), 2)?,34				$b.to_json($s.clone(), 2)?,35			)36		}37	}};38}3940#[builtin]41fn assert_throw(s: State, lazy: Thunk<Val>, message: String) -> Result<bool> {42	match lazy.evaluate(s) {43		Ok(_) => {44			throw_runtime!("expected argument to throw on evaluation, but it returned instead")45		}46		Err(e) => {47			let error = format!("{}", e.error());48			ensure_eq!(message, error);49		}50	}51	Ok(true)52}5354#[allow(dead_code)]55pub fn with_test(s: &State) {56	let mut bobj = ObjValueBuilder::new();57	bobj.member("assertThrow".into())58		.hide()59		.value(60			s.clone(),61			Val::Func(FuncVal::StaticBuiltin(assert_throw::INST)),62		)63		.expect("no error");6465	s.settings_mut()66		.globals67		.insert("test".into(), Val::Obj(bobj.build()));68}