Lines
100 %
Functions
Branches
//! A closure whose source cannot be carried reaches the client as the opaque
//! `#<closure>`: in a result, a list cell and a raise detail.
use super::*;
use crate::envelope::format_response;
const SITELESS: &str = "(let ((n 0)) (lambda () (setf n (+ n 1)) n))";
#[tokio::test]
async fn a_siteless_closure_result_is_a_closure_on_the_wire() {
let wire = eval_value_of(SITELESS).await;
assert_eq!(wire, "(:id 4 :value #<closure>)");
}
async fn a_list_holding_a_siteless_closure_prints_it_in_the_cell() {
let wire = eval_value_of(&format!("(list {SITELESS} 1)")).await;
assert_eq!(wire, "(:id 4 :value (#<closure> 1))");
async fn a_siteless_closure_joined_with_a_truth_value_prints_and_returns() {
let joined = format!("(let ((f {SITELESS}) (k (rpc-protocol-version))) (or (> k 5) f))");
assert_eq!(eval_value_of(&joined).await, "(:id 4 :value #<closure>)");
let printed = format!("(progn (print {joined}) 1)");
assert_eq!(eval_value_of(&printed).await, "(:id 4 :value 1)");
async fn a_raise_detail_holding_a_siteless_closure_prints_it() {
let mut session = Session::new(ScriptCtx::new(uuid::Uuid::nil())).expect("Session::new");
let outcome = session
.handle_request(&format!(r#"(error 'boom "m" {SITELESS})"#))
.await;
let wire = format_response(&Response {
stream: 0,
id: RequestId::Int(1),
payload: outcome.payload,
});
assert!(wire.contains(":detail #<closure>"), "{wire}");
async fn a_printed_closure_list_keeps_its_value() {
let printed = format!("(length (print (list {SITELESS})))");
async fn a_printed_typed_closure_and_condition_compile() {
let closure = "(let ((f (lambda (x) x))) (progn (print f) 1))";
assert_eq!(eval_value_of(closure).await, "(:id 4 :value 1)");
let condition = "(progn (print (handler-case (error 'boom \"m\") (t (e) e))) 2)";
assert_eq!(eval_value_of(condition).await, "(:id 4 :value 2)");
async fn a_printed_bare_lambda_answers_the_closure() {
for source in [
"(print (lambda (x) x))",
"(print 1 (lambda (x) x))",
"(print (if t (lambda (x) x) nil))",
"(progn (defun mk () (lambda (x) x)) (print (mk)))",
] {
assert_eq!(
eval_value_of(source).await,
"(:id 4 :value #<closure:0>)",
"{source}"
);
let called = "(let ((f (print (lambda (x) (+ x 1))))) (funcall f 2))";
assert_eq!(eval_value_of(called).await, "(:id 4 :value 3)");