-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest.php
More file actions
110 lines (91 loc) · 3.29 KB
/
test.php
File metadata and controls
110 lines (91 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
use PHPUnit\Framework\Attributes\Group;
require_once __DIR__ . '/vendor/autoload.php';
class Test extends \PHPUnit\Framework\TestCase {
#[Group('rotation')]
#[Group('pawn')]
public function testNoMoves() {
$this->runFile('tests/001-no-moves.test');
}
#[Group('pawn')]
public function testSimpleError() {
$this->runFile('tests/011-simple-error.test');
}
#[Group('rotation')]
#[Group('pawn')]
public function testSimple() {
$this->runFile('tests/012-simple-move.test');
}
#[Group('rotation')]
public function testColorRotationError() {
$this->runFile('tests/013-color-rotation-error.test');
}
#[Group('rotation')]
#[Group('pawn')]
public function testColorRotationCorrect() {
$this->runFile('tests/014-color-rotation-correct.test');
}
#[Group('rotation')]
#[Group('pawn')]
public function testPawnMovesOneSquareVertically() {
$this->runFile('tests/021-pawn-moves-one-square-vertically.test');
}
#[Group('rotation')]
#[Group('pawn')]
public function testPawnCanMoveTwoSquaresOnFirstMove() {
$this->runFile('tests/022-pawn-can-move-two-squares-on-first-move.test');
}
#[Group('pawn')]
public function testPawnCanNotMoveDiagonally() {
$this->runFile('tests/023-pawn-can-not-move-diagonally.test');
}
#[Group('rotation')]
#[Group('pawn')]
public function testPawnCapturesDiagonally() {
$this->runFile('tests/024-pawn-captures-diagonally.test');
}
#[Group('pawn')]
public function testPawnCanNotCaptureVertically() {
$this->runFile('tests/025-pawn-can-not-capture-vertically.test');
}
#[Group('pawn')]
public function testPawnCanNotMoveFartherOneSquare() {
$this->runFile('tests/026-pawn-can-not-move-farther-one-square.test');
}
#[Group('pawn')]
public function testPawnCanNotMoveAcrossFigure() {
$this->runFile('tests/027-pawn-can-not-move-across-figure.test');
}
/**
* Test file structure
*
* line 1: moves as arguments to chess.php
* line 2: 'correct' if all moves are correct, 'error' if there are incorrect moves
* Other lines: output of chess.php (final chess board) if all moves are correct
*
* @param string $file
*/
private function runFile($file) {
$lines = file($file);
$moves = trim($lines[0]);
$movesDesc = $moves ?: '(no moves)';
unset($lines[0]);
$isCorrect = trim($lines[1]) != 'error';
unset($lines[1]);
$redColor = '';
$noColor = '';
if (function_exists('posix_isatty') && posix_isatty(STDOUT)) {
$redColor = "\033[31m";
$noColor = "\033[m";
}
$out = [];
exec('php chess.php ' . $moves, $out, $err);
if ($isCorrect) {
$this->assertEquals(0, $err, $redColor . "Moves are correct, but chess.php thinks there is an error:\n" . $movesDesc . $noColor . "\n");
// does not work properly on windows
// $this->assertEquals(join("", $lines), join("\n", $out) . "\n");
} else {
$this->assertNotEquals(0, $err, $redColor . "Moves are invalid, but chess.php does not detect that:\n" . $movesDesc . $noColor . "\n");
}
}
}