testsuite.scm 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ;;; This is a simple testsuite.
  2. ;;; It requires my ASSERT macros
  3. ;;; ,open assert prometheus
  4. ;;; And run the following:
  5. (define (prometheus-test)
  6. (define (println . args)
  7. (for-each display args)
  8. (newline))
  9. (println "Testing Prometheus")
  10. (println "==================")
  11. (println "CLONE")
  12. (println "-----")
  13. (let* ((o1 (*the-root-object* 'clone))
  14. (o2 (o1 'clone)))
  15. (assert "Parent slot set by clone"
  16. (eq? *the-root-object* (o1 'parent)))
  17. (newline)
  18. (println "ADD-VALUE-SLOT!")
  19. (println "---------------")
  20. (o1 'add-value-slot! 'fnord 17)
  21. (assert "Read-Only Getter"
  22. (= 17 (o1 'fnord)))
  23. (o1 'add-value-slot! 'fnord 'set-fnord! 23)
  24. (assert "Getter"
  25. (= 23 (o1 'fnord)))
  26. (o2 'set-fnord! 5)
  27. (assert "Setter"
  28. (= 5 (o2 'fnord)))
  29. (assert "Setter does not modify parent"
  30. (= 23 (o1 'fnord)))
  31. (newline)
  32. (println "ADD-METHOD-SLOT!")
  33. (println "----------------")
  34. (o1 'add-method-slot! 'add (lambda (self resend a b)
  35. (+ a b)))
  36. (assert "Read-only getter"
  37. (= 5 (o1 'add 2 3)))
  38. (o1 'add-method-slot! 'add 'set-add! (lambda (self resend a b)
  39. (+ a b)))
  40. (assert "Getter"
  41. (= 17 (o2 'add 10 7)))
  42. (o2 'set-add! (lambda (self resend a b)
  43. (* a b)))
  44. (assert "Setter"
  45. (= 42 (o2 'add 6 7)))
  46. (assert "Setter does not modify parent"
  47. (= 17 (o1 'add 10 7)))
  48. )
  49. (newline)
  50. (println "ADD-PARENT-SLOT!")
  51. (println "----------------")
  52. (let* ((testparent (*the-root-object* 'clone))
  53. (o1 (*the-root-object* 'clone))
  54. (o2 (o1 'clone))
  55. (otherparent (*the-root-object* 'clone)))
  56. (testparent 'add-value-slot! 'testparent #t)
  57. (o1 'add-parent-slot! 'parent2 testparent)
  58. (assert "Children inherit stuff from their parents"
  59. (eq? #t (o2 'testparent)))
  60. (o1 'add-parent-slot! 'parent2 'set-parent2 testparent)
  61. (assert "Parent with setter slot"
  62. (eq? #t (o2 'testparent)))
  63. (otherparent 'add-value-slot! 'otherparent #f)
  64. (o2 'set-parent2 otherparent)
  65. (assert "Setter modifies parent slot"
  66. (eq? #f (o2 'otherparent)))
  67. (assert "Setter does not modify parents"
  68. (eq? #t (o1 'testparent)))
  69. (o2 'add-value-slot! 'parent2 #f)
  70. (assert-fails "Other types of slot overwrite parent slots"
  71. (o2 'otherparent))
  72. )
  73. (newline)
  74. (println "SELF")
  75. (println "----")
  76. (let* ((o1 (*the-root-object* 'clone))
  77. (o2 (o1 'clone)))
  78. (o1 'add-method-slot! 'get-self (lambda (self resend)
  79. self))
  80. (assert "Self is passed correctly in inheritance"
  81. (eq? o2 (o2 'get-self)))
  82. )
  83. (newline)
  84. (println "Resends")
  85. (println "-------")
  86. (let* ((a (*the-root-object* 'clone))
  87. (b (a 'clone))
  88. (c (b 'clone)))
  89. (define-method (a 'info self resend)
  90. 'a)
  91. (define-method (b 'info self resend)
  92. 'b)
  93. (define-method (c 'info self resend)
  94. 'c)
  95. (define-method (c 'get-info self resend where)
  96. (resend where 'info))
  97. (c 'add-value-slot! 'parent2 a)
  98. (assert "Local resend"
  99. (eq? 'c (c 'get-info #t)))
  100. (assert "Undirected resend"
  101. (eq? 'b (c 'get-info #f)))
  102. (assert "Directed resend"
  103. (eq? 'b (c 'get-info 'parent)))
  104. (assert "Directed resend to a non-parent"
  105. (eq? 'a (c 'get-info 'parent2)))
  106. )
  107. (newline)
  108. (println "Error handling")
  109. (println "--------------")
  110. (let* ((o1 (*the-root-object* 'clone))
  111. (o2.1 (o1 'clone))
  112. (o2.2 (o1 'clone))
  113. (o3 (o2.1 'clone)))
  114. (o3 'add-parent-slot! 'parent2 o2.2)
  115. (o2.1 'add-value-slot! 'fnord 5)
  116. (o2.2 'add-value-slot! 'fnord 23)
  117. (assert-fails "Unknown message signals error"
  118. (o3 'gobble-gobble-gobble-gobble-gobble))
  119. (assert-fails "Ambiguous message signals error"
  120. (o3 'fnord))
  121. (o1 'add-method-slot! 'message-not-understood
  122. (lambda (self resend msg args)
  123. (cons 'message-not-understood
  124. (cons msg args))))
  125. (o1 'add-method-slot! 'ambiguous-message-send
  126. (lambda (self resend msg args)
  127. (cons 'ambiguous-message-send
  128. (cons msg args))))
  129. (assert "Message-not-understood is called correctly"
  130. (equal? '(message-not-understood foo 1 2 3) (o3 'foo 1 2 3)))
  131. (assert "Ambiguous-message-send is called correctly"
  132. (equal? '(ambiguous-message-send fnord 5 17 23) (o3 'fnord 5 17 23)))
  133. ;; FIXME! This might/should already die?
  134. ;; (o3 'add-parent-slot! 'parent3 #f)
  135. ;; We can write an PROMETHEUS-OBJECT? predicate
  136. ;; with the help of an error handler...
  137. (o3 'add-parent-slot! 'parent3 (lambda args #f))
  138. (assert-fails "Parent slots that are not objects cause an error"
  139. (o3 'really-does-not-exist))
  140. )
  141. (let* ((a (*the-root-object* 'clone))
  142. (b (a 'clone)))
  143. (a 'add-parent-slot! 'parent2 b)
  144. (assert-fails "Parent cycles don't cause infinite loops"
  145. (a 'skiddoo))
  146. )
  147. )