-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathswitch-test.lisp
124 lines (105 loc) · 3.61 KB
/
switch-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
;;; Copyright 2020 Google LLC
;;;
;;; Use of this source code is governed by an MIT-style
;;; license that can be found in the LICENSE file or at
;;; https://opensource.org/licenses/MIT.
;;;;
;;;; Tests the switch utility.
;;;;
(defpackage #:ace.core.switch-test
(:use #:common-lisp
#:ace.core.switch
#:ace.test))
(in-package :ace.core.switch-test)
(defvar *red* "RED")
(defvar *blue* "BLUE")
(defvar *green* "GREEN")
(defvar *1* 1)
(deftest test-switch-eql ()
(expect (eq :A (switch *1*
(1 :A)
(2 :B))))
(expect (eq nil (switch *1*
(10 :A)
(20 :B))))
(expect (eq :C (switch *1*
(10 :A)
(20 :B)
:otherwise :C)))
(expect (eq :A (eswitch *1*
(1 :A)
(2 :B))))
(assert-error (eswitch *1*
(10 :A)
(20 :B))))
(deftest test-switch-test ()
(expect (eq :blue (switch *blue* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise (error "Unknown color"))))
(let ((otherwisep nil))
(expect (eq :otherwise (switch *green* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise
(setf otherwisep t)
:otherwise)))
(expect otherwisep))
(assert-error
(switch *green* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise (error "Unknown color"))))
(deftest test-eswitch-test ()
(assert-macro-error
(eswitch *blue* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise (error "Unknown color")))
(expect (eq :blue (eswitch *blue* :test 'string=
("RED" :red)
("BLUE" :blue))))
(expect (eq :blue (eswitch (intern "BLUE" :keyword)
(:red :red)
(:blue :blue))))
(assert-error
(eswitch *green* :test 'string=
("RED" :red)
("BLUE" :blue))))
(deftest test-switch* ()
(expect (eq :A (switch* *1*
(1 (return :A))
(2 :B))))
(expect (eq :B (switch* *1*
(1 :A)
(2 (return :B)))))
(expect (eq nil (switch* *1*
(10 :A)
(20 :B)))))
(deftest test-switch*-test ()
(expect (eq :blue (switch* *red* :test 'string=
("RED" :red)
("BLUE" (return :blue))
:otherwise (error "Unknown color"))))
(expect (eq :blue (switch* *blue* :test 'string=
("RED" :red)
("BLUE" (return :blue))
:otherwise (error "Unknown color"))))
(let ((otherwisep nil))
(expect (eq :otherwise (switch* *green* :test 'string=
("RED" (return :red))
("BLUE" (return :blue))
:otherwise
(setf otherwisep t)
:otherwise)))
(expect otherwisep))
(assert-error
(switch* *red* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise (error "Unknown color")))
(assert-error
(switch* *green* :test 'string=
("RED" :red)
("BLUE" :blue)
:otherwise (error "Unknown color"))))