;;;; coops-utils test -*- Scheme -*- ;;;; Kon Lovett, Jul '18 (import test) (import (only (chicken format) format)) (include-relative "test-gloss.incl") (import (chicken syntax) (srfi 1) (srfi 13) coops) ;;; (test-begin "Coops Utils") (import coops-utils) ; (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 (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 s1xy-inst (make 'x 1 'y 2)) (test-assert (instance? s1xy-inst)) (test-assert (instance-of? s1xy-inst )) ;(describe-object s1xy-inst) (define-class () (next)) (define-class () ()) (define-class () ()) (define 1st (make 'next (make 'next (make 'next "the end")))) (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)) ;make-copy (let ((inst (make-copy s1xy-inst 'y 23))) ;check copy (test-assert "make-copy" (instance? inst)) (test-assert "make-copy" (instance-of? inst )) (test "make-copy" 23 (slot@ inst y)) (test "make-copy" 1 (slot@ inst x)) ;copy own slots? (slot@ inst x = 32) (test "make-copy isolation" 32 (slot@ inst x)) (test "make-copy isolation" 1 (slot@ s1xy-inst x)) ) (let () (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)) ) ;primitive-instance? ;--- #| ;; 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))) ) ) |# ; (test-end "Coops Utils") ;; (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) ) ;; (import coops-introspection) (test-begin "Coops Introspection") (generic-name describe-object) (test-end "Coops Introspection") ;;; (test-exit)