Permalink
Browse files

Closes #514: Better readable diff for assertStringMatches* calls

  • Loading branch information...
1 parent 107f36c commit 963ece1073d4133ade3a33c5b132cf1939f55f52 @edorian edorian committed Apr 14, 2012
Showing with 42 additions and 25 deletions.
  1. +1 −0 ChangeLog.markdown
  2. +33 −16 PHPUnit/Framework/Constraint/StringMatches.php
  3. +8 −9 Tests/Framework/AssertTest.php
View
@@ -13,6 +13,7 @@ PHPUnit 3.7.0
* Implemented #333: Improved reporting when there are unused CLI arguments to avoid misconceptions.
* Implemented #377: Show messages and stracktraces in JSON output for skipped and incomplete tests.
* Implemented #504: Expanded the `@requires` annotation to allow for checking the existence of functions and extensions using multiple `@requires function name` statements.
+* Implemented #514: Failed `assertStringMatchesFormat()` calls now produce a better readable diff by only marking lines as different that don't match the format specifiers.
* The `@requires` annotation can now be used on the class DocBlock. Required versions can be overridden in the methods annotation, required functions and extensions will be merged.
* Implemented #86 #508: `@expectedExceptionCode` and `@expectedExceptionMessage` can now use constants like `Classname::CONST` as their parameters. They will get evaluated if the class constant exists and used for comparison so test authors can avoid duplication.
* Implemented #512: Test listeners now trigger one autoload call instead of being silently ignored when the class was not loaded.
@@ -69,8 +69,37 @@ class PHPUnit_Framework_Constraint_StringMatches extends PHPUnit_Framework_Const
*/
public function __construct($string)
{
- $this->pattern = preg_quote(preg_replace('/\r\n/', "\n", $string), '/');
- $this->pattern = str_replace(
+ $this->pattern = $this->createPatternFromFormat(
+ preg_replace('/\r\n/', "\n", $string)
+ );
+ $this->string = $string;
+ }
+
+ protected function failureDescription($other)
+ {
+ return "format description matches text";
+ }
+
+ protected function additionalFailureDescription($other)
+ {
+ $from = preg_split('(\r\n|\r|\n)', $this->string);
+ $to = preg_split('(\r\n|\r|\n)', $other);
+ foreach($from as $index => $line) {
+ if(isset($to[$index]) && $line !== $to[$index]) {
+ $line = $this->createPatternFromFormat($line);
+ if(preg_match($line, $to[$index]) > 0) {
+ $from[$index] = $to[$index];
+ }
+ }
+ }
+ $this->string = join("\n", $from);
+ $other = join("\n", $to);
+ return PHPUnit_Util_Diff::diff($this->string, $other);
+ }
+
+ protected function createPatternFromFormat($string)
+ {
+ $string = str_replace(
array(
'%e',
'%s',
@@ -97,21 +126,9 @@ public function __construct($string)
'[+-]?\.?\d+\.?\d*(?:[Ee][+-]?\d+)?',
'.'
),
- $this->pattern
+ preg_quote($string, '/')
);
-
- $this->pattern = '/^' . $this->pattern . '$/s';
- $this->string = $string;
- }
-
- protected function failureDescription($other)
- {
- return "format description matches text";
- }
-
- protected function additionalFailureDescription($other)
- {
- return PHPUnit_Util_Diff::diff($this->string, $other);
+ return '/^' . $string . '$/s';
}
}
@@ -3762,16 +3762,15 @@ public function testAssertStringEndsNotWith()
public function testAssertStringMatchesFormat()
{
$this->assertStringMatchesFormat('*%s*', '***');
+ }
- try {
- $this->assertStringMatchesFormat('*%s*', '**');
- }
-
- catch (PHPUnit_Framework_AssertionFailedError $e) {
- return;
- }
-
- $this->fail();
+ /**
+ * @covers PHPUnit_Framework_Assert::assertStringMatchesFormat
+ * @expectedException PHPUnit_Framework_AssertionFailedError
+ */
+ public function testAssertStringMatchesFormatFailure()
+ {
+ $this->assertStringMatchesFormat('*%s*', '**');
}
/**

0 comments on commit 963ece1

Please sign in to comment.