Lines
95.74 %
Functions
77.78 %
Branches
100 %
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! Which variables a loop body assigns, and the element an accumulator
//! store builds, both follow lexical scope: a rebinding shadows the outer
//! variable, quoted data assigns nothing, and disagreeing branch arms widen.
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Number, Value};
fn eval_last(src: &str) -> Value {
let mut interp = Interpreter::new(false).unwrap();
let results = interp
.eval(src)
.unwrap_or_else(|e| panic!("eval {src:?}: {e}"));
results
.into_iter()
.next_back()
.unwrap_or_else(|| panic!("eval {src:?} produced no value"))
}
fn n(v: i64) -> Value {
Value::Number(Number::Index(v))
#[test]
fn a_shadowed_store_leaves_the_outer_nil_a_nil() {
assert_eq!(
eval_last(
"(let ((x nil)) (dolist (i (list (entity-count))) (let ((x 0)) (setf x 5))) (if x 1 2))"
),
n(2)
);
fn stores_to_a_shadowing_binding_do_not_retype_the_outer_variable() {
for src in [
"(let ((x nil)) (dolist (i (list (entity-count))) (let ((x 0)) (setf x 5))) (length x))",
"(let ((x nil)) (dolist (i (list (entity-count))) (funcall (lambda (x) (setf x 5) x) 1)) (length x))",
"(let ((x nil)) (dolist (i (list (entity-count))) (do ((x 0 (+ x 1))) ((> x 2)) (setf x (+ x 0)))) (length x))",
] {
assert_eq!(eval_last(src), n(0), "{src}");
fn a_quoted_store_assigns_nothing() {
"(let ((x nil)) (dolist (i (list (entity-count))) (quote (setf x 5))) (length x))"
n(0)
fn a_string_accumulator_takes_its_element_from_the_car() {
eval_last("(let ((r nil)) (setf r (cons \"a\" r)) (length r))"),
n(1)
fn disagreeing_accumulator_arms_widen_the_element() {
"(let ((r nil)) (setf r (if (> (entity-count) 5) (cons 1 r) (list \"a\"))) (length r))"