-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_toyrobot.py
92 lines (75 loc) · 3.01 KB
/
test_toyrobot.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
#run by going `python -m unittest test_toyrobot`
import unittest
# import the toyrobot module
import toyrobot
# import the Robot class
from toyrobot import Robot
class RobotTestCase(unittest.TestCase):
def setUp(self):
self.robot = Robot('Testy', 5, 5)
def testSetUp(self):
self.assertEqual(self.robot.name, 'Testy')
self.assertEqual(self.robot.table_height, 5)
self.assertEqual(self.robot.table_width, 5)
def testValidPlacement(self):
# if the placement is allowed, place returns True
self.assertTrue(self.robot.place(0,4,'S'))
self.assertTrue(self.robot.place(4,4,'S'))
# go ahead and test the properties match our parameters
self.assertEqual(self.robot.facing, 'S')
self.assertEqual(self.robot.x, 4)
self.assertEqual(self.robot.y, 4)
def testInvalidPlacementFacing(self):
self.robot.place(4,4,'X') # should succeed, facing 'N' since 'X' is invalid
self.assertEqual(self.robot.facing, 'N')
def testInvalidPlacementCoords(self):
# our board is 5x5 and 0-indexed
# if the placement is refused, place returns False
# a position of 6 is off by two.
self.assertFalse(self.robot.place(6,6,'N'))
# a position of 5 is off by one:
self.assertFalse(self.robot.place(5,5,'N'))
def testValidMove(self):
self.robot.place(0,0,'N')
# shall move forward, where forward is one position in the direction
# it's facing, and shall refuse to move if doing so would result in
# the robot falling 'off' the table.
self.assertTrue(self.robot.move())
self.assertEqual(self.robot.x, 0)
self.assertEqual(self.robot.y, 1)
def testInvalidMove(self):
self.robot.place(0,0,'S')
self.assertFalse(self.robot.move())
self.assertEqual(self.robot.x, 0)
self.assertEqual(self.robot.y, 0)
def testValidTurn(self):
self.robot.place(0,0,'N')
self.assertEqual(self.robot.facing, 'N')
self.assertTrue(self.robot.turn('LEFT'))
self.assertEqual(self.robot.facing, 'W')
self.assertTrue(self.robot.turn('RIGHT'))
self.assertEqual(self.robot.facing, 'N')
def testInvalidTurn(self):
self.robot.place(0,0,'N')
self.assertEqual(self.robot.facing,'N')
self.assertFalse(self.robot.turn('STRAIGHT'))
self.assertEqual(self.robot.facing,'N')
def testReport(self):
self.robot.place(0,0,'N')
self.assertEqual(self.robot.report(), '0, 0, N')
self.robot.move() # 0, 1, 'N'
self.robot.move() # 0, 2, 'N'
self.robot.turn('RIGHT') # 0, 2, 'E'
self.robot.move() # 1, 2, 'E'
self.robot.move() # 2, 2, 'E'
self.robot.turn('RIGHT') # 2, 2, 'S'
self.assertEqual(self.robot.report(), '2, 2, S')
def testReportPriorToPlacement(self):
unplacedBot = Robot('unplacedBot', 5, 5)
self.assertEqual(unplacedBot.report(), 'Ready to be placed on table.')
def testMovePriorToPlacement(self):
unplacedBot = Robot('unplacedBot', 5, 5)
self.assertFalse(unplacedBot.move())
def testTurnPriorToPlacement(self):
unplacedBot = Robot('unplacedBot', 5, 5)
self.assertFalse(unplacedBot.turn('RIGHT'))