Lines
95.4 %
Functions
92.86 %
Branches
100 %
// Tests for lambda list keywords (&rest, &optional, etc.)
// These tests verify that the lambda list keyword infrastructure works correctly
use nomiscript::{
Reader, eval_program,
runtime::{SymbolTable, Value},
};
use num_rational::Ratio;
fn eval_expr(code: &str) -> Result<Value, nomiscript::Error> {
let program = Reader::parse(code)?;
let mut symbols = SymbolTable::with_builtins();
eval_program(&mut symbols, &program)
}
fn num(n: i64) -> Value {
Value::Number(Ratio::from_integer(n))
#[cfg(test)]
mod rest_parameter_tests {
use super::*;
#[test]
fn test_lambda_with_rest_basic() {
let result = eval_expr("((lambda (x &rest y) y) 1 2 3 4)");
match &result {
Err(e) => println!("Error: {e:?}"),
Ok(v) => println!("Result: {v:?}"),
assert!(result.is_ok(), "Lambda with &rest should work");
fn test_lambda_with_rest_no_extra_args() {
let result = eval_expr("((lambda (x &rest y) x) 42)");
assert!(
result.is_ok(),
"Lambda with &rest and no extra args should work"
);
fn test_defun_with_rest() {
let code = r"
(defun sum-all (first &rest others)
first)
(sum-all 10 20 30)
";
let result = eval_expr(code);
assert!(result.is_ok(), "DEFUN with &rest should work");
mod optional_parameter_tests {
fn test_lambda_with_optional_no_default() {
let result = eval_expr("((lambda (x &optional y) x) 42)");
assert!(result.is_ok(), "Lambda with &optional should work");
fn test_lambda_with_optional_with_default() {
let result = eval_expr("((lambda (x &optional (y 100)) x) 42)");
"Lambda with &optional and default should work"
fn test_defun_with_optional() {
let code = r#"
(defun greet (&optional (name "World"))
name)
(greet "Alice")
"#;
assert!(result.is_ok(), "DEFUN with &optional should work");
mod aux_parameter_tests {
fn test_lambda_with_aux() {
let result = eval_expr("((lambda (x &aux (temp 42)) x) 10)");
assert!(result.is_ok(), "Lambda with &aux should work");
fn test_defun_with_aux() {
(defun process (data &aux (result nil))
data)
(process 123)
assert!(result.is_ok(), "DEFUN with &aux should work");
mod key_parameter_tests {
fn test_lambda_with_key_basic() {
let result = eval_expr("((lambda (x &key name) x) 42 :name \"test\")");
assert!(result.is_ok(), "Lambda with &key should work");
fn test_lambda_with_key_no_keyword_args() {
let result = eval_expr("((lambda (x &key name) x) 42)");
"Lambda with &key and no keyword args should work"
fn test_defun_with_key() {
(defun greet (person &key (greeting "Hello"))
person)
(greet "Alice" :greeting "Hi")
assert!(result.is_ok(), "DEFUN with &key should work");
fn test_defmacro_with_key() {
(defmacro test-macro (name &key (type "default"))
(quote (+ 1 2)))
(test-macro foo :type "custom")
assert!(result.is_ok(), "DEFMACRO with &key should work");