git.delta.rocks / jrsonnet / refs/commits / ad1f65ce0a08

difftreelog

source

tests/tests/builtin.rs1.6 KiBsourcehistory
1mod common;23use jrsonnet_evaluator::{4	function::{builtin, builtin::Builtin, CallLocation, FuncVal},5	typed::Typed,6	ContextBuilder, Result, State, Thunk, Val,7};8use jrsonnet_stdlib::StateExt;910#[builtin]11fn a() -> Result<u32> {12	Ok(1)13}1415#[test]16fn basic_function() -> Result<()> {17	let a: a = a {};18	let v = u32::from_untyped(a.call(19		ContextBuilder::dangerous_empty_state().build(),20		CallLocation::native(),21		&(),22	)?)?;2324	ensure_eq!(v, 1);25	Ok(())26}2728#[builtin]29fn native_add(a: u32, b: u32) -> Result<u32> {30	Ok(a + b)31}3233#[test]34fn call_from_code() -> Result<()> {35	let s = State::default();36	s.with_stdlib();37	s.add_global(38		"nativeAdd".into(),39		Thunk::evaluated(Val::function(native_add::INST)),40	);4142	let v = s.evaluate_snippet(43		"snip".to_owned(),44		"45            assert nativeAdd(1, 2) == 3;46            assert nativeAdd(100, 200) == 300;47            null48        ",49	)?;50	ensure_val_eq!(v, Val::Null);51	Ok(())52}5354#[builtin(fields(55    a: u3256))]57fn curried_add(this: &curried_add, b: u32) -> Result<u32> {58	Ok(this.a + b)59}6061#[builtin]62fn curry_add(a: u32) -> Result<FuncVal> {63	Ok(FuncVal::builtin(curried_add { a }))64}6566#[test]67fn nonstatic_builtin() -> Result<()> {68	let s = State::default();69	s.with_stdlib();70	s.add_global(71		"curryAdd".into(),72		Thunk::evaluated(Val::function(curry_add::INST)),73	);7475	let v = s.evaluate_snippet(76		"snip".to_owned(),77		"78            local a = curryAdd(1);79            local b = curryAdd(4);8081            assert a(2) == 3;82            assert a(200) == 201;8384            assert b(2) == 6;85            assert b(200) == 204;86            null87        ",88	)?;89	ensure_val_eq!(v, Val::Null);90	Ok(())91}