Lines
91.49 %
Functions
90 %
Branches
100 %
// Tests for DOLIST special form
// These tests verify that DOLIST works correctly for list iteration
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 dolist_basic_tests {
use super::*;
#[test]
fn test_dolist_basic() {
let result = eval_expr("(dolist (x '(1 2 3)) nil)");
match &result {
Err(e) => println!("Error: {e:?}"),
Ok(v) => println!("Result: {v:?}"),
assert!(result.is_ok(), "Basic DOLIST should work");
assert_eq!(result.unwrap(), Value::Nil);
fn test_dolist_with_result() {
let result = eval_expr("(dolist (x '(1 2 3) 42) nil)");
assert!(result.is_ok(), "DOLIST with result expression should work");
fn test_dolist_empty_list() {
let result = eval_expr("(dolist (x '()) 'done)");
assert!(result.is_ok(), "DOLIST with empty list should work");
fn test_dolist_nil_list() {
let result = eval_expr("(dolist (x nil) 'done)");
assert!(result.is_ok(), "DOLIST with nil list should work");
fn test_dolist_wrong_arity() {
let result = eval_expr("(dolist)");
assert!(result.is_err(), "DOLIST with no arguments should fail");
fn test_dolist_invalid_var_spec() {
let result = eval_expr("(dolist x nil)");
assert!(
result.is_err(),
"DOLIST with invalid variable spec should fail"
);
fn test_dolist_var_not_symbol() {
let result = eval_expr("(dolist (42 '(1 2 3)) nil)");
"DOLIST with non-symbol variable should fail"
fn test_dolist_non_list_expression() {
let result = eval_expr("(dolist (x 42) nil)");
"DOLIST with non-list expression should fail"