tests unitaires

pull/96/head
Gnieark 8 years ago
parent b2c96795f9
commit 02457de1fe

@ -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
);
}
}

@ -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;
}
}

@ -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);
}
}

@ -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);
}
}
Loading…
Cancel
Save