1
// Tests for CAR and CDR native functions
2
// These tests verify that the CAR and CDR functions work correctly
3
// through the nomiscript evaluation system.
4

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

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

            
16
2
fn num(n: i64) -> Value {
17
2
    Value::Number(Number::Index(n))
18
2
}
19

            
20
#[cfg(test)]
21
mod car_cdr_basic_tests {
22
    use super::*;
23

            
24
    #[test]
25
1
    fn test_car_basic() {
26
1
        let result = eval_expr("(car '(1 2 3))").unwrap();
27
1
        assert_eq!(result, num(1));
28
1
    }
29

            
30
    #[test]
31
1
    fn test_cdr_basic() {
32
        // CDR should return something, but the exact structure might depend on implementation
33
1
        let result = eval_expr("(cdr '(1 2 3))");
34
1
        assert!(result.is_ok(), "CDR should work with a basic list");
35
1
    }
36

            
37
    #[test]
38
1
    fn test_car_with_nil() {
39
1
        let result = eval_expr("(car nil)").unwrap();
40
1
        assert_eq!(result, Value::Nil);
41
1
    }
42

            
43
    #[test]
44
1
    fn test_cdr_with_nil() {
45
1
        let result = eval_expr("(cdr nil)").unwrap();
46
1
        assert_eq!(result, Value::Nil);
47
1
    }
48

            
49
    #[test]
50
1
    fn test_car_with_empty_list() {
51
1
        let result = eval_expr("(car '())").unwrap();
52
1
        assert_eq!(result, Value::Nil);
53
1
    }
54

            
55
    #[test]
56
1
    fn test_cdr_with_empty_list() {
57
1
        let result = eval_expr("(cdr '())").unwrap();
58
1
        assert_eq!(result, Value::Nil);
59
1
    }
60

            
61
    #[test]
62
1
    fn test_car_wrong_arity() {
63
1
        let result = eval_expr("(car 1 2)");
64
1
        assert!(
65
1
            result.is_err(),
66
            "CAR should reject wrong number of arguments"
67
        );
68
1
    }
69

            
70
    #[test]
71
1
    fn test_cdr_wrong_arity() {
72
1
        let result = eval_expr("(cdr)");
73
1
        assert!(
74
1
            result.is_err(),
75
            "CDR should reject wrong number of arguments"
76
        );
77
1
    }
78

            
79
    #[test]
80
1
    fn test_car_with_non_list() {
81
1
        let result = eval_expr("(car 42)");
82
1
        assert!(result.is_err(), "CAR should reject non-list arguments");
83
1
    }
84

            
85
    #[test]
86
1
    fn test_cdr_with_non_list() {
87
1
        let result = eval_expr("(cdr \"hello\")");
88
1
        assert!(result.is_err(), "CDR should reject non-list arguments");
89
1
    }
90

            
91
    #[test]
92
1
    fn test_car_with_string_element() {
93
1
        let result = eval_expr("(car '(\"hello\" \"world\"))").unwrap();
94
1
        assert_eq!(result, Value::String("hello".into()));
95
1
    }
96

            
97
    #[test]
98
1
    fn test_car_cdr_case_insensitivity() {
99
        // nomiscript should handle case-insensitive function names
100
1
        let result1 = eval_expr("(CAR '(1 2))").unwrap();
101
1
        let result2 = eval_expr("(car '(1 2))").unwrap();
102
1
        assert_eq!(result1, result2);
103
1
        assert_eq!(result1, num(1));
104
1
    }
105
}