Add general test for functions as example

This commit is contained in:
Daniel M 2022-02-10 12:19:01 +01:00
parent c4d2f89d35
commit f2331d7de9
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,31 @@
fun square(a) {
return a * a;
}
fun add(a, b) {
return a + b;
}
fun mul(a, b) {
return a * b;
}
// Funtion with multiple args & nested calls to different functions
fun addmul(a, b, c) {
return mul(add(a, b), c);
}
a <- 10;
b <- 20;
c <- 3;
result <- addmul(a, b, c) + square(c);
// Access and modify outer variable. Argument `a` must not be used from outer var
fun sub_from_result(a) {
result = result - a;
}
sub_from_result(30);
print result;

View File

@ -114,5 +114,23 @@ mod tests {
assert_eq!(interpreter.output(), &expected_output);
}
#[test]
fn test_functions() {
let filename = "test_functions.nek";
let correct_result = 69;
let mut interpreter = Interpreter::new();
interpreter.capture_output = true;
let code = read_to_string(format!("examples/{filename}")).unwrap();
interpreter.run_str(&code);
let expected_output = [Value::I64(correct_result)];
assert_eq!(interpreter.output(), &expected_output);
}
}