1
//! By-value execution coverage for every `LambdaParams` shape
2
//! (`&optional`/`&rest`/`&key`/`&aux`) and lambda arity errors.
3
//!
4
//! Found missing during Phase 7 (`doc/ops/0010-common-lisp-booleans.org`)
5
//! review: `compile_and_bind_lambda_params` (`compiler/expr/call.rs`) and
6
//! its eval-only mirror `call_lambda` (`compiler/expr/eval.rs`) — both
7
//! reworked by this phase's argument-effect-ordering fixes — had only
8
//! compile-and-validate coverage (`scripting/nomiscript/tests/codegen/
9
//! binding_extras.rs`, `lambda_forms.rs`), never a test that calls the
10
//! function and checks the returned VALUE for `&optional` (with and
11
//! without a default), `&rest`, `&key` (supplied and defaulted), or
12
//! `&aux`.
13
//!
14
//! Each defun call below sits in EFFECT position (a non-final `progn`
15
//! form, all-constant arguments) rather than value/tail position: that
16
//! forces the SAME call through BOTH implementations in one execution —
17
//! `compiler/expr/effect.rs`'s `try_compile_defun_call_for_effect`
18
//! classifies it via a disposable-clone `eval_value`, which dispatches to
19
//! `call_lambda` (`eval.rs`); since the call is compile-time-constant, that
20
//! classification returns `Ok(false)` and the caller falls through to
21
//! `compile_value_producing_call_for_effect`'s `compile_for_stack`, which
22
//! re-compiles the SAME call for real through `compile_and_bind_lambda_params`
23
//! (`call.rs`). Both files' `bind_required_params` / `bind_optional_params`
24
//! / `bind_rest_param` / `bind_key_params` / `bind_aux_params` /
25
//! `install_bound_params` are exercised this way.
26

            
27
#![cfg(not(miri))]
28

            
29
use nms::interpreter::Interpreter;
30
use scripting::nomiscript::{Number, Value};
31

            
32
6
fn num(n: i64) -> Value {
33
6
    Value::Number(Number::Index(n))
34
6
}
35

            
36
#[test]
37
1
fn optional_param_uses_its_default_when_omitted() {
38
1
    let mut interp = Interpreter::new(false).unwrap();
39
1
    let results = interp
40
1
        .eval("(progn (defun f-opt (a &optional (b 10)) (+ a b)) (f-opt 1) (f-opt 1))")
41
1
        .unwrap();
42
1
    assert_eq!(results, vec![num(11)]);
43
1
}
44

            
45
#[test]
46
1
fn optional_param_uses_the_supplied_value_when_present() {
47
1
    let mut interp = Interpreter::new(false).unwrap();
48
1
    let results = interp
49
1
        .eval("(progn (defun f-opt (a &optional (b 10)) (+ a b)) (f-opt 1 2) (f-opt 1 2))")
50
1
        .unwrap();
51
1
    assert_eq!(results, vec![num(3)]);
52
1
}
53

            
54
#[test]
55
1
fn optional_param_with_no_default_falls_back_to_nil() {
56
    // No default expression at all: `&optional b` (no `(b default)` form)
57
    // binds `b` to `nil` when omitted.
58
1
    let mut interp = Interpreter::new(false).unwrap();
59
1
    let results = interp
60
1
        .eval("(progn (defun f-opt-nil (a &optional b) (null? b)) (f-opt-nil 1) (f-opt-nil 1))")
61
1
        .unwrap();
62
1
    assert_eq!(results, vec![Value::T]);
63
1
}
64

            
65
#[test]
66
1
fn rest_param_collects_every_remaining_argument() {
67
1
    let mut interp = Interpreter::new(false).unwrap();
68
1
    let results = interp
69
1
        .eval("(progn (defun f-rest (&rest xs) (length xs)) (f-rest 1 2 3) (f-rest 1 2 3))")
70
1
        .unwrap();
71
1
    assert_eq!(results, vec![num(3)]);
72
1
}
73

            
74
#[test]
75
1
fn rest_param_is_empty_when_no_extra_arguments() {
76
1
    let mut interp = Interpreter::new(false).unwrap();
77
1
    let results = interp
78
1
        .eval("(progn (defun f-rest0 (&rest xs) (null? xs)) (f-rest0) (f-rest0))")
79
1
        .unwrap();
80
1
    assert_eq!(results, vec![Value::T]);
81
1
}
82

            
83
#[test]
84
1
fn keyword_param_uses_its_default_when_omitted() {
85
1
    let mut interp = Interpreter::new(false).unwrap();
86
1
    let results = interp
87
1
        .eval("(progn (defun f-key (&key (level 0)) level) (f-key) (f-key))")
88
1
        .unwrap();
89
1
    assert_eq!(results, vec![num(0)]);
90
1
}
91

            
92
#[test]
93
1
fn keyword_param_uses_the_supplied_value_when_present() {
94
1
    let mut interp = Interpreter::new(false).unwrap();
95
1
    let results = interp
96
1
        .eval("(progn (defun f-key (&key (level 0)) level) (f-key :level 5) (f-key :level 5))")
97
1
        .unwrap();
98
1
    assert_eq!(results, vec![num(5)]);
99
1
}
100

            
101
#[test]
102
1
fn aux_param_is_initialized_from_an_already_bound_required_param() {
103
1
    let mut interp = Interpreter::new(false).unwrap();
104
1
    let results = interp
105
1
        .eval("(progn (defun f-aux (a &aux (b (+ a 1))) b) (f-aux 5) (f-aux 5))")
106
1
        .unwrap();
107
1
    assert_eq!(results, vec![num(6)]);
108
1
}
109

            
110
#[test]
111
1
fn aux_param_with_no_init_falls_back_to_nil() {
112
1
    let mut interp = Interpreter::new(false).unwrap();
113
1
    let results = interp
114
1
        .eval("(progn (defun f-aux-nil (a &aux b) (null? b)) (f-aux-nil 5) (f-aux-nil 5))")
115
1
        .unwrap();
116
1
    assert_eq!(results, vec![Value::T]);
117
1
}
118

            
119
#[test]
120
1
fn too_few_arguments_is_an_arity_error() {
121
1
    let mut interp = Interpreter::new(false).unwrap();
122
1
    let err = interp
123
1
        .eval("(defun f-arity (a b) (+ a b)) (f-arity 1)")
124
1
        .expect_err("too few required arguments must be an arity error");
125
1
    assert!(!err.to_string().is_empty());
126
1
}
127

            
128
#[test]
129
1
fn too_many_arguments_is_an_arity_error() {
130
    // No `&rest`/`&key`, so the max is `required + optional` — a third
131
    // argument overflows it.
132
1
    let mut interp = Interpreter::new(false).unwrap();
133
1
    let err = interp
134
1
        .eval("(defun f-arity2 (a) a) (f-arity2 1 2)")
135
1
        .expect_err("too many arguments with no &rest/&key must be an arity error");
136
1
    assert!(!err.to_string().is_empty());
137
1
}