1
//! Plan 0014 PERSIST S4 over the session: a cell that may be unbound is read
2
//! at run time, a binder restores its boundness, and a unit that does not
3
//! compile leaves every name as it was. The values are Common Lisp's.
4

            
5
use super::*;
6

            
7
3
async fn units(sources: &[&str]) -> Vec<String> {
8
3
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
9
3
    let mut outcomes = Vec::new();
10
3
    for source in sources {
11
9
        let outcome = session.handle_request(source).await;
12
9
        outcomes.push(match outcome.payload {
13
2
            ResponsePayload::Error { message, .. } => message,
14
7
            ResponsePayload::Value(value) => format!("{value:?}"),
15
        });
16
    }
17
3
    outcomes
18
3
}
19

            
20
3
fn value(text: &str) -> String {
21
3
    format!("{:?}", parse_to_value(text).expect("parses"))
22
3
}
23

            
24
#[tokio::test]
25
1
async fn a_read_of_an_unbound_cell_signals_unbound_variable() {
26
1
    let outcomes = units(&[
27
1
        "(progn (defvar x) (when (> (rpc-protocol-version) 99) (setf x 1)) (handler-case x (unbound-variable (e) 'u)))",
28
1
        "(progn (defvar y) (when (> (rpc-protocol-version) 99) (setf y 1)) y)",
29
1
    ])
30
1
    .await;
31
1
    assert_eq!(outcomes[0], value("u"));
32
1
    assert!(outcomes[1].contains("UNBOUND-VARIABLE"), "{}", outcomes[1]);
33
1
}
34

            
35
#[tokio::test]
36
1
async fn a_binder_of_an_unbound_special_leaves_it_unbound() {
37
1
    let outcomes = units(&[
38
1
        "(defvar x)",
39
1
        "(defun g () x)",
40
1
        "(list (let ((x 3)) (g)) (handler-case (g) (unbound-variable (e) 'u)))",
41
1
    ])
42
1
    .await;
43
1
    assert_eq!(outcomes[2], value("(3 u)"));
44
1
}
45

            
46
#[tokio::test]
47
1
async fn a_unit_that_does_not_compile_redefines_nothing() {
48
1
    let outcomes = units(&[
49
1
        "(defun f () 1)",
50
1
        "(defparameter *a* 1)",
51
1
        "(progn (defun f () 2) (defparameter *a* 2) (car 5))",
52
1
        "(list (f) *a*)",
53
1
    ])
54
1
    .await;
55
1
    assert!(
56
1
        outcomes[2].contains("CAR expects a list"),
57
        "{}",
58
        outcomes[2]
59
    );
60
1
    assert_eq!(outcomes[3], value("(1 1)"));
61
1
}
62

            
63
#[tokio::test]
64
1
async fn a_unit_interrupted_before_its_call_puts_back_its_definitions() {
65
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
66
1
    let _ = session.handle_request("(defun f () 1)").await;
67
1
    let program = Reader::parse("(defun f () 2)").expect("parses");
68
1
    let compiled = session
69
1
        .compiler
70
1
        .compile_eval_with_type(&program, &mut session.symbols)
71
1
        .expect("compiles");
72
1
    session.interrupt_handle().interrupt();
73
1
    let ran = session.run_compiled(&RequestId::Int(1), compiled).await;
74
1
    assert!(matches!(ran, Err(EvalFailure::Interrupted(_))));
75
1
    let outcome = session.handle_request("(f)").await;
76
1
    assert_eq!(
77
1
        outcome.payload,
78
1
        ResponsePayload::Value(parse_to_value("1").expect("parses"))
79
1
    );
80
1
}