Lines
100 %
Functions
Branches
//! By-value execution coverage for every `LambdaParams` shape
//! (`&optional`/`&rest`/`&key`/`&aux`) and lambda arity errors.
//!
//! Found missing during Phase 7 (`doc/ops/0010-common-lisp-booleans.org`)
//! review: `compile_and_bind_lambda_params` (`compiler/expr/call.rs`) and
//! its eval-only mirror `call_lambda` (`compiler/expr/eval.rs`) — both
//! reworked by this phase's argument-effect-ordering fixes — had only
//! compile-and-validate coverage (`scripting/nomiscript/tests/codegen/
//! binding_extras.rs`, `lambda_forms.rs`), never a test that calls the
//! function and checks the returned VALUE for `&optional` (with and
//! without a default), `&rest`, `&key` (supplied and defaulted), or
//! `&aux`.
//! Each defun call below sits in EFFECT position (a non-final `progn`
//! form, all-constant arguments) rather than value/tail position: that
//! forces the SAME call through BOTH implementations in one execution —
//! `compiler/expr/effect.rs`'s `try_compile_defun_call_for_effect`
//! classifies it via a disposable-clone `eval_value`, which dispatches to
//! `call_lambda` (`eval.rs`); since the call is compile-time-constant, that
//! classification returns `Ok(false)` and the caller falls through to
//! `compile_value_producing_call_for_effect`'s `compile_for_stack`, which
//! re-compiles the SAME call for real through `compile_and_bind_lambda_params`
//! (`call.rs`). Both files' `bind_required_params` / `bind_optional_params`
//! / `bind_rest_param` / `bind_key_params` / `bind_aux_params` /
//! `install_bound_params` are exercised this way.
#![cfg(not(miri))]
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Number, Value};
fn num(n: i64) -> Value {
Value::Number(Number::Index(n))
}
#[test]
fn optional_param_uses_its_default_when_omitted() {
let mut interp = Interpreter::new(false).unwrap();
let results = interp
.eval("(progn (defun f-opt (a &optional (b 10)) (+ a b)) (f-opt 1) (f-opt 1))")
.unwrap();
assert_eq!(results, vec![num(11)]);
fn optional_param_uses_the_supplied_value_when_present() {
.eval("(progn (defun f-opt (a &optional (b 10)) (+ a b)) (f-opt 1 2) (f-opt 1 2))")
assert_eq!(results, vec![num(3)]);
fn optional_param_with_no_default_falls_back_to_nil() {
// No default expression at all: `&optional b` (no `(b default)` form)
// binds `b` to `nil` when omitted.
.eval("(progn (defun f-opt-nil (a &optional b) (null? b)) (f-opt-nil 1) (f-opt-nil 1))")
assert_eq!(results, vec![Value::T]);
fn rest_param_collects_every_remaining_argument() {
.eval("(progn (defun f-rest (&rest xs) (length xs)) (f-rest 1 2 3) (f-rest 1 2 3))")
fn rest_param_is_empty_when_no_extra_arguments() {
.eval("(progn (defun f-rest0 (&rest xs) (null? xs)) (f-rest0) (f-rest0))")
fn keyword_param_uses_its_default_when_omitted() {
.eval("(progn (defun f-key (&key (level 0)) level) (f-key) (f-key))")
assert_eq!(results, vec![num(0)]);
fn keyword_param_uses_the_supplied_value_when_present() {
.eval("(progn (defun f-key (&key (level 0)) level) (f-key :level 5) (f-key :level 5))")
assert_eq!(results, vec![num(5)]);
fn aux_param_is_initialized_from_an_already_bound_required_param() {
.eval("(progn (defun f-aux (a &aux (b (+ a 1))) b) (f-aux 5) (f-aux 5))")
assert_eq!(results, vec![num(6)]);
fn aux_param_with_no_init_falls_back_to_nil() {
.eval("(progn (defun f-aux-nil (a &aux b) (null? b)) (f-aux-nil 5) (f-aux-nil 5))")
fn too_few_arguments_is_an_arity_error() {
let err = interp
.eval("(defun f-arity (a b) (+ a b)) (f-arity 1)")
.expect_err("too few required arguments must be an arity error");
assert!(!err.to_string().is_empty());
fn too_many_arguments_is_an_arity_error() {
// No `&rest`/`&key`, so the max is `required + optional` — a third
// argument overflows it.
.eval("(defun f-arity2 (a) a) (f-arity2 1 2)")
.expect_err("too many arguments with no &rest/&key must be an arity error");