Skip to content

Commit

Permalink
Issue: junit-team#3139 part 2
Browse files Browse the repository at this point in the history
Tasks completed:
1. Locate ConsoleLauncher output
2. Check if actual and expected of AssertionFailedError are both type of CharSequence
3. Add diff function dependency
4. Using diff to generate the output desired

Issue: junit-team#3139
  • Loading branch information
XuJ321 committed Aug 31, 2024
1 parent e0d2849 commit bc1b464
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*Date of Release:* ❓

*Scope:* ❓
* `ConsoleLauncher` output shows extra diff message for failed assertions on two `CharSequence` objects

For a complete list of all _closed_ issues and pull requests for this release, consult the
link:{junit5-repo}+/milestone/75?closed=1+[5.12.0-M1] milestone page in the
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jfrunit = { module = "org.moditect.jfrunit:jfrunit-core", version = "1.0.0.Alpha
jimfs = { module = "com.google.jimfs:jimfs", version = "1.3.0" }
jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "jmh" }
jmh-generator-annprocess = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh" }
java-diff-utils = { module = "io.github.java-diff-utils:java-diff-utils", version = "4.12" }
joox = { module = "org.jooq:joox", version = "2.0.1" }
jte = { module = "gg.jte:jte", version = "3.1.12" }
junit4 = { module = "junit:junit", version = { require = "[4.12,)", prefer = "4.13.2" } }
Expand Down
6 changes: 5 additions & 1 deletion junit-platform-console/junit-platform-console.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ dependencies {

compileOnly(libs.openTestReporting.events)

implementation(libs.java.diff.utils)

shadowed(libs.picocli)

osgiVerification(projects.junitJupiterEngine)
Expand All @@ -28,7 +30,9 @@ tasks {
"--add-modules", "org.opentest4j.reporting.events",
"--add-reads", "${project.projects.junitPlatformReporting.dependencyProject.javaModuleName}=org.opentest4j.reporting.events",
"--add-modules", "info.picocli",
"--add-reads", "${javaModuleName}=info.picocli"
"--add-reads", "${javaModuleName}=info.picocli",
"--add-modules", "io.github.javadiffutils",
"--add-reads", "${javaModuleName}=io.github.javadiffutils"
))
}
shadowJar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
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 All @@ -36,7 +40,6 @@
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;

Expand Down Expand Up @@ -186,15 +189,23 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
//adding diff code here
summary.getFailures().forEach(failure -> {
//get AssertionFailedError
if(failure.getException() instanceof AssertionFailedError){
AssertionFailedError assertionFailedError = (AssertionFailedError)failure.getException();
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));
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()));

System.out.println(
"\nPlease put the diff result below into a onli../ne markdown editor to see markdown effect: ");
for (DiffRow row : rows) {
System.out.println(" | " + row.getOldLine() + " | " + row.getNewLine() + " | ");
}
}

}
Expand All @@ -210,7 +221,6 @@ private boolean isCharSequence(ValueWrapper value) {
return value != null && CharSequence.class.isAssignableFrom(value.getType());
}


@FunctionalInterface
public interface Factory {
ConsoleTestExecutor create(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions);
Expand Down

0 comments on commit bc1b464

Please sign in to comment.