-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
key-comparison-test.lisp
148 lines (117 loc) · 5.52 KB
/
key-comparison-test.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;; Ensures that key-comparison.lisp and the testing library are always loaded
(eval-when (:compile-toplevel :load-toplevel :execute)
(load "key-comparison")
(ql:quickload :fiveam))
;; Defines the testing package with symbols from key-comparison and FiveAM in scope
;; The `run-tests` function is exported for use by both the user and test-runner
(defpackage :key-comparison-test
(:use :cl :fiveam :key-comparison)
(:export :run-tests))
;; Enter the testing package
(in-package :key-comparison-test)
;; forward declaration of test utilities
(declaim (ftype (function) open-room))
(defparameter +rooms+ nil)
;; Define and enter a new FiveAM test-suite
(def-suite key-comparison-suite)
(in-suite key-comparison-suite)
(test the-maze-of-object-identity "The key must match things exactly"
(is (eq 'victory (open-room :room-object-identity
#'key-object-identity))))
(test the-maze-of-numbers "The key must match numbers"
(is (eq 'victory (open-room :room-numbers
#'key-numbers))))
(test the-maze-of-different-types-of-numbers "The key must match numbers if they have the same numeric value."
(is (eq 'victory (open-room :room-numbers-of-different-types
#'key-numbers-of-different-types))))
(test the-maze-of-characters "The key must match characters"
(is (eq 'victory (open-room :room-characters #'key-characters))))
(test the-maze-of-case-insensitive-characters "The key must match characters regardless of case"
(is (eq 'victory (open-room :room-characters-case-insensitively
#'key-characters-case-insensitively))))
(test the-maze-of-strings "The key must match strings."
(is (eq 'victory (open-room :room-strings #'key-strings))))
(test the-maze-of-case-insensitive-strings "The key must match strings regardless of case"
(is (eq 'victory (open-room :room-strings-case-insensitively
#'key-strings-case-insensitively))))
(test the-maze-of-conses "The key must match conses"
(is (eq 'victory (open-room :room-conses-of-symbols
#'key-conses-of-symbols)))
(is (eq 'victory (open-room :room-conses-of-characters
#'key-conses-of-characters)))
(is (eq 'victory (open-room :room-conses-of-numbers
#'key-conses-of-numbers))))
(test the-maze-of-conses-part-2 "The key must match conses but with more flexibility of their contents"
(is (eq 'victory (open-room :room-conses-of-characters-case-insensitively
#'key-conses-of-characters-case-insensitively)))
(is (eq 'victory (open-room :room-conses-of-numbers-of-different-types
#'key-conses-of-numbers-of-different-types))))
(test the-maze-of-arrays "The key must match arrays"
(is (eq 'victory (open-room :room-arrays
#'key-arrays))))
(test the-maze-of-arrays-part-2 "The key must match arrays but with more flexibility of their contents"
(is (eq 'victory (open-room :room-arrays-loosely
#'key-arrays-loosely))))
(defun run-tests (&optional (test-or-suite 'key-comparison-suite))
"Provides human readable results of test run. Default to entire suite."
(run! test-or-suite))
;;;
;;; ==================================================
;;; Test Implementation Details
;;;
(defun open-room (room-id key-fn)
(let ((room (cdr (assoc room-id +rooms+))))
(loop for (door . behind-the-door) in room
when (apply key-fn door) do (return behind-the-door)
finally (return 'room-explodes))))
(defparameter +an-array+ #(1 2 3))
(defparameter +a-similar-but-different-array+ #(1 2.0 3))
(defparameter +a-different-array+ #(1 2 4))
(defparameter +rooms+
`(
(:room-object-identity
. ((("wrong" "WRONG") . explosion)
((2 2.0) . explosion)
((lisp LISP) . victory)))
(:room-characters
. (((#\a #\A) . explosion)
((#\a #\a) . victory)))
(:room-numbers
. (((1.0 1) . explosion)
((1.0 1.0) . victory)))
(:room-conses-of-symbols
. ((((a . b) (a . c)) . explosion)
(((a . b) (a . b)) . victory)))
(:room-conses-of-characters
. ((((#\a . #\b) (#\A . #\b)) . explosion)
(((#\a . #\b) (#\a . #\b)) . victory)))
(:room-conses-of-numbers
. ((((1 . 2) (1 . 2.0)) . explosion)
(((1 . 2) (1 . 2)) . victory)))
(:room-arrays
. (((,+an-array+ ,+a-similar-but-different-array+) . explosion)
((,+an-array+ ,+an-array+) . victory)))
(:room-strings
. ((("wrong" "WRONG") . explosion)
(("lisp" "lisp") . victory)))
(:room-characters-case-insensitively
. (((#\a #\b) . explosion)
((#\a #\A) . victory)))
(:room-numbers-of-different-types
. (((1.0 1.1) . explosion)
((1 1.0) . victory)))
(:room-strings-case-insensitively
. ((("right" "wrong") . explosion)
(("lisp" "LISP") . victory)))
(:room-conses-of-characters-case-insensitively
. ((((#\a . #\b) (#\a . #\c)) . explosion)
(((#\a . #\b) (#\A . #\B)) . victory)))
(:room-conses-of-numbers-of-different-types
. ((((1 . 1) (1 . 2)) . explosion)
(((1 . 1) (1.0 . 1.0)) . victory)))
(:room-arrays-loosely
. (((,+an-array+ ,+a-different-array+) . explosion)
((,+an-array+ ,+a-similar-but-different-array+) . victory))))
"Rooms are a sequence of pairs of a DOOR and a RESULT. A DOOR is a sequence of
things which will be given to the key. If a KEY opens a DOOR, the room will be
opened and evaluate to RESULT")