Permalink
Browse files

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
  • Loading branch information...
markstory committed Apr 5, 2010
1 parent 45ccba5 commit d990c6ed44afa8e670f9029dde17b8117db1f501
Showing with 39 additions and 5 deletions.
  1. +33 −2 cake/tests/cases/libs/cake_test_case.test.php
  2. +6 −3 cake/tests/lib/cake_test_case.php
@@ -138,7 +138,7 @@ function testAssertGoodTags() {
'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 @@ function testAssertGoodTags() {
'/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 @@ function testAssertGoodTags() {
$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
*
@@ -631,30 +631,33 @@ function assertTags($string, $expected, $fullDebug = false) {
}
$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);

0 comments on commit d990c6e

Please sign in to comment.