Skip to content

Commit 36c8363

Browse files
committed
Fix canonical string
1 parent 09cf2d2 commit 36c8363

7 files changed

Lines changed: 69 additions & 20 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"autoload-dev": {
2525
"psr-4": {
26-
"Syntatis\\Tests\\": "tests/"
26+
"Syntatis\\Tests\\": ["tests/", "tests/phpunit/"]
2727
}
2828
},
2929
"require": {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Syntatis\Tests\Commands;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
use function preg_replace;
10+
use function str_replace;
11+
use function trim;
12+
13+
abstract class CommandTestCase extends TestCase
14+
{
15+
protected static function normalizeOutput(string $value): string
16+
{
17+
$output = self::canonicalize($value);
18+
$output = str_replace("\n", ' ', $output);
19+
$output = preg_replace('/\s+/', ' ', $output);
20+
21+
if ($output === null) {
22+
return $value;
23+
}
24+
25+
return trim($output);
26+
}
27+
28+
protected static function canonicalize(string $str): string
29+
{
30+
// normalize EOL style
31+
$str = str_replace("\r\n", "\n", $str);
32+
33+
// trim newlines at end
34+
$str = rtrim($str, "\n");
35+
36+
// remove trailing whitespace on all lines
37+
$lines = explode("\n", $str);
38+
$lines = array_map(static function ($line): string {
39+
return rtrim($line, " \t");
40+
}, $lines);
41+
42+
return implode("\n", $lines);
43+
}
44+
}

tests/phpunit/Commands/EqualCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Syntatis\Tests\Commands;
66

7+
use Syntatis\Tests\Commands\CommandTestCase;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910
use Syntatis\Version\CLI\Commander;
1011

1112
use function sprintf;
1213

13-
class EqualCommandTest extends TestCase
14+
class EqualCommandTest extends CommandTestCase
1415
{
1516
private Commander $commander;
1617
private CommandTester $tester;
@@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec
3031

3132
self::assertStringContainsString(
3233
$expect,
33-
$this->tester->getDisplay(),
34+
self::normalizeOutput($this->tester->getDisplay()),
3435
);
3536
}
3637

tests/phpunit/Commands/GreaterThanCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Syntatis\Tests\Commands;
66

7+
use Syntatis\Tests\Commands\CommandTestCase;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910
use Syntatis\Version\CLI\Commander;
1011

1112
use function sprintf;
1213

13-
class GreaterThanCommandTest extends TestCase
14+
class GreaterThanCommandTest extends CommandTestCase
1415
{
1516
private Commander $commander;
1617
private CommandTester $tester;
@@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec
3031

3132
self::assertStringContainsString(
3233
$expect,
33-
$this->tester->getDisplay(),
34+
self::normalizeOutput($this->tester->getDisplay()),
3435
);
3536
}
3637

tests/phpunit/Commands/IncrementCommandTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Syntatis\Tests\Commands;
66

7+
use Syntatis\Tests\Commands\CommandTestCase;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910
use Syntatis\Version\CLI\Commander;
1011

1112
use function sprintf;
1213

13-
class IncrementCommandTest extends TestCase
14+
class IncrementCommandTest extends CommandTestCase
1415
{
1516
private Commander $commander;
1617
private CommandTester $tester;
@@ -30,7 +31,7 @@ public function testInvalidVersionArgument(string $version): void
3031

3132
self::assertStringContainsString(
3233
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
33-
$this->tester->getDisplay(),
34+
self::normalizeOutput($this->tester->getDisplay()),
3435
);
3536
}
3637

@@ -40,7 +41,7 @@ public function testIncrementPatch(): void
4041

4142
self::assertStringContainsString(
4243
'1.0.1',
43-
$this->tester->getDisplay(),
44+
self::normalizeOutput($this->tester->getDisplay()),
4445
);
4546
}
4647

@@ -50,7 +51,7 @@ public function testIncrementMinor(): void
5051

5152
self::assertStringContainsString(
5253
'1.1.0',
53-
$this->tester->getDisplay(),
54+
self::normalizeOutput($this->tester->getDisplay()),
5455
);
5556
}
5657

@@ -60,7 +61,7 @@ public function testIncrementMajor(): void
6061

6162
self::assertStringContainsString(
6263
'2.0.0',
63-
$this->tester->getDisplay(),
64+
self::normalizeOutput($this->tester->getDisplay()),
6465
);
6566
}
6667

@@ -70,7 +71,7 @@ public function testIncrementWithBuildMetadata(): void
7071

7172
self::assertStringContainsString(
7273
'1.0.1+123',
73-
$this->tester->getDisplay(),
74+
self::normalizeOutput($this->tester->getDisplay()),
7475
);
7576
}
7677

@@ -80,7 +81,7 @@ public function testIncrementWithPreRelease(): void
8081

8182
self::assertStringContainsString(
8283
'1.0.1-beta',
83-
$this->tester->getDisplay(),
84+
self::normalizeOutput($this->tester->getDisplay()),
8485
);
8586
}
8687

@@ -93,7 +94,7 @@ public function testIncrementPrerelease(string $version, string $expect): void
9394

9495
self::assertStringContainsString(
9596
$expect,
96-
$this->tester->getDisplay(),
97+
self::normalizeOutput($this->tester->getDisplay()),
9798
);
9899
}
99100

@@ -109,7 +110,7 @@ public function testIncrementPrereleaseWithPreTag(): void
109110

110111
self::assertStringContainsString(
111112
"[ERROR] Specifying a prerelease tag when incrementing the prerelease part is not allowed.",
112-
$this->tester->getDisplay(),
113+
self::normalizeOutput($this->tester->getDisplay()),
113114
);
114115
}
115116

@@ -119,7 +120,7 @@ public function testIncrementPrereleaseWithoutReleaseTag(): void
119120

120121
self::assertStringContainsString(
121122
"[ERROR] Unable to increment prerelease on a version without a prerelease tag.",
122-
$this->tester->getDisplay(),
123+
self::normalizeOutput($this->tester->getDisplay()),
123124
);
124125
}
125126

tests/phpunit/Commands/LessThanCommandTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Syntatis\Tests\Commands;
66

7+
use Syntatis\Tests\Commands\CommandTestCase;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910
use Syntatis\Version\CLI\Commander;
1011

1112
use function sprintf;
1213

13-
class LessThanCommandTest extends TestCase
14+
class LessThanCommandTest extends CommandTestCase
1415
{
1516
private Commander $commander;
1617
private CommandTester $tester;
@@ -30,7 +31,7 @@ public function testComparison(string $versionA, string $versionB, string $expec
3031

3132
self::assertStringContainsString(
3233
$expect,
33-
$this->tester->getDisplay(),
34+
self::normalizeOutput($this->tester->getDisplay()),
3435
);
3536
}
3637

tests/phpunit/Commands/ValidateCommandTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace Syntatis\Tests\Commands;
66

7+
use Syntatis\Tests\Commands\CommandTestCase;
78
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Console\Tester\CommandTester;
910
use Syntatis\Version\CLI\Commander;
1011

1112
use function sprintf;
1213

13-
class ValidateCommandTest extends TestCase
14+
class ValidateCommandTest extends CommandTestCase
1415
{
1516
private Commander $commander;
1617
private CommandTester $tester;
@@ -30,7 +31,7 @@ public function testInvalidVersionArgument(string $version): void
3031

3132
self::assertStringContainsString(
3233
sprintf("[ERROR] Version string '%s' is not valid and cannot be parsed", $version),
33-
$this->tester->getDisplay(),
34+
self::normalizeOutput($this->tester->getDisplay()),
3435
);
3536
}
3637

@@ -41,7 +42,7 @@ public function testValidVersionArgument(string $version): void
4142

4243
self::assertStringContainsString(
4344
sprintf("[OK] Version string '%s' is valid and can be parsed", $version),
44-
$this->tester->getDisplay(),
45+
self::normalizeOutput($this->tester->getDisplay()),
4546
);
4647
}
4748

0 commit comments

Comments
 (0)