;;;; coops-utils test -*- Scheme -*- ;;;; Kon Lovett, Jul '18 (import test) (import (only (chicken format) format) (test-utils gloss)) (import (chicken syntax)) (import (srfi 1)) (import (srfi 13)) (import coops) ;;; (test-begin "Coops Utils") (import coops-utils) ; (test-group "predicates" (test-assert (class? )) (test-assert (not (instance? ))) (test-assert (not (primitive-instance? ))) (test-assert (not (method? ))) (test-assert (not (generic? ))) (test-assert (not (class? 23))) (test-assert (not (instance? 23))) (test-assert (primitive-instance? 23)) (test-assert (not (method? 23))) (test-assert (not (generic? 23))) (test-assert (not (class? +))) (test-assert (not (instance? +))) (test-assert (primitive-instance? +)) (test-assert (not (generic? +))) (test-assert (not (method? +))) ) ;; setup test reference environment (define-class () ((x 's1xy-x) (y 's1xy-y))) (define-class () ((a 's1ab-a) (b 's1ab-b))) (define-class () ((x 's2xz-x) (z 's2xz-z))) (define-class () ((a 's2ac-a) (c 's2ac-c))) (define-class ( ) ((x 's1xys1ab-x) (y 's1xys1ab-y) (a 's1xys1ab-a) (b 's1xys1ab-b) (me '))) (define-class ( ) ((x 's2xzs2ac-x) (z 's2xzs2ac-z) (a 's2xzs2ac-a) (c 's2xzs2ac-c))) (define-class ( ) ()) (define-class ( ) ()) (define-generic (foo abc xyz)) (define-method (foo primary: (abc ) (xyz )) 'primary-) (define-method (foo before: (abc ) (xyz )) 'before-) (define-method (foo after: (abc ) (xyz )) 'after-) (define-method (foo around: (abc ) (xyz )) 'around-) (define-method (foo primary: (abc ) (xyz )) 'primary-) (define-method (foo before: (abc ) (xyz )) 'before-) (define-method (foo after: (abc ) (xyz )) 'after-) (define-method (foo around: (abc ) (xyz )) 'around-) (test-group "introspection" (test (list ) (class-precedence-list )) (test '(x z a b) (class-slots )) (test-assert (class? )) (test (list ) (class-direct-supers )) (test '(a c) (class-direct-slots )) (test-assert (class? )) (test (list ) (class-direct-supers )) (test '(me) (class-direct-slots )) (test 'foo (generic-name foo)) (test '(abc xyz) (generic-specialized-arguments foo)) (test-assert (eq? 2 (length (generic-primary-methods foo)))) (let ((primaries (generic-primary-methods foo))) (for-each (lambda (x) (test-assert (method? x)) (test-assert (procedure? (method-procedure x))) (let ((specials (method-specializers x))) (test-assert (list? specials)) (test-assert (every class? specials)) ) ) primaries) ) ) (define-class () (next)) (define-class () ()) (define-class () ()) (define 1st (make 'next (make 'next (make 'next "the end")))) (test-group "slot@" (test "the end" (slot@ 1st next next next)) (slot@ 1st next next next = "still the end") (test "still the end" (slot@ 1st next next next)) ) (define s1xy-inst (make 'x 1 'y 2)) (test-group "simple instance" (test-assert (instance? s1xy-inst)) (test-assert (instance-of? s1xy-inst )) ;(describe-object s1xy-inst) ) (test-group "make-copy instance" (test-error "cannot copy primitive object" (make-copy '())) (test-assert "coops-instance copy" (make-copy s1xy-inst 'y 23)) (test-error "no such slot for initform" (make-copy s1xy-inst 'foo 23)) (let ((inst (make-copy s1xy-inst 'y 23))) ;check copy (test-assert "made copy is an instance" (instance? inst)) (test-assert "made copy is correct instance" (instance-of? inst )) (test "made copy has overridden slot" 23 (slot@ inst y)) (test "made copy has original slot" 1 (slot@ inst x)) ;copy own slots? (slot@ inst x = 32) (test "made copy has slot assign" 32 (slot@ inst x)) (test "source has slot original" 1 (slot@ s1xy-inst x)) ) ) (test-group "primitive-instance" (import coops-primitive-objects) (test-assert "can copy primitive object (after import)" (make-copy '())) (let ((inst (make-copy '()))) ;check copy (test-assert "made copy is an instance" (instance? inst)) (test-assert "made copy is correct instance" (instance-of? inst )) ) ) (test-group "generic" (define-generic (city-market-class obj)) (define-generic (city-goods obj)) (define-class city () (name (market-class reader: city-market-class) sellers buyers (goods accessor: city-goods))) (define temphawa (make city 'name "Hawa" 'market-class 2)) (test-assert "make-copy temphawa" (make-copy temphawa)) ) ; (test-end "Coops Utils") ;; (test-begin "Coops Describe") (import coops-describe) (test-group "Coops Describe" (test-assert (generic? describe-object)) (glossln) (gloss "(describe-object )") (describe-object ) (glossln) (gloss "(describe-object s1xy-inst)") (describe-object s1xy-inst) (glossln) (gloss "(describe-object describe-object)") (describe-object describe-object) (glossln) (gloss "(print-closure describe-object)") (print-closure describe-object) ) (test-end "Coops Describe") ;; (test-begin "Coops Introspection") (import coops-introspection) (generic-name describe-object) (test-end "Coops Introspection") ;;; (test-exit) ;--- #| ;; Named (has a name) "concept" (define-generic (name obj)) (define-class () ( (namsym #:reader name) ) ) ;; Moves foreward thru a set of values "concept" (define-generic (step-function obj)) (define-class () ( (nxtval #:reader step-function) ) ) (define-generic (next-value obj)) (define-method (next-value (obj )) ((step-function obj))) ;; Parameterized extension "concept" (define-generic (parameters obj)) (define-generic (basis obj)) (define-class () ( (parms #:reader parameters) (src #:reader basis) ) ) ;; Parameterized generative set of random values "concept" (define-class ( ) ( temp ) ) ;; Create an instance of where the arguments are ;; the same as the documented procedural distribution API. ;; ;; SRFI 27 API: ({some distribution constructor} arg...) ;; OO API: (make-random-distribution {some distribution constructor} arg...) (define-syntax make-random-distribution (syntax-rules () ((_ ?ctor ?arg0 ...) (make 'temp (?ctor ?arg0 ...)) ) ) ) (define-method (initialize-instance (obj )) ;Reconstruct distribution api ctor invocation parameters ;(The 'ctor' must be a globally defined procedure compiled ;with procedure-information. So if following nomenclature then the last ;procedure name element will be the kind of distribution.) (let* ((temp (slot@ obj temp)) (ctor (car temp)) (procinfo (procedure-information ctor)) (name (and (pair? procinfo) (symbol->string (car procinfo)))) (name (and-let* ((name) (kndpos (string-index-right name #\-)) ) (substring/shared name (+ kndpos 1)) ) ) (dstr-vals (receive (apply ctor (cdr temp)))) (parms (and (<= 2 (length dstr-vals)) (receive ((second dstr-vals))))) ) (slot@ obj temp = #f) ;"free" the "any" slot (slot@ obj namsym = (string->symbol name)) (slot@ obj nxtval = (first dstr-vals)) (slot@ obj parms = (and parms (drop-right parms 1))) ) ) |#