Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding tests from 'tfs' fixes inconsistencies in extracting elements …

…with one path selector. Fixes #555
  • Loading branch information...
commit 6c357ee555261054f379801d99c59dd18db3b51f 1 parent fbaabad
@markstory markstory authored
Showing with 64 additions and 8 deletions.
  1. +7 −7 cake/libs/set.php
  2. +57 −1 cake/tests/cases/libs/set.test.php
View
14 cake/libs/set.php
@@ -447,18 +447,18 @@ function extract($path, $data = null, $options = array()) {
$ctext = array($context['key']);
if (!is_numeric($key)) {
$ctext[] = $token;
- $token = array_shift($tokens);
- if (isset($items[$token])) {
- $ctext[] = $token;
- $item = $items[$token];
+ $tok = array_shift($tokens);
+ if (isset($items[$tok])) {
+ $ctext[] = $tok;
+ $item = $items[$tok];
$matches[] = array(
'trace' => array_merge($context['trace'], $ctext),
- 'key' => $token,
+ 'key' => $tok,
'item' => $item,
);
break;
- } else {
- array_unshift($tokens, $token);
+ } elseif ($tok !== null) {
+ array_unshift($tokens, $tok);
}
} else {
$key = $token;
View
58 cake/tests/cases/libs/set.test.php
@@ -923,7 +923,6 @@ function testExtractParentSelector() {
)
)
);
-
$expected = array(array('Category' => $tree[1]['Category']));
$r = Set::extract('/Category[name=Category 2]', $tree);
$this->assertEqual($r, $expected);
@@ -1081,6 +1080,14 @@ function testExtractWithNonZeroArrays() {
$r = Set::extract('/User/id', $nonZero);
$this->assertEqual($r, $expected);
+ $expected = array(
+ array('User' => array('id' => 1, 'name' => 'John')),
+ array('User' => array('id' => 2, 'name' => 'Bob')),
+ array('User' => array('id' => 3, 'name' => 'Tony')),
+ );
+ $result = Set::extract('/User', $nonZero);
+ $this->assertEqual($result, $expected);
+
$nonSequential = array(
'User' => array(
0 => array('id' => 1),
@@ -1142,6 +1149,55 @@ function testExtractWithArrays() {
$this->assertEqual(Set::extract('/Level1/Level2', $data), array(array('Level2' => array('test1', 'test2'))));
$this->assertEqual(Set::extract('/Level1/Level2bis', $data), array(array('Level2bis' => array('test3', 'test4'))));
}
+
+/**
+ * test extract() with elements that have non-array children.
+ *
+ * @return void
+ */
+ function testExtractWithNonArrayElements() {
+ $data = array(
+ 'node' => array(
+ array('foo'),
+ 'bar'
+ )
+ );
+ $result = Set::extract('/node', $data);
+ $expected = array(
+ array('node' => array('foo')),
+ 'bar'
+ );
+ $this->assertEqual($result, $expected);
+
+ $data = array(
+ 'node' => array(
+ 'foo' => array('bar'),
+ 'bar' => array('foo')
+ )
+ );
+ $result = Set::extract('/node', $data);
+ $expected = array(
+ array('foo' => array('bar')),
+ array('bar' => array('foo')),
+ );
+ $this->assertEqual($result, $expected);
+
+ $data = array(
+ 'node' => array(
+ 'foo' => array(
+ 'bar'
+ ),
+ 'bar' => 'foo'
+ )
+ );
+ $result = Set::extract('/node', $data);
+ $expected = array(
+ array('foo' => array('bar')),
+ 'foo'
+ );
+ $this->assertEqual($result, $expected);
+ }
+
/**
* testMatches method
*
Please sign in to comment.
Something went wrong with that request. Please try again.