1
//! A `DEFSTRUCT` instance reaches the client as Common Lisp prints it, never as
2
//! an opaque placeholder: in a result, a list cell and a raise detail.
3

            
4
use super::*;
5
use crate::envelope::format_response;
6
use nomiscript::format_value;
7

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

            
21
#[tokio::test]
22
1
async fn an_instance_global_result_prints_its_name_and_fields() {
23
1
    let outcomes = printed_units(&["(defstruct pt x)", "(defvar *p* (make-pt :x 1))", "*p*"]).await;
24
1
    assert_eq!(outcomes[2], "#S(PT :X 1)");
25
1
}
26

            
27
#[tokio::test]
28
1
async fn a_list_holding_an_instance_prints_it_in_the_cell() {
29
1
    let outcomes = printed_units(&[
30
1
        "(defstruct pt x)",
31
1
        "(defvar *p* (make-pt :x 1))",
32
1
        "(list *p* 1)",
33
1
    ])
34
1
    .await;
35
1
    assert_eq!(outcomes[2], "(#S(PT :X 1) 1)");
36
1
}
37

            
38
#[tokio::test]
39
1
async fn a_constructor_result_prints_its_name_and_fields() {
40
1
    let outcomes = printed_units(&["(defstruct pt x)", "(make-pt :x 2)"]).await;
41
1
    assert_eq!(outcomes[1], "#S(PT :X 2)");
42
1
}
43

            
44
#[tokio::test]
45
1
async fn a_raise_detail_holding_an_instance_prints_it() {
46
1
    let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
47
1
    session.handle_request("(defstruct pt x)").await;
48
1
    session.handle_request("(defvar *p* (make-pt :x 1))").await;
49
1
    let outcome = session
50
1
        .handle_request(r#"(error 'boom "m" (list :row 7 :p *p*))"#)
51
1
        .await;
52
1
    let wire = format_response(&Response {
53
1
        stream: 0,
54
1
        id: RequestId::Int(1),
55
1
        payload: outcome.payload,
56
1
    });
57
1
    assert!(
58
1
        wire.contains(r#":detail (:ROW 7 :P #S(PT :X 1))"#),
59
1
        "{wire}"
60
1
    );
61
1
}
62

            
63
#[tokio::test]
64
1
async fn an_instance_result_is_a_struct_on_the_wire() {
65
1
    let wire = eval_value_of("(progn (defstruct pt x) (list (make-pt :x 2) 1))").await;
66
1
    assert_eq!(wire, "(:id 4 :value (#S(PT :X 2) 1))");
67
1
}