Skip to content

Commit

Permalink
Adding diffPrinter class
Browse files Browse the repository at this point in the history
1. Add diffPrinter class
2. replace diff in consoleTestExecutor with diffPrinter - will be deleted evetually
3. mark the location of adding diff in flatPrinter and verboseTreePrinter.

Issue: junit-team#3139
  • Loading branch information
XJ114514 committed Oct 7, 2024
1 parent 3248752 commit 04f789a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import com.github.difflib.text.DiffRow;
import com.github.difflib.text.DiffRowGenerator;

import org.apiguardian.api.API;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.ClassLoaderUtils;
Expand Down Expand Up @@ -103,10 +99,8 @@ private static void printFoundTestsSummary(PrintWriter out, TestPlan testPlan) {
private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> reportsDir) {
Launcher launcher = launcherSupplier.get();
SummaryGeneratingListener summaryListener = registerListeners(out, reportsDir, launcher);

LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(discoveryOptions);
launcher.execute(discoveryRequest);

TestExecutionSummary summary = summaryListener.getSummary();
if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) {
printSummary(summary, out);
Expand Down Expand Up @@ -195,17 +189,8 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
ValueWrapper actual = assertionFailedError.getActual();
//apply diff function
if (isCharSequence(expected) && isCharSequence(actual)) {
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord(
true).oldTag(f -> "~").newTag(f -> "**").build();
List<DiffRow> rows = generator.generateDiffRows(
Arrays.asList(expected.getStringRepresentation()),
Arrays.asList(actual.getStringRepresentation()));

out.printf(
"\nPlease put the diff result below into a online markdown editor to see markdown effect: \n");
for (DiffRow row : rows) {
out.printf(" | %s | %s | \n", row.getOldLine(), row.getNewLine());
}
DiffPrinter.printDiff(out, expected.getStringRepresentation(),
actual.getStringRepresentation());
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2015-2024 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package org.junit.platform.console.tasks;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import com.github.difflib.text.DiffRow;
import com.github.difflib.text.DiffRowGenerator;

/**
* class provide access to printDiff function
*/
class DiffPrinter {
//print the difference of two print to out
static void printDiff(PrintWriter out, String expected, String actual) {
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord(true).oldTag(
f -> "~").newTag(f -> "**").build();
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(expected), Arrays.asList(actual));
out.printf("\nPlease put the diff result below into a online markdown editor to see markdown effect: \n");
for (DiffRow row : rows) {
out.printf(" | %s | %s | \n", row.getOldLine(), row.getNewLine());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void printlnTestDescriptor(Style style, String message, TestIdentifier t

private void printlnException(Style style, Throwable throwable) {
printlnMessage(style, "Exception", ExceptionUtils.readStackTrace(throwable));
//add diff here
}

private void printlnMessage(Style style, String message, String detail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void executionStarted(TestIdentifier testIdentifier) {
@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
testExecutionResult.getThrowable().ifPresent(t -> printDetail(Style.FAILED, "caught", readStackTrace(t)));
//add diff here
if (testIdentifier.isContainer()) {
Long creationMillis = frames.pop();
printVerticals(theme.end());
Expand Down

0 comments on commit 04f789a

Please sign in to comment.