Skip to content

Commit

Permalink
added looper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedevin committed Oct 3, 2015
1 parent 020bf8d commit 86732cf
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Tipsy/Looper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct() {

// if anyone knows any way to pass func_get_args by reference i would love you. i want string manipulation
public static function o() {
$iterator = new ReflectionClass(get_called_class());
$iterator = new \ReflectionClass(get_called_class());
return $iterator->newInstanceArgs(func_get_args());
}

Expand All @@ -36,6 +36,15 @@ public function items() {
public function get($index) {
return $this->_items[$index];
}

public function set($var, $val) {
if ($var) {
foreach ($this->_items as $item) {
$item->{$var} = $val;
};
}
return $this;
}

public function eq($pos) {
$pos = $pos < 0 ? count($this->_items) - abs($pos) : $pos;
Expand Down
125 changes: 125 additions & 0 deletions tests/LooperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php


class LooperTest extends Tipsy_Test {

public function setUp() {
$this->tip = new Tipsy\Tipsy;
$this->useOb = true; // for debug use

$this->tip->config('tests/config.ini');
}

public function testLoop() {
$loop = new \Tipsy\Looper([1,2,3]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->scalar;
});
$this->assertEquals(6, $val);
}

public function testBreak() {
$loop = new \Tipsy\Looper([1,2,3]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->scalar;
return \Tipsy\Looper::DONE;
});
$this->assertEquals(1, $val);
}

public function testObjects() {
$loop = new \Tipsy\Looper([
(object)['a' => 1],
(object)['a' => 2],
(object)['a' => 3]
]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(6, $val);
}

public function testRemove() {
$loop = new \Tipsy\Looper([1,2,3]);
$loop->remove(2);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->scalar;
});
$this->assertEquals(3, $val);
}

public function testSet() {
$loop = new \Tipsy\Looper([
(object)['a' => 1],
(object)['a' => 2],
(object)['a' => 3]
]);
$loop->set('a', 1);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(3, $val);
}

public function testFilter() {
$loop = new \Tipsy\Looper([
(object)['a' => 1, 'b' => 1],
(object)['a' => 2, 'b' => 1],
(object)['a' => 3, 'b' => 3]
]);
$loop = $loop->filter([
'b' => 1
]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->a;
});
$this->assertEquals(3, $val);
}

public function testEq() {
$loop = new \Tipsy\Looper([1,2,3]);
$val = $loop->eq(-1);
$this->assertEquals(3, $val);
}

public function testGet() {
$loop = new \Tipsy\Looper([1,2,3]);
$val = $loop->eq(0);
$this->assertEquals(1, $val);
}

public function testMultiArray() {
$loop = new \Tipsy\Looper([1,2,3], [4,5,6]);
$val = 0;
$loop->each(function() use (&$val) {
$val += $this->scalar;
});
$this->assertEquals(21, $val);
}

public function testMultiComplex() {
$loop = (new \Tipsy\Looper([
(object)['a' => 1, 'b' => 1, 'c' => 1],
(object)['a' => 2, 'b' => 1, 'c' => 1],
(object)['a' => 3, 'b' => 3, 'c' => 1]
], [
(object)['a' => 4, 'b' => 1, 'c' => 1],
(object)['a' => 5, 'b' => 1, 'c' => 1],
(object)['a' => 7, 'b' => 3, 'c' => 1]
]))->filter(['b' => 3])
->set('c', 2)
->parent();
$val = 0;

$loop->each(function() use (&$val) {
$val += $this->c;
});
$this->assertEquals(8, $val);
}
}

0 comments on commit 86732cf

Please sign in to comment.