Skip to content

Commit 04f789a

Browse files
committed
Adding diffPrinter class
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
1 parent 3248752 commit 04f789a

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616
import java.net.URL;
1717
import java.net.URLClassLoader;
1818
import java.nio.file.Path;
19-
import java.util.Arrays;
2019
import java.util.EnumSet;
2120
import java.util.List;
2221
import java.util.Optional;
2322
import java.util.function.Supplier;
2423

25-
import com.github.difflib.text.DiffRow;
26-
import com.github.difflib.text.DiffRowGenerator;
27-
2824
import org.apiguardian.api.API;
2925
import org.junit.platform.commons.JUnitException;
3026
import org.junit.platform.commons.util.ClassLoaderUtils;
@@ -103,10 +99,8 @@ private static void printFoundTestsSummary(PrintWriter out, TestPlan testPlan) {
10399
private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> reportsDir) {
104100
Launcher launcher = launcherSupplier.get();
105101
SummaryGeneratingListener summaryListener = registerListeners(out, reportsDir, launcher);
106-
107102
LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(discoveryOptions);
108103
launcher.execute(discoveryRequest);
109-
110104
TestExecutionSummary summary = summaryListener.getSummary();
111105
if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) {
112106
printSummary(summary, out);
@@ -195,17 +189,8 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
195189
ValueWrapper actual = assertionFailedError.getActual();
196190
//apply diff function
197191
if (isCharSequence(expected) && isCharSequence(actual)) {
198-
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord(
199-
true).oldTag(f -> "~").newTag(f -> "**").build();
200-
List<DiffRow> rows = generator.generateDiffRows(
201-
Arrays.asList(expected.getStringRepresentation()),
202-
Arrays.asList(actual.getStringRepresentation()));
203-
204-
out.printf(
205-
"\nPlease put the diff result below into a online markdown editor to see markdown effect: \n");
206-
for (DiffRow row : rows) {
207-
out.printf(" | %s | %s | \n", row.getOldLine(), row.getNewLine());
208-
}
192+
DiffPrinter.printDiff(out, expected.getStringRepresentation(),
193+
actual.getStringRepresentation());
209194
}
210195

211196
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.console.tasks;
12+
13+
import java.io.PrintWriter;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
import com.github.difflib.text.DiffRow;
18+
import com.github.difflib.text.DiffRowGenerator;
19+
20+
/**
21+
* class provide access to printDiff function
22+
*/
23+
class DiffPrinter {
24+
//print the difference of two print to out
25+
static void printDiff(PrintWriter out, String expected, String actual) {
26+
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord(true).oldTag(
27+
f -> "~").newTag(f -> "**").build();
28+
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(expected), Arrays.asList(actual));
29+
out.printf("\nPlease put the diff result below into a online markdown editor to see markdown effect: \n");
30+
for (DiffRow row : rows) {
31+
out.printf(" | %s | %s | \n", row.getOldLine(), row.getNewLine());
32+
}
33+
}
34+
}

junit-platform-console/src/main/java/org/junit/platform/console/tasks/FlatPrintingListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ private void printlnTestDescriptor(Style style, String message, TestIdentifier t
7979

8080
private void printlnException(Style style, Throwable throwable) {
8181
printlnMessage(style, "Exception", ExceptionUtils.readStackTrace(throwable));
82+
//add diff here
8283
}
8384

8485
private void printlnMessage(Style style, String message, String detail) {

junit-platform-console/src/main/java/org/junit/platform/console/tasks/VerboseTreePrintingListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void executionStarted(TestIdentifier testIdentifier) {
9797
@Override
9898
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
9999
testExecutionResult.getThrowable().ifPresent(t -> printDetail(Style.FAILED, "caught", readStackTrace(t)));
100+
//add diff here
100101
if (testIdentifier.isContainer()) {
101102
Long creationMillis = frames.pop();
102103
printVerticals(theme.end());

0 commit comments

Comments
 (0)