From 62712e8a4cee7c1165fc318955efe48d0e123eaf Mon Sep 17 00:00:00 2001 From: Axel Gross Date: Wed, 22 Nov 2017 12:09:13 +0100 Subject: [PATCH 1/2] Prototype diff formatting for discussion Feature request: It would be good to be able to see not only that actual and expected results are different, but also one would like to see in which way they differ. I couldnt find a way to extend leo for diffing in plugin-kind of way. So I started prototyping. This commit prototypes a possible implementation with the following properties * Because the relevant different depends on the type of matcher, each matcher supporting diffs (see SameMatcher) gets a $differ method to be called by the formatter * also the matcher adds a "diff" to the format template vars * on match, the abstract matcher adds the differ to match the result iff the result is unexpected * Formatter reads and prints the $diff member from the matcher * diffing is actually not implemented, that probably should be plugged in using a thirdparty lib This indicates * leo probably should not force diff calculation on users * there should be a kind of extension point to add a differ implementation So this bears the questions: * is there a way already to hook in diffing? * is there a better place than Matcher/Match/Formatter ? ** should diff calculation happen when matching or when formatting * where/how would I register a Differ? * should the Differ be added as mix-in on the Matchers? --- src/Formatter/Formatter.php | 1 + src/Matcher/AbstractMatcher.php | 7 ++++++- src/Matcher/SameMatcher.php | 6 +++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Formatter.php b/src/Formatter/Formatter.php index 68ad935..b742210 100644 --- a/src/Formatter/Formatter.php +++ b/src/Formatter/Formatter.php @@ -98,6 +98,7 @@ protected function getTemplateVars(TemplateInterface $template) $vars = [ 'expected' => $this->match->getExpected(), 'actual' => $this->match->getActual(), + 'diff' => is_callable($this->match->differ)?call_user_func($this->match->differ, $this->match->getActual(), $this->match->getExpected()):"" ]; if ($tplVars = $template->getTemplateVars()) { diff --git a/src/Matcher/AbstractMatcher.php b/src/Matcher/AbstractMatcher.php index 033ff79..57d9889 100644 --- a/src/Matcher/AbstractMatcher.php +++ b/src/Matcher/AbstractMatcher.php @@ -31,8 +31,13 @@ public function match($actual = '') { $isMatch = $this->doMatch($actual); $isNegated = $this->isNegated(); + $fulfillsExpectation = ($isMatch xor $isNegated); - return new Match($isMatch xor $isNegated, $this->expected, $actual, $isNegated); + $match = new Match($fulfillsExpectation, $this->expected, $actual, $isNegated); + if ((!$fulfillsExpectation) && method_exists($this, "differ")) { + $match->differ = [$this, "differ"]; + } + return $match; } /** diff --git a/src/Matcher/SameMatcher.php b/src/Matcher/SameMatcher.php index 62a64dc..118c249 100644 --- a/src/Matcher/SameMatcher.php +++ b/src/Matcher/SameMatcher.php @@ -11,6 +11,10 @@ */ class SameMatcher extends AbstractMatcher { + public function differ($actual, $expected) { + return "THE CALCULATED DIFF"; + } + /** * Match if the actual value is identical to the expected value using an === * comparison. @@ -31,7 +35,7 @@ public function doMatch($actual) public function getDefaultTemplate() { return new ArrayTemplate([ - 'default' => 'Expected {{actual}} to be identical to {{expected}}', + 'default' => 'Expected {{actual}} to be identical to {{expected}}. Difference: {{diff}}', 'negated' => 'Expected {{actual}} not to be identical to {{expected}}', ]); } From 12c7082700c04ec2d1e33afc8e34412e821e7421 Mon Sep 17 00:00:00 2001 From: Axel Gross Date: Wed, 5 Feb 2020 16:07:10 +0100 Subject: [PATCH 2/2] Make specs happy --- src/Formatter/Formatter.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/Formatter/Formatter.php diff --git a/src/Formatter/Formatter.php b/src/Formatter/Formatter.php old mode 100644 new mode 100755 index b742210..f1164db --- a/src/Formatter/Formatter.php +++ b/src/Formatter/Formatter.php @@ -95,10 +95,11 @@ public function objectToString($obj) */ protected function getTemplateVars(TemplateInterface $template) { + $differExists = (property_exists($this->match, "differ") && is_callable($this->match->differ)); $vars = [ 'expected' => $this->match->getExpected(), 'actual' => $this->match->getActual(), - 'diff' => is_callable($this->match->differ)?call_user_func($this->match->differ, $this->match->getActual(), $this->match->getExpected()):"" + 'diff' => $differExists?call_user_func($this->match->differ, $this->match->getActual(), $this->match->getExpected()):"", ]; if ($tplVars = $template->getTemplateVars()) {