1
//! Plan 0014 stage GJ: a global's home type is the join of its init and
2
//! every store of its own unit, and nothing outlives the unit.
3

            
4
use super::*;
5

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

            
19
#[tokio::test]
20
1
async fn hints_do_not_leak_into_the_next_unit() {
21
1
    let outcomes = units(&[
22
1
        "(defvar g nil) (setf g 5)",
23
1
        "(defvar h 1) (setf h 2) (+ h 1)",
24
1
    ])
25
1
    .await;
26
1
    assert_eq!(
27
1
        outcomes,
28
1
        ["Value(Number(Index(5)))", "Value(Number(Index(3)))",]
29
1
    );
30
1
}
31

            
32
const FOUR: &str = "Value(Number(Index(2)))";
33

            
34
#[tokio::test]
35
1
async fn a_stored_global_read_in_a_later_unit_is_the_carried_value() {
36
1
    let outcomes = units(&["(defvar g 1) (setf g 2)", "g"]).await;
37
1
    assert_eq!(
38
1
        outcomes,
39
1
        ["Value(Number(Index(2)))", "Value(Number(Index(2)))",]
40
1
    );
41
1
}
42

            
43
#[tokio::test]
44
1
async fn a_later_unit_defines_its_own_home() {
45
1
    let outcomes = units(&["(defvar g 1) (setf g 2)", "(defparameter g 1) (setf g 2) g"]).await;
46
1
    assert_eq!(outcomes, [FOUR, FOUR]);
47
1
}
48

            
49
#[tokio::test]
50
1
async fn a_widening_redefinition_in_a_later_unit_settles_its_own_home() {
51
1
    let outcomes = units(&[
52
1
        "(defvar g 0) (setf g 1)",
53
1
        "(defparameter g 1) (setf g 2)",
54
1
        "(defparameter g (index->scalar (rpc-protocol-version))) g",
55
1
        "(defparameter g 1/2) g",
56
1
    ])
57
1
    .await;
58
1
    assert_eq!(
59
1
        outcomes,
60
1
        [
61
1
            "Value(Number(Index(1)))",
62
1
            FOUR,
63
1
            "Value(Number(Scalar(Ratio { numer: 1, denom: 1 })))",
64
1
            "Value(Number(Scalar(Ratio { numer: 1, denom: 2 })))",
65
1
        ]
66
1
    );
67
1
}
68

            
69
#[tokio::test]
70
1
async fn an_unstored_global_keeps_its_value_across_units() {
71
1
    let outcomes = units(&["(defvar g 1)", "(setf g 2)", "g"]).await;
72
1
    assert_eq!(outcomes[1], FOUR);
73
1
    assert_eq!(outcomes[2], FOUR);
74
1
}