1
use crate::ast::Expr;
2
use crate::reader::Reader;
3

            
4
6
fn template(source: &str) -> Expr {
5
6
    match Reader::parse(source).expect("parse").exprs.remove(0) {
6
6
        Expr::Quasiquote(inner) => *inner,
7
        other => other,
8
    }
9
6
}
10

            
11
1
fn names(template: &Expr) -> Vec<String> {
12
1
    template
13
1
        .unquoted_forms()
14
1
        .iter()
15
2
        .map(|form| format!("{form:?}"))
16
        .collect()
17
1
}
18

            
19
#[test]
20
1
fn unquoted_forms_are_listed_in_order() {
21
1
    assert_eq!(names(&template("`(a ,x ,@y b)")).len(), 2);
22
1
}
23

            
24
#[test]
25
1
fn an_unquote_under_a_quote_is_found() {
26
1
    let found = template("`(a ',x b)").unquoted_forms().len();
27
1
    assert_eq!(found, 1);
28
1
}
29

            
30
#[test]
31
1
fn plain_template_text_has_no_unquoted_forms() {
32
1
    assert!(template("`(a (setf x 1) b)").unquoted_forms().is_empty());
33
1
}
34

            
35
#[test]
36
1
fn a_nested_template_unquote_is_not_run_at_the_outer_depth() {
37
1
    assert!(template("`(a `(b ,x))").unquoted_forms().is_empty());
38
1
    assert_eq!(template("`(a `(b ,,x))").unquoted_forms().len(), 1);
39
1
}
40

            
41
#[test]
42
1
fn map_unquoted_rewrites_only_the_unquoted_forms() {
43
1
    let mapped = template("`(a ',x ,@y)")
44
2
        .map_unquoted(&mut |_| Ok(Expr::Nil))
45
1
        .expect("map");
46
1
    assert_eq!(mapped.unquoted_forms(), vec![&Expr::Nil, &Expr::Nil]);
47
1
}