public function testLast() { // from js $this->assertEquals(3, __u::last(array(1, 2, 3)), 'can pull out the last element of an array'); $func = function () { return __u(func_get_args())->last(); }; $result = $func(1, 2, 3, 4); $this->assertEquals(4, $result, 'works on arguments'); $this->assertEquals('', join(', ', __u::last(array(1, 2, 3), 0)), 'can pass n to last'); $this->assertEquals('2, 3', join(', ', __u::last(array(1, 2, 3), 2)), 'can pass n to last'); $this->assertEquals('1, 2, 3', join(', ', __u::last(array(1, 2, 3), 5)), 'can pass n to last'); $result = __u::map(array(array(1, 2, 3), array(1, 2, 3)), function ($item) { return __u::last($item); }); $this->assertEquals('3,3', join(',', $result), 'works well with map'); // docs $this->assertEquals(1, __u::last(array(5, 4, 3, 2, 1))); }
public function testClon() { // from js $moe = array('name' => 'moe', 'lucky' => array(13, 27, 34)); $clone = __u::clon($moe); $this->assertEquals('moe', $clone['name'], 'the clone as the attributes of the original'); $moe_obj = (object) $moe; $clone_obj = __u::clon($moe_obj); $this->assertEquals('moe', $clone_obj->name, 'the clone as the attributes of the original'); $clone['name'] = 'curly'; $this->assertTrue($clone['name'] === 'curly' && $moe['name'] === 'moe', 'clones can change shallow attributes without affecting the original'); $clone_obj->name = 'curly'; $this->assertTrue($clone_obj->name === 'curly' && $moe_obj->name === 'moe', 'clones can change shallow attributes without affecting the original'); $clone['lucky'][] = 101; $this->assertEquals(101, __u::last($moe['lucky']), 'changes to deep attributes are shared with the original'); $clone_obj->lucky[] = 101; $this->assertEquals(101, __u::last($moe_obj->lucky), 'changes to deep attributes are shared with the original'); $val = 1; $this->assertEquals(1, __u::clon($val), 'non objects should not be changed by clone'); $val = null; $this->assertEquals(null, __u::clon($val), 'non objects should not be changed by clone'); // extra $foo = array('name' => 'Foo'); $bar = __u($foo)->clon(); $this->assertEquals('Foo', $bar['name'], 'works with OO-style call'); // docs $stooge = (object) array('name' => 'moe'); $this->assertEquals((object) array('name' => 'moe'), __u::clon($stooge)); }