2024-05-21 20:29:54 +00:00
|
|
|
use dust_lang::*;
|
2024-03-09 03:34:17 +00:00
|
|
|
|
2024-03-29 19:52:02 +00:00
|
|
|
#[test]
|
|
|
|
fn function_call_with_type_argument() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret(
|
|
|
|
"test",
|
|
|
|
"
|
2024-05-18 20:21:46 +00:00
|
|
|
foobar = fn (T)(x : T) T { x }
|
2024-03-29 19:52:02 +00:00
|
|
|
foobar::(int)::(42)
|
|
|
|
",
|
|
|
|
),
|
|
|
|
Ok(Some(Value::integer(42)))
|
|
|
|
);
|
|
|
|
}
|
2024-03-24 19:35:19 +00:00
|
|
|
|
2024-03-09 03:34:17 +00:00
|
|
|
#[test]
|
|
|
|
fn function_call() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret(
|
2024-03-24 19:35:19 +00:00
|
|
|
"test",
|
2024-03-09 03:34:17 +00:00
|
|
|
"
|
2024-05-18 20:21:46 +00:00
|
|
|
foobar = fn (message : str) str { message }
|
2024-03-09 03:34:17 +00:00
|
|
|
foobar('Hiya')
|
|
|
|
",
|
|
|
|
),
|
|
|
|
Ok(Some(Value::string("Hiya".to_string())))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn callback() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret(
|
2024-03-24 19:35:19 +00:00
|
|
|
"test",
|
2024-03-09 03:34:17 +00:00
|
|
|
"
|
2024-05-18 20:21:46 +00:00
|
|
|
foobar = fn (cb: fn() -> str) str {
|
2024-03-09 03:34:17 +00:00
|
|
|
cb()
|
|
|
|
}
|
2024-05-18 20:21:46 +00:00
|
|
|
foobar(fn () str { 'Hiya' })
|
2024-03-09 03:34:17 +00:00
|
|
|
",
|
|
|
|
),
|
|
|
|
Ok(Some(Value::string("Hiya".to_string())))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn built_in_function_call() {
|
2024-03-24 19:35:19 +00:00
|
|
|
assert_eq!(interpret("test", "io.write_line('Hiya')"), Ok(None));
|
2024-03-09 03:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-05-21 20:29:54 +00:00
|
|
|
fn function_context_captures_values() {
|
2024-03-09 03:34:17 +00:00
|
|
|
assert_eq!(
|
|
|
|
interpret(
|
2024-03-24 19:35:19 +00:00
|
|
|
"test",
|
2024-03-09 03:34:17 +00:00
|
|
|
"
|
2024-05-18 20:21:46 +00:00
|
|
|
bar = fn () int { 2 }
|
|
|
|
foo = fn () int { bar() }
|
2024-03-09 03:34:17 +00:00
|
|
|
foo()
|
|
|
|
"
|
|
|
|
),
|
|
|
|
Ok(Some(Value::integer(2)))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursion() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret(
|
2024-03-24 19:35:19 +00:00
|
|
|
"test",
|
2024-03-09 03:34:17 +00:00
|
|
|
"
|
2024-05-18 20:21:46 +00:00
|
|
|
fib = fn (i: int) int {
|
2024-03-09 03:34:17 +00:00
|
|
|
if i <= 1 {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
fib(i - 1) + fib(i - 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fib(8)
|
|
|
|
"
|
|
|
|
),
|
|
|
|
Ok(Some(Value::integer(34)))
|
|
|
|
);
|
|
|
|
}
|