(load "../defstruct.scm") (import defstruct) (require-extension test) (defstruct empty) (test-group "empty" (test #t (empty? (make-empty)))) (defstruct simple field) (test-group "simple" (test #t (simple? (make-simple field: 'foo))) (test 'foo (simple-field (make-simple field: 'foo)))) (defstruct init (field-1 'one) (field-2 (list 'two))) (test-group "with initializer" (test #t (init? (make-init 'foo))) (test 'foo (init-field-1 (make-init field-1: 'foo))) ;; (list) should get evaluated twice (test #t (not (eq? (init-field-2 (make-init)) (init-field-2 (make-init)))))) (defstruct complex field-1 (field-2 'two) field-3 (field-4 (list 'four))) (test-group "complex" (test #t (complex? (make-complex))) (test 'foo (complex-field-1 (make-complex field-1: 'foo))) (test #f (complex-field-3 (make-complex field-1: 'foo))) (test #f (complex-field-1 (make-complex field-2: 'foo))) (test 'two (complex-field-2 (make-complex field-1: 'foo))) (test 'hai (complex-field-2 (make-complex field-2: 'hai field-1: 'foo)))) (test-group "updaters" (let* ((c1 (make-complex field-1: 'foo)) (c2 (complex-copy c1 field-1: 'bar)) (c3 (complex-copy c1 field-2: 'qux))) (test 'foo (complex-field-1 c1)) (test 'two (complex-field-2 c1)) (test 'bar (complex-field-1 c2)) (test 'two (complex-field-2 c2)) (test 'foo (complex-field-1 c3)) ;; (list) initializer should not get re-evaluated on copy (test #t (eq? (complex-field-4 c1) (complex-field-4 c2))) (test #t (eq? (complex-field-4 c1) (complex-field-4 c3))) (test #f (complex-field-3 c1)) (set-complex! c1 field-1: 'mutated) (test 'mutated (complex-field-1 c1)) (test 'bar (complex-field-1 c2)))) (test-group "hygiene" (defstruct ini-capture (uninitialized 1)) (define i1 (make-ini-capture)) (test 1 (ini-capture-uninitialized (ini-capture-copy i1))) (test 2 (ini-capture-uninitialized (ini-capture-copy i1 uninitialized: 2))))