Lines
94.29 %
Functions
63.64 %
Branches
100 %
// Skipped under Miri: these tests compile+run wasm via wasmtime, whose
// Cranelift backend refuses to run under Miri.
#![cfg(not(miri))]
//! A runtime list written as a script's result decodes element by element,
//! nested lists and mixed element types included, instead of a `t` marker.
use nms::interpreter::Interpreter;
use scripting::nomiscript::{Number, Value, format_value};
fn printed(src: &str) -> String {
let mut interp = Interpreter::new(false).unwrap();
let results = interp
.eval(src)
.unwrap_or_else(|e| panic!("eval {src:?}: {e}"));
let value = results
.into_iter()
.next_back()
.unwrap_or_else(|| panic!("eval {src:?} produced no value"));
format_value(&value)
}
fn assert_prints(cases: &[(&str, &str)]) {
for (src, expected) in cases {
assert_eq!(printed(src), *expected, "{src}");
#[test]
fn a_runtime_list_prints_its_elements() {
assert_prints(&[
("(list (entity-count) 2)", "(0 2)"),
("(cons (entity-count) nil)", "(0)"),
("(cdr (list (entity-count) 2))", "(2)"),
// A runtime test: `(if (entity-count) …)` would fold, because an
// index is never nil, and so would not exercise a runtime list.
("(if (> (entity-count) 5) '(1 2) '(3 4))", "(3 4)"),
]);
fn an_accumulated_local_prints_as_a_list() {
("(let ((acc nil)) (setf acc (cons 1 acc)) acc)", "(1)"),
("(let ((r (list 0))) (setf r (cons 1 r)) r)", "(1 0)"),
("(let ((acc nil)) (setf acc (cons 1 acc)))", "(1)"),
fn mixed_elements_print_by_their_runtime_shape() {
(
"(list \"a\" 1/2 t nil (entity-count))",
"(\"a\" 1/2 t nil 0)",
),
("(cons 1/2 (cons (entity-count) nil))", "(1/2 0)"),
fn a_list_in_a_dynamic_cell_prints_nested() {
"(if (> (entity-count) 5) \"s\" (list (entity-count) 2))",
"(0 2)",
"(list (if (> (entity-count) 5) \"s\" (list (entity-count) 2)) 3)",
"((0 2) 3)",
"(list 1 (if (> (entity-count) 5) \"s\" (list (entity-count) (if (> (entity-count) 5) \"t\" (list 7)))) 3)",
"(1 (0 (7)) 3)",
("(or (> (transaction-tag-count 0) 0) (list 1))", "(1)"),
fn an_empty_runtime_list_is_nil() {
.eval("(cdr (list (entity-count)))")
.unwrap_or_else(|e| panic!("{e}"));
assert_eq!(results.last(), Some(&Value::Nil));
.eval("(car (list (entity-count)))")
assert_eq!(results.last(), Some(&Value::Number(Number::Index(0))));