Skip to content

Commit

Permalink
Added throw_error matcher. Fixed constant check causing a warning.
Browse files Browse the repository at this point in the history
  • Loading branch information
noonat committed Dec 7, 2009
1 parent 474d406 commit 42aa946
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/pecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function expect($actual) {
}
EOC;
// eval is the only way to execute within global namespace
if (constant('\PECS_GLOBALS') !== false) eval($code); // global ns aliases
if (@constant('\PECS_GLOBALS') !== false) eval($code); // global ns aliases
eval("namespace pecs;\n$code"); // local ns aliases

/// Run the tests.
Expand Down Expand Up @@ -151,7 +151,7 @@ function __call($method, $args) {
if (isset($this->_aliases[$method]))
$method = $this->_aliases[$method];
if (!method_exists($this, $method))
throw new \Exception("Unknown expect assertion \"{$method}\"");
throw new \Exception("Unknown expectation assertion \"{$method}\"");
$this->_assert($method, $args, $expectedResult);
return $this;
}
Expand Down Expand Up @@ -259,6 +259,21 @@ function have_length_within($min, $max) {
'expected %s to have count within %d and %d, was %d',
$this->actual, $min, $max, $n);
}

function throw_error($className=null, $message=null) {
try {
$func = $this->actual;
$func();
return false;
}
catch (\Exception $e) {
if ((!$className || $e instanceof $className) &&
(!$message || $e->getMessage() == $message)) {
return true;
}
throw $e;
}
}
}

class Failure extends \Exception {
Expand Down

0 comments on commit 42aa946

Please sign in to comment.