Lines
100 %
Functions
Branches
use crate::ast::{Expr, KeyParam, Number};
use crate::runtime::Value;
use super::{format_expr, format_form, format_runtime_value, not_callable};
fn sym(name: &str) -> Expr {
Expr::Symbol(name.into())
}
#[test]
fn nil_and_the_empty_list_print_as_nil() {
assert_eq!(format_expr(&Expr::Nil), "nil");
assert_eq!(format_expr(&Expr::List(vec![])), "nil");
assert_eq!(format_runtime_value(&Value::Nil), "nil");
fn a_struct_value_prints_as_common_lisp_does() {
let value = Value::Struct {
name: "PT".to_string(),
fields: vec![("X".to_string(), Value::Number(Number::Index(8)))],
};
assert_eq!(format_runtime_value(&value), "#S(PT :X 8)");
fn a_cons_chain_ending_in_nil_prints_as_a_proper_list() {
assert_eq!(format_expr(&Expr::cons(sym("A"), Expr::Nil)), "(A)");
assert_eq!(
format_expr(&Expr::cons(sym("A"), Expr::cons(sym("B"), Expr::Nil))),
"(A B)"
);
fn a_cons_chain_ending_in_an_atom_keeps_its_dot() {
assert_eq!(format_expr(&Expr::cons(sym("A"), sym("B"))), "(A . B)");
format_expr(&Expr::cons(sym("A"), Expr::cons(sym("B"), sym("C")))),
"(A B . C)"
fn not_callable_names_the_head_as_the_reader_writes_it() {
let quoted = Expr::Quote(Box::new(Expr::List(vec![
Expr::Number(Number::Index(1.into())),
Expr::Number(Number::Index(2.into())),
])));
not_callable("ed).to_string(),
"Compile error: not callable: '(1 2)"
not_callable(&Expr::Number(Number::Index(5.into()))).to_string(),
"Compile error: not callable: 5"
fn not_callable_tells_a_quoted_symbol_head_to_use_funcall() {
let quoted = Expr::Quote(Box::new(Expr::symbol("CAR")));
"Compile error: illegal function call: ('CAR …); use (funcall 'car …)"
fn text(s: &str) -> Expr {
Expr::String(s.into())
fn a_form_keeps_string_quotes_at_any_depth() {
assert_eq!(format_form(&text("5")), "\"5\"");
let nested = Expr::Quote(Box::new(Expr::List(vec![
Expr::List(vec![text("2")]),
assert_eq!(format_form(&nested), "'(1 (\"2\"))");
fn a_form_escapes_quotes_and_backslashes() {
assert_eq!(format_form(&text("a\"b\\c")), "\"a\\\"b\\\\c\"");
fn a_form_prints_a_dotted_pair_with_quoted_strings() {
let pair = Expr::cons(text("a"), text("b"));
assert_eq!(format_form(&pair), "(\"a\" . \"b\")");
fn a_form_quotes_strings_inside_lambda_defaults() {
let params = crate::ast::LambdaParams {
optional: vec![("X".into(), Some(text("d")))],
..crate::ast::LambdaParams::simple(vec![])
let lambda = Expr::Lambda(params, Box::new(sym("X")));
assert_eq!(format_form(&lambda), "(LAMBDA (&optional (X \"d\")) X)");
assert_eq!(format_expr(&lambda), "(LAMBDA (&optional (X d)) X)");
fn princ_printing_leaves_strings_bare() {
assert_eq!(format_expr(&text("a\"b")), "a\"b");
format_expr(&Expr::List(vec![
text("2")
])),
"(1 2)"
fn not_callable_quotes_a_string_head() {
not_callable(&text("5")).to_string(),
"Compile error: not callable: \"5\""
fn a_labels_alias_prints_as_the_function_the_user_wrote() {
let alias = Expr::List(vec![
Expr::Symbol("M (LABELS 7)".to_string()),
Expr::Symbol("Y".to_string()),
]);
assert_eq!(format_form(&alias), "(M Y)");
fn a_form_spells_a_renamed_key_parameter_with_its_keyword() {
let mut params = crate::ast::LambdaParams::simple(vec![]);
params.key = vec![
KeyParam {
name: "FMT".into(),
keyword: "FORMAT".into(),
default: Some(text("spec")),
},
KeyParam::new("W", None),
];
let lambda = Expr::Lambda(params, Box::new(sym("FMT")));
format_form(&lambda),
"(LAMBDA (&key ((:format FMT) \"spec\") W) FMT)"
fn a_whole_scalar_prints_with_its_denominator() {
let whole = Expr::Number(Number::Scalar(2.into()));
assert_eq!(format_expr(&whole), "2/1");
assert_eq!(format_form(&whole), "2/1");
assert_eq!(format_expr(&Expr::Number(Number::Index(2.into()))), "2");