#65 IgnoreWhitespaceExpectation: ignore whitespace

open
nobody
5
2009-11-11
2009-11-11
daniel hahler
No

I'd like to have something like IgnoreWhitespaceExpectation (bad name probably!), which would compare values, while ignoring whitespace.

My suggestion is something in line of:
/**
* Test for a pattern, ignoring (amout of) whitespace.
* @package SimpleTest
* @subpackage UnitTester
*/
class IgnoreWhitespaceExpectation extends PatternExpectation {
/**
* Sets the value to compare against.
* @param string $pattern Pattern to search for.
* @param string $message Customised message on failure.
* @access public
*/
function __construct($pattern, $message = '%s') {
$pattern = '~^\s*'.preg_quote($pattern, '~').'\s*$~';
$pattern = preg_replace('~\s+~', '\\s+', $pattern);
parent::__construct($pattern, $message);
}
}

This allows to use something like:
$DB->expectAt(6, 'query', array(new IgnoreWhitespaceExpectation("INSERT INTO slugs (slug_title, slug_Article_ID) VALUES ('bar', '1')")));
where line breaks and leading/trailing whitespace is not important.

I think this should get improved to not replace "\s+" in strings, i.e.:
"SELECT 'foo bar'"
should become
"^\s*SELECT\s+'foo bar'\s*$"

What do you think?

Discussion