Add general test for functions as example
This commit is contained in:
parent
c4d2f89d35
commit
f2331d7de9
31
examples/test_functions.nek
Normal file
31
examples/test_functions.nek
Normal 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;
|
||||||
18
src/lib.rs
18
src/lib.rs
@ -114,5 +114,23 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(interpreter.output(), &expected_output);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user