Skip to content

Commit 10f3063

Browse files
author
Orta
authored
Merge pull request microsoft#32421 from orta/improve_mulitline_exceptions
Make it easier to read multi-line exceptions
2 parents dc38ace + d3f3c8e commit 10f3063

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/harness/fourslash.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ namespace FourSlash {
948948

949949
const actual = checker.typeToString(type);
950950
if (actual !== expected) {
951-
this.raiseError(`Expected: '${expected}', actual: '${actual}'`);
951+
this.raiseError(displayExpectedAndActualString(expected, actual));
952952
}
953953
}
954954

@@ -1024,9 +1024,7 @@ namespace FourSlash {
10241024
private assertObjectsEqual<T>(fullActual: T, fullExpected: T, msgPrefix = ""): void {
10251025
const recur = <U>(actual: U, expected: U, path: string) => {
10261026
const fail = (msg: string) => {
1027-
this.raiseError(`${msgPrefix} At ${path}: ${msg}
1028-
Expected: ${stringify(fullExpected)}
1029-
Actual: ${stringify(fullActual)}`);
1027+
this.raiseError(`${msgPrefix} At ${path}: ${msg} ${displayExpectedAndActualString(stringify(fullExpected), stringify(fullActual))}`);
10301028
};
10311029

10321030
if ((actual === undefined) !== (expected === undefined)) {
@@ -1058,9 +1056,7 @@ Actual: ${stringify(fullActual)}`);
10581056
if (fullActual === fullExpected) {
10591057
return;
10601058
}
1061-
this.raiseError(`${msgPrefix}
1062-
Expected: ${stringify(fullExpected)}
1063-
Actual: ${stringify(fullActual)}`);
1059+
this.raiseError(`${msgPrefix} ${displayExpectedAndActualString(stringify(fullExpected), stringify(fullActual))}`);
10641060
}
10651061
recur(fullActual, fullExpected, "");
10661062

@@ -2111,9 +2107,7 @@ Actual: ${stringify(fullActual)}`);
21112107
public verifyCurrentLineContent(text: string) {
21122108
const actual = this.getCurrentLineContent();
21132109
if (actual !== text) {
2114-
throw new Error("verifyCurrentLineContent\n" +
2115-
"\tExpected: \"" + text + "\"\n" +
2116-
"\t Actual: \"" + actual + "\"");
2110+
throw new Error("verifyCurrentLineContent\n" + displayExpectedAndActualString(text, actual, /* quoted */ true));
21172111
}
21182112
}
21192113

@@ -2139,25 +2133,19 @@ Actual: ${stringify(fullActual)}`);
21392133
public verifyTextAtCaretIs(text: string) {
21402134
const actual = this.getFileContent(this.activeFile.fileName).substring(this.currentCaretPosition, this.currentCaretPosition + text.length);
21412135
if (actual !== text) {
2142-
throw new Error("verifyTextAtCaretIs\n" +
2143-
"\tExpected: \"" + text + "\"\n" +
2144-
"\t Actual: \"" + actual + "\"");
2136+
throw new Error("verifyTextAtCaretIs\n" + displayExpectedAndActualString(text, actual, /* quoted */ true));
21452137
}
21462138
}
21472139

21482140
public verifyCurrentNameOrDottedNameSpanText(text: string) {
21492141
const span = this.languageService.getNameOrDottedNameSpan(this.activeFile.fileName, this.currentCaretPosition, this.currentCaretPosition);
21502142
if (!span) {
2151-
return this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" +
2152-
"\tExpected: \"" + text + "\"\n" +
2153-
"\t Actual: undefined");
2143+
return this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" + displayExpectedAndActualString("\"" + text + "\"", "undefined"));
21542144
}
21552145

21562146
const actual = this.getFileContent(this.activeFile.fileName).substring(span.start, ts.textSpanEnd(span));
21572147
if (actual !== text) {
2158-
this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" +
2159-
"\tExpected: \"" + text + "\"\n" +
2160-
"\t Actual: \"" + actual + "\"");
2148+
this.raiseError("verifyCurrentNameOrDottedNameSpanText\n" + displayExpectedAndActualString(text, actual, /* quoted */ true));
21612149
}
21622150
}
21632151

@@ -3690,7 +3678,7 @@ ${code}
36903678
expected = makeWhitespaceVisible(expected);
36913679
actual = makeWhitespaceVisible(actual);
36923680
}
3693-
return `Expected:\n${expected}\nActual:\n${actual}`;
3681+
return displayExpectedAndActualString(expected, actual);
36943682
}
36953683

36963684
function differOnlyByWhitespace(a: string, b: string) {
@@ -3710,6 +3698,14 @@ ${code}
37103698
}
37113699
}
37123700
}
3701+
3702+
function displayExpectedAndActualString(expected: string, actual: string, quoted = false) {
3703+
const expectMsg = "\x1b[1mExpected\x1b[0m\x1b[31m";
3704+
const actualMsg = "\x1b[1mActual\x1b[0m\x1b[31m";
3705+
const expectedString = quoted ? "\"" + expected + "\"" : expected;
3706+
const actualString = quoted ? "\"" + actual + "\"" : actual;
3707+
return `\n${expectMsg}:\n${expectedString}\n\n${actualMsg}:\n${actualString}`;
3708+
}
37133709
}
37143710

37153711
namespace FourSlashInterface {

0 commit comments

Comments
 (0)