-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
020bf8d
commit 86732cf
Showing
2 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |