Skip to content

Commit 633cb7a

Browse files
authored
Merge pull request #2 from codewars/show-outputs
Show outputs captured by test cases
2 parents 8796488 + de82f17 commit 633cb7a

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

src/CodewarsResultPrinter.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,37 @@ class CodewarsResultPrinter extends DefaultResultPrinter
2424
* @var TestSuite
2525
*/
2626
private $wrapperSuite = null;
27-
/**
28-
* @var bool
29-
*/
30-
protected $lastTestFailed = false;
31-
27+
// Temporarily store failure messages so that the outputs can be written before them.
28+
private $failures = array();
3229

3330
/**
3431
* An error occurred.
3532
*/
3633
public function addError(Test $test, \Throwable $t, float $time): void
3734
{
38-
$this->lastTestFailed = true;
39-
$this->write(sprintf("\n<ERROR::>%s\n", self::getMessage($t)));
40-
$this->write(sprintf("\n<LOG::-Details>%s\n", self::escapeLF(self::getDetails($t))));
35+
$this->failures[] = sprintf("\n<ERROR::>%s\n", self::getMessage($t));
36+
$this->failures[] = sprintf("\n<LOG::-Stacktrace>%s\n", self::escapeLF(self::getDetails($t)));
4137
}
4238

4339
/**
4440
* A warning occurred.
4541
*/
4642
public function addWarning(Test $test, Warning $e, float $time): void
4743
{
48-
$this->lastTestFailed = true;
49-
$this->write(sprintf("\n<ERROR::>%s\n", self::getMessage($e)));
50-
$this->write(sprintf("\n<LOG::-Details>%s\n", self::escapeLF(self::getDetails($e))));
44+
$this->failures[] = sprintf("\n<ERROR::>%s\n", self::getMessage($e));
45+
$this->failures[] = sprintf("\n<LOG::-Stacktrace>%s\n", self::escapeLF(self::getDetails($e)));
5146
}
5247

5348
/**
5449
* A failure occurred.
5550
*/
5651
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
5752
{
58-
$this->lastTestFailed = true;
5953
$msg = self::getMessage($e);
6054
if ($e instanceof ExpectationFailedException) {
6155
$msg .= self::getAssertionDetails($e);
6256
}
63-
$this->write(sprintf("\n<FAILED::>%s\n", self::escapeLF($msg)));
64-
$this->write(sprintf("\n<LOG::-Details>%s\n", self::escapeLF(self::getDetails($e))));
57+
$this->failures[] = sprintf("\n<FAILED::>%s\n", self::escapeLF($msg));
6558
}
6659

6760
/**
@@ -130,16 +123,24 @@ public function endTestSuite(TestSuite $suite): void
130123
public function startTest(Test $test): void
131124
{
132125
$this->write(sprintf("\n<IT::>%s\n", $test->getName()));
126+
$this->failures = array();
133127
}
134128

135129
/**
136130
* A test ended.
137131
*/
138132
public function endTest(Test $test, float $time): void
139133
{
140-
if (!$this->lastTestFailed) {
134+
if (\method_exists($test, 'hasOutput') && \method_exists($test, 'getActualOutput')) {
135+
if ($test->hasOutput()) {
136+
$this->write($test->getActualOutput());
137+
}
138+
}
139+
140+
if (empty($this->failures)) {
141141
$this->write("\n<PASSED::>Test Passed\n");
142-
$this->lastTestFailed = false;
142+
} else {
143+
$this->write(join("\n", $this->failures));
143144
}
144145
$this->write(sprintf("\n<COMPLETEDIN::>%.4f\n", $time * 1000));
145146
}

0 commit comments

Comments
 (0)