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 20, 2025
1 parent 249392a commit a8870ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes to Calva.

## [Unreleased]

- Fix: [Have test runner show diff between "actual" and "expected"](https://github.com/BetterThanTomorrow/calva/issues/1007)

## [2.0.483] - 2025-01-08

- Fix: [Paredit garbles while backspacing rapidly](https://github.com/BetterThanTomorrow/calva/issues/2611)
Expand All @@ -12,7 +14,7 @@ Changes to Calva.

## [2.0.482] - 2024-12-03

- Fix: [Added 'replace-refer-all-with-alias' & 'replace-refer-all-with-refer' actions to calva.](https://github.com/BetterThanTomorrow/calva/issues/2667)
- Fix: [Added 'replace-refer-all-with-alias' & 'replace-refer-all-with-refer' actions to calva.](https://github.com/BetterThanTomorrow/calva/issues/2667)

## [2.0.481] - 2024-10-29

Expand Down
31 changes: 31 additions & 0 deletions src/extension-test/unit/test-runner-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,35 @@ orange`
apple`
);
});

it('can produce detailed messages with diffs', () => {
expect(
cider.detailedMessage({
type: 'fail',
ns: 'core',
context: 'ctx',
index: 2,
expected: '{:key1 "value1", :key2 "value2"}',
actual: '{:key1 "wrongValue", :key2 "value2"}',
var: 'test',
file: 'core.clj',
line: 10,
message: 'Values do not match',
diffs: [
[
'{:key1 "wrongValue", :key2 "value2"}',
['{:key1 "value1", :key2 "value2"}', '{:key1 "wrongValue"}'],
],
],
})
).toBe(`; FAIL in core/test (core.clj:10):
; ctx: Values do not match
; expected:
{:key1 "value1", :key2 "value2"}
; actual:
{:key1 "wrongValue", :key2 "value2"}
; diff:
- {:key1 "value1", :key2 "value2"}
+ {:key1 "wrongValue"}`);
});
});
32 changes: 32 additions & 0 deletions src/nrepl/cider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ export function lineInformation(result: TestResult): string {
return ` (${result.file}:${result.line})`;
}

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

if (!diffs || !Array.isArray(diffs) || diffs.length === 0) {
return messages;
}

diffs.forEach((diffPair: [string, [string, string]]) => {
if (Array.isArray(diffPair) && diffPair.length === 2) {
const [_, [removed, added]] = diffPair;

if (removed || added) {
messages.push('; diff:');
messages.push(removed ? `- ${removed}` : '');
if (added) {
messages.push(`+ ${added}`);
}
}
} 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 Down Expand Up @@ -173,6 +200,11 @@ export function detailedMessage(result: TestResult): string | undefined {
if (result.actual) {
messages.push(`; actual:\n${result.actual}`);
}

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

0 comments on commit a8870ec

Please sign in to comment.