/
vending_test.clj
112 lines (91 loc) · 2.68 KB
/
vending_test.clj
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
; These are tests for an implementation of the Vending Machine specification
; from the St. Louis Lambda Lounge. See the "specification" link at
; http://groups.google.com/group/lambda-lounge/web/language-shootout
; author: R. Mark Volkmann, Object Computing, Inc.
(use 'clojure.contrib.test-is)
(use 'com.ociweb.vending)
(defn my-fixture
"wraps the execution of each test function to provide setup and teardown"
[test-function]
(fill-machine) ; setup
(test-function)
; no tear-down is needed
)
(use-fixtures :each my-fixture)
(defn commands
"calls the vending machine command function
on each command in a comma-delimited string"
[s]
(doseq [c (.split s ",")] (command c)))
(defn output-is
"compares the output of a comma-delimited set of commands
to an expected string"
[cmds expected]
(is (= expected (with-out-str (commands cmds)))))
(defn output-matches
"compares the output of a comma-delimited set of commands
to an expected regular expression"
[cmds expected-re]
(is (re-find expected-re (with-out-str (commands cmds)))))
;---------------------------------------------------------------------------
(deftest buy-with-exact-change
(output-is "q,q,d,n,A" "A\n"))
(deftest buy-with-excess-change
(output-is "1,1,A" "1
q
d
A
"))
(deftest buy-with-insufficient-change
(output-is "q,q,n,A" "insert $0.10 more\n"))
(deftest change-test
(output-is "change" "machine holds:
5 nickels
3 dimes
4 quarters
2 dollars
"))
(deftest help-test
(output-matches "help" #"^Commands are:\n"))
(deftest inserted-test
(output-is "inserted" "amount inserted is $0.00\n")
(output-is "1,q,inserted" "amount inserted is $1.25\n"))
(deftest items-test
(output-is "items" "A - 3 Juicy Fruit $0.65
B - 2 Baked Lays $1.00
C - 4 Pepsi $1.50
"))
(deftest return-test
(output-is "n,d,d,q,1,return" "1
q
q
"))
(deftest reset-machine-test
(reset-machine)
(output-is "change" "machine holds:\n")
(output-is "inserted" "amount inserted is $0.00\n")
(output-is "items" ""))
(deftest add-item-test
(reset-machine)
(add-item "A" "Juicy Fruit" 65 1)
(output-is "items" "A - 1 Juicy Fruit $0.65\n"))
(deftest add-moneytest
(reset-machine)
(add-money 25 1)
(output-is "change" "machine holds:
1 quarter
"))
(defn item-quantity
"gets the quantity of a given item that is in the machine"
[selector]
((@item-map-ref selector) :quantity))
(deftest bad-change-test
(reset-machine)
(add-item "A" "Juicy Fruit" 65 1)
(add-money 25 1)
(let [expected (item-quantity "A")]
(output-is "q,q,q,A" "use correct change\n")
; Verify that the quantity of the item didn't change.
(is (= expected (item-quantity "A")))))
; Run all the tests in the current namespace.
(run-tests)