1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) 2018 Douglas Reith. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Reith\ToyRobot\Domain\Robot; |
11
|
|
|
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Reith\ToyRobot\Domain\Space\Table; |
14
|
|
|
|
15
|
|
|
class RobotTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public static function testCreatingARobot() |
18
|
|
|
{ |
19
|
|
|
// This is an example of a 5x5 table |
20
|
|
|
$table = Table::create(5); |
21
|
|
|
|
22
|
|
|
// To create (or reset) a robot it must be placed in a space. |
23
|
|
|
// Default placement is 0,0 (for 2 dimensions) |
|
|
|
|
24
|
|
|
$robot = $table->placeRobot(); |
25
|
|
|
|
26
|
|
|
self::assertInstanceOf(Robot::class, $robot); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @test |
31
|
|
|
*/ |
32
|
|
|
public static function robotCanMove() |
33
|
|
|
{ |
34
|
|
|
$table = Table::create(5); |
35
|
|
|
$robot = $table->placeRobot(); |
36
|
|
|
|
37
|
|
|
// From origin, 0,0, robot can move northward |
38
|
|
|
$place = $robot->moveNorthward()->getPlacement(); |
39
|
|
|
|
40
|
|
|
// Now it should be at 0,1 |
41
|
|
|
self::assertEquals([0, 1], $place->getVector()); |
42
|
|
|
|
43
|
|
|
$place = $robot->moveSouthward()->getPlacement(); |
44
|
|
|
|
45
|
|
|
// Now it should be back at origin |
46
|
|
|
self::assertEquals([0, 0], $place->getVector()); |
47
|
|
|
|
48
|
|
|
// Let's get it dancing |
49
|
|
|
$place = $robot |
50
|
|
|
->moveEastward() |
51
|
|
|
->moveEastward() |
52
|
|
|
->moveNorthward() |
53
|
|
|
->moveNorthward() |
54
|
|
|
->moveWestward() |
55
|
|
|
->moveNorthward() |
56
|
|
|
->getPlacement() |
57
|
|
|
; |
58
|
|
|
|
59
|
|
|
self::assertEquals([1, 3], $place->getVector()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @expectedException \Assert\AssertionFailedException |
65
|
|
|
*/ |
66
|
|
|
public static function robotCannotStartWithABadDirection() |
67
|
|
|
{ |
68
|
|
|
$table = Table::create(3); |
|
|
|
|
69
|
|
|
|
70
|
|
|
Robot::create( |
71
|
|
|
Table::create(4), |
72
|
|
|
Place::create([0, 1]), |
73
|
|
|
'U' |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
* @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException |
80
|
|
|
*/ |
81
|
|
|
public static function robotCannotFallOffTheSouthSide() |
82
|
|
|
{ |
83
|
|
|
$table = Table::create(5); |
84
|
|
|
$robot = $table->placeRobot(); |
85
|
|
|
|
86
|
|
|
$robot->moveSouthward(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @test |
91
|
|
|
* @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException |
92
|
|
|
*/ |
93
|
|
|
public static function robotCannotFallOffTheWestSide() |
94
|
|
|
{ |
95
|
|
|
$table = Table::create(5); |
96
|
|
|
$robot = $table->placeRobot(); |
97
|
|
|
|
98
|
|
|
$robot->moveWestward(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
* @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException |
104
|
|
|
*/ |
105
|
|
|
public static function robotCannotFallOffTheNorthSide() |
106
|
|
|
{ |
107
|
|
|
// Smaller 3 x 3 table |
108
|
|
|
$table = Table::create(3); |
109
|
|
|
$robot = $table->placeRobot(); |
110
|
|
|
|
111
|
|
|
$robot |
112
|
|
|
->moveNorthward() |
113
|
|
|
->moveNorthward() |
114
|
|
|
->moveNorthward() |
115
|
|
|
; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.