r5176: *** empty log message ***
[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: tests.lisp,v 1.8 2003/06/17 06:33:54 kevin Exp $
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 (defclass person (hyperobject)
20   ((first-name :initarg :first-name :accessor first-name
21                :value-type (varchar 20)
22                :value-constraint stringp
23                :null-allowed nil)
24    (last-name :initarg :last-name :accessor last-name
25               :value-type (varchar 30)
26               :value-constraint stringp
27               :hyperlink find-person-by-last-name
28               :hyperlink-parameters (("narrow" . "yes"))
29               :null-allowed nil)
30    (full-name :value-type string :stored nil)
31    (dob :initarg :dob :accessor dob
32         :value-type integer
33         :print-formatter date-string
34         :value-constraint integerp
35         :input-filter convert-to-date)
36    (resume :initarg :resume :accessor resume
37            :value-type string
38            :value-constraint stringp)
39    ;;   (addresses :value-type (list-of subobject) :initarg :addresses :accessor addresses))
40    (addresses :initarg :addresses :accessor addresses
41                :subobject t))
42   (:metaclass hyperobject-class)
43   (:default-initargs :first-name "" :last-name "" :dob 0 :resume nil) 
44   (:default-print-slots first-name last-name dob resume)
45   (:user-name "Person")
46   (:description "A Person")
47   (:direct-rules
48    (:rule-1 (:dependants (last-name first-name) :volatile full-name)
49             (setf full-name (concatenate 'string first-name " " last-name)))))
50
51 (defclass address (hyperobject)
52   ((title :initarg :title :accessor title
53            :value-type (varchar 20)
54            :value-constraint stringp)
55    (street :initarg :street :accessor street
56            :value-type (varchar 30)
57            :value-constraint stringp)
58    (phones :initarg :phones :accessor phones
59             :subobject t))
60   (:metaclass hyperobject-class)
61   (:default-initargs :title nil :street nil) 
62   (:user-name "Address" "Addresses")
63   (:default-print-slots title street)
64   (:description "An address"))
65
66 (defclass phone (hyperobject)
67   ((title :initarg :title :accessor title
68           :value-type (varchar 20)
69           :value-constraint stringp)
70    (phone-number :initarg :phone-number :accessor phone-number
71                  :value-type (varchar 16)
72                  :value-constraint stringp
73                  :hyperlink search-phone-number))
74   (:metaclass hyperobject-class)
75   (:user-name "Phone Number")
76   (:default-initargs :title nil :phone-number nil)
77   (:default-print-slots title phone-number)
78   (:description "A phone number"))
79
80 (defparameter home-phone-1 (make-instance 'phone :title "Voice" :phone-number "367-9812"))
81 (defparameter home-phone-2 (make-instance 'phone :title "Fax" :phone-number "367-9813"))
82
83 (defparameter office-phone-1 (make-instance 'phone :title "Main line" :phone-number "123-0001"))
84 (defparameter office-phone-2 (make-instance 'phone :title "Staff line" :phone-number "123-0002"))
85 (defparameter office-phone-3 (make-instance 'phone :title "Fax" :phone-number "123-0005"))
86
87 (defparameter home (make-instance 'address :title "Home" :street "321 Shady Lane"
88                                   :phones (list home-phone-1 home-phone-2)))
89
90 (defparameter office (make-instance 'address :title "Office" :street "113 Main St."
91                                     :phones (list office-phone-1 office-phone-2 office-phone-3)))
92
93                               
94 (defparameter mary (make-instance 'person :first-name "Mary" :last-name "Jackson"
95                             :dob (encode-universal-time
96                                   1 2 3 4 5 2000)
97                             :addresses (list home office)
98                             :resume "Style & Grace"))
99
100
101 (defun view-to-string (obj &rest args)
102   (with-output-to-string (strm)
103     (apply #'view obj :stream strm args)))
104
105 (rem-all-tests)
106
107 (deftest p1 (view-to-string mary) "Person:
108   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
109 ")
110
111 (deftest p2 (view-to-string mary :subobjects t) "Person:
112   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
113   Addresses:
114     Home 321 Shady Lane
115     Phone Numbers:
116       Voice 367-9812
117       Fax 367-9813
118     Office 113 Main St.
119     Phone Numbers:
120       Main line 123-0001
121       Staff line 123-0002
122       Fax 123-0005
123 ")
124
125 (deftest p3 (view-to-string mary :category :compact-text-labels)
126   "Person:
127   first-name Mary last-name Jackson dob Thu, 4 May 2000 03:02:01 resume Style & Grace
128 ")
129