From e0d2849163b1084d9b17842e0524de8f44e83101 Mon Sep 17 00:00:00 2001 From: XuJiang Date: Sat, 31 Aug 2024 18:12:39 -0400 Subject: [PATCH] Issue: #3139 part 1 Tasks completed: 1. Locate ConsoleLauncher output 2. Check if actual and expected of AssertionFailedError are both type of CharSequence To do list: 1. Add diff function dependency 2. Using diff to generate the output desired Issue: #3139 --- .../console/tasks/ConsoleTestExecutor.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java index 1cdddf814e10..e38a27865856 100644 --- a/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java +++ b/junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java @@ -36,6 +36,9 @@ import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; +//for console output of diff +import org.opentest4j.AssertionFailedError; +import org.opentest4j.ValueWrapper; /** * @since 1.0 @@ -180,11 +183,34 @@ private Optional createXmlWritingListener(PrintWriter out private void printSummary(TestExecutionSummary summary, PrintWriter out) { // Otherwise the failures have already been printed in detail if (EnumSet.of(Details.NONE, Details.SUMMARY, Details.TREE).contains(outputOptions.getDetails())) { + //adding diff code here + summary.getFailures().forEach(failure -> { + //get AssertionFailedError + if(failure.getException() instanceof AssertionFailedError){ + AssertionFailedError assertionFailedError = (AssertionFailedError)failure.getException(); + ValueWrapper expected = assertionFailedError.getExpected(); + ValueWrapper actual = assertionFailedError.getActual(); + //apply diff function + if (isCharSequence(expected) && isCharSequence(actual)) { + out.printf("Expected %s\n", expected.getStringRepresentation()); + out.printf("Actual %s\n", actual.getStringRepresentation()); + //out.printf("Diff %s", calculateDiff(expected, actual)); + } + + } + }); + + summary.getFailures(); summary.printFailuresTo(out); } summary.printTo(out); } + private boolean isCharSequence(ValueWrapper value) { + return value != null && CharSequence.class.isAssignableFrom(value.getType()); + } + + @FunctionalInterface public interface Factory { ConsoleTestExecutor create(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions);