1
//! A closure whose source cannot be carried reaches the client as the opaque
2
//! `#<closure>`: in a result, a list cell and a raise detail.
3

            
4
use super::*;
5
use crate::envelope::format_response;
6

            
7
const SITELESS: &str = "(let ((n 0)) (lambda () (setf n (+ n 1)) n))";
8

            
9
#[tokio::test]
10
1
async fn a_siteless_closure_result_is_a_closure_on_the_wire() {
11
1
    let wire = eval_value_of(SITELESS).await;
12
1
    assert_eq!(wire, "(:id 4 :value #<closure>)");
13
1
}
14

            
15
#[tokio::test]
16
1
async fn a_list_holding_a_siteless_closure_prints_it_in_the_cell() {
17
1
    let wire = eval_value_of(&format!("(list {SITELESS} 1)")).await;
18
1
    assert_eq!(wire, "(:id 4 :value (#<closure> 1))");
19
1
}
20

            
21
#[tokio::test]
22
1
async fn a_siteless_closure_joined_with_a_truth_value_prints_and_returns() {
23
1
    let joined = format!("(let ((f {SITELESS}) (k (rpc-protocol-version))) (or (> k 5) f))");
24
1
    assert_eq!(eval_value_of(&joined).await, "(:id 4 :value #<closure>)");
25
1
    let printed = format!("(progn (print {joined}) 1)");
26
1
    assert_eq!(eval_value_of(&printed).await, "(:id 4 :value 1)");
27
1
}
28

            
29
#[tokio::test]
30
1
async fn a_raise_detail_holding_a_siteless_closure_prints_it() {
31
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
32
1
    let outcome = session
33
1
        .handle_request(&format!(r#"(error 'boom "m" {SITELESS})"#))
34
1
        .await;
35
1
    let wire = format_response(&Response {
36
1
        stream: 0,
37
1
        id: RequestId::Int(1),
38
1
        payload: outcome.payload,
39
1
    });
40
1
    assert!(wire.contains(":detail #<closure>"), "{wire}");
41
1
}
42

            
43
#[tokio::test]
44
1
async fn a_printed_closure_list_keeps_its_value() {
45
1
    let printed = format!("(length (print (list {SITELESS})))");
46
1
    assert_eq!(eval_value_of(&printed).await, "(:id 4 :value 1)");
47
1
}
48

            
49
#[tokio::test]
50
1
async fn a_printed_typed_closure_and_condition_compile() {
51
1
    let closure = "(let ((f (lambda (x) x))) (progn (print f) 1))";
52
1
    assert_eq!(eval_value_of(closure).await, "(:id 4 :value 1)");
53
1
    let condition = "(progn (print (handler-case (error 'boom \"m\") (t (e) e))) 2)";
54
1
    assert_eq!(eval_value_of(condition).await, "(:id 4 :value 2)");
55
1
}
56

            
57
#[tokio::test]
58
1
async fn a_printed_bare_lambda_answers_the_closure() {
59
1
    for source in [
60
1
        "(print (lambda (x) x))",
61
1
        "(print 1 (lambda (x) x))",
62
1
        "(print (if t (lambda (x) x) nil))",
63
1
        "(progn (defun mk () (lambda (x) x)) (print (mk)))",
64
1
    ] {
65
4
        assert_eq!(
66
4
            eval_value_of(source).await,
67
4
            "(:id 4 :value #<closure:0>)",
68
            "{source}"
69
        );
70
    }
71
1
    let called = "(let ((f (print (lambda (x) (+ x 1))))) (funcall f 2))";
72
1
    assert_eq!(eval_value_of(called).await, "(:id 4 :value 3)");
73
1
}