|
| 1 | +/* |
| 2 | + * Copyright 2015-2026 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.output; |
| 12 | + |
| 13 | +import com.github.difflib.text.DiffRowGenerator; |
| 14 | + |
| 15 | +import org.junit.platform.commons.util.ExceptionUtils; |
| 16 | +import org.opentest4j.AssertionFailedError; |
| 17 | + |
| 18 | +final class RichDiffFormatter { |
| 19 | + public String format(AssertionFailedError assertionFailed) { |
| 20 | + if (!(assertionFailed.isActualDefined() && assertionFailed.isExpectedDefined())) { |
| 21 | + return ExceptionUtils.readStackTrace(assertionFailed); |
| 22 | + } |
| 23 | + |
| 24 | + StringBuilder builder = new StringBuilder(); |
| 25 | + |
| 26 | + builder.append(assertionFailed.getClass().getSimpleName()); |
| 27 | + if (assertionFailed.isReasonDefined()) { |
| 28 | + builder.append(": "); |
| 29 | + builder.append(assertionFailed.getReason()); |
| 30 | + } |
| 31 | + builder.append(System.lineSeparator()); |
| 32 | + |
| 33 | + builder.append("+ actual - expected"); |
| 34 | + builder.append(System.lineSeparator()); |
| 35 | + |
| 36 | + var generator = DiffRowGenerator.create().mergeOriginalRevised(true).build(); |
| 37 | + |
| 38 | + // TODO: But how to render the stacktrace? |
| 39 | + |
| 40 | + builder.append(generator.generateDiffRows(assertionFailed.getExpected().toString().lines().toList(), |
| 41 | + assertionFailed.getActual().toString().lines().toList())); |
| 42 | + |
| 43 | + return builder.toString(); |
| 44 | + } |
| 45 | +} |
0 commit comments