git.fiddlerwoaroof.com
Browse code

Adjusting parser to simplify record processing

fiddlerwoaroof authored on 28/04/2016 02:44:35
Showing 3 changed files
... ...
@@ -5,6 +5,11 @@
5 5
    (total-hours :initform 0 :initarg :total-hours :accessor total-hours)
6 6
    (client-totals :initarg :client-totals :accessor client-totals :initform (make-hash-table :test 'equalp))))
7 7
 
8
+(defclass report ()
9
+  ((status-calculator :initarg :status-calculator :accessor status-calculator)
10
+   (status-lines :initform nil :accessor :status-lines)
11
+   (entries :initform nil :accessor :entries)))
12
+
8 13
 (defun make-status-calculator (rate)
9 14
   (make-instance 'status-calculator :rate rate))
10 15
 
... ...
@@ -24,7 +24,7 @@
24 24
 
25 25
   (defclass time-mod ()
26 26
     ((amount :initarg :amount)
27
-     (unit :initarg :unit))))
27
+     (unit :initarg :unit :type (member '(:hour :minute))))))
28 28
 
29 29
 (defgeneric unparse (token &optional stream)
30 30
   (:method ((token time-mod) &optional stream)
... ...
@@ -60,7 +60,7 @@
60 60
 (make-equality date-obj
61 61
   (day-of-week ==)
62 62
   (year) (month) (day))
63
-(make-simple-equality time-mod :test equal)
63
+(make-simple-equality time-mod :test eql)
64 64
 
65 65
 
66 66
 (defun make-day-entry (date records)
... ...
@@ -70,15 +70,18 @@
70 70
   (make-instance 'time-record :client (car memo) :ranges ranges :memo (cadr memo)))
71 71
 
72 72
 (defun make-time-mod (amnt unt)
73
-  (setf unt (string-downcase unt))
74
-  (when (string= "min" unt)
75
-    (setf unt "mins"))
76
-  (when (string= "hr" unt)
77
-    (setf unt "hours"))
78
-  (alet (make-instance 'time-mod)
79
-    (with-slots (amount unit) it
80
-      (setf amount amnt unit unt)
81
-      it)))
73
+  (setf unt (string-upcase unt))
74
+  (make-instance 'time-mod
75
+                 :amount amnt
76
+                 :unit (string-ecase unt
77
+                         ("min" :MINUTE)
78
+                         ("mins" :MINUTE)
79
+                         ("minutes" :MINUTE)
80
+                         ("minute" :MINUTE)
81
+                         ("hr" :HOUR)
82
+                         ("hrs" :HOUR)
83
+                         ("hour" :HOUR)
84
+                         ("hours" :HOUR))))
82 85
 
83 86
 (define-condition parsing-error (parse-error)
84 87
   ((failed-chunk :initarg :failed-chunk :reader failed-chunk)))
... ...
@@ -212,6 +212,31 @@
212 212
   (st:should be == "   "
213 213
              (smug:parse (.initial-space) "   ")))
214 214
 
215
+(st:deftest make-time-mod ()
216
+  (st:should be ==
217
+             (make-instance 'time-mod :unit :hour :amount 0) 
218
+             (make-time-mod 0 "hours"))
219
+
220
+  (st:should be ==
221
+             (make-time-mod 0 "hours")
222
+             (make-time-mod 0 "hours"))
223
+
224
+  (st:should be ==
225
+             (make-time-mod 0 "hr")
226
+             (make-time-mod 0 "hours"))
227
+
228
+  (st:should be ==
229
+             (make-time-mod 0 "hrs")
230
+             (make-time-mod 0 "hours"))
231
+
232
+  (st:should be ==
233
+             (make-time-mod 0 "min")
234
+             (make-time-mod 0 "minutes"))
235
+
236
+  (st:should be ==
237
+             (make-time-mod 0 "mins")
238
+             (make-time-mod 0 "minutes")))
239
+
215 240
 (st:deftest date-test ()
216 241
   (st:should be == nil
217 242
              (caar (smug:run (.date) "Monday 2020/01-01")))