Skip to content

Commit a8870ec

Browse files
committed
BetterThanTomorrow#1007: show diff between expected and actual in test runner
1 parent 249392a commit a8870ec

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changes to Calva.
44

55
## [Unreleased]
66

7+
- Fix: [Have test runner show diff between "actual" and "expected"](https://github.com/BetterThanTomorrow/calva/issues/1007)
8+
79
## [2.0.483] - 2025-01-08
810

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

1315
## [2.0.482] - 2024-12-03
1416

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

1719
## [2.0.481] - 2024-10-29
1820

src/extension-test/unit/test-runner-test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,35 @@ orange`
167167
apple`
168168
);
169169
});
170+
171+
it('can produce detailed messages with diffs', () => {
172+
expect(
173+
cider.detailedMessage({
174+
type: 'fail',
175+
ns: 'core',
176+
context: 'ctx',
177+
index: 2,
178+
expected: '{:key1 "value1", :key2 "value2"}',
179+
actual: '{:key1 "wrongValue", :key2 "value2"}',
180+
var: 'test',
181+
file: 'core.clj',
182+
line: 10,
183+
message: 'Values do not match',
184+
diffs: [
185+
[
186+
'{:key1 "wrongValue", :key2 "value2"}',
187+
['{:key1 "value1", :key2 "value2"}', '{:key1 "wrongValue"}'],
188+
],
189+
],
190+
})
191+
).toBe(`; FAIL in core/test (core.clj:10):
192+
; ctx: Values do not match
193+
; expected:
194+
{:key1 "value1", :key2 "value2"}
195+
; actual:
196+
{:key1 "wrongValue", :key2 "value2"}
197+
; diff:
198+
- {:key1 "value1", :key2 "value2"}
199+
+ {:key1 "wrongValue"}`);
200+
});
170201
});

src/nrepl/cider.ts

+32
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ export function lineInformation(result: TestResult): string {
142142
return ` (${result.file}:${result.line})`;
143143
}
144144

145+
function getDiffMessages(result: TestResult): string[] {
146+
const diffs = result.diffs;
147+
const messages: string[] = [];
148+
149+
if (!diffs || !Array.isArray(diffs) || diffs.length === 0) {
150+
return messages;
151+
}
152+
153+
diffs.forEach((diffPair: [string, [string, string]]) => {
154+
if (Array.isArray(diffPair) && diffPair.length === 2) {
155+
const [_, [removed, added]] = diffPair;
156+
157+
if (removed || added) {
158+
messages.push('; diff:');
159+
messages.push(removed ? `- ${removed}` : '');
160+
if (added) {
161+
messages.push(`+ ${added}`);
162+
}
163+
}
164+
} else {
165+
console.warn('Invalid diff pair format:', diffPair);
166+
}
167+
});
168+
169+
return messages;
170+
}
171+
145172
// Return a detailed message about why a test failed.
146173
// If the test passed, return the empty string.
147174
// The message contains "comment" lines that are prepended with ;
@@ -173,6 +200,11 @@ export function detailedMessage(result: TestResult): string | undefined {
173200
if (result.actual) {
174201
messages.push(`; actual:\n${result.actual}`);
175202
}
203+
204+
const diffMessages = getDiffMessages(result);
205+
if (diffMessages.length > 0) {
206+
messages.push(...diffMessages);
207+
}
176208
}
177209
return messages.length > 0 ? messages.join('\n') : undefined;
178210
}

src/testRunner.ts

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ async function onTestResult(
139139
run.errored(assertion, new vscode.TestMessage(cider.shortMessage(result)));
140140
break;
141141
case 'fail':
142+
run.failed(assertion, new vscode.TestMessage(cider.detailedMessage(result)));
143+
break;
142144
default:
143145
run.failed(assertion, new vscode.TestMessage(cider.shortMessage(result)));
144146
break;

0 commit comments

Comments
 (0)