Adding tests for single quoted attributes to assertTags(). Because of the __array_permute works ensuring quotes match is a non-trivial problem. Fixing single quoted attributes not being matchable with assertTags(). Fixes #539

This commit is contained in:
Mark Story 2010-04-04 21:43:29 -04:00
parent 45ccba5d94
commit d990c6ed44
2 changed files with 39 additions and 5 deletions

View file

@ -138,7 +138,7 @@ class CakeTestCaseTest extends CakeTestCase {
'My link',
'/a'
);
$this->assertTrue($this->Case->assertTags($input, $pattern));
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Attributes in wrong order. %s');
$input = "<a href=\"/test.html\"\t\n\tclass=\"active\"\tid=\"primary\">\t<span>My link</span></a>";
$pattern = array(
@ -148,7 +148,7 @@ class CakeTestCaseTest extends CakeTestCase {
'/span',
'/a'
);
$this->assertTrue($this->Case->assertTags($input, $pattern));
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Whitespace consumption %s');
$input = '<p class="info"><a href="/test.html" class="active"><strong onClick="alert(\'hey\');">My link</strong></a></p>';
$pattern = array(
@ -163,6 +163,37 @@ class CakeTestCaseTest extends CakeTestCase {
$this->assertTrue($this->Case->assertTags($input, $pattern));
}
/**
* test that assertTags knows how to handle correct quoting.
*
* @return void
*/
function testAssertTagsQuotes() {
$input = '<a href="/test.html" class="active">My link</a>';
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Double quoted attributes %s');
$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => '/test.html', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
$input = "<a href='/test.html' class='active'>My link</a>";
$pattern = array(
'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
'My link',
'/a'
);
$this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s');
}
/**
* testNumericValuesInExpectationForAssertTags
*

View file

@ -631,30 +631,33 @@ class CakeTestCase extends UnitTestCase {
}
$attrs = array();
$explanations = array();
$i = 1;
foreach ($attributes as $attr => $val) {
if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$attrs[] = $matches[1];
$explanations[] = sprintf('Regex "%s" matches', $matches[1]);
continue;
} else {
$quotes = '"';
$quotes = '["\']';
if (is_numeric($attr)) {
$attr = $val;
$val = '.+?';
$explanations[] = sprintf('Attribute "%s" present', $attr);
} elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
$quotes = '"?';
$quotes = '["\']?';
$val = $matches[1];
$explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
} else {
$explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
$val = preg_quote($val, '/');
}
$attrs[] = '[\s]+'.preg_quote($attr, '/').'='.$quotes.$val.$quotes;
$attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
}
$i++;
}
if ($attrs) {
$permutations = $this->__array_permute($attrs);
$permutationTokens = array();
foreach ($permutations as $permutation) {
$permutationTokens[] = implode('', $permutation);