-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_script
More file actions
279 lines (229 loc) Β· 8.76 KB
/
Copy pathtest_script
File metadata and controls
279 lines (229 loc) Β· 8.76 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
#
# A simple framework for testing the binconv (bctest) scripts
#
# Returns the number of failed test cases.
#
# Format of a test:
# test 'command' expected_return_value 'stdin text' 'expected stdout' 'expected stderr'
#
# Some example test cases are given. You should add more test cases.
#
# Sam Scott, McMaster University, 2025
# GLOBALS: tc = test case number, fails = number of failed cases
declare -i tc=0
declare -i fails=0
############################################
# Run a single test. Runs a given command 3 times
# to check the return value, stdout, and stderr
#
# GLOBALS: tc, fails
# PARAMS: $1 = command
# $2 = expected return value
# $3 = standard input text to send
# $4 = expected stdout
# $5 = expected stderr
# RETURNS: 0 = success, 1 = bad return,
# 2 = bad stdout, 3 = bad stderr
############################################
test() {
tc=tc+1
local COMMAND=$1
local RETURN=$2
local STDIN=$3
local STDOUT=$4
local STDERR=$5
# CHECK RETURN VALUE
$COMMAND <<< "$STDIN" >/dev/null 2>/dev/null
local A_RETURN=$?
if [[ "$A_RETURN" != "$RETURN" ]]; then
echo "Test $tc Failed"
echo " $COMMAND"
echo " Expected Return: $RETURN"
echo " Actual Return: $A_RETURN"
fails=$fails+1
return 1
fi
# CHECK STDOUT
local A_STDOUT=$($COMMAND <<< "$STDIN" 2>/dev/null)
if [[ "$STDOUT" != "$A_STDOUT" ]]; then
echo "Test $tc Failed"
echo " $COMMAND"
echo " Expected STDOUT: $STDOUT"
echo " Actual STDOUT: $A_STDOUT"
fails=$fails+1
return 2
fi
# CHECK STDERR
local A_STDERR=$($COMMAND <<< "$STDIN" 2>&1 >/dev/null)
if [[ "$STDERR" != "$A_STDERR" ]]; then
echo "Test $tc Failed"
echo " $COMMAND"
echo " Expected STDERR: $STDERR"
echo " Actual STDERR: $A_STDERR"
fails=$fails+1
return 3
fi
# SUCCESS
echo "Test $tc Passed"
return 0
}
##########################################
# EXAMPLE TEST CASES
##########################################
# Test #1 - No arguments (usage())
test './vendingmachine_cov' 1 '' 'Usage: vendingmachine [CATEGORY (s|S|d||D|f|F)]' ''
# Test 2: Help flag
test './vendingmachine_cov --help' 0 '' 'vendingmachine: get the category name
Usage: vendingmachine [CATEGORY (s|S|d||D|f|F)]
Options:
vendingmachine [CATEGORY (s|S|d||D|f|F)] The categories that the user can choose from.
s|S Snacks
d|D Drinks
f|F FirstAid
--help This displays this help message.
Examples:
./vendingmachine s Prints all the snack items available.
./vendingmachine d Prints all the drink items available.
./vendingmachine --help Prints this help function.' ''
# Test 3: Valid snacks (only check header)
test './vendingmachine_cov s' 0 '' '
ππ‘π¬π Requested Category: Snacks
ID | Item | Price | Quantity
--------------------------------------------------------------
S12 | Chips Ahoy Cookies | 2.25 | 12
S01 | Cheesy Doritos | 2.67 | 12
S25 | Trail Mix | 2.75 | 12
S19 | Granola Bar | 1.75 | 12
S08 | Crunch Chocolate Bar | 3.99 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 4: Valid drinks (only check header)
test './vendingmachine_cov d' 0 '' '
πΉπ₯€ Requested Category: Drinks
ID | Item | Price | Quantity
--------------------------------------------------------------
D07 | Orange Juice | 2.75 | 12
D14 | Gatorade | 2.08 | 12
D05 | Coca-Cola | 1.97 | 12
D02 | Water Bottle | 1.25 | 12
D22 | Iced Tea | 2.77 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 5: Valid FirstAid (only check header)
test './vendingmachine_cov f' 0 '' '
ππ§° Requested Category: FirstAid
ID | Item | Price | Quantity
--------------------------------------------------------------
F03 | Hand Sanitizer | 4.99 | 12
F11 | Disinfectant Wipes | 3.97 | 12
F01 | Bandages | 5.97 | 12
F17 | Tylenol | 8.79 | 12
F09 | Small Cold Pack | 1.84 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 6: Uppercase S
test './vendingmachine_cov S' 0 '' '
ππ‘π¬π Requested Category: Snacks
ID | Item | Price | Quantity
--------------------------------------------------------------
S12 | Chips Ahoy Cookies | 2.25 | 12
S01 | Cheesy Doritos | 2.67 | 12
S25 | Trail Mix | 2.75 | 12
S19 | Granola Bar | 1.75 | 12
S08 | Crunch Chocolate Bar | 3.99 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 7: Uppercase D
test './vendingmachine_cov D' 0 '' '
πΉπ₯€ Requested Category: Drinks
ID | Item | Price | Quantity
--------------------------------------------------------------
D07 | Orange Juice | 2.75 | 12
D14 | Gatorade | 2.08 | 12
D05 | Coca-Cola | 1.97 | 12
D02 | Water Bottle | 1.25 | 12
D22 | Iced Tea | 2.77 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 8: Uppercase F
test './vendingmachine_cov F' 0 '' '
ππ§° Requested Category: FirstAid
ID | Item | Price | Quantity
--------------------------------------------------------------
F03 | Hand Sanitizer | 4.99 | 12
F11 | Disinfectant Wipes | 3.97 | 12
F01 | Bandages | 5.97 | 12
F17 | Tylenol | 8.79 | 12
F09 | Small Cold Pack | 1.84 | 12
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 9: Invalid category Z
test './vendingmachine_cov Z' 0 '' '
Category not known.
Usage: vendingmachine [CATEGORY (s|S|d||D|f|F)]
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 10: Multi-character category
test './vendingmachine_cov abc' 0 '' '
Category not known.
Usage: vendingmachine [CATEGORY (s|S|d||D|f|F)]
Enter item ID to purchase: Item with ID was not found.
Purchase failed.' ''
# Test 11: Missing inventory file
mv inventory.csv inventory_temp.csv
test './vendingmachine_cov s' 1 '' 'Failed to open inventory.csv' ''
mv inventory_temp.csv inventory.csv
cat defaultInventory.csv > inventory.csv
# Test 12
test './vendingmachine_cov s' 0 'S01
cash
3.02' '
ππ‘π¬π Requested Category: Snacks
ID | Item | Price | Quantity
--------------------------------------------------------------
S12 | Chips Ahoy Cookies | 2.25 | 12
S01 | Cheesy Doritos | 2.67 | 11
S25 | Trail Mix | 2.75 | 12
S19 | Granola Bar | 1.75 | 12
S08 | Crunch Chocolate Bar | 3.99 | 12
Enter item ID to purchase: Your total is $2.67
Are you paying by cash or card? (enter cash/card): The total is $3.02. Please insert your cash: Your change is 0.00
Amount user entered 3.02
Receipt
--------------------------------------------------
Item : Cheesy Doritos
ID : S01
Price : $2.67
Tax : $0.35
Total : $3.02
User Inputted : $3.02
Change : $0.00
Status : Purchase was successful!
--------------------------------------------------' ''
cat defaultInventory.csv > inventory.csv
# # Restoring Inventory - TA's suggestion
# cat defaultInventory.csv > inventory.csv
# Failing to open inventory
mv inventory.csv inventory_temp.csv
./vendingmachine_cov s
mv inventory_temp.csv inventory.csv
# YESS passed because it prints Failed to open inventory.csv
# empty inventory or failure to load
> inventory.csv # empty the file
./vendingmachine_cov s
cat defaultInventory.csv > inventory.csv # restore
# also passed since Item with ID INVALID_I was not found:
# purchase failing
echo "INVALID_ID" | ./vendingmachine_cov s
# also passed since Purchase failed.
# triggering free in loading_inventory
echo "ID,Category,Name,Price,Quantity" > inventory.csv
./vendingmachine_cov s
# buy item failures
echo "INVALID" | ./vendingmachine_cov s
# out of stock
sed -i 's/12$/0/' inventory.csv
echo "S01\ncash\n5.00" | ./vendingmachine_cov s
# return code
exit $fails