1
// Tests for DOLIST special form
2
// These tests verify that DOLIST works correctly for list iteration
3

            
4
use nomiscript::{
5
    Reader, eval_program,
6
    runtime::{SymbolTable, Value},
7
};
8

            
9
8
fn eval_expr(code: &str) -> Result<Value, nomiscript::Error> {
10
8
    let program = Reader::parse(code)?;
11
8
    let mut symbols = SymbolTable::with_builtins();
12
8
    eval_program(&mut symbols, &program)
13
8
}
14

            
15
#[cfg(test)]
16
mod dolist_basic_tests {
17
    use super::*;
18

            
19
    #[test]
20
1
    fn test_dolist_basic() {
21
1
        let result = eval_expr("(dolist (x '(1 2 3)) nil)");
22
1
        match &result {
23
            Err(e) => println!("Error: {e:?}"),
24
1
            Ok(v) => println!("Result: {v:?}"),
25
        }
26
1
        assert!(result.is_ok(), "Basic DOLIST should work");
27
1
        assert_eq!(result.unwrap(), Value::Nil);
28
1
    }
29

            
30
    #[test]
31
1
    fn test_dolist_with_result() {
32
1
        let result = eval_expr("(dolist (x '(1 2 3) 42) nil)");
33
1
        assert!(result.is_ok(), "DOLIST with result expression should work");
34
1
    }
35

            
36
    #[test]
37
1
    fn test_dolist_empty_list() {
38
1
        let result = eval_expr("(dolist (x '()) 'done)");
39
1
        assert!(result.is_ok(), "DOLIST with empty list should work");
40
1
    }
41

            
42
    #[test]
43
1
    fn test_dolist_nil_list() {
44
1
        let result = eval_expr("(dolist (x nil) 'done)");
45
1
        assert!(result.is_ok(), "DOLIST with nil list should work");
46
1
    }
47

            
48
    #[test]
49
1
    fn test_dolist_wrong_arity() {
50
1
        let result = eval_expr("(dolist)");
51
1
        assert!(result.is_err(), "DOLIST with no arguments should fail");
52
1
    }
53

            
54
    #[test]
55
1
    fn test_dolist_invalid_var_spec() {
56
1
        let result = eval_expr("(dolist x nil)");
57
1
        assert!(
58
1
            result.is_err(),
59
            "DOLIST with invalid variable spec should fail"
60
        );
61
1
    }
62

            
63
    #[test]
64
1
    fn test_dolist_var_not_symbol() {
65
1
        let result = eval_expr("(dolist (42 '(1 2 3)) nil)");
66
1
        assert!(
67
1
            result.is_err(),
68
            "DOLIST with non-symbol variable should fail"
69
        );
70
1
    }
71

            
72
    #[test]
73
1
    fn test_dolist_non_list_expression() {
74
1
        let result = eval_expr("(dolist (x 42) nil)");
75
1
        assert!(
76
1
            result.is_err(),
77
            "DOLIST with non-list expression should fail"
78
        );
79
1
    }
80
}