Lines
100 %
Functions
Branches
// Tests for CAR and CDR native functions
// These tests verify that the CAR and CDR functions work correctly
// through the nomiscript evaluation system.
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 car_cdr_basic_tests {
use super::*;
#[test]
fn test_car_basic() {
let result = eval_expr("(car '(1 2 3))").unwrap();
assert_eq!(result, num(1));
fn test_cdr_basic() {
// CDR should return something, but the exact structure might depend on implementation
let result = eval_expr("(cdr '(1 2 3))");
assert!(result.is_ok(), "CDR should work with a basic list");
fn test_car_with_nil() {
let result = eval_expr("(car nil)").unwrap();
assert_eq!(result, Value::Nil);
fn test_cdr_with_nil() {
let result = eval_expr("(cdr nil)").unwrap();
fn test_car_with_empty_list() {
let result = eval_expr("(car '())").unwrap();
fn test_cdr_with_empty_list() {
let result = eval_expr("(cdr '())").unwrap();
fn test_car_wrong_arity() {
let result = eval_expr("(car 1 2)");
assert!(
result.is_err(),
"CAR should reject wrong number of arguments"
);
fn test_cdr_wrong_arity() {
let result = eval_expr("(cdr)");
"CDR should reject wrong number of arguments"
fn test_car_with_non_list() {
let result = eval_expr("(car 42)");
assert!(result.is_err(), "CAR should reject non-list arguments");
fn test_cdr_with_non_list() {
let result = eval_expr("(cdr \"hello\")");
assert!(result.is_err(), "CDR should reject non-list arguments");
fn test_car_with_string_element() {
let result = eval_expr("(car '(\"hello\" \"world\"))").unwrap();
assert_eq!(result, Value::String("hello".into()));
fn test_car_cdr_case_insensitivity() {
// nomiscript should handle case-insensitive function names
let result1 = eval_expr("(CAR '(1 2))").unwrap();
let result2 = eval_expr("(car '(1 2))").unwrap();
assert_eq!(result1, result2);
assert_eq!(result1, num(1));