Lines
96.77 %
Functions
100 %
Branches
use crate::ast::Expr;
use crate::reader::Reader;
fn template(source: &str) -> Expr {
match Reader::parse(source).expect("parse").exprs.remove(0) {
Expr::Quasiquote(inner) => *inner,
other => other,
}
fn names(template: &Expr) -> Vec<String> {
template
.unquoted_forms()
.iter()
.map(|form| format!("{form:?}"))
.collect()
#[test]
fn unquoted_forms_are_listed_in_order() {
assert_eq!(names(&template("`(a ,x ,@y b)")).len(), 2);
fn an_unquote_under_a_quote_is_found() {
let found = template("`(a ',x b)").unquoted_forms().len();
assert_eq!(found, 1);
fn plain_template_text_has_no_unquoted_forms() {
assert!(template("`(a (setf x 1) b)").unquoted_forms().is_empty());
fn a_nested_template_unquote_is_not_run_at_the_outer_depth() {
assert!(template("`(a `(b ,x))").unquoted_forms().is_empty());
assert_eq!(template("`(a `(b ,,x))").unquoted_forms().len(), 1);
fn map_unquoted_rewrites_only_the_unquoted_forms() {
let mapped = template("`(a ',x ,@y)")
.map_unquoted(&mut |_| Ok(Expr::Nil))
.expect("map");
assert_eq!(mapped.unquoted_forms(), vec![&Expr::Nil, &Expr::Nil]);