-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
executable file
·2963 lines (2419 loc) · 126 KB
/
test.py
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
import signal
import unittest
import xmlrunner
from mock import (MagicMock, patch)
class GameTest(unittest.TestCase):
"""
Tests game class.
A few iterations:
-testGame1: Command does not involve passage of time.
-testGame2: Command does involve passage of time. Chance of battle = 0%.
-testGame3: Command does involve passage of time. Chance of battle = 100%.
"""
def testGame1(self):
"""
Testing that helpCommand.execute.called when command does not
involve passage of time.
"""
from game import Game
g = Game()
#Create mock objects
helpCommand = MagicMock()
helpCommand.execute = MagicMock()
helpCommand._time = False
battle = MagicMock()
g._parser.getNextCommand = MagicMock(return_value=helpCommand)
g._nextTurn()
errorMsg = "battle should not have been called but was."
self.assertFalse(battle.called, errorMsg)
errorMsg = "Game._nextTurn() failed to execute command"
self.assertTrue(helpCommand.execute.called, errorMsg)
def testCase2(self):
"""
Testing that helpCommand.execute.called when command involves
a passing of time. Battle probability = 0%.
"""
from game import Game
from space import Space
from player import Player
from battle_engine import battle
g = Game()
space = Space("Shire", "Home of the Russians", "Eregion", battleProbability = 0)
player = Player("Russian", space)
#Create mock objects
helpCommand = MagicMock()
helpCommand.execute = MagicMock()
helpCommand._time = True
battle = MagicMock()
g._parser.getNextCommand = MagicMock(return_value=helpCommand)
g._nextTurn()
errorMsg = "battle should not have been called but was."
self.assertFalse(battle.called, errorMsg)
errorMsg = "Game._nextTurn() failed to execute command"
self.assertTrue(helpCommand.execute.called, errorMsg)
def testCase3(self):
"""
Testing that helpCommand.execute.called when command involves
a passing of time. Battle probability = 100%.
"""
from game import Game
from space import Space
from player import Player
from battle_engine import battle
g = Game()
space = Space("Shire", "Home of the Russians", "Eregion", battleProbability = 1)
player = Player("Russian", space)
#Create mock objects
helpCommand = MagicMock()
helpCommand.execute = MagicMock()
helpCommand._time = True
battle = MagicMock()
g._parser.getNextCommand = MagicMock(return_value=helpCommand)
g._nextTurn()
errorMsg = "battle was supposed to have been called but was not."
self.assertTrue(battle.called, errorMsg)
errorMsg = "Game._nextTurn() failed to execute command"
self.assertTrue(helpCommand.execute.called, errorMsg)
class ParserTest(unittest.TestCase):
"""
Tests Parser class.
"""
def testGetNextCommand(self):
from parser import Parser
commandWords = MagicMock()
p = Parser(commandWords)
#Create mock objects
#raw_input = MagicMock(side_effect=["unrecognized command", "good command"])
p._commandRecognized = MagicMock(side_effect=[False, True])
fakeCommand = MagicMock()
p._commandWords.getCommand = MagicMock(return_value=fakeCommand)
#Patch raw_input and call getNextCommand()
rawInputMock = MagicMock(side_effect=["unrecognized cmd", "valid cmd"])
with patch('parser.raw_input', create=True, new=rawInputMock):
command = p.getNextCommand()
#Assert calls made
errorMsg = "Expected raw_input to be called twice."
self.assertEqual(rawInputMock.call_count, 2, errorMsg)
errorMsg = "Expected Parser._commandRecognized() to be called twice."
self.assertEqual(p._commandRecognized.call_count, 2, errorMsg)
errorMsg = "Parser.getNextCommand() did not respond expected command."
self.assertEqual(command, fakeCommand, errorMsg)
def testCommandRecognized(self):
from parser import Parser
commandWords = MagicMock()
commandWords.isCommand = MagicMock(return_value=True)
p = Parser(commandWords)
result = p._commandRecognized("valid command")
errorMsg = "Expected Parser._commandRecognized() to return True."
self.assertTrue(result, errorMsg)
class ItemTest(unittest.TestCase):
"""
Tests Item class.
"""
def testItem(self):
from items.item import Item
name = "Generic item"
description = "Generic description"
weight = 9
cost = 10
item = Item(name, description, weight, cost)
errorMsg = "Expected item name to be '%s'." % name
self.assertEqual(item.getName(), name, errorMsg)
errorMsg = "Expected item description to be '%s'." % description
self.assertEqual(item.getDescription(), description, errorMsg)
errorMsg = "Expected item weight to be '%s'." % weight
self.assertEqual(item.getWeight(), weight, errorMsg)
errorMsg = "Expected item cost to b e '%s'." % cost
self.assertEqual(item.getCost(), cost, errorMsg)
class ItemSetTest(unittest.TestCase):
"""
Tests ItemSet class.
"""
INITIAL_COUNT = 3
INITIAL_WEIGHT = 4
def setUp(self):
from items.item import Item
from items.item_set import ItemSet
sword = Item("sword", "made by elves", 2, 2)
helmet = Item("helmet", "made by men", 1, 1)
potion = Item("potion", "restores health", 1, 1)
self._itemList = [sword, helmet, potion]
self._items = ItemSet([sword, helmet, potion])
def tearDown(self):
self._itemList = self._items = None
def testInitItemSet(self):
errorMsg = "ItemSet object has more objects than it was given " \
"during initialization."
self.assertEqual(len(self._items._items), ItemSetTest.INITIAL_COUNT, errorMsg)
errorMsg = "ItemSet object does not include all objects given " \
"during initialization."
for item in self._itemList:
self.assertTrue(item in self._items._items, errorMsg)
def testCountItems(self):
expectedCount = ItemSetTest.INITIAL_COUNT
actualCount = self._items.count()
errorMsg = "Actual count and expected count different for ItemSet object."
self.assertEqual(expectedCount, actualCount, errorMsg)
def testAddRemoveContainsItems(self):
from items.item import Item
antidote = Item("antidote", "cures poison", 1, 1)
#Verify item not included in collection
errorMsg = "ItemSet.containsItem() claimed to contain item not present."
self.assertFalse(self._items.containsItem(antidote), errorMsg)
#Add item
self._items.addItem(antidote)
errorMsg = "ItemSet.containsItem() failed to identify existing item."
self.assertTrue(self._items.containsItem(antidote), errorMsg)
errorMsg = "ItemSet.containsItemWithName() failed to identify existing item."
self.assertTrue(self._items.containsItemWithName("antidote"), errorMsg)
#Remove item
self._items.removeItem(antidote)
errorMsg = "ItemSet.containsItem() claimed to contain item not present."
self.assertFalse(self._items.containsItem(antidote), errorMsg)
def testItemsWeight(self):
from items.item import Item
errorMsg = "Initial weight of ItemSet object incorrect."
expectedWeight = ItemSetTest.INITIAL_WEIGHT
actualWeight = self._items.getWeight()
self.assertEqual(expectedWeight, actualWeight, errorMsg)
heavyRock = Item("heavy rock", "weighs a ton", 2000, 0)
#Add item
self._items.addItem(heavyRock)
errorMsg = "ItemSet.weight() reported incorrect weight."
expectedWeight += 2000
actualWeight = self._items.getWeight()
self.assertEqual(expectedWeight, actualWeight, errorMsg)
#Remove item
self._items.removeItem(heavyRock)
expectedWeight -= 2000
actualWeight = self._items.getWeight()
self.assertEqual(expectedWeight, actualWeight, errorMsg)
def testItemSetIter(self):
#Verify iterator returns by ItemSet object visits the exact
#collection of objects added to ItemSet
#(Implicitly) use iterator in for loop
for item in self._items:
#Verify item returned is recognized
errorMsg = "ItemSet iterator returned unrecognized object."
self.assertTrue(item in self._itemList, errorMsg)
#Remove item from original list of items
self._itemList.remove(item)
#Assert all items are accounted for
errorMsg = "ItemSet object contained Item not added during initialization."
self.assertEqual(len(self._itemList), 0, errorMsg)
class SpaceTest(unittest.TestCase):
"""
Test for spaces.
"""
def testItems(self):
from space import Space
from items.item import Item
import constants
#Prepare items
blade = Item("blade", "appears to be dull", 1, 1)
bow = Item("bow", "long bow", 2, 2)
#Create space
space = Space("shire", "Home of the Hobbits.", constants.RegionType.MORDOR)
items = space.getItems()
#Assert space initially empty
errorMsg = "New space contains items; should be empty"
self.assertEqual(items.count(), 0, errorMsg)
errorMsg = "Space claims to contain item even though it is empty."
self.assertFalse(space.containsItem(blade), errorMsg)
self.assertFalse(space.containsItem(bow), errorMsg)
#Add blade
space.addItem(blade)
errorMsg = "Space failed to report that it contained item known to exist."
self.assertTrue(space.containsItem(blade), errorMsg)
#Add bow
space.addItem(bow)
self.assertTrue(space.containsItem(bow), errorMsg)
#Get room's items. Assert blade and bow exist
items = space.getItems()
self.assertEqual(items.count(), 2, "Room should contain exactly two items.")
self.assertTrue(items.containsItem(blade), "Could not find blade in room's set of items.")
self.assertTrue(items.containsItem(bow), "Could not find bow in room's set of items.")
#Remove blade
space.removeItem(blade)
errorMsg = "Space claims to have item that was removed."
self.assertFalse(space.containsItem(blade), errorMsg)
errorMsg = "Space missing item that should still exist."
self.assertTrue(space.containsItem(bow), errorMsg)
#Get room's items. Assert only bow exists
items = space.getItems()
self.assertEqual(items.count(), 1, "Room should contain exactly one item.")
self.assertFalse(items.containsItem(blade),
"Blade found in room (even though it was removed).")
self.assertFalse(items.containsItemWithName("blade"), "Blade found in room (even though it was removed).")
self.assertTrue(items.containsItem(bow), "Could not find bow in room's set of items.")
def testRegion(self):
from space import Space
import constants
space = Space("West Emnet", "Horses for riding", "Rohan")
errorMsg = "space.getRegion() should return 'Rohan.'"
self.assertEquals(space.getRegion(), "Rohan", errorMsg)
def testCities(self):
from space import Space
from cities.city import City
#Create city
newYorkCity = City("New York City", "An enormous city", "Come test here")
#Create space
newYork = Space("New York", "A huge space", "Welcome to our space", city = newYorkCity)
#Assert city in space
errorMsg = "space.getCity() should return newYorkCity but does not."
self.assertEqual(newYork.getCity(), newYorkCity, errorMsg)
def testUniquePlace(self):
from space import Space
from unique_place import UniquePlace
#Create UniquePlace
dmitriyHouse = UniquePlace("Dmitriy's House", "Lots of vodka", "[Knocks once.]")
#Create space
chocolateMountain = Space("Chocolate Mountain", "Chocolate rain here", "Welcome.", uniquePlace = dmitriyHouse)
#Assert uniquePlace in space
errorMsg = "space.getUniquePlace() should return dmitriyHouse but does not."
self.assertEqual(chocolateMountain.getUniquePlace(), dmitriyHouse, errorMsg)
class MovementTest(unittest.TestCase):
"""
Tests the movement methods of space and movement commands.
"""
def testMovement(self):
"""
General, encompassing test for movement, including movement commands.
"""
from space import Space
from player import Player
from commands.north_command import NorthCommand
from commands.south_command import SouthCommand
from commands.west_command import WestCommand
from commands.east_command import EastCommand
from constants import Direction
space = Space("Shire", "Home of the hobbits", "Mordor")
player = Player("Russian", space)
northCmd = NorthCommand("North", "Moves player north", player)
southCmd = SouthCommand("South", "Moves player south", player)
eastCmd = EastCommand("East", "Moves player east", player)
westCmd = WestCommand("West", "Moves player west", player)
#Non-movement case - ports not created
errorMsg = "Player should still be in space but is not."
northCmd.execute()
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
southCmd.execute()
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
eastCmd.execute()
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
westCmd.execute()
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = Space
#Create destinations and two-way ports
north = Space("North Space", "Very cold", "Welcome")
south = Space("South Space", "Very warm", "Welcome")
east = Space("East Space", "Very mountainous", "Welcome")
west = Space("West Space", "Coastal", "Welcome")
space.createExit("north", north, outgoingOnly = False)
space.createExit("south", south, outgoingOnly = False)
space.createExit("east", east, outgoingOnly = False)
space.createExit("west", west, outgoingOnly = False)
#Test getExit() for destination spaces
errorMsg = "getExit() test failed."
self.assertEqual(north.getExit("south"), space, errorMsg)
self.assertEqual(south.getExit("north"), space, errorMsg)
self.assertEqual(east.getExit("west"), space, errorMsg)
self.assertEqual(west.getExit("east"), space, errorMsg)
#Test ports created using _isExit() for space
errorMsg = "Ports are supposed to be created but are not - using _isExit()."
self.assertEqual(space._isExit("north"), True, errorMsg)
self.assertEqual(space._isExit("south"), True, errorMsg)
self.assertEqual(space._isExit("east"), True, errorMsg)
self.assertEqual(space._isExit("west"), True, errorMsg)
#Test ports created without using direct access for Space
errorMsg = "Ports are supposed to be created but were not - by direct attribute access."
self.assertEqual(space._exits[Direction.NORTH], north, errorMsg)
self.assertEqual(space._exits[Direction.SOUTH], south, errorMsg)
self.assertEqual(space._exits[Direction.EAST], east, errorMsg)
self.assertEqual(space._exits[Direction.WEST], west, errorMsg)
#Test ports created without using direct access for destination Spaces
errorMsg = "Ports are supposed to have been created but were not - by direct attribute access for destination spaces."
self.assertEqual(north._exits[Direction.SOUTH], space, errorMsg)
self.assertEqual(south._exits[Direction.NORTH], space, errorMsg)
self.assertEqual(east._exits[Direction.WEST], space, errorMsg)
self.assertEqual(west._exits[Direction.EAST], space, errorMsg)
#Test two-way movement
player._location = space
northCmd.execute()
errorMsg = "Player should be in north space but is not."
self.assertEqual(player.getLocation(), north, errorMsg)
southCmd.execute()
errorMsg = "Player should be in space but is not."
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
southCmd.execute()
errorMsg = "Player should be in south space but is not."
self.assertEqual(player.getLocation(), south, errorMsg)
northCmd.execute()
errorMsg = "Player should be in space but is not."
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
eastCmd.execute()
errorMsg = "Player should be in east space but is not."
self.assertEqual(player.getLocation(), east, errorMsg)
westCmd.execute()
errorMsg = "Player should be in space but is not."
self.assertEqual(player.getLocation(), space, errorMsg)
player._location = space
westCmd.execute()
errorMsg = "Player should be in west space but is not."
self.assertEqual(player.getLocation(), west, errorMsg)
eastCmd.execute()
errorMsg = "Player should be in space but is not."
self.assertEqual(player.getLocation(), space, errorMsg)
#Test clearExit() method
errorMsg = "Port should have been cleared but was not."
space.clearExit("north")
self.assertTrue(space._exits[Direction.North], None, errorMsg)
space.clearExit("south")
self.assertTrue(space._exits[Direction.South], None, errorMsg)
space.clearExit("east")
self.assertTrue(space._exits[Direction.East], None, errorMsg)
space.clearExit("west")
self.assertTrue(space._exits[Direction.West], None, errorMsg)
#Test _isExit() method where there are no ports
errorMsg = "_isExit() failed to return the correct port where no ports exist."
self.assertTrue(space._isExit("north"), None, errorMsg)
self.assertTrue(space._isExit("south"), None, errorMsg)
self.assertTrue(space._isExit("east"), None, errorMsg)
self.assertTrue(space._isExit("west"), None, errorMsg)
def testMovement2(self):
"""
Tests for one-way ports and one-way movement.
"""
from space import Space
from player import Player
from commands.north_command import NorthCommand
from commands.south_command import SouthCommand
from commands.west_command import WestCommand
from commands.east_command import EastCommand
space = Space("Shire", "Home of the hobbits", "Mordor")
player = Player("Russian", space)
northCmd = NorthCommand("North", "Moves player north", player)
southCmd = SouthCommand("South", "Moves player south", player)
eastCmd = EastCommand("East", "Moves player east", player)
westCmd = WestCommand("West", "Moves player west", player)
north = Space("North Space", "Very cold", "Welcome")
south = Space("South Space", "Very warm", "Welcome")
east = Space("East Space", "Very mountainous", "Welcome")
west = Space("West Space", "Coastal", "Welcome")
#Create one-way ports
space.createExit("north", north, outgoingOnly = True)
space.createExit("south", south, outgoingOnly = True)
space.createExit("east", east, outgoingOnly = True)
space.createExit("west", west, outgoingOnly = True)
#Test one-way movement
player._location = space
northCmd.execute()
errorMsg = "Player should be in north space but is not."
self.assertEqual(player.getLocation(), north, errorMsg)
southCmd.execute()
errorMsg = "Player should be in north space but is not."
self.assertEqual(player.getLocation(), north, errorMsg)
player._location = space
southCmd.execute()
errorMsg = "Player should be in south space but is not."
self.assertEqual(player.getLocation(), south, errorMsg)
northCmd.execute()
errorMsg = "Player should be in south but is not."
self.assertEqual(player.getLocation(), south, errorMsg)
player._location = space
eastCmd.execute()
errorMsg = "Player should be in east space but is not."
self.assertEqual(player.getLocation(), east, errorMsg)
westCmd.execute()
errorMsg = "Player should be in east but is not."
self.assertEqual(player.getLocation(), east, errorMsg)
player._location = space
westCmd.execute()
errorMsg = "Player should be in west space but is not."
self.assertEqual(player.getLocation(), west, errorMsg)
eastCmd.execute()
errorMsg = "Player should be in west but is not."
self.assertEqual(player.getLocation(), west, errorMsg)
class PickUpTest(unittest.TestCase):
"""
Test PickUp class.
"""
def testExecute(self):
"""
Test case: player picks up an item that may be picked up.
"""
from space import Space
from player import Player
from items.item import Item
from commands.pick_up_command import PickUpCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
item = Item("Dagger", "A trusty blade", 0, 0)
pickUpCmd = PickUpCommand("pick up", "Picks up an object", player)
space.addItem(item)
#Test pre-test conditions
self.assertTrue(space.containsItem(item), "Space should have item but does not.")
inventory = player.getInventory()
self.assertFalse(inventory.containsItem(item), "Player inventory should not have item but does.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(item), "Player equipment should not have item but does.")
#Execute pickUpCmd and assert item in player inventory but not in space and not in equipment
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.pick_up_command.raw_input', create=True, new=rawInputMock):
pickUpCmd.execute()
self.assertFalse(space.containsItem(item), "Space should not have item but does.")
inventory = player.getInventory()
self.assertTrue(inventory.containsItem(item), "Player should have item in inventory but does not.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(item), "Player should not have item in equipment but does.")
def testExecute2(self):
"""
Test case: player tries to pick up non-existent item.
"""
from space import Space
from player import Player
from commands.pick_up_command import PickUpCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
pickUpCmd = PickUpCommand("pick up", "Picks up an object", player)
#Test pre-test conditions
self.assertEqual(space._items._items, [], "Space should have no items but does.")
self.assertEqual(player._inventory._items, [], "player._inventory should have no items but does.")
self.assertEqual(player._equipped._items, [], "player._equipped should have no items but does.")
#Execute pickUpCmd and test that nothing has changed
rawInputMock = MagicMock(return_value="Shiny Acorns")
with patch('commands.pick_up_command.raw_input', create=True, new=rawInputMock):
pickUpCmd.execute()
self.assertEqual(space._items._items, [], "Space should have no items but does - post-test.")
self.assertEqual(player._inventory._items, [], "player._inventory should have no items but does - post-test.")
self.assertEqual(player._equipped._items, [], "player._equipped should have no items but does - post-test.")
class DropTest(unittest.TestCase):
"""
Test Drop class.
"""
def testExecute(self):
"""
Test case where items in inventory and equipment.
"""
from space import Space
from player import Player
from items.weapon import Weapon
from items.armor import Armor
from commands.drop_command import DropCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
dropCmd = DropCommand("drop", "Drops an object from inventory to space", player)
weapon = Weapon("Dagger", "A trusty blade", 2, 2, 2)
armor = Armor("Shield of Faith", "Quenches fiery darts", 2, 2, 2)
player.addToInventory(weapon)
player.equip(weapon)
player.addToInventory(armor)
player.equip(armor)
#Test pre-test conditions
self.assertFalse(space.containsItem(weapon), "Space should not have weapon but does.")
self.assertFalse(space.containsItem(armor), "Space should not have armor but does.")
inventory = player.getInventory()
self.assertTrue(inventory.containsItem(weapon), "Inventory should have weapon but does not.")
self.assertTrue(inventory.containsItem(armor), "Inventory should have armor but does not.")
equipped = player.getEquipped()
self.assertTrue(equipped.containsItem(weapon), "Equipped should have weapon but does not.")
self.assertTrue(equipped.containsItem(armor), "Equipped should have armor but does not.")
#Assert item in space but not in player inventory and not in equipment
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
rawInputMock = MagicMock(return_value="Shield of Faith")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
self.assertTrue(space.containsItemString("Dagger"), "Space should have weapon but does not.")
self.assertTrue(space.containsItemString("Shield of Faith"), "Space should have armor but does not.")
inventory = player.getInventory()
self.assertFalse(inventory.containsItem(weapon), "Inventory should not have weapon but does.")
self.assertFalse(inventory.containsItem(armor), "Inventory should not have armor but does.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(weapon), "Equipment should not have armor but does.")
self.assertFalse(equipped.containsItem(armor), "Equipment should not have armor but does.")
def testExecute2(self):
"""
Test case where items in inventory but not in equipment.
"""
from space import Space
from player import Player
from items.weapon import Weapon
from items.armor import Armor
from commands.drop_command import DropCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
dropCmd = DropCommand("drop", "Drops an object from inventory to space", player)
weapon = Weapon("Dagger", "A trusty blade", 2, 2, 2)
armor = Armor("Shield of Faith", "Quenches fiery darts", 2, 2, 2)
player.addToInventory(weapon)
player.addToInventory(armor)
#Test pre-test conditions
self.assertFalse(space.containsItem(weapon), "Space should not have weapon but does.")
self.assertFalse(space.containsItem(armor), "Space should not have armor but does.")
inventory = player.getInventory()
self.assertTrue(inventory.containsItem(weapon), "Inventory should have weapon but does not.")
self.assertTrue(inventory.containsItem(armor), "Inventory should have armor but does not.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(weapon), "Equipped should not have weapon but does.")
self.assertFalse(equipped.containsItem(armor), "Equipped should not have armor but does.")
#Assert item in space but not in player inventory and not in equipment
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
rawInputMock = MagicMock(return_value="Shield of Faith")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
self.assertTrue(space.containsItemString("Dagger"), "Space should have weapon but does not.")
self.assertTrue(space.containsItemString("Shield of Faith"), "Space should have armor but does not.")
inventory = player.getInventory()
self.assertFalse(inventory.containsItem(weapon), "Inventory should not have weapon but does.")
self.assertFalse(inventory.containsItem(armor), "Inventory should not have armor but does.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(weapon), "Equipment should not have weapon but does.")
self.assertFalse(equipped.containsItem(armor), "Equipment should not have armor but does.")
def testExecute3(self):
"""
Test case for when player does not have item in either inventory or equipment.
"""
from space import Space
from player import Player
from commands.drop_command import DropCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
dropCmd = DropCommand("drop", "Drops an object from inventory to space", player)
#Test pre-test conditions
self.assertEqual(space._items._items, [], "Space should have no items but does.")
self.assertEqual(player._inventory._items, [], "player._inventory should have no items but does.")
self.assertEqual(player._equipped._items, [], "player._equipped should have no items but does.")
#Attempt to drop item that does not exist
rawInputMock = MagicMock(return_value="Melted Cheese")
with patch('commands.drop_command.raw_input', create=True, new=rawInputMock):
dropCmd.execute()
self.assertEqual(space._items._items, [], "Space should have no items but does - post-test.")
self.assertEqual(player._inventory._items, [], "player._inventory should have no items but does - post-test.")
self.assertEqual(player._equipped._items, [], "player._equipped should have no items but does - post-test.")
class EquipTest(unittest.TestCase):
"""
Tests Equip Command.
"""
def testPositiveCase(self):
"""
Testcase: for equipping an item that may be equipped.
"""
from player import Player
from space import Space
from items.weapon import Weapon
from items.armor import Armor
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
weapon = Weapon("Dagger", "A trusty blade", 2, 2, 2)
armor = Armor("Shield", "Very faithful", 2, 2, 2)
inventory = player.getInventory()
inventory.addItem(weapon)
inventory.addItem(armor)
#Equipping equippable items
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
rawInputMock = MagicMock(return_value="Shield")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
equipped = player.getEquipped()
self.assertTrue(inventory.containsItem(weapon), "Inventory should still have weapon but does not.")
self.assertTrue(inventory.containsItem(armor), "Inventory should still have armor but does not.")
self.assertTrue(equipped.containsItem(weapon), "Player failed to equip equipable weapon.")
self.assertTrue(equipped.containsItem(armor), "Player failed to equip equipable armor.")
def testNegativeCase1(self):
"""
Attempting to equip items not in inventory.
"""
from player import Player
from space import Space
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
#Test pre-test conditions
inventory = player.getInventory()
errorMsg = "player._inventory is supposed to be empty but is not."
self.assertEqual(inventory.count(), 0, errorMsg)
equipped = player.getEquipped()
errorMsg = "player._equipped is supposed to be empty but is not."
self.assertEqual(equipped.count(), 0, errorMsg)
#Trying to equip items not in inventory
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
rawInputMock = MagicMock(return_value="Shield")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
errorMsg = "player._inventory is supposed to be empty but is not."
self.assertEqual(inventory.count(), 0, errorMsg)
errorMsg = "player._equipped is supposed to be empty but is not."
self.assertEqual(equipped.count(), 0, errorMsg)
def testNegativeCase2(self):
"""
Attempting to equip an item that may not be equipped.
In this instance, item is of the Item class.
"""
from player import Player
from space import Space
from items.item import Item
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
item = Item("Charm", "Unknown effects", 1, 1)
inventory = player.getInventory()
inventory.addItem(item)
#Trying to equip item that cannot be equipped
rawInputMock = MagicMock(return_value="Charm")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
self.assertTrue(inventory.containsItem(item), "Inventory should have item.")
equipped = player.getEquipped()
self.assertFalse(equipped.containsItem(item), "Player equipped item of Item class.")
def testNegativeCase3(self):
"""
Attempting to equip items that are already equipped.
"""
from player import Player
from space import Space
from items.weapon import Weapon
from items.armor import Armor
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
weapon = Weapon("Dagger", "A trusty blade", 2, 2, 2)
armor = Armor("Shield", "of faith", 2, 2, 2)
inventory = player.getInventory()
inventory.addItem(weapon)
inventory.addItem(armor)
equipped = player.getEquipped()
equipped.addItem(weapon)
equipped.addItem(armor)
#Test - preconditions
errorMsg = "Weapon and armor are supposed to be in inventory but are not."
self.assertTrue(weapon in inventory._items, errorMsg)
self.assertTrue(armor in inventory._items, errorMsg)
errorMsg = "Inventory is supposed to have two items."
self.assertEqual(player._inventory.count(), 2, errorMsg)
errorMsg = "Weapon is supposed to be in player._equipped but is not."
self.assertTrue(weapon in player._equipped, errorMsg)
errorMsg = "Armor is supposed to be in player._equipped but it is not."
self.assertTrue(armor in player._equipped, errorMsg)
errorMsg = "player._equipped is supposed to have two items but does not."
self.assertEqual(player._equipped.count(), 2, errorMsg)
#Equipping an item that is already equipped
rawInputMock = MagicMock(return_value="Dagger")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
rawInputMock = MagicMock(return_value="Shield")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
#Test still only two equipped items
errorMsg = "Weapon and armor are supposed to be in inventory but are not."
self.assertTrue(weapon in player._inventory, errorMsg)
self.assertTrue(armor in player._inventory, errorMsg)
errorMsg = "Player inventory is only supposed to have two items."
self.assertEqual(player._inventory.count(), 2, errorMsg)
errorMsg = "Weapon is supposed to be in player._equipped but is not."
self.assertTrue(weapon in player._equipped, errorMsg)
errorMsg = "Armor is supposed to be in player._equipped but it is not."
self.assertTrue(armor in player._equipped, errorMsg)
errorMsg = "Player is supposed to have two equipped items but lists more."
self.assertEqual(player._equipped.count(), 2, errorMsg)
def testPlayerWeaponStats(self):
from player import Player
from space import Space
from items.weapon import Weapon
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
defaultAttack = player._attack
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
weapon = Weapon("Sword of the Spirit", "Sharper than any double-edged sword", 1, 1, 1)
player.addToInventory(weapon)
#Test preconditions
errorMsg = "Weapon should be in player._inventory._items but is not."
self.assertTrue(weapon in player._inventory._items, errorMsg)
errorMsg = "Weapon shoud not be in player._equipped._items but is not."
self.assertTrue(weapon not in player._equipped._items, errorMsg)
#Equip weapon and check that _totalAttack and _weaponAttack update
rawInputMock = MagicMock(return_value="Sword of the Spirit")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
errorMsg = "Weapon should be in player._inventory._items but is not."
self.assertTrue(player._inventory.containsItem(weapon), errorMsg)
errorMsg = "Weapon should be equipped but is not."
self.assertTrue(player._equipped.containsItem(weapon), errorMsg)
#Test for change
errorMsg = "player._attack changed with weapon equip when it should not have."
self.assertEqual(player._attack, defaultAttack, errorMsg)
errorMsg = "player._weaponAttack not updated to correct value."
self.assertEqual(player._weaponAttack, weapon._attack, errorMsg)
errorMsg = "player._totalAttack not updated to correct value."
self.assertEqual(player._totalAttack, defaultAttack + weapon._attack, errorMsg)
def testPlayerArmorStats(self):
from player import Player
from space import Space
from items.armor import Armor
from commands.equip_command import EquipCommand
space = Space("Shire", "Home of the Hobbits.", "Mordor")
player = Player("Frodo", space)
equipCmd = EquipCommand("Equip", "Equips item in inventory to player", player)
armor = Armor("Shield of Faith", "For quenching fiery darts", 1, 1, 1)
player.addToInventory(armor)
#Test preconditions
errorMsg = "Armor should be in player._inventory._items but is not."
self.assertTrue(armor in player._inventory._items, errorMsg)
errorMsg = "Armor should not be in player._equipped._items but is not."
self.assertTrue(armor not in player._equipped._items, errorMsg)
#Equip armor and check that player._defense updates
rawInputMock = MagicMock(return_value="Shield of Faith")
with patch('commands.equip_command.raw_input', create=True, new=rawInputMock):
equipCmd.execute()
errorMsg = "Armor should be in inventory but is not."
self.assertTrue(player._inventory.containsItem(armor), errorMsg)
errorMsg = "Armor should be equipped but is not."
self.assertTrue(player._equipped.containsItem(armor), errorMsg)
#Test for change
errorMsg = "player._armorDefense stat was not updated correctly."
self.assertEqual(player._armorDefense, armor._defense, errorMsg)
class UnequipTest(unittest.TestCase):
"""
Tests Unequip Command.
"""
def testPositiveCase(self):
"""
Unequipping an unequippable item.
"""
from player import Player