-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.lisp
188 lines (163 loc) · 3.32 KB
/
tests.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
;;;; tests.lisp
(in-package #:shadchen)
(eos:test list1
(eos:is (equal (list :q :r :s)
(match (list :s :r :q)
((list x y z)
(list z y x))))))
(eos:test list2
(eos:is (equal
(* 1 2 3)
(match (list 1 2 3)
((list x y z)
(* x y z))))))
(eos:test list-tail
(eos:is (equal '(x y z)
(match '(q r s t u v w x y z)
((list _ _ _ _ _ _ _ (tail the-tail))
the-tail)))))
(eos:test numeric-literal
(eos:is (equal
:matched (match 10
(11 :did-not-match)
(10 :matched)))))
(eos:test string-literal
(eos:is (equal
:matched (match "cat"
(15 :did-not-match)
("cat" :matched)))))
(eos:test keyword-literal
(eos:is (equal
:matched
(match :x
(15 :did-not-match)
(:x :matched)))))
(eos:test cons
(eos:is
(equal '(b c)
(match '(a b c)
(15 :did-not-match)
((cons -ignore- tail)
tail)))))
(eos:test quote
(eos:is
(equal :matched
(match 'x
((cons _ _)
:did-not-match)
('x :matched)))))
(eos:test number1
(eos:is
(equal :matched
(match 10
((symbol) :did-not-match)
((number) :matched)))))
(eos:test number2
(eos:is
(equal :matched
(match 10
((number (p #'(lambda (x)
(< x 9))))
:did-not-match)
((number (p #'(lambda (x)
(> x 9))))
:matched)))))
(eos:test string1
(eos:is
(equal :matched
(match "x"
((symbol) :did-not-match)
((string) :matched)))))
(eos:test string2
(eos:is
(equal :matched
(match "cat"
((string (p #'(lambda (x)
(= 7 (length x)))))
:did-not-match)
((string (p #'(lambda (x)
(= (length x) 3))))
:matched)))))
(eos:test keyword1
(eos:is
(equal :matched
(match :x
((non-kw-symbol) :did-not-match)
((keyword) :matched)))))
(eos:test keyword2
(eos:is
(equal :matched
(match :cat
((keyword (p #'(lambda (x)
(= 7 (length (symbol-name x))))))
:did-not-match)
((keyword (p #'(lambda (x)
(= (length (symbol-name x)) 3))))
:matched)))))
(eos:test p
(eos:is
(equal 10
(match 10
((p #'numberp n)
n)))))
(eos:test funcall
(eos:is
(equal 11
(match 10
(:x :did-not-match)
((funcall #'(lambda (x)
(+ x 1)) r)
r)))))
(eos:test or
(eos:is
(equal :matched
(match 10
((or 14 :x)
:did-not-match)
((or 10
11)
:matched)))))
(eos:test bq
(eos:is
(equal
'b
(match '(a b c)
((bq (a (uq x) c))
x)))))
(eos:test let
(eos:is
(equal '(a b c)
(match (list)
((let
(x 'a)
(y 'b)
(z 'c))
(list x y z))))))
(eos:test must-match-succeed-case
(eos:is
(equal 10
(match 10
((must-match (number x))
x)))))
(eos:test must-match-fail-case
(eos:signals (simple-error)
(equal :x
(match :x
((must-match (number x))
x)))))
(defpackage shadchen-tests)
(defun-match- shadchen-tests::product (nil acc)
acc)
(defun-match shadchen-tests::product ((list (! (number x))
(tail rest))
acc)
(shadchen-tests::product rest (* x acc)))
(defun-match shadchen-tests::product (anything)
(shadchen-tests::product anything 1))
(eos:test defun-match
(eos:is (equal 24
(shadchen-tests::product '(1 2 3 4)))))
(eos:test defun-match-with-fail
(eos:signals (simple-error)
(shadchen-tests::product '(1 a 3 4))))
(eos:run!)