Lines
98.08 %
Functions
93.1 %
Branches
100 %
// Skipped under Miri: these tests compile+run wasm via wasmtime.
#![cfg(not(miri))]
//! A compile-time-only DEFVAR inside a branch that a runtime test selects is
//! refused: the symbol table cannot make it conditional. A SETF of a global an
//! earlier unit defined is a runtime cell, so it runs only when its branch is
//! taken. The same effects inside the runtime test itself run unconditionally,
//! as does a SETF of a runtime-backed local.
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Fraction, Number, Value};
const DEFVAR_REFUSED: &str = "DEFVAR: a definition must be statically executable";
fn eval_error(interp: &mut Interpreter, src: &str) -> String {
match interp.eval(src) {
Ok(values) => panic!("expected {src:?} to be refused, got {values:?}"),
Err(e) => e.to_string(),
}
fn eval_last(interp: &mut Interpreter, src: &str) -> Value {
interp
.eval(src)
.unwrap_or_else(|e| panic!("eval {src:?}: {e}"))
.into_iter()
.next_back()
.unwrap_or_else(|| panic!("eval {src:?} produced no value"))
fn n(v: i64) -> Value {
Value::Number(Number::Index(v))
/// Runs `form` after `(defvar *x* 1)` and asserts `*x*` is `after`.
fn assert_setf_conditional(form: &str, after: i64) {
let mut interp = Interpreter::new(false).unwrap();
eval_last(&mut interp, "(defvar *x* 1) *x*");
eval_last(&mut interp, form);
assert_eq!(eval_last(&mut interp, "*x*"), n(after), "{form}");
fn assert_defvar_refused(form: &str, name: &str) {
let err = eval_error(&mut interp, form);
assert!(err.contains(DEFVAR_REFUSED), "{form}: {err}");
assert!(interp.eval(name).is_err(), "{name} must stay unbound");
#[test]
fn cond_deferred_nil_arm_setf_is_conditional() {
assert_setf_conditional(
"(block b (cond ((= (entity-count) 9) (setf *x* 42) nil) (t 1)))",
1,
);
fn cond_deferred_literal_arm_setf_is_conditional() {
"(block b (cond ((= (entity-count) 9) (setf *x* 42) 5) (t 1)))",
fn cond_constant_list_arm_setf_is_conditional() {
"(block b (cond ((= (entity-count) 9) (setf *x* 42) '(1 2)) (t nil)))",
fn cond_tail_arm_setf_is_conditional() {
"(block b (cond ((= (entity-count) 9) 1) (t (setf *x* 42) 2)))",
42,
fn cond_later_clause_body_setf_is_conditional() {
"(block b (cond ((= (entity-count) 9) 1) ((= (entity-count) 8) (setf *x* 42) nil) (t 3)))",
fn cond_later_clause_test_setf_is_conditional() {
"(block b (cond ((= (entity-count) 9) 1) ((progn (setf *x* 42) (= (entity-count) 8)) 2) (t 3)))",
fn cond_tail_arm_defvar_is_refused_in_a_repl_session() {
assert_defvar_refused(
"(cond ((= (entity-count) 5) 1) (t (defvar *z* 7) nil))",
"*z*",
fn cond_first_clause_body_defvar_is_refused() {
"(cond ((= (entity-count) 5) (defvar *z* 7) nil) (t 1))",
fn if_then_arm_setf_in_effect_position_is_conditional() {
assert_setf_conditional("(if (= (entity-count) 5) (setf *x* 42) 0) 1", 1);
fn if_then_arm_setf_in_stack_position_is_conditional() {
assert_setf_conditional("(block b (if (= (entity-count) 5) (setf *x* 42) 0))", 1);
fn if_else_arm_setf_is_conditional() {
assert_setf_conditional("(block b (if (= (entity-count) 0) 1 (setf *x* 42)))", 1);
fn if_arm_setf_in_a_let_initializer_is_conditional() {
assert_setf_conditional("(let ((v (if (= (entity-count) 5) (setf *x* 42) 0))) v)", 1);
fn when_body_setf_is_conditional() {
assert_setf_conditional("(when (= (entity-count) 5) (setf *x* 42)) 1", 1);
fn if_arm_defvar_is_refused() {
"(if (= (entity-count) 5) (progn (defvar *w* 5) nil) 3)",
"*w*",
fn a_defvar_in_the_first_cond_test_still_applies() {
eval_last(
&mut interp,
"(cond ((progn (defvar *q* 3) (= (entity-count) 5)) 1) (t 2))",
assert_eq!(eval_last(&mut interp, "*q*"), n(3));
fn a_setf_in_the_first_cond_test_still_applies() {
"(block b (cond ((progn (setf *x* 9) (= (entity-count) 5)) 1) (t 2)))",
assert_eq!(eval_last(&mut interp, "*x*"), n(9));
fn a_defvar_in_the_if_test_still_applies() {
"(if (progn (defvar *q* 4) (= (entity-count) 5)) 1 2)",
assert_eq!(eval_last(&mut interp, "*q*"), n(4));
fn a_runtime_backed_setf_in_a_branch_still_runs() {
let value = eval_last(
"(let ((s 0)) (if (= (entity-count) 0) (setf s 5) 0) s)",
assert_eq!(value, n(5));
fn integer_literal_cond_arms_adopt_a_sibling_scalar() {
"(let ((v (cond ((= (entity-count) 5) 1/2) ((= (entity-count) 0) 1) (t 2)))) (+ v 1/2))",
assert_eq!(value, Value::Number(Number::Scalar(Fraction::new(3, 2))));
const HANDLER_BODY: &str = "(if (= (entity-count) 0) (error 'boom \"x\") 5)";
fn handler_clause_setf_runs_when_taken() {
&format!("(block b (handler-case {HANDLER_BODY} (t (e) (setf *x* 42) 2)))"),
fn handler_clause_defvar_is_refused() {
&format!("(block b (handler-case {HANDLER_BODY} (boom (e) (defvar *h* 3) 2)))"),
"*h*",
fn a_runtime_backed_setf_in_a_handler_clause_still_runs() {
&format!("(let ((s 0)) (handler-case {HANDLER_BODY} (t (e) (setf s 7) 2)) s)"),
assert_eq!(value, n(7));