| 1 | module scheme |
| 2 | |
| 3 | fn test_class_printers_see_the_port_and_cyclic_graphs_only_invoke_them_once() { |
| 4 | mut vm := new() |
| 5 | result := vm.eval("(define-class <stamp> () (n #:init-value 7)) |
| 6 | (define stamp (make <stamp>)) |
| 7 | (define calls 0) |
| 8 | (slot-set! <stamp> 'print |
| 9 | (lambda (object out) |
| 10 | (set! calls (+ calls 1)) |
| 11 | (display \"@\" out) |
| 12 | (display (slot-ref object 'n) out))) |
| 13 | (define p (open-output-string)) |
| 14 | (define cell (list stamp)) |
| 15 | (set-cdr! cell cell) |
| 16 | (write cell p) |
| 17 | (parameterize ((current-output-port p)) (display (vector stamp))) |
| 18 | (put-datum p stamp) |
| 19 | (list calls (get-output-string p))") or { panic(err) } |
| 20 | actual := write(result) or { panic(err) } |
| 21 | if actual != '(3 "#0=(@7 . #0#)#(@7)@7")' { panic(actual) } |
| 22 | if _ := vm.eval('(write-simple cell p)') { panic('write-simple accepted a cycle') } |
| 23 | after := vm.eval('calls') or { panic(err) } |
| 24 | if after.whole() != 3 { panic('failed graph formatting ran a printer') } |
| 25 | printed := vm.eval_alt('write(stamp,p)\nget-output-string(p)') or { panic(err) } |
| 26 | text := printed.as_string() or { panic(err) } |
| 27 | if !text.ends_with('@7@7') { panic(text) } |
| 28 | } |
| 29 | |
| 30 | fn test_class_printer_continuation_retains_only_remaining_output() { |
| 31 | mut vm := new() |
| 32 | result := vm.eval("(define-class <item> ()) |
| 33 | (define p (open-output-string)) |
| 34 | (define resume #f) |
| 35 | (slot-set! <item> 'print |
| 36 | (lambda (item out) |
| 37 | (write-string \"A\" out) |
| 38 | (call/cc (lambda (k) (set! resume k))) |
| 39 | (write-string \"B\" out))) |
| 40 | (write-shared (list (make <item>) 2) p) |
| 41 | (get-output-string p)") or { panic(err) } |
| 42 | if result.text() != '(AB 2)' { panic(result.text()) } |
| 43 | vm.eval('(resume #f)') or { panic(err) } |
| 44 | after := vm.eval('(get-output-string p)') or { panic(err) } |
| 45 | if after.text() != '(AB 2)B 2)' { panic(after.text()) } |
| 46 | } |
| 47 | |
| 48 | fn test_nested_primitive_write_methods_reach_the_class_printer_via_next_method() { |
| 49 | mut vm := new() |
| 50 | result := vm.eval("(define-class <item> ()) |
| 51 | (slot-set! <item> 'print (lambda (item port) (write-string \"leaf\" port))) |
| 52 | (define-method (write (item <item>) port) |
| 53 | (write-string \"{\" port) (next-method) (write-string \"}\" port)) |
| 54 | (define item (make <item>)) (define p (open-output-string)) |
| 55 | (write (vector item (list item)) p) |
| 56 | (get-output-string p)") or { panic(err) } |
| 57 | if result.text() != '#({leaf} ({leaf}))' { panic(result.text()) } |
| 58 | } |
| 59 | |