From 9417d0d160548eb6b2eb765d9eecb14fc92dcd11 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 6 Jan 2024 02:18:30 -0500 Subject: [PATCH] Write tests --- tests/functions.rs | 143 +++++++++++++++++++++++++++++++++++++++++++++ tests/interpret.rs | 52 +---------------- 2 files changed, 145 insertions(+), 50 deletions(-) create mode 100644 tests/functions.rs diff --git a/tests/functions.rs b/tests/functions.rs new file mode 100644 index 0000000..710862d --- /dev/null +++ b/tests/functions.rs @@ -0,0 +1,143 @@ +use dust_lang::*; + +#[test] +fn function_call() { + assert_eq!( + interpret( + " + foobar = (message ) { message } + foobar('Hiya') + ", + ), + Ok(Value::string("Hiya".to_string())) + ); +} + +#[test] +fn call_empty_function() { + assert_eq!( + interpret( + " + foobar = (message ) {} + foobar('Hiya') + ", + ), + Ok(Value::none()) + ); +} + +#[test] +fn callback() { + assert_eq!( + interpret( + " + foobar = (cb <() -> str>) { + cb() + } + foobar(() { 'Hiya' }) + ", + ), + Ok(Value::string("Hiya".to_string())) + ); +} + +#[test] +fn built_in_function_call() { + assert_eq!(interpret("output('Hiya')"), Ok(Value::Option(None))); +} + +#[test] +fn function_context_does_not_capture_normal_values() { + assert_eq!( + interpret( + " + x = 1 + + foo = () { x } + " + ), + Err(Error::VariableIdentifierNotFound("x".to_string())) + ); +} + +#[test] +fn function_context_captures_functions() { + assert_eq!( + interpret( + " + x = 1 + foo = (x ) { x } + foo(2) + " + ), + Ok(Value::Integer(2)) + ); + + assert_eq!( + interpret( + " + bar = () { 2 } + foo = () { bar() } + foo() + " + ), + Ok(Value::Integer(2)) + ); + + assert_eq!( + interpret( + " + foo = () { bar() } + foo() + bar = () { 2 } + " + ), + Ok(Value::Integer(2)) + ); +} + +#[test] +fn function_context_captures_structure_definitions() { + let map = Map::new(); + + map.set("name".to_string(), Value::string("bob"), None) + .unwrap(); + + assert_eq!( + interpret( + " + User = struct { + name + } + + bob = () { + new User { + name = 'bob' + } + } + + bob() + " + ), + Ok(Value::Map(map.clone())) + ); + + assert_eq!( + interpret( + " + bob = () { + new User { + name = 'bob' + } + } + + User = struct { + name + } + + bob() + " + ), + Ok(Value::Map(map)) + ); +} diff --git a/tests/interpret.rs b/tests/interpret.rs index cb7514c..087ce30 100644 --- a/tests/interpret.rs +++ b/tests/interpret.rs @@ -1,3 +1,5 @@ +mod functions; + mod structure { use std::collections::BTreeMap; @@ -233,56 +235,6 @@ mod value { } } -mod function_call { - use dust_lang::*; - - #[test] - fn function_call() { - assert_eq!( - interpret( - " - foobar = (message ) { message } - foobar('Hiya') - ", - ), - Ok(Value::string("Hiya".to_string())) - ); - } - - #[test] - fn call_empty_function() { - assert_eq!( - interpret( - " - foobar = (message ) {} - foobar('Hiya') - ", - ), - Ok(Value::none()) - ); - } - - #[test] - fn callback() { - assert_eq!( - interpret( - " - foobar = (cb <() -> str>) { - cb() - } - foobar(() { 'Hiya' }) - ", - ), - Ok(Value::string("Hiya".to_string())) - ); - } - - #[test] - fn built_in_function_call() { - assert_eq!(interpret("output('Hiya')"), Ok(Value::Option(None))); - } -} - mod if_else { use dust_lang::*;