Skip to content

Commit

Permalink
added test for Assert::match()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 17, 2013
1 parent b766034 commit 33c211f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Tester/Framework/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,14 @@ public static function isEqual($expected, $actual, $level = 0)
* %ns% PHP namespace
* @param string
* @param string
* @return bool
* @return void
*/
public static function match($pattern, $actual)
{
if (!self::isMatching($pattern, $actual)) {
if (!is_string($pattern)) {
throw new \Exception('Pattern must be a string.');

} elseif (!is_scalar($actual) || !self::isMatching($pattern, $actual)) {
self::fail(Dumper::toLine($actual) . ' should match ' . Dumper::toLine($pattern), $pattern, $actual);
}
}
Expand All @@ -326,9 +329,13 @@ public static function match($pattern, $actual)
*/
public static function isMatching($pattern, $actual)
{
$pattern = rtrim(preg_replace("#[\t ]+\n#", "\n", str_replace("\r\n", "\n", $pattern)));
$actual = rtrim(preg_replace("#[\t ]+\n#", "\n", str_replace("\r\n", "\n", $actual)));
if (!is_string($pattern) && !is_scalar($actual)) {
throw new \Exception('Value and pattern must be strings.');
}

$pattern = str_replace("\r\n", "\n", $pattern);
$actual = str_replace("\r\n", "\n", $actual);
$pattern = preg_replace("#[\t ]*\n#", "%s?%\n", rtrim($pattern));
$re = strtr($pattern, array(
'%a%' => '[^\r\n]+', // one or more of anything except the end of line characters
'%a?%'=> '[^\r\n]*', // zero or more of anything except the end of line characters
Expand All @@ -350,7 +357,7 @@ public static function isMatching($pattern, $actual)
'.' => '\.', '\\' => '\\\\', '+' => '\+', '*' => '\*', '?' => '\?', '[' => '\[', '^' => '\^', // preg quote
']' => '\]', '$' => '\$', '(' => '\(', ')' => '\)', '{' => '\{', '}' => '\}', '=' => '\=', '!' => '\!',
'>' => '\>', '<' => '\<', '|' => '\|', ':' => '\:', '-' => '\-', "\x00" => '\000', '#' => '\#',
));
)) . '\\s*';

$old = ini_set('pcre.backtrack_limit', '10000000');
$res = preg_match("#^$re$#sU", $actual);
Expand Down
69 changes: 69 additions & 0 deletions tests/Assert.match.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use Tester\Assert;

require __DIR__ . '/bootstrap.php';


$matches = array(
array('1', '1'),
array('1', 1),
array('a', "a \t\r\n\t \n"),
array("a \t\r\n", 'a'),
array('%a%', 'a b'),
array('%a?%', 'a b'),
array('%a?%', ''),
array('%A%', "a\nb"),
array('%A?%', "a\nb"),
array('%A?%', ''),
array('%s%', " \t"),
array('%s?%', " \t"),
array('%s?%', ''),
array('a%c%c', 'abc'),
array('a%c%c', 'a c'),
array('%d%', '123'),
array('%d?%', '123'),
array('%d?%', ''),
array('%i%', '-123'),
array('%i%', '+123'),
array('%f%', '-123'),
array('%f%', '+123.5'),
array('%f%', '-1e5'),
array('%h%', 'aBcDeF'),
array('%ds%%ds%', '\\/'),
array('.\\+*?[^]$(){}=!<>|:-#', '.\\+*?[^]$(){}=!<>|:-#'),
);

$notMatches = array(
array('a', ' a ', "' a ' should match 'a'"),
array('%a%', "a\nb"),
array('%a%', ''),
array('%A%', ''),
array('a%s%b', "a\nb"),
array('%s?%', 'a'),
array('a%c%c', 'abbc'),
array('a%c%c', 'ac'),
array('a%c%c', "a\nc"),
array('%d%', ''),
array('%i%', '-123.5'),
array('%i%', ''),
array('%f%', ''),
array('%h%', 'gh'),
array('%h%', ''),
);

foreach ($matches as $case) {
list($expected, $actual) = $case;
Assert::match($expected, $actual);
}

foreach ($notMatches as $case) {
@list($expected, $actual, $message) = $case;
Assert::exception(function() use ($expected, $actual) {
Assert::match($expected, $actual);
}, 'Tester\AssertException', $message);
}

Assert::exception(function(){
Assert::match(NULL, '');
}, 'Exception', 'Pattern must be a string.');

0 comments on commit 33c211f

Please sign in to comment.