tests unitaires

This commit is contained in:
Gnieark 2016-07-11 22:58:13 +02:00
parent b2c96795f9
commit 02457de1fe
4 changed files with 86 additions and 8 deletions

View File

@ -0,0 +1,19 @@
<?php
class Coords{
public $x;
public $y;
public function __construct(int $x = 0, int $y = 0) {
$this->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
);
}
}

View File

@ -10,10 +10,36 @@ class Direction
private static $right = 3; private static $right = 3;
private $value; private $value;
public $deltaX;
public $deltaY;
public function __construct(){ public function __construct(){
$this->value = 0; $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(){ public function __toString(){
switch ($this->value){ switch ($this->value){
case Direction::$top: case Direction::$top:
@ -30,25 +56,24 @@ class Direction
break; break;
} }
} }
public static function make($str){ public static function make($str){
$dir = new Direction(); $dir = new Direction();
switch((string)$str){ switch((string)$str){
case "x+": case "x+":
$dir->value = Direction::$right; $dir->setValue(Direction::$right);
break; break;
case "x-": case "x-":
$dir->value = Direction::$left; $dir->setValue(Direction::$left);
break; break;
case "y+": case "y+":
$dir->value = Direction::$top; $dir->setValue(Direction::$top);
break; break;
case "y-": case "y-":
$dir->value = Direction::$bottom; $dir->setValue(Direction::$bottom);
break; break;
default: default:
throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received."); throw new InvalidDirectionException("expected 'x+', 'x-', 'y+' or 'y-'". (string)$str."received.");
break;
} }
return $dir; return $dir;
} }
@ -61,7 +86,7 @@ class Direction
); );
$opposite = new Direction(); $opposite = new Direction();
$opposite->value = $opposites[$this->value]; $opposite->setValue($opposites[$this->value]);
return $opposite; return $opposite;
} }
} }

View File

@ -0,0 +1,27 @@
<?php
use PHPUnit\Framework\TestCase;
require_once '../Coords.php';
require_once '../Direction.php';
class CoordsTest extends TestCase {
public function testCirculaire(){
$startCoord = new Coords(15,3);
$endCoord = $startCoord->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);
}
}

View File

@ -33,6 +33,14 @@ class Directiontest extends TestCase {
$this->assertInstanceOf(Direction::class,Direction::make($validString)); $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 * @dataProvider validStrings
*/ */
@ -59,6 +67,5 @@ class Directiontest extends TestCase {
$opop = $dir->opposite()->opposite(); $opop = $dir->opposite()->opposite();
$this->assertTrue($dir == $opop); $this->assertTrue($dir == $opop);
} }
} }