From 7b101c944aec8ef1d94be72c4ae9ad4bbaf8c9e0 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 30 Jan 2024 11:48:29 -0500 Subject: [PATCH] Add tests --- tests/dust_examples.rs | 2 - tests/for_loop.rs | 138 +++++++++++++++++++++++++++++++++++------ 2 files changed, 119 insertions(+), 21 deletions(-) diff --git a/tests/dust_examples.rs b/tests/dust_examples.rs index eed853d..c1b6845 100644 --- a/tests/dust_examples.rs +++ b/tests/dust_examples.rs @@ -18,7 +18,6 @@ fn async_download() { } #[test] -#[ignore] fn clue_solver() { let file_contents = read_to_string("examples/clue_solver.ds").unwrap(); @@ -34,7 +33,6 @@ fn fetch() { } #[test] -#[ignore] fn fibonacci() { let file_contents = read_to_string("examples/fibonacci.ds").unwrap(); diff --git a/tests/for_loop.rs b/tests/for_loop.rs index 2352242..44a774a 100644 --- a/tests/for_loop.rs +++ b/tests/for_loop.rs @@ -1,20 +1,24 @@ use dust_lang::*; #[test] -fn simple_for_loop() { +fn list_for_loop() { let result = interpret("for i in [1 2 3] { output(i) }"); assert_eq!(Ok(Value::none()), result); } #[test] -fn modify_value() { +fn range_for_loop() { let result = interpret( " - list = [] - for i in [1 2 3] { list += i } - list - ", + numbers = [] + + for i in 1..3 { + numbers += i + } + + numbers + ", ); assert_eq!( @@ -28,13 +32,82 @@ fn modify_value() { } #[test] -fn modify_iteration_values() { +fn map_for_loop() { let result = interpret( " - list = [1 2 3] - for i in list { i += i } - list - ", + map = { + x = 'y' + foo = 'bar' + } + list = [] + + for [key, value] in map { + list += value + } + + list + ", + ); + + assert_eq!( + Ok(Value::List(List::with_items(vec![ + Value::String("y".to_string()), + Value::String("bar".to_string()), + ]))), + result + ); +} + +#[test] +fn modify_list() { + let result = interpret( + " + list = [] + for i in [1 2 3] { list += i } + list + ", + ); + + assert_eq!( + Ok(Value::List(List::with_items(vec![ + Value::Integer(1), + Value::Integer(2), + Value::Integer(3), + ]))), + result + ); +} + +#[test] +fn modify_map() { + let result = interpret( + " + map = {} + + for i in [['x', 1] ['y', 2]] { + map:(i:0) = i:1 + } + + map + ", + ); + + let map = Map::new(); + + map.set("x".to_string(), Value::Integer(1)).unwrap(); + map.set("y".to_string(), Value::Integer(2)).unwrap(); + + assert_eq!(Ok(Value::Map(map)), result); +} + +#[test] +fn modify_list_values() { + let result = interpret( + " + list = [1 2 3] + for i in list { i += i } + list + ", ); assert_eq!( @@ -47,19 +120,46 @@ fn modify_iteration_values() { ); } +#[test] +fn modify_map_values() { + let result = interpret( + " + map = { + x = 0 + y = 1 + } + + for [key, value] in map { + value += 1 + } + + map + ", + ); + + let map = Map::new(); + + map.set("x".to_string(), Value::Integer(1)).unwrap(); + map.set("y".to_string(), Value::Integer(2)).unwrap(); + + assert_eq!(Ok(Value::Map(map)), result); +} + #[test] fn r#break() { let result = interpret( " - list = [] - for i in [1 2 3] { - if i > 2 { - break - } else { - list += i - } + list = [] + + for i in [1 2 3] { + if i > 2 { + break + } else { + list += i } - list + } + + list ", );