goops.test 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. ;;;; goops.test --- test suite for GOOPS -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2001,2003,2004, 2006, 2008, 2009, 2011, 2012, 2014, 2015, 2017, 2021 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite test-goops)
  19. #:use-module (test-suite lib)
  20. #:autoload (srfi srfi-1) (unfold))
  21. (define exception:no-applicable-method
  22. '(goops-error . "^No applicable method"))
  23. (pass-if "GOOPS loads"
  24. (false-if-exception
  25. (begin (resolve-module '(oop goops))
  26. #t)))
  27. (use-modules (oop goops))
  28. ;;; more tests here...
  29. (with-test-prefix "basic classes"
  30. (with-test-prefix "<top>"
  31. (pass-if "instance?"
  32. (instance? <top>))
  33. (pass-if "class-of"
  34. (eq? (class-of <top>) <class>))
  35. (pass-if "is a class?"
  36. (is-a? <top> <class>))
  37. (pass-if "class-name"
  38. (eq? (class-name <top>) '<top>))
  39. (pass-if "direct superclasses"
  40. (equal? (class-direct-supers <top>) '()))
  41. (pass-if "superclasses"
  42. (equal? (class-precedence-list <top>) (list <top>)))
  43. (pass-if "direct slots"
  44. (equal? (class-direct-slots <top>) '()))
  45. (pass-if "slots"
  46. (equal? (class-slots <top>) '())))
  47. (with-test-prefix "<object>"
  48. (pass-if "instance?"
  49. (instance? <object>))
  50. (pass-if "class-of"
  51. (eq? (class-of <object>) <class>))
  52. (pass-if "is a class?"
  53. (is-a? <object> <class>))
  54. (pass-if "class-name"
  55. (eq? (class-name <object>) '<object>))
  56. (pass-if "direct superclasses"
  57. (equal? (class-direct-supers <object>) (list <top>)))
  58. (pass-if "superclasses"
  59. (equal? (class-precedence-list <object>) (list <object> <top>)))
  60. (pass-if "direct slots"
  61. (equal? (class-direct-slots <object>) '()))
  62. (pass-if "slots"
  63. (equal? (class-slots <object>) '())))
  64. (with-test-prefix "<class>"
  65. (pass-if "instance?"
  66. (instance? <class>))
  67. (pass-if "class-of"
  68. (eq? (class-of <class>) <class>))
  69. (pass-if "is a class?"
  70. (is-a? <class> <class>))
  71. (pass-if "class-name"
  72. (eq? (class-name <class>) '<class>))
  73. (pass-if "direct superclass"
  74. (equal? (class-direct-supers <class>) (list <object>))))
  75. (with-test-prefix "class-precedence-list"
  76. (for-each (lambda (class)
  77. (run-test (if (slot-bound? class 'name)
  78. (class-name class)
  79. (with-output-to-string
  80. (lambda ()
  81. (display class))))
  82. #t
  83. (lambda ()
  84. (catch #t
  85. (lambda ()
  86. (equal? (class-precedence-list class)
  87. (compute-cpl class)))
  88. (lambda args #t)))))
  89. (let ((table (make-hash-table)))
  90. (let rec ((class <top>))
  91. (hash-create-handle! table class #f)
  92. (for-each rec (class-direct-subclasses class)))
  93. (hash-fold (lambda (class ignore classes)
  94. (cons class classes))
  95. '()
  96. table))))
  97. )
  98. (with-test-prefix "classes for built-in types"
  99. (pass-if "subr"
  100. (eq? (class-of fluid-ref) <procedure>))
  101. (pass-if "gsubr"
  102. (eq? (class-of hashq-ref) <procedure>))
  103. (pass-if "car"
  104. (eq? (class-of car) <procedure>))
  105. (pass-if "string"
  106. (eq? (class-of "foo") <string>))
  107. (pass-if "port"
  108. (is-a? (%make-void-port "w") <port>))
  109. (pass-if "struct vtable"
  110. ;; Previously, `class-of' would fail for nameless structs, i.e., structs
  111. ;; for which `struct-vtable-name' is #f.
  112. (is-a? (class-of (make-vtable
  113. (string-append standard-vtable-fields "pwpwpw")))
  114. <class>))
  115. ;; Two cases: one for structs created before goops, one after.
  116. (pass-if "early vtable class cached"
  117. (eq? (class-of (current-module))
  118. (class-of (current-module))))
  119. (pass-if "late vtable class cached"
  120. (let ((vtable (make-vtable
  121. (string-append standard-vtable-fields "pwpwpw"))))
  122. (eq? (class-of vtable)
  123. (class-of vtable)))))
  124. (with-test-prefix "defining classes"
  125. (with-test-prefix "define-class"
  126. (pass-if "creating a new binding"
  127. (if (eval '(defined? '<foo-0>) (current-module))
  128. (throw 'unresolved))
  129. (eval '(define-class <foo-0> ()) (current-module))
  130. (eval '(is-a? <foo-0> <class>) (current-module)))
  131. (pass-if "overwriting a binding to a non-class"
  132. (eval '(define <foo> #f) (current-module))
  133. (eval '(define-class <foo> ()) (current-module))
  134. (eval '(is-a? <foo> <class>) (current-module)))
  135. (pass-if "bad init-thunk"
  136. (catch #t
  137. (lambda ()
  138. (eval '(define-class <foo> ()
  139. (x #:init-thunk (lambda (x) 1)))
  140. (current-module))
  141. #f)
  142. (lambda args
  143. #t)))
  144. (pass-if "interaction with `struct-ref'"
  145. (eval '(define-class <class-struct> ()
  146. (foo #:init-keyword #:foo)
  147. (bar #:init-keyword #:bar))
  148. (current-module))
  149. (eval '(let ((x (make <class-struct>
  150. #:foo 'hello
  151. #:bar 'world)))
  152. (and (struct? x)
  153. (eq? (struct-ref x 0) 'hello)
  154. (eq? (struct-ref x 1) 'world)))
  155. (current-module)))
  156. (pass-if "interaction with `struct-set!'"
  157. (eval '(define-class <class-struct-2> ()
  158. (foo) (bar))
  159. (current-module))
  160. (eval '(let ((x (make <class-struct-2>)))
  161. (struct-set! x 0 'hello)
  162. (struct-set! x 1 'world)
  163. (and (struct? x)
  164. (eq? (struct-ref x 0) 'hello)
  165. (eq? (struct-ref x 1) 'world)))
  166. (current-module)))
  167. (pass-if "with accessors"
  168. (eval '(define-class <qux> ()
  169. (x #:accessor x #:init-value 123)
  170. (z #:accessor z #:init-value 789))
  171. (current-module))
  172. (eval '(equal? (x (make <qux>)) 123) (current-module)))
  173. (pass-if-exception "cannot redefine fields of <class>"
  174. '(misc-error . "cannot be redefined")
  175. (eval '(begin
  176. (define-class <test-class> (<class>)
  177. name)
  178. (make <test-class>))
  179. (current-module)))))
  180. (with-test-prefix "defining generics"
  181. (with-test-prefix "define-generic"
  182. (pass-if "creating a new top-level binding"
  183. (if (eval '(defined? 'foo-0) (current-module))
  184. (throw 'unresolved))
  185. (eval '(define-generic foo-0) (current-module))
  186. (eval '(and (is-a? foo-0 <generic>)
  187. (null? (generic-function-methods foo-0)))
  188. (current-module)))
  189. (pass-if "overwriting a top-level binding to a non-generic"
  190. (eval '(define (foo) #f) (current-module))
  191. (eval '(define-generic foo) (current-module))
  192. (eval '(and (is-a? foo <generic>)
  193. (= 1 (length (generic-function-methods foo))))
  194. (current-module)))
  195. (pass-if "overwriting a top-level binding to a generic"
  196. (eval '(define (foo) #f) (current-module))
  197. (eval '(define-generic foo) (current-module))
  198. (eval '(define-generic foo) (current-module))
  199. (eval '(and (is-a? foo <generic>)
  200. (null? (generic-function-methods foo)))
  201. (current-module)))
  202. (pass-if-exception "getters do not have setters"
  203. exception:wrong-type-arg
  204. (eval '(setter foo) (current-module)))))
  205. (with-test-prefix "defining methods"
  206. (pass-if "define-method"
  207. (let ((m (current-module)))
  208. (eval '(define-method (my-plus (s1 <string>) (s2 <string>))
  209. (string-append s1 s2))
  210. m)
  211. (eval '(define-method (my-plus (i1 <integer>) (i2 <integer>))
  212. (+ i1 i2))
  213. m)
  214. (eval '(and (is-a? my-plus <generic>)
  215. (= (length (generic-function-methods my-plus))
  216. 2))
  217. m)))
  218. (pass-if "method-more-specific?"
  219. (eval '(let* ((m+ (generic-function-methods my-plus))
  220. (m1 (car m+))
  221. (m2 (cadr m+))
  222. (arg-types (list <string> <string>)))
  223. (if (memq <string> (method-specializers m1))
  224. (method-more-specific? m1 m2 arg-types)
  225. (method-more-specific? m2 m1 arg-types)))
  226. (current-module)))
  227. (pass-if-exception "method-more-specific? (failure)"
  228. exception:wrong-type-arg
  229. (eval '(let* ((m+ (generic-function-methods my-plus))
  230. (m1 (car m+))
  231. (m2 (cadr m+)))
  232. (method-more-specific? m1 m2 '()))
  233. (current-module))))
  234. (with-test-prefix "the method cache"
  235. (pass-if "defining a method with a rest arg"
  236. (let ((m (current-module)))
  237. (eval '(define-method (foo bar . baz)
  238. (cons bar baz))
  239. m)
  240. (eval '(foo 1)
  241. m)
  242. (eval '(foo 1 2)
  243. m)
  244. (eval '(equal? (foo 1 2) '(1 2))
  245. m))))
  246. (with-test-prefix "defining accessors"
  247. (with-test-prefix "define-accessor"
  248. (pass-if "creating a new top-level binding"
  249. (if (eval '(defined? 'foo-1) (current-module))
  250. (throw 'unresolved))
  251. (eval '(define-accessor foo-1) (current-module))
  252. (eval '(and (is-a? foo-1 <generic-with-setter>)
  253. (null? (generic-function-methods foo-1)))
  254. (current-module)))
  255. (pass-if "accessors have setters"
  256. (procedure? (eval '(setter foo-1) (current-module))))
  257. (pass-if "overwriting a top-level binding to a non-accessor"
  258. (eval '(define (foo) #f) (current-module))
  259. (eval '(define-accessor foo) (current-module))
  260. (eval '(and (is-a? foo <generic-with-setter>)
  261. (= 1 (length (generic-function-methods foo))))
  262. (current-module)))
  263. (pass-if "overwriting a top-level binding to an accessor"
  264. (eval '(define (foo) #f) (current-module))
  265. (eval '(define-accessor foo) (current-module))
  266. (eval '(define-accessor foo) (current-module))
  267. (eval '(and (is-a? foo <generic-with-setter>)
  268. (null? (generic-function-methods foo)))
  269. (current-module)))))
  270. (with-test-prefix "object update"
  271. (pass-if "defining class"
  272. (eval '(define-class <foo> ()
  273. (x #:accessor x #:init-value 123)
  274. (z #:accessor z #:init-value 789)
  275. #:metaclass <redefinable-class>)
  276. (current-module))
  277. (eval '(is-a? <foo> <class>) (current-module)))
  278. (pass-if "making instance"
  279. (eval '(define foo (make <foo>)) (current-module))
  280. (eval '(and (is-a? foo <foo>) (= (x foo) 123)) (current-module)))
  281. (pass-if "redefining class"
  282. (eval '(define-class <foo> ()
  283. (x #:accessor x #:init-value 123)
  284. (y #:accessor y #:init-value 456)
  285. (z #:accessor z #:init-value 789)
  286. #:metaclass <redefinable-class>)
  287. (current-module))
  288. (eval '(and (= (y foo) 456) (= (z foo) 789)) (current-module)))
  289. (pass-if "changing class"
  290. (let* ((c1 (class ()
  291. (the-slot #:init-keyword #:value)
  292. #:metaclass <redefinable-class>))
  293. (c2 (class ()
  294. (the-slot #:init-keyword #:value)
  295. (the-other-slot #:init-value 888)
  296. #:metaclass <redefinable-class>))
  297. (o1 (make c1 #:value 777)))
  298. (and (is-a? o1 c1)
  299. (not (is-a? o1 c2))
  300. (equal? (slot-ref o1 'the-slot) 777)
  301. (let ((o2 (change-class o1 c2)))
  302. (and (eq? o1 o2)
  303. (is-a? o2 c2)
  304. (not (is-a? o2 c1))
  305. (equal? (slot-ref o2 'the-slot) 777))))))
  306. (pass-if "`hell' in `goops.c' grows as expected"
  307. ;; This snippet yielded a segfault prior to the 2008-08-19 `goops.c'
  308. ;; fix (i.e., Guile 1.8.5 and earlier). The root of the problem was
  309. ;; that `go_to_hell ()' would not reallocate enough room for the `hell'
  310. ;; array, leading to out-of-bounds accesses.
  311. (let* ((parent-class (class ()
  312. #:name '<class-that-will-be-redefined>
  313. #:metaclass <redefinable-class>))
  314. (classes
  315. (unfold (lambda (i) (>= i 20))
  316. (lambda (i)
  317. (make-class (list parent-class)
  318. '((the-slot #:init-value #:value)
  319. (the-other-slot))
  320. #:name (string->symbol
  321. (string-append "<foo-to-redefine-"
  322. (number->string i)
  323. ">"))
  324. #:metaclass <redefinable-class>))
  325. (lambda (i)
  326. (+ 1 i))
  327. 0))
  328. (objects
  329. (map (lambda (class)
  330. (make class #:value 777))
  331. classes)))
  332. (define-method (change-class (foo parent-class)
  333. (new <redefinable-class>))
  334. ;; Called by `scm_change_object_class ()', via `purgatory ()'.
  335. (if (null? classes)
  336. (next-method)
  337. (let ((class (car classes))
  338. (object (car objects)))
  339. (set! classes (cdr classes))
  340. (set! objects (cdr objects))
  341. ;; Redefine the class so that its instances are eventually
  342. ;; passed to `scm_change_object_class ()'. This leads to
  343. ;; nested `scm_change_object_class ()' calls, which increases
  344. ;; the size of HELL and increments N_HELL.
  345. (class-redefinition class
  346. (make-class '() (class-direct-slots class)
  347. #:name (class-name class)
  348. #:metaclass <redefinable-class>))
  349. ;; Use `slot-ref' to trigger the `scm_change_object_class ()'
  350. ;; and `go_to_hell ()' calls.
  351. (slot-ref object 'the-slot)
  352. (next-method))))
  353. ;; Initiate the whole `change-class' chain.
  354. (let* ((class (car classes))
  355. (object (change-class (car objects) class)))
  356. (is-a? object class)))))
  357. (with-test-prefix "object comparison"
  358. (pass-if "default method"
  359. (eval '(begin
  360. (define-class <c> ()
  361. (x #:accessor x #:init-keyword #:x)
  362. (y #:accessor y #:init-keyword #:y))
  363. (define o1 (make <c> #:x '(1) #:y '(2)))
  364. (define o2 (make <c> #:x '(1) #:y '(3)))
  365. (define o3 (make <c> #:x '(4) #:y '(3)))
  366. (define o4 (make <c> #:x '(4) #:y '(3)))
  367. (not (eqv? o1 o2)))
  368. (current-module)))
  369. (pass-if "equal?"
  370. (eval '(begin
  371. (define-method (equal? (a <c>) (b <c>))
  372. (equal? (y a) (y b)))
  373. (equal? o2 o3))
  374. (current-module)))
  375. (pass-if "not equal?"
  376. (eval '(not (equal? o1 o2))
  377. (current-module)))
  378. (pass-if "="
  379. (eval '(begin
  380. (define-method (= (a <c>) (b <c>))
  381. (and (equal? (x a) (x b))
  382. (equal? (y a) (y b))))
  383. (= o3 o4))
  384. (current-module)))
  385. (pass-if "not ="
  386. (eval '(not (= o1 o2))
  387. (current-module)))
  388. )
  389. (use-modules (oop goops active-slot))
  390. (with-test-prefix "active-slot"
  391. (pass-if "defining class with active slot"
  392. (eval '(begin
  393. (define z '())
  394. (define-class <bar> ()
  395. (x #:accessor x
  396. #:init-value 1
  397. #:allocation #:active
  398. #:before-slot-ref
  399. (lambda (o)
  400. (set! z (cons 'before-ref z))
  401. #t)
  402. #:after-slot-ref
  403. (lambda (o)
  404. (set! z (cons 'after-ref z)))
  405. #:before-slot-set!
  406. (lambda (o v)
  407. (set! z (cons* v 'before-set! z)))
  408. #:after-slot-set!
  409. (lambda (o v)
  410. (set! z (cons* v (x o) 'after-set! z))))
  411. #:metaclass <active-class>)
  412. (define bar (make <bar>))
  413. (x bar)
  414. (set! (x bar) 2)
  415. (equal? (reverse z)
  416. '(before-set! 1 before-ref after-ref
  417. after-set! 1 1 before-ref after-ref
  418. before-set! 2 before-ref after-ref after-set! 2 2)))
  419. (current-module))))
  420. (use-modules (oop goops composite-slot))
  421. (with-test-prefix "composite-slot"
  422. (pass-if "creating instance with propagated slot"
  423. (eval '(begin
  424. (define-class <a> ()
  425. (x #:accessor x #:init-keyword #:x)
  426. (y #:accessor y #:init-keyword #:y))
  427. (define-class <c> ()
  428. (o1 #:accessor o1 #:init-form (make <a> #:x 1 #:y 2))
  429. (o2 #:accessor o2 #:init-form (make <a> #:x 3 #:y 4))
  430. (x #:accessor x
  431. #:allocation #:propagated
  432. #:propagate-to '(o1 (o2 y)))
  433. #:metaclass <composite-class>)
  434. (define o (make <c>))
  435. (is-a? o <c>))
  436. (current-module)))
  437. (pass-if "reading propagated slot"
  438. (eval '(= (x o) 1) (current-module)))
  439. (pass-if "writing propagated slot"
  440. (eval '(begin
  441. (set! (x o) 5)
  442. (and (= (x (o1 o)) 5)
  443. (= (y (o1 o)) 2)
  444. (= (x (o2 o)) 3)
  445. (= (y (o2 o)) 5)))
  446. (current-module))))
  447. (with-test-prefix "no-applicable-method"
  448. (pass-if-exception "calling generic, no methods"
  449. exception:no-applicable-method
  450. (eval '(begin
  451. (define-class <qux> ())
  452. (define-generic quxy)
  453. (quxy 1))
  454. (current-module)))
  455. (pass-if "calling generic, one method, applicable"
  456. (eval '(begin
  457. (define-method (quxy (q <qux>))
  458. #t)
  459. (define q (make <qux>))
  460. (quxy q))
  461. (current-module)))
  462. (pass-if-exception "calling generic, one method, not applicable"
  463. exception:no-applicable-method
  464. (eval '(quxy 1)
  465. (current-module))))
  466. (with-test-prefix "foreign slots"
  467. (define-class <foreign-test> ()
  468. (a #:init-keyword #:a #:class <foreign-slot>
  469. #:accessor test-a)
  470. (b #:init-keyword #:b #:init-form 3 #:class <foreign-slot>
  471. #:accessor test-b))
  472. (pass-if-equal "constructing, no initargs"
  473. '(0 3)
  474. (let ((x (make <foreign-test>)))
  475. (list (slot-ref x 'a)
  476. (slot-ref x 'b))))
  477. (pass-if-equal "constructing, initargs"
  478. '(1 2)
  479. (let ((x (make <foreign-test> #:a 1 #:b 2)))
  480. (list (slot-ref x 'a)
  481. (slot-ref x 'b))))
  482. (pass-if-equal "getters"
  483. '(0 3)
  484. (let ((x (make <foreign-test>)))
  485. (list (test-a x) (test-b x))))
  486. (pass-if-equal "setters"
  487. '(10 20)
  488. (let ((x (make <foreign-test>)))
  489. (set! (test-a x) 10)
  490. (set! (test-b x) 20)
  491. (list (test-a x) (test-b x))))
  492. (pass-if-exception "out of range"
  493. exception:out-of-range
  494. (make <foreign-test> #:a (ash 1 64))))
  495. (with-test-prefix "#:class slot allocation"
  496. (pass-if-equal "basic class slot allocation" #:class
  497. (eval '(begin
  498. (define-class <has-a-class-slot> ()
  499. (bar #:allocation #:class #:init-value 'baz))
  500. (slot-definition-allocation
  501. (class-slot-definition <has-a-class-slot> 'bar)))
  502. (current-module))))
  503. (with-test-prefix "#:each-subclass"
  504. (let* ((<subclass-allocation-test>
  505. (class ()
  506. (test #:init-value '() #:allocation #:each-subclass)
  507. #:name '<subclass-allocation-test>))
  508. (a (make <subclass-allocation-test>)))
  509. (pass-if-equal '() (slot-ref a 'test))
  510. (let ((b (make <subclass-allocation-test>)))
  511. (pass-if-equal '() (slot-ref b 'test))
  512. (slot-set! a 'test 100)
  513. (pass-if-equal 100 (slot-ref a 'test))
  514. (pass-if-equal 100 (slot-ref b 'test))
  515. ;; #:init-value of the class shouldn't reinitialize slot when
  516. ;; instances are allocated.
  517. (make <subclass-allocation-test>)
  518. (pass-if-equal 100 (slot-ref a 'test))
  519. (pass-if-equal 100 (slot-ref b 'test))
  520. (let ((<test-subclass>
  521. (class (<subclass-allocation-test>))))
  522. (pass-if-equal 100 (slot-ref a 'test))
  523. (pass-if-equal 100 (slot-ref b 'test))
  524. (let ((c (make <test-subclass>)))
  525. (pass-if-equal 100 (slot-ref a 'test))
  526. (pass-if-equal 100 (slot-ref b 'test))
  527. (pass-if-equal '() (slot-ref c 'test))
  528. (slot-set! c 'test 200)
  529. (pass-if-equal 200 (slot-ref c 'test))
  530. (make <test-subclass>)
  531. (pass-if-equal 100 (slot-ref a 'test))
  532. (pass-if-equal 100 (slot-ref b 'test))
  533. (pass-if-equal 200 (slot-ref c 'test)))))))
  534. (define-class <food> ())
  535. (define-class <fruit> (<food>))
  536. (define-class <spice> (<food>))
  537. (define-class <apple> (<fruit>))
  538. (define-class <cinnamon> (<spice>))
  539. (define-class <pie> (<apple> <cinnamon>))
  540. (define-class <d> ())
  541. (define-class <e> ())
  542. (define-class <f> ())
  543. (define-class <b> (<d> <e>))
  544. (define-class <c> (<e> <f>))
  545. (define-class <a> (<b> <c>))
  546. (with-test-prefix "compute-cpl"
  547. (pass-if-equal "<pie>"
  548. (list <pie> <apple> <fruit> <cinnamon> <spice> <food> <object> <top>)
  549. (compute-cpl <pie>))
  550. (pass-if-equal "<a>"
  551. (list <a> <b> <d> <c> <e> <f> <object> <top>)
  552. (compute-cpl <a>)))
  553. (with-test-prefix "accessor slots"
  554. (let* ((a-accessor (make-accessor 'a))
  555. (b-accessor (make-accessor 'b))
  556. (<a> (class ()
  557. (a #:init-keyword #:a #:accessor a-accessor)
  558. #:name '<a>))
  559. (<b> (class ()
  560. (b #:init-keyword #:b #:accessor b-accessor)
  561. #:name '<b>))
  562. (<ab> (class (<a> <b>) #:name '<ab>))
  563. (<ba> (class (<b> <a>) #:name '<ba>))
  564. (<cab> (class (<ab>)
  565. (a #:init-keyword #:a)
  566. #:name '<cab>))
  567. (<cba> (class (<ba>)
  568. (a #:init-keyword #:a)
  569. #:name '<cba>))
  570. (a (make <a> #:a 'a))
  571. (b (make <b> #:b 'b))
  572. (ab (make <ab> #:a 'a #:b 'b))
  573. (ba (make <ba> #:a 'a #:b 'b))
  574. (cab (make <cab> #:a 'a #:b 'b))
  575. (cba (make <cba> #:a 'a #:b 'b)))
  576. (pass-if-equal "a accessor on a" 'a (a-accessor a))
  577. (pass-if-equal "a accessor on ab" 'a (a-accessor ab))
  578. (pass-if-equal "a accessor on ba" 'a (a-accessor ba))
  579. (pass-if-exception "a accessor on cab" exception:no-applicable-method
  580. (a-accessor cab))
  581. (pass-if-exception "a accessor on cba" exception:no-applicable-method
  582. (a-accessor cba))
  583. (pass-if-equal "b accessor on a" 'b (b-accessor b))
  584. (pass-if-equal "b accessor on ab" 'b (b-accessor ab))
  585. (pass-if-equal "b accessor on ba" 'b (b-accessor ba))
  586. (pass-if-equal "b accessor on cab" 'b (b-accessor cab))
  587. (pass-if-equal "b accessor on cba" 'b (b-accessor cba))))
  588. (with-test-prefix "static slot allocation"
  589. (let* ((<a> (class () (a) #:name '<a> #:static-slot-allocation? #t))
  590. (<b> (class () (b) #:name '<b> #:static-slot-allocation? #t))
  591. (<c> (class () (c) #:name '<c>))
  592. (<ac> (class (<a> <c>) #:name '<ac>))
  593. (<ca> (class (<c> <a>) #:name '<ca>)))
  594. (pass-if-equal "slots of <ac>" '(a c)
  595. (map slot-definition-name (class-slots <ac>)))
  596. (pass-if-equal "slots of <ca>" '(a c)
  597. (map slot-definition-name (class-slots <ca>)))
  598. (pass-if-exception "can't make <ab>"
  599. '(misc-error . "static slot")
  600. (class (<a> <b>) #:name '<ab>))
  601. ;; It should be possible to create subclasses of static classes
  602. ;; whose slots are statically allocated, as long as there is no
  603. ;; diamond inheritance among static superclasses, but for now we
  604. ;; don't support it at all.
  605. (pass-if-exception "static subclass"
  606. '(misc-error . "static slot")
  607. (class (<a>) (slot) #:name '<static-sub> #:static-slot-allocation? #t))
  608. (pass-if-equal "non-static subclass" '(a d)
  609. (map slot-definition-name (class-slots (class (<a>) (d) #:name '<ad>))))))
  610. (with-test-prefix "dispatch"
  611. (pass-if-equal "multi-arity dispatch" 0
  612. (eval '(begin
  613. (define-method (dispatch (x <number>) . args) 0)
  614. (dispatch 1)
  615. (dispatch 1 2)
  616. ;; By now "dispatch" is forced into multi-arity mode. Test
  617. ;; that the multi-arity dispatcher works:
  618. (dispatch 1 2 3))
  619. (current-module))))
  620. ;; The defined? check in define-accessor prevents a local definition of
  621. ;; get-the-bar, sadly!
  622. (define-accessor get-the-bar)
  623. (with-test-prefix "slot options on redefinable classes"
  624. (let ((<meta> (class (<class>)))
  625. (box make-variable)
  626. (unbox variable-ref))
  627. (define-class <meta> (<class>))
  628. (define (boxed-slot? slot)
  629. (get-keyword #:box? (slot-definition-options slot)))
  630. (define-method (compute-getter-method (class <meta>) slot)
  631. (if (boxed-slot? slot)
  632. (make <method>
  633. #:specializers (list class)
  634. #:procedure (let ((slot-name (slot-definition-name slot)))
  635. (lambda (obj)
  636. (unbox (slot-ref obj slot-name)))))
  637. (next-method)))
  638. (define-method (compute-setter-method (class <meta>) slot)
  639. (if (boxed-slot? slot)
  640. (make <method>
  641. #:specializers (list class <top>)
  642. #:procedure (let ((slot-name (slot-definition-name slot)))
  643. (lambda (obj value)
  644. (set-box! (slot-ref obj slot-name) value))))
  645. (next-method)))
  646. (let* ((<redefinable-meta> (class (<meta> <redefinable-class>)))
  647. (<foo>
  648. (class ()
  649. (bar #:accessor get-the-bar #:box? #t #:init-form (box 123))
  650. #:metaclass <meta>))
  651. (<redefinable-foo>
  652. (class ()
  653. (bar #:accessor get-the-bar #:box? #t #:init-form (box 123))
  654. #:metaclass <redefinable-meta>)))
  655. (pass-if-equal 123 (get-the-bar (make <foo>)))
  656. (pass-if-equal 123 (get-the-bar (make <redefinable-foo>))))))