-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests, fixed lack of 'Qu' tile
- Loading branch information
Showing
7 changed files
with
231 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/* | ||
vendor/* | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
header('Content-Type: text/plain; charset=utf-8'); | ||
|
||
// if you just use composer: | ||
//include(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
// if you don't have an autoloader for some reason | ||
include(__DIR__ . '/../src/BoggleTile.php'); | ||
include(__DIR__ . '/../src/BoggleException.php'); | ||
include(__DIR__ . '/../src/BoggleSolver.php'); | ||
|
||
// testing a board with a Qu tile | ||
$boggleBoard = "IZLEERCVADJQuITDI"; | ||
|
||
$boggle = new \BoggleSolver\BoggleSolver(); | ||
|
||
try { | ||
$boggle->loadBoard($boggleBoard); | ||
} catch (\BoggleSolver\BoggleException $e) { | ||
die("exiting on error: " . $e->getMessage()); | ||
} | ||
|
||
$words = $boggle->findWords(); | ||
|
||
|
||
echo "Boggle Board:\n\n"; | ||
echo $boggle->displayBoard(); | ||
|
||
echo "\n"; | ||
|
||
foreach ($words as $word) { | ||
echo $word . "\n"; | ||
} | ||
|
||
echo "\n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
PHPUNIT=vendor/phpunit/phpunit/phpunit | ||
|
||
tests: tests/* src/* ; $(PHPUNIT) tests | ||
|
||
coverage: tests/* src/* ; $(PHPUNIT) --coverage-html coverage tests | ||
|
||
clean: ; rm -rf coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace BoggleSolver; | ||
|
||
class BoggleSolverTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public $lessWords = array( | ||
"ABCD", "ABC", "ABBA", "ABABA", "ABQU", "XYZ", "ABBBBBBBBBBBBBBBB", "AB" | ||
); | ||
|
||
public function testLoadDictBuildsDictionary() | ||
{ | ||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('getWords')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->once()) | ||
->method('getWords') | ||
->will($this->returnValue($this->lessWords)); | ||
|
||
$solverMock->boardLookup = array('A' => 1); | ||
$solverMock->size = 3; | ||
|
||
$solverMock->loadDict(); | ||
|
||
$expectedDict = array( | ||
'A' => array( | ||
'B' => array( | ||
'C' => array( | ||
"ABC", | ||
'D' => array( | ||
"ABCD", | ||
), | ||
), | ||
'B' => array( | ||
'A' => array( | ||
"ABBA", | ||
), | ||
), | ||
'A' => array( | ||
'B' => array( | ||
'A' => array( | ||
"ABABA", | ||
), | ||
), | ||
), | ||
'Qu' => array( | ||
"ABQU" | ||
), | ||
), | ||
), | ||
); | ||
|
||
$this->assertEquals($expectedDict, $solverMock->dict); | ||
} | ||
|
||
public function testLoadBoardLoadsBoard() | ||
{ | ||
$exampleBoard = "A B C\nX Y Z\nQu R S"; | ||
|
||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('loadDict')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->once()) | ||
->method('loadDict'); | ||
|
||
$solverMock->loadBoard($exampleBoard); | ||
|
||
$expectedLookup = array( | ||
'A', 'B', 'C', 'X', 'Y', 'Z', 'Q', 'R', 'S' | ||
); | ||
|
||
$this->assertEquals(3, $solverMock->size); | ||
|
||
$this->assertEquals($expectedLookup, array_keys($solverMock->boardLookup)); | ||
} | ||
|
||
public function testLoadBoardLoadsBoardOfFourByFour() | ||
{ | ||
$exampleBoard = "A B C D\nX Y Z A\nQu R S T\nO K A Y"; | ||
|
||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('loadDict')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->once()) | ||
->method('loadDict'); | ||
|
||
$solverMock->loadBoard($exampleBoard); | ||
|
||
$this->assertEquals(4, $solverMock->size); | ||
|
||
} | ||
|
||
public function testLoadBoardThrowsExceptionOnUnknownBoardSize() | ||
{ | ||
$exampleBoard = "A"; | ||
|
||
$this->setExpectedException('\BoggleSolver\BoggleException'); | ||
|
||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('loadDict')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->never()) | ||
->method('loadDict'); | ||
|
||
$solverMock->loadBoard($exampleBoard); | ||
} | ||
|
||
public function testDisplayBoardReturnsBoard() | ||
{ | ||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('loadDict')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->once()) | ||
->method('loadDict'); | ||
|
||
$solverMock->loadBoard("ABCDEFGHI"); | ||
|
||
$result = $solverMock->displayBoard(); | ||
|
||
$this->assertEquals("ABC\nDEF\nGHI\n", $result); | ||
} | ||
|
||
public function testFindWords() | ||
{ | ||
$solverMock = $this->getMockBuilder('\BoggleSolver\BoggleSolver') | ||
->setMethods(array('getWords')) | ||
->getMock(); | ||
|
||
$solverMock->expects($this->once()) | ||
->method('getWords') | ||
->will($this->returnValue($this->lessWords)); | ||
|
||
$solverMock->boardLookup = array('A' => 1); | ||
$solverMock->size = 3; | ||
|
||
$solverMock->loadBoard("ABCABDABA"); | ||
|
||
$result = $solverMock->findWords(); | ||
|
||
$expected = array( | ||
"ABC", "ABCD", "ABBA", "ABABA", | ||
); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace BoggleSolver; | ||
|
||
class BoggleTileTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructor() | ||
{ | ||
$tile = new BoggleTile('A', 0); | ||
|
||
$this->assertEquals('A', $tile->letter); | ||
$this->assertEquals(0, $tile->id); | ||
} | ||
} |