From 30128cf96b9ea01e4b0e2572a777c916c1b2d8bf Mon Sep 17 00:00:00 2001 From: Alexis von Glasow Date: Mon, 6 Apr 2015 18:28:07 +0200 Subject: [PATCH] Issues #41 Add errors when assert operators. When operators are evaluated, we can found some asserter false. This patch add a management of errors in the asserter. Operators name and value is added in errors stack to identify which operator fail. Two methods are available to identify if we have errors and to get there errors: * hasErrors return a boolean which identify if some errors have been raised. * getErrors return an array with operator name as key and arguments as values. * resetErrors clean all errors stored. --- Test/Unit/Visitor/Asserter.php | 85 ++++++++++++++++++++++++++++++++++ Visitor/Asserter.php | 44 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 Test/Unit/Visitor/Asserter.php diff --git a/Test/Unit/Visitor/Asserter.php b/Test/Unit/Visitor/Asserter.php new file mode 100644 index 0000000..3feed20 --- /dev/null +++ b/Test/Unit/Visitor/Asserter.php @@ -0,0 +1,85 @@ + + * @copyright Copyright © 2007-2015 Alexis von Glasow. + * @license New BSD License + */ + +class Asserter extends Test\Unit\Suite { + + public function case_keep_errors ( ) { + $this + ->given( + $ruler = new LUT(), + $fExecuted = false, + $asserter = $ruler->getDefaultAsserter(), + $asserter->setOperator( + 'f', + function ( $a = false ) use ( &$fExecuted ) { + + $fExecuted = true; + + return $a; + } + ), + $rule = 'f(false)' + ) + ->when($result = $ruler->assert($rule, new LUT\Context())) + ->then + ->boolean($result) + ->isFalse() + ->boolean($fExecuted) + ->isTrue() + ->boolean($asserter->hasError()) + ->isTrue() + ->array($asserter->getErrors()) + ->isNotEmpty() + ->hasKey('f') + ; + } +} + diff --git a/Visitor/Asserter.php b/Visitor/Asserter.php index 66991a1..3db8da8 100644 --- a/Visitor/Asserter.php +++ b/Visitor/Asserter.php @@ -67,6 +67,13 @@ class Asserter implements Visitor\Visit { */ protected $_operators = []; + /** + * List of errors. + * + * @var \Hoa\Ruler\Visitor\Asserter array + */ + protected $errors = []; + /** @@ -159,6 +166,10 @@ protected function visitOperator ( Ruler\Model\Operator $element, &$handle = nul $value = $argument->accept($this, $handle, $eldnah); $arguments[] = $value; + // Add operators name and their values in errors lists + if (false === $value) + $this->errors[$element->getName()] = $element->getArguments(); + if($element::LAZY_BREAK === $element->shouldBreakLazyEvaluation($value)) break; } @@ -487,4 +498,37 @@ public function getOperators ( ) { return $this->_operators; } + + /** + * Get all errors. + * + * @access public + * @return array + */ + public function getErrors ( ) { + + return $this->errors; + } + + /** + * The evaluation of operators has it raised errors. + * + * @access public + * @return boolean + */ + public function hasError ( ) { + + return !empty($this->errors); + } + + /** + * Reset all errors raised + * + * @access public + * @return void + */ + public function resetErrors ( ) { + + $this->errors = []; + } }