public function testEach() { // from js $test =& $this; __u::each(array(1, 2, 3), function ($num, $i) use($test) { $test->assertEquals($num, $i + 1, 'each iterators provide value and iteration count'); }); $answers = array(); $context = (object) array('multiplier' => 5); __u::each(array(1, 2, 3), function ($num) use(&$answers, $context) { $answers[] = $num * $context->multiplier; }); $this->assertEquals(array(5, 10, 15), $answers, 'context object property accessed'); $answers = array(); $obj = (object) array('one' => 1, 'two' => 2, 'three' => 3); __u::each($obj, function ($value, $key) use(&$answers) { $answers[] = $key; }); $this->assertEquals(array('one', 'two', 'three'), $answers, 'iterating over objects works'); $answer = null; __u::each(array(1, 2, 3), function ($num, $index, $arr) use(&$answer) { if (__u::includ($arr, $num)) { $answer = true; } }); $this->assertTrue($answer, 'can reference the original collection from inside the iterator'); $answers = 0; __u::each(null, function () use(&$answers) { $answers++; }); $this->assertEquals(0, $answers, 'handles a null property'); // extra $test =& $this; __u(array(1, 2, 3))->each(function ($num, $i) use($test) { $test->assertEquals($num, $i + 1, 'each iterators provide value and iteration count within OO-style call'); }); // docs $str = ''; __u::each(array(1, 2, 3), function ($num) use(&$str) { $str .= $num . ','; }); $this->assertEquals('1,2,3,', $str); $str = ''; $multiplier = 2; __u::each(array(1, 2, 3), function ($num, $index) use($multiplier, &$str) { $str .= $index . '=' . $num * $multiplier . ','; }); $this->assertEquals('0=2,1=4,2=6,', $str); }