From a93fe7b7b93a66fb78512193f9ae99785a588468 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Barrois?= Date: Tue, 16 Mar 2010 01:18:56 +0100 Subject: [PATCH] Add unit tests for callbacks and PlSubIterator MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaël Barrois --- ut/callbacktest.php | 51 ++++++++++++++++++++++++++++++ ut/subiteratortest.php | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 ut/callbacktest.php create mode 100644 ut/subiteratortest.php diff --git a/ut/callbacktest.php b/ut/callbacktest.php new file mode 100644 index 0000000..585d309 --- /dev/null +++ b/ut/callbacktest.php @@ -0,0 +1,51 @@ + '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 index 0000000..0ed267e --- /dev/null +++ b/ut/subiteratortest.php @@ -0,0 +1,85 @@ + 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: +?> -- 2.1.4