1
//! Runtime behavior of the universal nomiscript prelude (ADR-0029).
2
//!
3
//! The prelude math:* / list:* helpers are auto-loaded into `with_builtins()`,
4
//! so these forms eval without defining anything. Pure-constant calls
5
//! const-fold through the eval path, exercising the helper bodies directly.
6

            
7
use nomiscript::{
8
    Reader, eval_program,
9
    runtime::{SymbolTable, Value},
10
};
11

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

            
18
4
fn num(n: i64) -> Value {
19
4
    Value::Number(num_rational::Ratio::from_integer(n))
20
4
}
21

            
22
#[test]
23
1
fn math_square_computes() {
24
1
    assert_eq!(eval_expr("(math:square 7)").expect("square"), num(49));
25
1
}
26

            
27
#[test]
28
1
fn math_cube_computes() {
29
1
    assert_eq!(eval_expr("(math:cube 3)").expect("cube"), num(27));
30
1
}
31

            
32
#[test]
33
1
fn list_second_extracts() {
34
1
    assert_eq!(
35
1
        eval_expr("(list:second (list 10 20 30))").expect("second"),
36
1
        num(20)
37
    );
38
1
}
39

            
40
#[test]
41
1
fn list_third_extracts() {
42
1
    assert_eq!(
43
1
        eval_expr("(list:third (list 10 20 30))").expect("third"),
44
1
        num(30)
45
    );
46
1
}