aboutsummaryrefslogtreecommitdiffstats
path: root/Praspel.php
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-06-10 16:48:03 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-06-10 16:48:03 +0200
commit95d7bfe7ec36e52b1ec46a0d1dda5982f0958c31 (patch)
tree687ebd284e3fdf38383d2f2387dd98d102213295 /Praspel.php
parente7681de68cbd9b0ff906857264f00585ef082533 (diff)
downloadPraspel-95d7bfe7ec36e52b1ec46a0d1dda5982f0958c31.zip
Praspel-95d7bfe7ec36e52b1ec46a0d1dda5982f0958c31.tar.gz
Praspel-95d7bfe7ec36e52b1ec46a0d1dda5982f0958c31.tar.bz2
RAC checks @throwable clause.
Diffstat (limited to 'Praspel.php')
-rw-r--r--Praspel.php112
1 files changed, 89 insertions, 23 deletions
diff --git a/Praspel.php b/Praspel.php
index 4dce900..1643127 100644
--- a/Praspel.php
+++ b/Praspel.php
@@ -161,7 +161,7 @@ class Praspel {
*
* @access public
* @return bool
- * @throw \Hoa\Praspel\Exception
+ * @throw \Hoa\Praspel\Exception\Group
*/
public function evaluate ( ) {
@@ -176,7 +176,6 @@ class Praspel {
if($reflection instanceof \ReflectionMethod)
$reflection->setAccessible(true);
-
// Prepare data.
$data = $this->getData() ?: $this->generateData();
$arguments = array();
@@ -198,7 +197,6 @@ class Praspel {
$arguments[$name] = $parameter->getDefaultValue();
}
-
// Check precondition.
$precondition = true;
@@ -216,37 +214,54 @@ class Praspel {
if(0 < count($exceptions))
throw $exceptions;
+ try {
- // Invoke.
- if($reflection instanceof \ReflectionFunction)
- $return = $reflection->invokeArgs($arguments);
- else {
+ // Invoke.
+ if($reflection instanceof \ReflectionFunction)
+ $return = $reflection->invokeArgs($arguments);
+ else {
- $_callback = $callable->getValidCallback();
- $_object = $_callback[0];
- $return = $reflection->invokeArgs($_object, $arguments);
- }
+ $_callback = $callable->getValidCallback();
+ $_object = $_callback[0];
+ $return = $reflection->invokeArgs($_object, $arguments);
+ }
+ // Check normal postcondition.
+ $postcondition = true;
- // Check postcondition.
- $postcondition = true;
+ if(true === $specification->clauseExists('ensures')) {
- if(true === $specification->clauseExists('ensures')) {
+ $ensures = $specification->getClause('ensures');
+ $arguments['\result'] = $return;
+ $postcondition = $this->checkClause(
+ $ensures,
+ $arguments,
+ $exceptions,
+ __NAMESPACE__ . '\Exception\Failure\Postcondition'
+ );
+ }
+ }
+ catch ( \Exception $exception ) {
- $ensures = $specification->getClause('ensures');
- $arguments['\result'] = $return;
- $postcondition = $this->checkClause(
- $ensures,
- $arguments,
- $exceptions,
- __NAMESPACE__ . '\Exception\Failure\Postcondition'
- );
+ // Check exceptional postcondition.
+ $postcondition = true;
+
+ if(true === $specification->clauseExists('throwable')) {
+
+ $throwable = $specification->getClause('throwable');
+ $arguments['\result'] = $exception;
+ $postcondition = $this->checkExceptionalClause(
+ $throwable,
+ $arguments,
+ $exceptions,
+ __NAMESPACE__ . '\Exception\Failure\Exceptional'
+ );
+ }
}
if(0 < count($exceptions))
throw $exceptions;
-
// Verdict.
return $precondition && $postcondition;
}
@@ -293,6 +308,57 @@ class Praspel {
}
/**
+ * Check an exceptional clause.
+ *
+ * @access protected
+ * @param \Hoa\Praspel\Model\Throwable $clause Clause.
+ * @param array &$data Data.
+ * @param \Hoa\Praspel\Exception\Group $exceptions Exceptions group.
+ * @param string $exception Exception to
+ * throw.
+ * @return bool
+ * @throw \Hoa\Praspel\Exception
+ */
+ protected function checkExceptionalClause ( Model\Throwable $clause,
+ Array &$data,
+ Exception\Group $exceptions,
+ $exception ) {
+
+ $verdict = false;
+
+ foreach($clause as $identifier) {
+
+ $_exception = $clause[$identifier];
+ $instanceName = $_exception->getInstanceName();
+
+ if($data['\result'] instanceof $instanceName) {
+
+ $verdict = true;
+ break;
+ }
+
+ foreach((array) $_exception->getDisjunction() as $_identifier) {
+
+ $__exception = $clause[$_identifier];
+ $_instanceName = $__exception->getInstanceName();
+
+ if($exception instanceof $_instanceName) {
+
+ $verdict = true;
+ break;
+ }
+ }
+ }
+
+ if(false === $verdict)
+ $exceptions[] = new $exception(
+ 'The exception %s has been thrown and it is not specified.',
+ 0, array(get_class($data['\result'])));
+
+ return $verdict;
+ }
+
+ /**
* Set specification.
*
* @access protected