Lines
100 %
Functions
Branches
use super::{Arm, arm_of_form, in_runtime_branch, progn_value_arm};
use crate::ast::Fraction;
use crate::ast::{Expr, Number, WasmType};
use crate::error::Error;
use crate::runtime::SymbolTable;
#[test]
fn in_runtime_branch_marks_only_the_closure() {
let mut symbols = SymbolTable::new();
let inside = in_runtime_branch(&mut symbols, |symbols| {
Ok(symbols.is_under_any_runtime_guard())
});
assert!(inside.unwrap());
assert!(!symbols.is_under_any_runtime_guard());
}
fn in_runtime_branch_prefers_the_closure_error() {
let result: crate::error::Result<()> = in_runtime_branch(&mut symbols, |symbols| {
symbols.exit_runtime_branch()?;
Err(Error::Compile("closure failed".to_string()))
assert!(result.unwrap_err().to_string().contains("closure failed"));
fn in_runtime_branch_reports_an_exit_failure_after_a_successful_closure() {
let result = in_runtime_branch(&mut symbols, |symbols| symbols.exit_runtime_branch());
assert!(
result
.unwrap_err()
.to_string()
.contains("exit_runtime_branch()")
);
fn progn_ending_in(head: &str, value: Expr) -> Expr {
Expr::List(vec![
Expr::Symbol(head.into()),
Expr::Number(Number::Index(1.into())),
value,
])
fn progn_nil(head: &str) -> Expr {
progn_ending_in(head, Expr::Nil)
fn a_progn_ending_in_nil_or_a_number_has_that_arm() {
assert_eq!(progn_value_arm(&progn_nil("PROGN")), Some(Arm::Nil));
assert_eq!(progn_value_arm(&progn_nil("LIST")), None);
let ratio = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
assert_eq!(
progn_value_arm(&progn_ending_in("PROGN", ratio)),
Some(Arm::NumberLiteral(WasmType::Ratio))
let nested = progn_ending_in(
"PROGN",
progn_ending_in("PROGN", Expr::Number(Number::Index(2.into()))),
progn_value_arm(&nested),
Some(Arm::NumberLiteral(WasmType::I32))
let call = Expr::List(vec![Expr::Symbol("F".into())]);
assert_eq!(progn_value_arm(&progn_ending_in("PROGN", call)), None);
progn_value_arm(&Expr::Number(Number::Index(2.into()))),
None
fn arm_of_form_defers_a_progn_ending_in_nil() {
let value = Expr::Number(Number::Index(5.into()));
let arm = arm_of_form(&progn_nil("PROGN"), &value).unwrap();
assert!(matches!(arm, Arm::Nil));
fn arm_of_form_keeps_the_literal_a_progn_ends_in() {
let runtime = Expr::WasmRuntime(WasmType::I32);
let arm = arm_of_form(
&progn_ending_in("PROGN", Expr::Number(Number::Index(2.into()))),
&runtime,
)
.unwrap();
assert_eq!(arm, Arm::NumberLiteral(WasmType::I32));
fn a_value_with_no_stack_type_is_a_structured_error() {
let cons = Expr::Cons(
Box::new(Expr::Number(Number::Index(1.into()))),
Box::new(Expr::Number(Number::Index(2.into()))),
let err = arm_of_form(&cons, &cons).unwrap_err();
assert!(err.to_string().contains("no runtime value type"), "{err}");
fn a_quoted_symbol_arm_is_a_symbol() {
let quoted = Expr::Quote(Box::new(Expr::Symbol("FOO".into())));
arm_of_form("ed, "ed).unwrap(),
Arm::Value(WasmType::SymbolRef)