Skip to content

Commit

Permalink
Adding tests from 'tfs' fixes inconsistencies in extracting elements …
Browse files Browse the repository at this point in the history
…with one path selector. Fixes #555
  • Loading branch information
markstory committed Apr 10, 2010
1 parent fbaabad commit 6c357ee
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
14 changes: 7 additions & 7 deletions cake/libs/set.php
Expand Up @@ -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;
Expand Down
58 changes: 57 additions & 1 deletion cake/tests/cases/libs/set.test.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 6c357ee

Please sign in to comment.