1
//! Plan 0014 Stage 4 slice 4.0: today's `Session` behaviour of the re-owned
2
//! issues whose Session and standalone results differ (ISS-122, ISS-124), and
3
//! of the other section-B rows run through `Session`.
4

            
5
use rpc::{ScriptCtx, Session};
6
use uuid::Uuid;
7

            
8
2
fn compile_error(message: &str) -> String {
9
2
    format!(
10
2
        "(:id {{id}} :error (:code compile :message \"Compile error: {message}\" :detail \"Dimension(\\\"{message}\\\")\"))"
11
    )
12
2
}
13

            
14
9
async fn responses(forms: &[&str]) -> Vec<String> {
15
9
    let mut session = Session::new(ScriptCtx::new(Uuid::new_v4())).unwrap();
16
9
    let mut out = Vec::new();
17
9
    for (id, form) in forms.iter().enumerate() {
18
17
        out.push(
19
17
            session
20
17
                .handle_form(&format!("(:id {id} :form {form})"))
21
17
                .await,
22
        );
23
    }
24
9
    out
25
9
}
26

            
27
const FRACTION: &str = "+ cannot mix an index (count) with a scalar; bridge explicitly with `(index->scalar ...)`, `(scalar->index ...)`, or `(convert-commodity ...)`";
28

            
29
7
async fn last(forms: &[&str]) -> String {
30
7
    responses(forms).await.pop().unwrap_or_default()
31
7
}
32

            
33
2
fn expect_error(response: &str, id: usize, message: &str) {
34
2
    assert_eq!(
35
2
        response,
36
2
        compile_error(message).replace("{id}", &id.to_string())
37
    );
38
2
}
39

            
40
#[tokio::test(flavor = "current_thread")]
41
1
async fn iss_122_labels_parameter_store_leaves_the_global_alone() {
42
1
    let got = responses(&[
43
1
        "(defvar x 1/1)",
44
1
        "(labels ((f (x) (setf x 2) x)) (f 3))",
45
1
        "(+ x 1/2)",
46
1
    ])
47
1
    .await;
48
1
    assert_eq!(
49
1
        got,
50
1
        [
51
1
            "(:id 0 :value nil)",
52
1
            "(:id 1 :value 2/1)",
53
1
            "(:id 2 :value 3/2)"
54
1
        ]
55
1
    );
56
1
}
57

            
58
#[tokio::test(flavor = "current_thread")]
59
1
async fn iss_124_session_accepts_a_non_list_cond_clause_today() {
60
1
    let got = responses(&["(defvar x 0)", "(setf x 1)", "(+ 1 (cond ((= x 1) 2) 5))"]).await;
61
1
    assert_eq!(
62
1
        got,
63
1
        ["(:id 0 :value nil)", "(:id 1 :value 1)", "(:id 2 :value 3)"]
64
1
    );
65
1
}
66

            
67
#[tokio::test(flavor = "current_thread")]
68
1
async fn iss_084_fraction_beside_an_effect_operand_is_rejected() {
69
1
    expect_error(
70
1
        &last(&["(+ 1/2 (progn (print \"x\") 1))"]).await,
71
1
        0,
72
1
        FRACTION,
73
1
    );
74
1
}
75

            
76
#[tokio::test(flavor = "current_thread")]
77
1
async fn iss_084_fraction_beside_an_effect_defun_call_is_rejected() {
78
1
    let got = last(&["(defun h (x) (print \"x\") x)", "(+ 1/2 (h 1))"]).await;
79
1
    expect_error(&got, 1, FRACTION);
80
1
}
81

            
82
#[tokio::test(flavor = "current_thread")]
83
1
async fn iss_105a_a_computed_closure_operand_is_called() {
84
1
    let got = last(&["(funcall (funcall (lambda (x) (lambda (y) (+ x y))) 1) 2)"]).await;
85
1
    assert_eq!(got, "(:id 0 :value 3)");
86
1
}
87

            
88
#[tokio::test(flavor = "current_thread")]
89
1
async fn iss_105b_a_defun_returned_closure_keeps_its_capture() {
90
1
    let got = last(&[
91
1
        "(defun mk (n) (lambda (y) (+ n y)))",
92
1
        "(map (mk (+ 1 2)) (list 1 2))",
93
1
    ])
94
1
    .await;
95
1
    assert_eq!(got, "(:id 1 :value (4 5))");
96
1
}
97

            
98
#[tokio::test(flavor = "current_thread")]
99
1
async fn iss_106_a_malformed_call_in_a_defun_body_is_accepted_today() {
100
1
    assert_eq!(
101
1
        last(&["(defun f () (car 1 2))", "4"]).await,
102
1
        "(:id 1 :value 4)"
103
1
    );
104
1
}
105

            
106
#[tokio::test(flavor = "current_thread")]
107
1
async fn iss_119_a_recursive_defun_fraction_result_is_a_scalar() {
108
1
    let got = last(&[
109
1
        "(defun r (n) (if (= n 0) 1/2 (r (- n 1))))",
110
1
        "(+ 1/2 (r (rpc-protocol-version)))",
111
1
    ])
112
1
    .await;
113
1
    assert_eq!(got, "(:id 1 :value 1/1)");
114
1
}
115

            
116
#[tokio::test(flavor = "current_thread")]
117
1
async fn iss_134_a_nil_block_exit_as_an_if_test_takes_the_else_arm() {
118
1
    assert_eq!(
119
1
        last(&["(if (block b (return-from b nil) 1) 1 2)"]).await,
120
1
        "(:id 0 :value 2)"
121
1
    );
122
1
}