Skip to content

Commit 8e8d3a1

Browse files
committed
property based testing for the win
catching all my bugs...
1 parent 238a1a7 commit 8e8d3a1

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

Diff for: packages/common/src/testUtil/mockEditor.test.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,33 @@ suite("mockEditor", () => {
4343
"plaintext",
4444
contents,
4545
);
46+
let tot: number = 0;
47+
for (let lineno = 0; lineno < doc.lineCount; lineno++) {
48+
const line = doc.lineAt(lineno);
49+
tot += line.rangeIncludingLineBreak.end.character;
50+
assert.equal(line.lineNumber, lineno);
51+
assert.equal(line.range.start.line, lineno);
52+
assert.equal(line.range.end.line, lineno);
53+
assert.equal(line.rangeIncludingLineBreak.start.line, lineno);
54+
assert.equal(line.rangeIncludingLineBreak.end.line, lineno);
55+
assert.equal(
56+
line.rangeIncludingLineBreak.end.character,
57+
line.text.length,
58+
);
59+
assert.equal(
60+
line.rangeIncludingLineBreak.end.character,
61+
line.range.end.character,
62+
);
63+
}
64+
assert.equal(tot, contents.length);
65+
4666
for (let i = 0; i < contents.length; i++) {
4767
const pos = doc.positionAt(i);
4868
// positions must be within the range of a line
49-
if (pos.character > doc.lineAt(pos.line).range.end.character) {
50-
return false;
51-
}
69+
assert.ok(pos.character <= doc.lineAt(pos.line).range.end.character);
5270
const offset = doc.offsetAt(pos);
5371
// positionAt and offsetAt are inverses
54-
if (offset !== i) {
55-
return false;
56-
}
72+
assert.equal(offset, i);
5773
return true;
5874
}
5975
}),

Diff for: packages/common/src/testUtil/mockEditor.ts

+21-16
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ import {
1616
export class MockTextLine implements TextLine {
1717
readonly lineNumber: number;
1818
readonly text: string;
19-
private eol: string;
2019
readonly range: Range;
2120
readonly rangeIncludingLineBreak: Range;
2221
readonly firstNonWhitespaceCharacterIndex: number;
2322
readonly lastNonWhitespaceCharacterIndex: number;
2423
readonly isEmptyOrWhitespace: boolean;
2524

26-
constructor(lineNumber: number, text: string, eol: string) {
25+
constructor(lineNumber: number, text: string) {
2726
if (lineNumber < 0) {
2827
throw new Error("lineNumber must be non-negative");
2928
}
3029
this.lineNumber = lineNumber;
31-
this.text = text;
32-
if (eol !== "\n" && eol !== "\r\n") {
33-
throw new Error("eol must be \\n or \\r\\n");
30+
// capture any trailing \r\n or \n as eol (possibly neither is present)
31+
const eol = text.match(/(\r?\n)$/)?.[1] ?? "";
32+
if (eol.length > 0) {
33+
this.text = text.slice(0, -eol.length);
34+
} else {
35+
this.text = text;
3436
}
35-
this.eol = eol;
3637
this.range = new Range(
3738
this.lineNumber,
3839
0,
@@ -43,7 +44,7 @@ export class MockTextLine implements TextLine {
4344
this.lineNumber,
4445
0,
4546
this.lineNumber,
46-
this.text.length + this.eol.length,
47+
this.text.length + eol.length,
4748
);
4849
const first = this.text.search(/\S/);
4950
this.firstNonWhitespaceCharacterIndex =
@@ -74,17 +75,21 @@ export class MockTextDocument implements TextDocument {
7475
throw new Error("InMemoryTextDocument does not support CRLF (yet?)");
7576
}
7677
this.contents = contents;
77-
const rawLines = contents.split("\n");
78+
const rawLines: string[] = contents.match(/[^\n]*\n|[^\n]+/g) ?? [];
7879
this.lines = rawLines.map((line, i) => {
79-
return new MockTextLine(i, line, "\n");
80+
return new MockTextLine(i, line);
8081
});
81-
const lastLineRange = this.lines[this.lines.length - 1].range;
82-
this.range = new Range(
83-
0,
84-
0,
85-
lastLineRange.end.line,
86-
lastLineRange.end.character,
87-
);
82+
if (this.lines.length === 0) {
83+
this.range = new Range(0, 0, 0, 0);
84+
} else {
85+
const lastLineRange = this.lines[this.lines.length - 1].range;
86+
this.range = new Range(
87+
0,
88+
0,
89+
lastLineRange.end.line,
90+
lastLineRange.end.character,
91+
);
92+
}
8893
this.eol = "LF";
8994
}
9095

0 commit comments

Comments
 (0)