Add tests for issue #104

This commit is contained in:
mark_story 2012-02-21 22:21:24 -05:00
parent ff5e72c9a7
commit 8cadac3ee5
2 changed files with 47 additions and 0 deletions

View file

@ -833,6 +833,45 @@ class Set2Test extends CakeTestCase {
$this->assertEquals($expected, $result);
}
/**
* Test that uneven keys are handled correctly.
*
* @return void
*/
public function testExtractUnevenKeys() {
$data = array(
'Level1' => array(
'Level2' => array('test1', 'test2'),
'Level2bis' => array('test3', 'test4')
)
);
$this->assertEquals(
array('test1', 'test2'),
Set2::extract($data, 'Level1.Level2')
);
$this->assertEquals(
array('test3', 'test4'),
Set2::extract($data, 'Level1.Level2bis')
);
$data = array(
'Level1' => array(
'Level2bis' => array(
array('test3', 'test4'),
array('test5', 'test6')
)
)
);
$expected = array(
array('test3', 'test4'),
array('test5', 'test6')
);
$this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis'));
$data['Level1']['Level2'] = array('test1', 'test2');
$this->assertEquals($expected, Set2::extract($data, 'Level1.Level2bis'));
}
/**
* testSort method
*

View file

@ -376,6 +376,14 @@ class Set2 {
/**
* Returns a formated series of values extracted from `$data`, using
* `$format` as the format and `$paths` as the values to extract.
*
* Usage:
*
* {{{
* $result = Set::format($users, array('{n}.User.id', '{n}.User.name'), '%s : %s');
* }}}
*
* The `$format` string can use any format options that `vsprintf()` and `sprintf()` do.
*
* @param array $data Source array from which to extract the data
* @param string $paths An array containing one or more Set2::extract()-style key paths