From 02457de1fe8ee1d850f9d55c63f5c58bb91de47b Mon Sep 17 00:00:00 2001 From: Gnieark Date: Mon, 11 Jul 2016 22:58:13 +0200 Subject: [PATCH] tests unitaires --- src/arenas/tron/Coords.php | 19 +++++++++++++ src/arenas/tron/Direction.php | 39 +++++++++++++++++++++----- src/arenas/tron/test/CoordsTest.php | 27 ++++++++++++++++++ src/arenas/tron/test/DirectionTest.php | 9 +++++- 4 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 src/arenas/tron/Coords.php create mode 100644 src/arenas/tron/test/CoordsTest.php diff --git a/src/arenas/tron/Coords.php b/src/arenas/tron/Coords.php new file mode 100644 index 0000000..34a16a7 --- /dev/null +++ b/src/arenas/tron/Coords.php @@ -0,0 +1,19 @@ +x = $x; + $this->y = $y; + } + + public function __toString(){ + return $this->x.",".$this->y; + } + public function addDirection(Direction $dir){ + return new Coords( + $this->x + $dir->deltaX, + $this->y + $dir->deltaY + ); + } +} \ No newline at end of file diff --git a/src/arenas/tron/Direction.php b/src/arenas/tron/Direction.php index 8f2a360..4f1396f 100644 --- a/src/arenas/tron/Direction.php +++ b/src/arenas/tron/Direction.php @@ -10,10 +10,36 @@ class Direction private static $right = 3; private $value; + + public $deltaX; + public $deltaY; public function __construct(){ $this->value = 0; } + + private function setValue($value){ + $this->value = $value; + switch ($value){ + case Direction::$bottom: + $this->deltaY = -1; + $this->deltaX = 0; + break; + case Direction::$top: + $this->deltaY = 1; + $this->deltaX = 0; + break; + case Direction::$left: + $this->deltaY = 0; + $this->deltaX = -1; + break; + case Direction::$right: + $this->deltaY = 0; + $this->deltaX = 1; + break; + } + } + public function __toString(){ switch ($this->value){ case Direction::$top: @@ -30,25 +56,24 @@ class Direction break; } } + public static function make($str){ $dir = new Direction(); switch((string)$str){ case "x+": - $dir->value = Direction::$right; + $dir->setValue(Direction::$right); break; case "x-": - $dir->value = Direction::$left; + $dir->setValue(Direction::$left); break; case "y+": - $dir->value = Direction::$top; + $dir->setValue(Direction::$top); break; case "y-": - $dir->value = Direction::$bottom; + $dir->setValue(Direction::$bottom); break; default: throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received."); - break; - } return $dir; } @@ -61,7 +86,7 @@ class Direction ); $opposite = new Direction(); - $opposite->value = $opposites[$this->value]; + $opposite->setValue($opposites[$this->value]); return $opposite; } } \ No newline at end of file diff --git a/src/arenas/tron/test/CoordsTest.php b/src/arenas/tron/test/CoordsTest.php new file mode 100644 index 0000000..e70966f --- /dev/null +++ b/src/arenas/tron/test/CoordsTest.php @@ -0,0 +1,27 @@ +addDirection(Direction::make('x+')) + ->addDirection(Direction::make('y-')) + ->addDirection(Direction::make('x-')) + ->addDirection(Direction::make('y+')); + + + + $this->assertTrue($endCoord == $startCoord); + } + public function testIsDifferent(){ + $startCoord = new Coords(15,3); + $endCoord = $startCoord->addDirection(Direction::make('x+')); + fwrite(STDERR, $startCoord ."\n"); + fwrite(STDERR, $endCoord ."\n"); + $this->assertFalse($endCoord == $startCoord); + } +} \ No newline at end of file diff --git a/src/arenas/tron/test/DirectionTest.php b/src/arenas/tron/test/DirectionTest.php index b381382..89359b6 100644 --- a/src/arenas/tron/test/DirectionTest.php +++ b/src/arenas/tron/test/DirectionTest.php @@ -33,6 +33,14 @@ class Directiontest extends TestCase { $this->assertInstanceOf(Direction::class,Direction::make($validString)); } + /** + * @dataProvider validStrings + */ + public function testDeltaXY($validString){ + $dir = Direction::make($validString); + $this->assertTrue($dir->deltaX != 0 || $dir->deltaY != 0); + } + /** * @dataProvider validStrings */ @@ -59,6 +67,5 @@ class Directiontest extends TestCase { $opop = $dir->opposite()->opposite(); $this->assertTrue($dir == $opop); } - } \ No newline at end of file