2024-01-06 07:26:51 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
2024-01-30 16:48:29 +00:00
|
|
|
fn list_for_loop() {
|
2024-01-06 07:26:51 +00:00
|
|
|
let result = interpret("for i in [1 2 3] { output(i) }");
|
|
|
|
|
|
|
|
assert_eq!(Ok(Value::none()), result);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-01-30 16:48:29 +00:00
|
|
|
fn range_for_loop() {
|
2024-01-06 07:26:51 +00:00
|
|
|
let result = interpret(
|
|
|
|
"
|
2024-01-30 16:48:29 +00:00
|
|
|
numbers = []
|
|
|
|
|
|
|
|
for i in 1..3 {
|
|
|
|
numbers += i
|
|
|
|
}
|
|
|
|
|
|
|
|
numbers
|
|
|
|
",
|
2024-01-06 07:26:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Ok(Value::List(List::with_items(vec![
|
|
|
|
Value::Integer(1),
|
|
|
|
Value::Integer(2),
|
|
|
|
Value::Integer(3),
|
|
|
|
]))),
|
|
|
|
result
|
|
|
|
);
|
|
|
|
}
|
2024-01-30 05:24:13 +00:00
|
|
|
|
2024-01-30 16:48:29 +00:00
|
|
|
#[test]
|
2024-02-18 10:36:51 +00:00
|
|
|
fn mutate_list() {
|
2024-01-30 16:48:29 +00:00
|
|
|
let result = interpret(
|
|
|
|
"
|
|
|
|
list = []
|
|
|
|
for i in [1 2 3] { list += i }
|
|
|
|
list
|
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-02-16 20:07:24 +00:00
|
|
|
result,
|
2024-01-30 16:48:29 +00:00
|
|
|
Ok(Value::List(List::with_items(vec![
|
|
|
|
Value::Integer(1),
|
|
|
|
Value::Integer(2),
|
|
|
|
Value::Integer(3),
|
|
|
|
]))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-18 10:36:51 +00:00
|
|
|
fn do_not_mutate_list_items() {
|
2024-01-30 16:48:29 +00:00
|
|
|
let result = interpret(
|
|
|
|
"
|
|
|
|
list = [1 2 3]
|
|
|
|
for i in list { i += i }
|
|
|
|
list
|
|
|
|
",
|
2024-01-30 05:24:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-02-16 20:07:24 +00:00
|
|
|
result,
|
2024-01-30 05:24:13 +00:00
|
|
|
Ok(Value::List(List::with_items(vec![
|
2024-02-18 10:36:51 +00:00
|
|
|
Value::Integer(1),
|
2024-01-30 05:24:13 +00:00
|
|
|
Value::Integer(2),
|
|
|
|
Value::Integer(3),
|
|
|
|
]))),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn r#break() {
|
|
|
|
let result = interpret(
|
|
|
|
"
|
2024-01-30 16:48:29 +00:00
|
|
|
list = []
|
|
|
|
|
|
|
|
for i in [1 2 3] {
|
|
|
|
if i > 2 {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
list += i
|
2024-01-30 05:24:13 +00:00
|
|
|
}
|
2024-01-30 16:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list
|
2024-01-30 05:24:13 +00:00
|
|
|
",
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Ok(Value::List(List::with_items(vec![
|
|
|
|
Value::Integer(1),
|
|
|
|
Value::Integer(2),
|
|
|
|
]))),
|
|
|
|
result
|
|
|
|
);
|
|
|
|
}
|