Skip to content

Commit eb86e96

Browse files
committed
add fast-check package, use it, simplify code slightly
1 parent 3b63cb1 commit eb86e96

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

packages/common/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@types/lodash": "4.14.181",
2121
"@types/mocha": "^8.0.4",
2222
"@types/sinon": "^10.0.2",
23+
"fast-check": "3.12.0",
2324
"js-yaml": "^4.1.0",
2425
"mocha": "^10.2.0",
2526
"sinon": "^11.1.1"

packages/common/src/testUtil/mockEditor.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as assert from "assert";
22
import { MockTextDocument, Range } from "..";
3+
import * as fc from "fast-check";
34

45
suite("mockEditor", () => {
56
test("basic", () => {
@@ -33,4 +34,29 @@ suite("mockEditor", () => {
3334
assert.equal(line1.rangeIncludingLineBreak.start.character, 0);
3435
assert.equal(line1.lastNonWhitespaceCharacterIndex, 0);
3536
});
37+
38+
test("fastcheck", () => {
39+
fc.assert(
40+
fc.property(fc.string(), (contents) => {
41+
const doc: MockTextDocument = new MockTextDocument(
42+
"test.txt",
43+
"plaintext",
44+
contents,
45+
);
46+
for (let i = 0; i < contents.length; i++) {
47+
const pos = doc.positionAt(i);
48+
// positions must be within the range of a line
49+
if (pos.character > doc.lineAt(pos.line).range.end.character) {
50+
return false;
51+
}
52+
const offset = doc.offsetAt(pos);
53+
// positionAt and offsetAt are inverses
54+
if (offset !== i) {
55+
return false;
56+
}
57+
return true;
58+
}
59+
}),
60+
);
61+
});
3662
});

packages/common/src/testUtil/mockEditor.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ export class MockTextDocument implements TextDocument {
7878
this.lines = rawLines.map((line, i) => {
7979
return new MockTextLine(i, line, "\n");
8080
});
81+
const lastLineRange = this.lines[this.lines.length - 1].range;
8182
this.range = new Range(
8283
0,
8384
0,
84-
this.lineCount - 1,
85-
this.lineLength(this.lineCount - 1),
85+
lastLineRange.end.line,
86+
lastLineRange.end.character,
8687
);
8788
this.eol = "LF";
8889
}
@@ -91,10 +92,6 @@ export class MockTextDocument implements TextDocument {
9192
return this.lines.length;
9293
}
9394

94-
private lineLength(line: number): number {
95-
return this.lines[line].text.length + 1; // EOF
96-
}
97-
9895
public lineAt(x: number | Position): TextLine {
9996
if (typeof x === "number") {
10097
return this.lines[x];

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)