module scheme
fn redefinition_text(mut vm VM, source string) string {
return write(vm.eval(source) or { panic('${source}: ${err.msg()}') }) or { panic(err) }
}
fn test_goops_ordinary_class_definition_replaces_binding_without_changing_instances() {
mut vm := new()
assert redefinition_text(mut vm, '(define-class () (x #:init-value 1))
(define old ) (define object (make ))
(define-class () (y #:init-value 2))
(list (eq? old ) (eq? (class-of object) old) (slot-ref object (quote x)))') == '(#f #t 1)'
}
fn test_goops_redefinable_classes_keep_identity_and_migrate_descendants_lazily() {
mut vm := new()
assert redefinition_text(mut vm, '(define a (make-class (quote ())
(quote ((x #:init-value 1) (gone #:init-value 2)))
#:name (quote a) #:metaclass ))
(define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b)))
(define object (make b)) (slot-set! object (quote x) 10)
(define fresh (make-class (quote ()) (quote ((added #:init-value 4) (x #:init-value 5)))
#:name (quote a) #:metaclass ))
(define returned (class-redefinition a fresh))
(list (eq? returned a) (eq? (class-of object) b)
(eq? (car (class-direct-supers b)) a)
(slot-ref object (quote x)) (slot-ref object (quote added))
(slot-ref object (quote own)) (slot-exists? object (quote gone)))') == '(#t #t #t 10 4 3 #f)'
object := vm.lookup('object') or { panic(err) }
assert !object.goops_instance_pointer().migrating
assert object.goops_instance_pointer().layout_version == object.goops_instance_pointer().class.version
}
fn test_goops_redefinition_initializes_shared_cells_once_and_keeps_subclass_sharing() {
mut vm := new()
assert redefinition_text(mut vm, '(define a (make-class (quote ())
(quote ((shared #:allocation #:class #:init-value 1)
(per #:allocation #:each-subclass #:init-value 2))) #:metaclass ))
(define b (make-class (list a) (quote ())))
(define first (make b)) (define second (make b))
(slot-set! first (quote shared) 10) (slot-set! first (quote per) 20)
(class-redefinition a (make-class (quote ())
(quote ((shared #:allocation #:class #:init-value 3)
(per #:allocation #:each-subclass #:init-value 4))) #:metaclass ))
(define before (list (slot-ref first (quote shared)) (slot-ref first (quote per))))
(slot-set! first (quote shared) 30) (slot-set! first (quote per) 40)
(list before (slot-ref second (quote shared)) (slot-ref second (quote per))
(slot-ref (make a) (quote shared)) (slot-ref (make a) (quote per)))') == '((3 4) 30 40 30 4)'
}
fn test_goops_redefinition_repairs_accessors_new_specializers_and_retained_raw_getters() {
mut vm := new()
assert redefinition_text(mut vm, '(define get-x (make-accessor (quote get-x)))
(define gone-get (make-generic (quote gone-get)))
(define a (make-class (quote ())
(list (list (quote x) #:init-value 1 #:accessor get-x)
(list (quote gone) #:init-value 2 #:getter gone-get))
#:name (quote a) #:metaclass ))
(define b (make-class (list a) (quote ((own #:init-value 3))) #:name (quote b)))
(define object (make b)) (set! (get-x object) 10)
(define raw (slot-ref (class-slot-definition a (quote x)) (quote slot-ref)))
(define-method (describe (x