Skip to content

Commit

Permalink
100% coverage on Event and Candy Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Aug 17, 2009
1 parent 50159d7 commit f47f2f6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
54 changes: 52 additions & 2 deletions recess/test/recess/core/CandyTest.php
Expand Up @@ -66,8 +66,58 @@ function testLIFO() {
function testLazyLinks() {
$candy = new Candy(function($num) { return $num + 1; });
$candy->wrap(function($candy, &$num) { $num += 1; })
->wrap(function($candy, $num) { /* echo or log something */ });
->wrap(function($candy, $num) { /* echo or log something */ });
$this->assertEquals(3, $candy(1));
}

}
function testNonCallableException() {
try {
$candy = new Candy('unknownFunc');
$this->assertTrue(false);
} catch(Exception $e) {
$this->assertTrue(true);
return;
}
$this->assertTrue(false);
}

function testPlainOldFunction() {
$candy = new Candy('callMe');
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}

function testStaticMethod() {
$candy = new Candy(array('SomeStaticClass','callMe'));
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}

function testMethod() {
$candy = new Candy(array(new SomeStaticClass,'callMe'));
$this->assertEquals('pass',$candy());
$candy->wrap(function ($candy) { return $candy() . 'me'; });
$this->assertEquals('passme',$candy());
}

function testManyArgsUnwrapped() {
$candy = new Candy(function() { return array_sum(func_get_args()); });
$this->assertEquals(0, $candy());
$this->assertEquals(1, $candy(1));
$this->assertEquals(2, $candy(1,1));
$this->assertEquals(3, $candy(1,1,1));
$this->assertEquals(4, $candy(1,1,1,1));
$this->assertEquals(5, $candy(1,1,1,1,1));
$this->assertEquals(6, $candy(1,1,1,1,1,1));
$this->assertEquals(7, $candy(1,1,1,1,1,1,1));
}
}


class SomeStaticClass {
static function callMe() { return 'pass'; }
function callMeToo() { return 'pass'; }
}
function callMe() { return 'pass'; }
24 changes: 24 additions & 0 deletions recess/test/recess/core/EventTest.php
Expand Up @@ -65,4 +65,28 @@ function testInvokeManyArgs() {
$onSum(1,1,1,1,1,1,1,1,1,1);
$this->assertEquals(10, $theSum);
}

function testVaryingArgNumbers() {
$onSum = new Event;
$theSum = 0;
$onSum->call(function() use (&$theSum) { $theSum = array_sum(func_get_args()); });
$onSum(1);
$this->assertEquals(1,$theSum);
$theSum = 0;
$onSum(1,1);
$this->assertEquals(2,$theSum);
$theSum = 0;
$onSum(1,1,1);
$this->assertEquals(3,$theSum);
$theSum = 0;
$onSum(1,1,1,1);
$this->assertEquals(4,$theSum);
$theSum = 0;
$onSum(1,1,1,1,1);
$this->assertEquals(5,$theSum);
$theSum = 0;
$onSum(1,1,1,1,1,1);
$this->assertEquals(6,$theSum);
$theSum = 0;
}
}

0 comments on commit f47f2f6

Please sign in to comment.