-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Output diff of expected and actual values in ConsoleLauncher
#4017
base: main
Are you sure you want to change the base?
Changes from 22 commits
e0d2849
bc1b464
2fa9a8e
50552b4
e8d11bd
7b4088d
bb29695
2c27b53
af16685
3d73d1e
c8a7b95
53d884c
26bcaa4
3248752
04f789a
983e090
eef49f1
bcd0608
cc69bfb
705b4cf
2383f3e
82f723a
536e9c7
f44f929
eef975b
de00ade
a3f8f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 static java.lang.String.join; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.github.difflib.text.DiffRow; | ||
import com.github.difflib.text.DiffRowGenerator; | ||
|
||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
|
||
/** | ||
* class provide access to printDiff function | ||
*/ | ||
class DiffPrinter { | ||
private final TestPlan testPlan; | ||
|
||
public DiffPrinter(TestPlan testPlan) { | ||
this.testPlan = testPlan; | ||
} | ||
|
||
//print the difference of two print to out | ||
void printDiff(PrintWriter out, String expected, String actual, TestIdentifier testIdentifier) { | ||
out.printf(" %s:", describeTest(testIdentifier)); | ||
boolean inlineDiffByWordFlag = false; | ||
if (expected.contains(" ") || actual.contains(" ")) { | ||
inlineDiffByWordFlag = true; | ||
} | ||
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord( | ||
inlineDiffByWordFlag).oldTag(f -> "~~").newTag(f -> "**").build(); | ||
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(expected), Arrays.asList(actual)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we have to split the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What the difference is if we split them into lines? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure. I haven't used this library before and I was just curious why it expects lists of Strings. Do you know the reason? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know the details but all their methods use |
||
out.println(); | ||
out.println(" | expected | actual |"); | ||
out.println(" | -------- | ------ |"); | ||
for (DiffRow row : rows) { | ||
XJ114514 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
out.printf(" | %s | %s |", row.getOldLine(), row.getNewLine()); | ||
out.println(); | ||
} | ||
} | ||
|
||
private String describeTest(TestIdentifier testIdentifier) { | ||
List<String> descriptionParts = new ArrayList<>(); | ||
collectTestDescription(testIdentifier, descriptionParts); | ||
return join(":", descriptionParts); | ||
} | ||
|
||
private void collectTestDescription(TestIdentifier identifier, List<String> descriptionParts) { | ||
descriptionParts.add(0, identifier.getDisplayName()); | ||
testPlan.getParent(identifier).ifPresent(parent -> collectTestDescription(parent, descriptionParts)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done in place where we print the exceptions (as it is in #3397). Otherwise, it would be difficult to find the test that belongs to the diff in case of multiple failing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which part do you wish to change? The first print happens in
DefaultLaucher
and the second happens inMutableTestExecutionSummary
. Both are injunit-platform-laucher
module. Changes will affect all Junit API output I believe?I can try to work on it over the weekend if you wish.
At the same time, can you help to find why the package documentation test case fails (Test cases can not find
diff package
once I add it to the module-info.java for documentation)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to make sure that the diff is printed if a details mode other than
Details.NONE
,Details.SUMMARY
, orDetails.TREE
is used. We should be able to extract the diff-printing code to a separate class and call it fromMutableTestExecutionSummary
, and the other listeners (FlatPrintingListener
etc.).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The solutions I can think of:
junit-platform-commons...util.StringUtils
FlatPrintingListener
,TreePrintingListener
,VerboseTreePrintingListener
andMutableTestExecutionSummary
I will start to work on it if the solution looks right for you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should stay in junit-platform-console. Please create a separate
DiffPrinter
class or similar.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiffPrinter
class has been added.FlatPrintingListener
andVerboseTreePrintingListener