From da11afdba9a38bd1d367f09809f1c7484c22843c Mon Sep 17 00:00:00 2001 From: mykolagolubyev Date: Wed, 26 Jul 2023 18:04:55 -0400 Subject: [PATCH] matchers: map contain analyzer refactor --- .../contain/handlers/MapContainHandler.java | 40 +++++-------------- .../handlers/StringContainHandler.java | 6 +-- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/MapContainHandler.java b/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/MapContainHandler.java index 168b1ffce..7bd5cd9cc 100644 --- a/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/MapContainHandler.java +++ b/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/MapContainHandler.java @@ -51,47 +51,29 @@ private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object expectedKey = expectedEntry.getKey(); ValuePath propertyPath = actualPath.property(expectedKey.toString()); - - if (isNegative) { - analyzeNegative(containAnalyzer, actualMap, propertyPath, expectedEntry); - } else { - analyzePositive(containAnalyzer, actualMap, propertyPath, expectedEntry); - } + analyzePositiveNegative(containAnalyzer, actualMap, propertyPath, expectedEntry, isNegative); } } - private void analyzePositive(ContainAnalyzer containAnalyzer, - Map actualMap, - ValuePath propertyPath, - Map.Entry expectedEntry) { + private void analyzePositiveNegative(ContainAnalyzer containAnalyzer, + Map actualMap, + ValuePath propertyPath, + Map.Entry expectedEntry, + boolean isNegative) { if (!actualMap.containsKey(expectedEntry.getKey())) { containAnalyzer.reportMismatch(this, propertyPath, tokenizedMessage().matcher("is missing")); } else { CompareToComparator comparator = CompareToComparator.comparator(); Object actualValue = actualMap.get(expectedEntry.getKey()); - boolean actualValueEqual = comparator.compareIsEqual(propertyPath, - actualValue, expectedEntry.getValue()); + boolean actualValueEqual = isNegative ? + !comparator.compareIsNotEqual(propertyPath, actualValue, expectedEntry.getValue()): + comparator.compareIsEqual(propertyPath, actualValue, expectedEntry.getValue()); if (!actualValueEqual) { containAnalyzer.reportMismatch(this, propertyPath, comparator.generateEqualMismatchReport()); - } - } - } - - private void analyzeNegative(ContainAnalyzer containAnalyzer, - Map actualMap, - ValuePath propertyPath, - Map.Entry expectedEntry) { - if (actualMap.containsKey(expectedEntry.getKey())) { - CompareToComparator comparator = CompareToComparator.comparator(); - - Object actualValue = actualMap.get(expectedEntry.getKey()); - boolean actualValueNotEqual = comparator.compareIsNotEqual(propertyPath, - actualValue, expectedEntry.getValue()); - - if (!actualValueNotEqual) { - containAnalyzer.reportMatch(this, propertyPath, comparator.generateNotEqualMismatchReport()); + } else { + containAnalyzer.reportMatch(this, propertyPath, comparator.generateEqualMatchReport()); } } } diff --git a/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/StringContainHandler.java b/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/StringContainHandler.java index 0e96c5218..df5f0305c 100644 --- a/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/StringContainHandler.java +++ b/webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/StringContainHandler.java @@ -31,15 +31,15 @@ public boolean handle(Object actual, Object expected) { @Override public void analyzeContain(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) { - analyze(containAnalyzer, actualPath, actual, expected, false); + analyze(containAnalyzer, actualPath, actual, expected); } @Override public void analyzeNotContain(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) { - analyze(containAnalyzer, actualPath, actual, expected, true); + analyze(containAnalyzer, actualPath, actual, expected); } - private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected, boolean isNegative) { + private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) { CharSequence actualText = (CharSequence) actual; CharSequence expectedText = (CharSequence) expected;