r4664: *** 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.2 2003/04/28 19:06:13 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (defpackage #:hyperobject-tests
17   (:use #:hyperobject #:cl #:rtest))
18 (in-package #:hyperobject-tests)
19
20 (defun format-date (ut)
21   (when (typep ut 'integer)
22     (multiple-value-bind (sec min hr day mon year dow daylight-p zone)
23         (decode-universal-time ut)
24       (declare (ignore daylight-p zone))
25       (format nil "~[Mon~;Tue~;Wed~;Thu~;Fri~;Sat~;Sun~], ~d ~[Jan~;Feb~;Mar~;Apr~;May~;Jun~;Jul~;Aug~;Sep~;Oct~;Nov~;Dec~] ~d ~2,'0d:~2,'0d:~2,'0d" 
26               dow
27               day
28               (1- mon)
29               year
30               hr min sec))))
31
32 (defclass person (hyperobject)
33   ((first-name :initarg :first-name :accessor first-name
34                :value-type (varchar 20)
35                :value-constraint stringp
36                :null-allowed nil)
37    (last-name :initarg :last-name :accessor last-name
38               :value-type (varchar 30)
39               :value-constraint stringp
40               :hyperlink find-person-by-last-name
41               :null-allowed nil)
42    (full-name :value-type string :stored nil)
43    (dob :initarg :dob :accessor dob
44         :value-type integer
45         :print-formatter format-date
46         :value-constraint integerp
47         :input-filter convert-to-date)
48    (resume :initarg :resume :accessor resume
49            :value-type string
50            :value-constraint stringp)
51    ;;   (addresses :value-type (list-of subobject) :initarg :addresses :accessor addresses))
52    (addresses :initarg :addresses :accessor addresses
53                :subobject t))
54   (:metaclass hyperobject-class)
55   (:default-initargs :first-name "" :last-name "" :dob 0 :resume nil) 
56   (:default-print-slots first-name last-name dob resume)
57   (:user-name "Person")
58   (:description "A Person")
59   (:direct-rules
60    (:rule-1 (:dependants (last-name first-name) :volatile full-name)
61             (setf full-name (concatenate 'string first-name " " last-name)))))
62
63 (defclass address (hyperobject)
64   ((title :initarg :title :accessor title
65            :value-type (varchar 20)
66            :value-constraint stringp)
67    (street :initarg :street :accessor street
68            :value-type (varchar 30)
69            :value-constraint stringp)
70    (phones :initarg :phones :accessor phones
71             :subobject t))
72   (:metaclass hyperobject-class)
73   (:default-initargs :title nil :street nil) 
74   (:user-name "Address")
75   (:default-print-slots title street)
76   (:description "An address"))
77
78 (defclass phone (hyperobject)
79   ((title :initarg :title :accessor title
80           :value-type (varchar 20)
81           :value-constraint stringp)
82    (phone-number :initarg :phone-number :accessor phone-number
83                  :value-type (varchar 16)
84                  :value-constraint stringp))
85   (:metaclass hyperobject-class)
86   (:user-name "Phone Number")
87   (:default-initargs :title nil :phone-number nil)
88   (:default-print-slots title phone-number)
89   (:description "A phone number"))
90
91 (defparameter home-phone-1 (make-instance 'phone :title "Voice" :phone-number "367-9812"))
92 (defparameter home-phone-2 (make-instance 'phone :title "Fax" :phone-number "367-9813"))
93
94 (defparameter office-phone-1 (make-instance 'phone :title "Main line" :phone-number "123-0001"))
95 (defparameter office-phone-2 (make-instance 'phone :title "Staff line" :phone-number "123-0002"))
96 (defparameter office-phone-3 (make-instance 'phone :title "Fax" :phone-number "123-0005"))
97
98 (defparameter home (make-instance 'address :title "Home" :street "321 Shady Lane"
99                                   :phones (list home-phone-1 home-phone-2)))
100
101 (defparameter office (make-instance 'address :title "Office" :street "113 Main St."
102                                     :phones (list office-phone-1 office-phone-2 office-phone-3)))
103
104                               
105 (defparameter mary (make-instance 'person :first-name "Mary" :last-name "Jackson"
106                             :dob (encode-universal-time
107                                   1 2 3 4 5 2000)
108                             :addresses (list home office)
109                             :resume "Style & Grace"))
110
111 ;(format t "~&Text Format~%")
112 ;(view mary :subobjects t)
113
114 ;(format t "~&XML Format with field labels and hyperlinks~%")
115 ;(view mary :subobjects t :category :xml-link-labels)
116
117 (defun view-to-string (obj &rest args)
118   (with-output-to-string (strm)
119     (apply #'view obj :stream strm args)))
120
121 (rem-all-tests)
122
123 (deftest p1 (view-to-string mary) " Person:
124   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
125 ")
126
127 (deftest p2 (view-to-string mary :subobjects t) " Person:
128   Mary Jackson Thu, 4 May 2000 03:02:01 Style & Grace
129   Addresss:
130     Home 321 Shady Lane
131     Phone Numbers:
132       Voice 367-9812
133       Fax 367-9813
134     Office 113 Main St.
135     Phone Numbers:
136       Main line 123-0001
137       Staff line 123-0002
138       Fax 123-0005
139 ")