Add unit tests for callbacks and PlSubIterator
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 16 Mar 2010 00:18:56 +0000 (01:18 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 16 Mar 2010 01:51:56 +0000 (02:51 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
ut/callbacktest.php [new file with mode: 0644]
ut/subiteratortest.php [new file with mode: 0644]

diff --git a/ut/callbacktest.php b/ut/callbacktest.php
new file mode 100644 (file)
index 0000000..585d309
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once dirname(__FILE__) . '/../include/test.inc.php';
+
+class CallbackTest extends PlTestCase
+{
+    public function testArrayCallback()
+    {
+        $arr = array('x' => 'v');
+        $cb = PlIteratorUtils::arrayValueCallback('x');
+        $this->assertSame(call_user_func($cb, $arr), 'v');
+    }
+
+    public function testObjectCallback()
+    {
+        $obj = new MyObj(42);
+        $cb = PlIteratorUtils::objectPropertyCallback('prop');
+        $this->assertSame(call_user_func($cb, $obj), 42);
+    }
+}
+
+class MyObj
+{
+    public $prop = 1;
+    public function __construct($prop)
+    {
+        $this->prop = $prop;
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>
diff --git a/ut/subiteratortest.php b/ut/subiteratortest.php
new file mode 100644 (file)
index 0000000..0ed267e
--- /dev/null
@@ -0,0 +1,85 @@
+<?php
+/***************************************************************************
+ *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+require_once dirname(__FILE__) . '/../include/test.inc.php';
+
+class SubIteratorTest extends PlTestCase
+{
+    public function testSimple()
+    {
+        $iter = PlIteratorUtils::fromArray(array('a' => 1,
+                                                 'b' => 1,
+                                                 'c' => 2,
+                                                 'd' => 3));
+        $cb = PlIteratorUtils::arrayValueCallback('value');
+
+        $it = PlIteratorUtils::subIterator($iter, $cb);
+
+        $subit = $it->next();
+        $this->assertTrue($it->first());
+        $this->assertFalse($it->last());
+
+        $v = $subit->next();
+        $this->assertSame(1, $subit->value());
+        $this->assertSame(array('keys' => array('a'), 'value' => 1), $v);
+
+        $this->assertTrue($subit->first());
+        $this->assertFalse($subit->last());
+
+        $this->assertSame(array('keys' => array('b'), 'value' => 1), $subit->next());
+        $this->assertFalse($subit->first());
+        $this->assertTrue($subit->last());
+
+        $this->assertNull($subit->next());
+
+        $this->assertTrue($it->first());
+
+        $subit2 = $it->next();
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertSame(2, $subit2->value());
+        $this->assertSame(array('keys' => array('c'), 'value' => 2), $subit2->next());
+
+        $this->assertTrue($subit2->first());
+        $this->assertTrue($subit2->last());
+
+        $this->assertNull($subit2->next());
+
+        $subit3 = $it->next();
+        $this->assertFalse($it->first());
+        $this->assertFalse($it->last());
+
+        $this->assertSame(3, $subit3->value());
+        $this->assertSame(array('keys' => array('d'), 'value' => 3), $subit3->next());
+
+        $this->assertTrue($subit3->first());
+        $this->assertTrue($subit3->last());
+
+        $this->assertNull($subit3->next());
+
+        $this->assertTrue($it->last());
+        $this->assertNull($it->next());
+    }
+}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+?>