Skip to content

Commit

Permalink
BetterThanTomorrow#1007: show diff between expected and actual in tes…
Browse files Browse the repository at this point in the history
…t runner
  • Loading branch information
SebastianAas committed Jan 2, 2025
1 parent d1a9e61 commit f729e0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/nrepl/cider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { diff } from 'semver';
// https://github.com/clojure-emacs/cider-nrepl/blob/a740583c3aa8b582f3097611787a276775131d32/src/cider/nrepl/middleware/test.clj#L45
export interface TestSummary {
ns: number;
Expand Down Expand Up @@ -142,6 +143,24 @@ export function lineInformation(result: TestResult): string {
return ` (${result.file}:${result.line})`;
}

function testDiffInformation(result: TestResult): string[] {
const diffs = result.diffs;
const messages: string[] = [];

if (diffs && Array.isArray(diffs)) {
diffs.forEach((diffPair: [string, string]) => {
if (Array.isArray(diffPair) && diffPair.length === 2) {
messages.push(`; expected:\n${diffPair[0]}`);
messages.push(`; actual:\n${diffPair[1]}`);
} else {
console.warn('Invalid diff pair format:', diffPair);
}
});
}

return messages;
}

// Return a detailed message about why a test failed.
// If the test passed, return the empty string.
// The message contains "comment" lines that are prepended with ;
Expand All @@ -167,11 +186,18 @@ export function detailedMessage(result: TestResult): string | undefined {
if (message) {
messages.push(`; ${message}`);
}
if (result.expected) {
messages.push(`; expected:\n${result.expected}`);
}
if (result.actual) {
messages.push(`; actual:\n${result.actual}`);

const diffMessages = testDiffInformation(result);

if (diffMessages.length > 0) {
messages.push(...diffMessages);
} else {
if (result.expected) {
messages.push(`; expected:\n${result.expected}`);
}
if (result.actual) {
messages.push(`; actual:\n${result.actual}`);
}
}
}
return messages.length > 0 ? messages.join('\n') : undefined;
Expand Down
1 change: 1 addition & 0 deletions src/testRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async function onTestResult(
run.errored(assertion, new vscode.TestMessage(cider.shortMessage(result)));
break;
case 'fail':
run.failed(assertion, new vscode.TestMessage(cider.detailedMessage(result)));
default:
run.failed(assertion, new vscode.TestMessage(cider.shortMessage(result)));
break;
Expand Down

0 comments on commit f729e0a

Please sign in to comment.