;;;; coops-utils test -*- Scheme -*- ;;;; Kon Lovett, Jul '18 (import test) (import (chicken syntax) (srfi 1) (srfi 13) coops) ;;; (test-begin "Coops Utils") (import coops-utils) ; (test-assert (class? )) (test-assert (class? )) ;; 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) (let ((specials (method-specializers x))) (test-assert (list? specials)) (test-assert (every class? specials)) ) (test-assert (procedure? (method-procedure x))) (test-assert (method? x)) ) 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))) (test "make-copy" 23 (slot@ inst y)) ) (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" (describe-object ) (newline) (describe-object s1xy-inst) (newline) (describe-object describe-object) (newline) (print-closure describe-object) ) ;; (import coops-introspection) (test-begin "Coops Introspection") (generic-name describe-object) (test-end "Coops Introspection") ;;; (test-exit)