r11456: remove empty line
[hyperobject.git] / tests.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          hyperobject-tests.lisp
6 ;;;; Purpose:       Hyperobject tests file
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2003
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
14
15 (defpackage #:hyperobject-tests
16   (:use #:hyperobject #:cl #:rtest #:kmrcl))
17 (in-package #:hyperobject-tests)
18
19 (defvar *now* (get-universal-time))
20 (defun get-now () *now*)
21
22 (defclass person (hyperobject)
23   ((first-name :initarg :first-name :accessor first-name
24                :value-type (varchar 20)
25                :value-constraint stringp
26                :null-allowed nil)
27    (last-name :initarg :last-name :accessor last-name
28               :value-type (varchar 30)
29               :value-constraint stringp
30               :hyperlink find-person-by-last-name
31               :hyperlink-parameters (("narrow" . "yes"))
32               :null-allowed nil)
33    (full-name :value-type string :stored nil)
34    (dob :initarg :dob :accessor dob
35         :value-type integer
36         :print-formatter date-string
37         :value-constraint integerp
38         :input-filter convert-to-date)
39    (resume :initarg :resume :accessor resume
40            :value-type string
41            :value-constraint stringp)
42    ;;   (addresses :value-type (list-of subobject) :initarg :addresses :accessor addresses))
43    (addresses :initarg :addresses :accessor addresses
44                :subobject t)
45    (create-time :accessor create-time :compute-cached-value (get-now)))
46   (:metaclass hyperobject-class)
47   (:default-initargs :first-name "" :last-name "" :dob 0 :resume nil)
48   (:default-print-slots first-name last-name dob resume)
49   (:user-name "Person")
50   (:description "A Person")
51   (:direct-rules
52    (:rule-1 (:dependants (last-name first-name) :volatile full-name)
53             (setf full-name (concatenate 'string first-name " " last-name)))))
54
55 (defclass address (hyperobject)
56   ((title :initarg :title :accessor title
57            :value-type (varchar 20)
58            :value-constraint stringp)
59    (street :initarg :street :accessor street
60            :value-type (varchar 30)
61            :value-constraint stringp)
62    (phones :initarg :phones :accessor phones
63             :subobject t)
64    (years-at-address :initarg :years-at-address :value-type fixnum
65                      :accessor years-at-address
66                      :value-constraint integerp))
67   (:metaclass hyperobject-class)
68   (:default-initargs :title nil :street nil)
69   (:user-name "Address" "Addresses")
70   (:default-print-slots title street years-at-address)
71   (:description "An address"))
72
73 (defclass phone (hyperobject)
74   ((title :initarg :title :accessor title
75           :value-type (varchar 20)
76           :value-constraint stringp)
77    (phone-number :initarg :phone-number :accessor phone-number
78                  :value-type (varchar 16)
79                  :value-constraint stringp
80                  :hyperlink search-phone-number))
81   (:metaclass hyperobject-class)
82   (:user-name "Phone Number")
83   (:default-initargs :title nil :phone-number nil)
84   (:default-print-slots title phone-number)
85   (:description "A phone number"))
86
87 (defparameter home-phone-1 (make-instance 'phone :title "Voice" :phone-number "367-9812"))
88 (defparameter home-phone-2 (make-instance 'phone :title "Fax" :phone-number "367-9813"))
89
90 (defparameter office-phone-1 (make-instance 'phone :title "Main line" :phone-number "123-0001"))
91 (defparameter office-phone-2 (make-instance 'phone :title "Staff line" :phone-number "123-0002"))
92 (defparameter office-phone-3 (make-instance 'phone :title "Fax" :phone-number "123-0005"))
93
94 (defparameter home (make-instance 'address :title "Home" :street "321 Shady Lane"
95                                   :years-at-address 10
96                                   :phones (list home-phone-1 home-phone-2)))
97
98 (defparameter office (make-instance 'address :title "Office" :street "113 Main St."
99                                     :years-at-address 5
100                                     :phones (list office-phone-1 office-phone-2 office-phone-3)))
101
102
103 (defparameter mary (make-instance 'person :first-name "Mary" :last-name "Jackson"
104                             :dob (encode-universal-time
105                                   1 2 3 4 5 2000)
106                             :addresses (list home office)
107                             :resume "Style & Grace"))
108
109
110 (defun view-to-string (obj &rest args)
111   (with-output-to-string (strm)
112     (apply #'view obj :stream strm args)))
113
114 (rem-all-tests)
115
116 (deftest :p1 (view-to-string mary :vid :compact-text) "Person:
117   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
118 ")
119
120 (deftest :p2 (view-to-string mary :subobjects t :vid :compact-text) "Person:
121   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
122   Addresses:
123     Home 321 Shady Lane 10
124     Phone Numbers:
125       Voice 367-9812
126       Fax 367-9813
127     Office 113 Main St. 5
128     Phone Numbers:
129       Main line 123-0001
130       Staff line 123-0002
131       Fax 123-0005
132 ")
133
134 (deftest :p3 (view-to-string mary :vid :compact-text-labels)
135   "Person:
136   first-name Mary last-name Jackson dob Thu, 4 May 2000 03:02:01 resume Style & Grace
137 ")
138
139 (deftest :p4 (view-to-string mary :vid :compact-text)
140 "Person:
141   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
142 ")
143
144 (deftest :cv1 (years-at-address home)
145   10)
146
147 (deftest :cv2 (years-at-address office)
148   5)
149
150 (deftest :cv3 (equal (create-time mary) *now*)
151   t)
152
153 (deftest :s1 (slot-value (class-of mary) 'ho::user-name)
154   "Person")
155
156 (deftest :s2 (slot-value (class-of mary) 'ho::user-name-plural)
157   "Persons")
158
159 (deftest :s3 (slot-value (class-of home) 'ho::user-name-plural)
160   "Addresses")
161
162 (deftest :s4 (slot-value (class-of mary) 'ho::description)
163   "A Person")