-
Notifications
You must be signed in to change notification settings - Fork 0
/
library_test.rb
470 lines (397 loc) · 11.4 KB
/
library_test.rb
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
require_relative "library"
require "test/unit"
class TestLibrary < Test::Unit::TestCase
self.test_order = :defined
# create instance variables of objects for all tests
def setup
@cal = Calendar.instance
@book = Book.new(1, "1984", "George Orwell")
@lib = Library.new
@mem = Member.new("Anon", @lib)
@lib.open
@lib.issue_card("Alice")
@lib.close
end
# testing the Calendar class
# must be called before all Calendar.advance methods to pass
def test_calendar_init
assert_equal(1, @cal.get_date)
end
def test_calendar_advance
start = @cal.get_date
@cal.advance
assert_equal(start + 1, @cal.get_date)
end
# testing the Book class
def test_book_get_id
assert_equal(1, @book.get_id)
end
def test_book_get_title
assert_equal("1984", @book.get_title)
end
def test_book_get_author
assert_equal("George Orwell", @book.get_author)
end
def test_book_get_due_date_nil
assert_nil(@book.get_due_date)
end
def test_book_check_out
date = @cal.get_date + 14
@book.check_out(date)
assert_equal(date, @book.get_due_date)
end
def test_book_check_in
@book.check_in
assert_nil(@book.get_due_date)
end
def test_book_to_s
assert_equal("1: 1984, by George Orwell", @book.to_s)
end
def test_member_get_name
assert_equal("Anon", @mem.get_name)
end
def test_member_no_books
assert(@mem.get_books.empty?)
end
def test_member_check_out
@mem.check_out(@book)
temp_set = Set.new(@book)
assert_equal(temp_set, @mem.get_books)
end
def test_member_give_back
@mem.give_back(@book)
assert(@mem.get_books.empty?)
end
def test_member_send_overdue_notice
assert_equal("Anon: Books are overdue",
@mem.send_overdue_notice("Books are overdue"))
end
# open
def test_library_open
start = @cal.get_date + 1
assert_equal("Today is day #{start}.", @lib.open)
end
def test_library_open_date
tempCal = @cal.get_date
@lib.open
assert_equal(tempCal + 1, @cal.get_date)
end
def test_library_already_open
@lib.open
assert_raise(RuntimeError.new("The library is already open!")) do @lib.open
end
end
# find all overdue books
def test_library_find_all_overdue_books_none
@lib.open
assert_equal("No books are overdue.", @lib.find_all_overdue_books)
end
def test_library_find_all_overdue_books
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
i = 8
while i > 0
@lib.close
@lib.open
i -= 1
end
assert_equal("Overdue books for Alice:\n1: 1984, by George Orwell\n",
@lib.find_all_overdue_books)
end
def test_library_find_all_overdue_books_multiline
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
@lib.issue_card("Fred")
@lib.serve("Fred")
@lib.check_out([3,4])
i = 8
while i > 0
@lib.close
@lib.open
i -= 1
end
assert_equal("Overdue books for Alice:\n1: 1984, by George Orwell\nOverdue books for Fred:\n3: The Cider House Rules, by John Irving\n4: Atlas Shrugged, by Ayn Rand\n",
@lib.find_all_overdue_books)
end
# issue card
def test_library_issue_card
@lib.open
assert_equal("Library card issued to Fred.", @lib.issue_card("Fred"))
end
def test_library_issue_card_already
@lib.open
assert_equal("Alice already has a library card.", @lib.issue_card("Alice"))
end
def test_library_issue_card_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.issue_card("Alice")
end
end
# serve
def test_library_serve
@lib.open
assert_equal("Now serving Alice", @lib.serve("Alice"))
end
def test_library_serve_current
@lib.open
@lib.serve("Alice")
assert_equal("Alice", @lib.current_member.get_name)
end
def test_library_serve_no_card
@lib.open
assert_equal("Fred does not have a library card.", @lib.serve("Fred"))
end
def test_library_serve_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.serve("Alice")
end
end
# find overdue books
def test_library_find_overdue_books_none
@lib.open
@lib.serve("Alice")
assert_equal("Overdue books for Alice:\nNone\n", @lib.find_overdue_books)
end
def test_library_find_overdue_books
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
i = 8
while i > 0
@lib.close
@lib.open
i -=1
end
@lib.serve("Alice")
assert_equal("Overdue books for Alice:\n1: 1984, by George Orwell\n",
@lib.find_overdue_books)
end
def test_library_find_overdue_books_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.find_overdue_books
end
end
def test_library_find_overdue_books_no_member
@lib.open
assert_raise(RuntimeError.new("No member is currently being served.")) do @lib.find_overdue_books
end
end
# check in
def test_library_check_in
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
assert_equal("Alice has returned 1 book.", @lib.check_in([1]))
end
def test_library_check_in_current
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
@lib.check_in([1])
assert(@lib.current_member.get_books.empty?)
end
def test_library_check_in_multi
@lib.open
@lib.serve("Alice")
@lib.check_out([1,2])
assert_equal("Alice has returned 2 books.", @lib.check_in([1, 2]))
end
def test_library_check_in_collection
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
@lib.check_in([1])
assert_equal("1: 1984, by George Orwell\n", @lib.search("1984"))
end
def test_library_check_in_members_books
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
@lib.check_in([1])
assert(@lib.current_member.get_books.empty?)
end
def test_library_check_in_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.check_in([1])
end
end
def test_library_check_in_no_member
@lib.open
assert_raise(RuntimeError.new("No member is currently being served.")) do @lib.check_in([1])
end
end
def test_library_check_in_wrong_book
@lib.open
@lib.serve("Alice")
assert_raise(RuntimeError.new("The member does not have book 2.")) do @lib.check_in([2])
end
end
# search
def test_library_search_multiline
assert_equal("1: 1984, by George Orwell\n5: 1985, by Georgina Doorbell\n",
@lib.search("geor"))
end
def test_library_search_none
assert_equal("No books found.", @lib.search("SDGJRIOGH"))
end
def test_library_search_4chars
assert_equal("Search string must contain at least four characters.",
@lib.search("S"))
end
# check out
def test_library_check_out
@lib.open
@lib.serve("Alice")
assert_equal("1 book has been checked out to Alice.", @lib.check_out([1]))
end
def test_library_check_out_current
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
assert([email protected]_member.get_books.empty?)
end
def test_library_check_out_all
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
assert_equal("No books found.", @lib.search("Orwell"))
end
def test_library_check_out_multi
@lib.open
@lib.serve("Alice")
assert_equal("2 books have been checked out to Alice.", @lib.check_out([1, 2]))
end
def test_library_check_out_over3_at_once
@lib.open
@lib.serve("Alice")
assert_raise(RuntimeError.new("This transaction will make Alice go over the 3 book limit.")) do @lib.check_out([1,2,3,4])
end
end
def test_library_check_out_over3_individual
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
@lib.check_out([2])
@lib.check_out([3])
assert_raise(RuntimeError.new("This transaction will make Alice go over the 3 book limit.")) do @lib.check_out([4])
end
end
def test_library_check_out_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.check_out([1])
end
end
def test_library_check_out_no_member
@lib.open
assert_raise(RuntimeError.new("No member is currently being served.")) do @lib.check_out([1])
end
end
def test_library_check_out_wrong_book
@lib.open
@lib.serve("Alice")
assert_raise(RuntimeError.new("The library does not have book 99.")) do @lib.check_out([99])
end
end
# renew
def test_library_renew
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
assert_equal("1 book has been renewed for Alice.", @lib.renew([1]))
end
def test_library_renew_current
start = @cal.get_date # e.g. date = 0
@lib.open # date = 1
@lib.serve("Alice")
@lib.check_out([1])
@lib.close
@lib.open # date = 2
@lib.serve("Alice")
@lib.renew([1]) # new due date = 2 + 7 = 9
assert_equal(start + 9, check_due_date(1, @lib.current_member))
end
# helper method for test_library_renew_current
def check_due_date(id, mem)
date_array = Array.new
mem.get_books.each do |b|
date_array << b.get_due_date
end
date_array[0]
end
def test_library_renew_multi
@lib.open
@lib.serve("Alice")
@lib.check_out([1, 2])
assert_equal("2 books have been renewed for Alice.", @lib.renew([1, 2]))
end
def test_library_renew_members_books
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
assert_equal("1 book has been renewed for Alice.", @lib.renew([1]))
end
def test_library_renew_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.renew([1])
end
end
def test_library_renew_no_member
@lib.open
assert_raise(RuntimeError.new("No member is currently being served.")) do @lib.renew([1])
end
end
def test_library_renew_wrong_book
@lib.open
@lib.serve("Alice")
assert_raise(RuntimeError.new("The member does not have book 2.")) do @lib.renew([2])
end
end
# close
def library_test_close_string
@lib.open
assert_equal("Good night.", @lib.close)
end
def test_library_close_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.close
end
end
# quit
def test_library_quit
assert_equal("The library is now closed for renovations.", @lib.quit)
end
def test_library_add_book
@lib.add_book("Winnie The Pooh\tA.A. Milne")
assert_equal("6: Winnie The Pooh, by A.A. Milne\n", @lib.search("pooh"))
end
def test_library_is_not_open
assert_raise(RuntimeError.new("The library is not open.")) do @lib.is_not_open
end
end
def test_library_no_member
assert_raise(RuntimeError.new("No member is currently being served.")) do @lib.no_member
end
end
def test_library_find_overdue_books_for_member_none
@lib.open
@lib.serve("Alice")
assert_equal("Overdue books for Alice:\nNone\n", @lib.find_overdue_books_for_member(@lib.current_member))
end
def test_library_find_overdue_books_for_member
@lib.open
@lib.serve("Alice")
@lib.check_out([1])
i = 8
while i > 0
@lib.close
@lib.open
i -= 1
end
@lib.serve("Alice")
assert_equal("Overdue books for Alice:\n1: 1984, by George Orwell\n", @lib.find_overdue_books_for_member(@lib.current_member))
end
def test_library_a_to_multiline_s
b = Book.new(7, "a made up", "book")
assert_equal("1\na cat\n7: a made up, by book\n", @lib.a_to_multiline_s([1, "a cat", b]))
end
def test_library_find_book_by_id
temp_book_array = Array.new
temp_book_array << @book
assert_equal("1: 1984, by George Orwell", @lib.find_book_by_id(1, temp_book_array).to_s)
end
end