Skip to content

Commit

Permalink
Added tests for the remaining collection methods for CharCollection
Browse files Browse the repository at this point in the history
* references "feature"
  • Loading branch information
nozavroni committed Dec 5, 2016
1 parent e414716 commit bd7d0f6
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CSVelte/Collection/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ public function filter(callable $callback, $flag = ARRAY_FILTER_USE_BOTH)
public function first(callable $callback)
{
foreach ($this->data as $index => $value) {
if ($ret = $callback($value, $index)) {
return $ret;
if ($callback($value, $index)) {
return $value;
}
}
return null;
Expand Down
67 changes: 67 additions & 0 deletions tests/CSVelte/Collection/CharCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,71 @@ public function testMapUsesCallbackOnEachChar()
});
$this->assertEquals('-------------nopqrstuvwxyz', (string) $newstr);
}

public function testWalkUsesCallbackOnEachChar()
{
$str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz');
$exp = [];
$str->walk(function($val, $key, $extra) use (&$exp) {
if ($key % 2 == 0) {
$exp[] = $val . $extra[0];
} else {
$exp[] = $val . $extra[1];
}
}, ['foo','bar']);
$this->assertEquals("afoo", $exp[0]);
$this->assertEquals("bbar", $exp[1]);
}

public function testReduceUsesCallbackToReturnSingleValue()
{
$str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz');
$isstr = $str->reduce(function($carry, $elem){
return (is_string($elem) && $carry);
}, true);
$this->assertTrue($isstr);
$isnum= $str->reduce(function($carry, $elem){
return (is_numeric($elem) && $carry);
}, true);
$this->assertFalse($isnum);
}

public function testFilterRemovesCorrectChars()
{
$str = new CharCollection($exp = 'abcdefghijklmnopqrstuvwxyz');
$newstr = $str->filter(function($val) {
return ($val > 'm');
});
$this->assertEquals('nopqrstuvwxyz', (string) $newstr);
}

public function testFirstReturnsFirstMatchingValue()
{
$str = new CharCollection($exp = 'I like char collections.');
$char = $str->first(function($val) {
return ($val > 'm');
});
$this->assertEquals('r', $char);
}

public function testLastReturnsLastMatchingValue()
{
$str = new CharCollection($exp = 'I like char collections.');
$char = $str->last(function($val) {
return ($val > 'm');
});
$this->assertEquals('s', $char);
}

public function testReverse()
{
$str = new CharCollection($exp = 'I like char collections.');
$this->assertEquals(strrev($exp), (string) $str->reverse());
}

public function testUnique()
{
$str = new CharCollection($exp = 'I like char collections.');
$this->assertEquals('I likecharotns.', (string) $str->unique());
}
}

0 comments on commit bd7d0f6

Please sign in to comment.