Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding tests for {n} and {s}

  • Loading branch information...
commit e87901ac6dca4339dddc98e3077226dd687cb218 1 parent 37181e9
@markstory markstory authored
Showing with 70 additions and 0 deletions.
  1. +70 −0 lib/Cake/Test/Case/Utility/Set2Test.php
View
70 lib/Cake/Test/Case/Utility/Set2Test.php
@@ -599,6 +599,11 @@ public function testExtractBasic() {
$this->assertEquals(array('Second Article'), $result);
}
+/**
+ * Test the {n} selector
+ *
+ * @return void
+ */
public function testExtractNumericKey() {
$data = self::articleData();
$result = Set2::extract($data, '{n}.Article.title');
@@ -615,7 +620,69 @@ public function testExtractNumericKey() {
);
$this->assertEquals($expected, $result);
}
+
+/**
+ * Test the {n} selector with inconsistent arrays
+ *
+ * @return void
+ */
+ public function testExtractNumbericMixedKeys() {
+ $data = array(
+ 'User' => array(
+ 0 => array(
+ 'id' => 4,
+ 'name' => 'Neo'
+ ),
+ 1 => array(
+ 'id' => 5,
+ 'name' => 'Morpheus'
+ ),
+ 'stringKey' => array(
+ 'name' => 'Fail'
+ )
+ )
+ );
+ $result = Set2::extract($data, 'User.{n}.name');
+ $expected = array('Neo', 'Morpheus');
+ $this->assertEquals($expected, $result);
+ }
+
+/**
+ * Test the {n} selector with non-zero based arrays
+ *
+ * @return void
+ */
+ public function testExtractNumericNonZero() {
+ $data = array(
+ 1 => array(
+ 'User' => array(
+ 'id' => 1,
+ 'name' => 'John',
+ )
+ ),
+ 2 => array(
+ 'User' => array(
+ 'id' => 2,
+ 'name' => 'Bob',
+ )
+ ),
+ 3 => array(
+ 'User' => array(
+ 'id' => 3,
+ 'name' => 'Tony',
+ )
+ )
+ );
+ $result = Set2::extract($data, '{n}.User.name');
+ $expected = array('John', 'Bob', 'Tony');
+ $this->assertEquals($expected, $result);
+ }
+/**
+ * Test the {s} selector.
+ *
+ * @return void
+ */
public function testExtractStringKey() {
$data = self::articleData();
$result = Set2::extract($data, '{n}.{s}.user');
@@ -625,6 +692,9 @@ public function testExtractStringKey() {
'mariano'
);
$this->assertEquals($expected, $result);
+
+ $result = Set2::extract($data, '{n}.{s}.Nesting.test.1');
+ $this->assertEquals(array('foo'), $result);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.