-
-
Notifications
You must be signed in to change notification settings - Fork 88
start playing with hat tests #1815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
26ae40a
a88f79d
53a07b8
c377196
6ca0319
f4bd698
250b404
98ade7e
4b4f894
4b3186b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# do not modify line endings of our hat test golden files | ||
packages/cursorless-engine/src/test/fixtures/hat-stats/*.golden -crlf | ||
packages/cursorless-engine/src/test/fixtures/hat-stats/*.stats |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as assert from "assert"; | ||
import { MockTextDocument, Range } from ".."; | ||
import * as fc from "fast-check"; | ||
|
||
suite("mockEditor", () => { | ||
test("basic", () => { | ||
const s = "abc\n\n123\n"; | ||
const doc: MockTextDocument = new MockTextDocument( | ||
"test.txt", | ||
"plaintext", | ||
s, | ||
); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
const pos = doc.positionAt(i); | ||
const offset = doc.offsetAt(pos); | ||
assert.equal(offset, i); | ||
} | ||
const line0 = doc.lineAt(0); | ||
assert.equal(line0.text, "abc"); | ||
assert.equal(line0.firstNonWhitespaceCharacterIndex, 0); | ||
assert.equal(line0.isEmptyOrWhitespace, false); | ||
assert.equal(line0.lineNumber, 0); | ||
assert.ok(line0.range.isEqual(new Range(0, 0, 0, 3))); | ||
assert.equal(line0.rangeIncludingLineBreak.start.character, 0); | ||
assert.equal(line0.lastNonWhitespaceCharacterIndex, 2); | ||
|
||
const line1 = doc.lineAt(1); | ||
assert.equal(line1.text, ""); | ||
assert.equal(line1.firstNonWhitespaceCharacterIndex, 0); | ||
assert.equal(line1.isEmptyOrWhitespace, true); | ||
assert.equal(line1.lineNumber, 1); | ||
assert.ok(line1.range.isEqual(new Range(1, 0, 1, 0))); | ||
assert.equal(line1.rangeIncludingLineBreak.start.character, 0); | ||
assert.equal(line1.lastNonWhitespaceCharacterIndex, 0); | ||
}); | ||
|
||
test("fastcheck", () => { | ||
fc.assert( | ||
fc.property(fc.string(), (contents) => { | ||
const doc: MockTextDocument = new MockTextDocument( | ||
"test.txt", | ||
"plaintext", | ||
contents, | ||
); | ||
let tot: number = 0; | ||
for (let lineno = 0; lineno < doc.lineCount; lineno++) { | ||
const line = doc.lineAt(lineno); | ||
tot += line.rangeIncludingLineBreak.end.character; | ||
assert.equal(line.lineNumber, lineno); | ||
assert.equal(line.range.start.line, lineno); | ||
assert.equal(line.range.end.line, lineno); | ||
assert.equal(line.rangeIncludingLineBreak.start.line, lineno); | ||
assert.equal(line.rangeIncludingLineBreak.end.line, lineno); | ||
assert.equal( | ||
line.rangeIncludingLineBreak.end.character, | ||
line.text.length, | ||
); | ||
assert.equal( | ||
line.rangeIncludingLineBreak.end.character, | ||
line.range.end.character, | ||
); | ||
} | ||
assert.equal(tot, contents.length); | ||
|
||
for (let i = 0; i < contents.length; i++) { | ||
const pos = doc.positionAt(i); | ||
// positions must be within the range of a line | ||
assert.ok(pos.character <= doc.lineAt(pos.line).range.end.character); | ||
const offset = doc.offsetAt(pos); | ||
// positionAt and offsetAt are inverses | ||
assert.equal(offset, i); | ||
return true; | ||
} | ||
}), | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wow ok. We were planning to just use https://github.com/microsoft/vscode-languageserver-node as we'll need that for lsp anyway 😅. Looks like your code goes a bit further, as they stop at text document and you go all the way to editor. But would be good to use as much of their code as possible as we'll be pulling that in anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pokey I went to pull this in and hit a speed bump. Their TextDocument type implements a smaller interface than the cursorless TextDocument interface:
Range and eol are trivial, but unfortunately, lineAt returns a non-trivial TextLine interface, so there's a meaningful amount of code missing. I could try to (a) shrink our interface or (b) build our mock document as a wrapper around theirs, harder because of the TextLine thing, or (c) just continue with our own top-to-bottom mock. Or I guess (d) try to upstream code to make theirs match our needs. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd lean towards a), falling back to b) as necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sigh. Plan B isn't promising either. We still need our implementation of TextLine, and re-implementing the rest in terms of their interface is janky. Not done, but the diff-stat isn't overwhelming:
I think we should either (a) have you take a crack at this or (b) discuss again at a meet-up. Maybe there's something clever we can do that I'm missing...but these two interfaces don't line up nicely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this discussion to #1882 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { URI } from "vscode-uri"; | ||
import { | ||
EndOfLine, | ||
Position, | ||
Range, | ||
Selection, | ||
TextDocument, | ||
TextEditor, | ||
TextEditorOptions, | ||
TextLine, | ||
} from ".."; | ||
|
||
// See the TextLine, TextEditor, and TextDocument interfaces | ||
// for documentation of these classes and their fields. | ||
|
||
export class MockTextLine implements TextLine { | ||
readonly lineNumber: number; | ||
readonly text: string; | ||
readonly range: Range; | ||
readonly rangeIncludingLineBreak: Range; | ||
readonly firstNonWhitespaceCharacterIndex: number; | ||
readonly lastNonWhitespaceCharacterIndex: number; | ||
readonly isEmptyOrWhitespace: boolean; | ||
|
||
constructor(lineNumber: number, text: string) { | ||
if (lineNumber < 0) { | ||
throw new Error("lineNumber must be non-negative"); | ||
} | ||
this.lineNumber = lineNumber; | ||
// capture any trailing \r\n or \n as eol (possibly neither is present) | ||
const eol = text.match(/(\r?\n)$/)?.[1] ?? ""; | ||
if (eol.length > 0) { | ||
this.text = text.slice(0, -eol.length); | ||
} else { | ||
this.text = text; | ||
} | ||
this.range = new Range( | ||
this.lineNumber, | ||
0, | ||
this.lineNumber, | ||
this.text.length, | ||
); | ||
this.rangeIncludingLineBreak = new Range( | ||
this.lineNumber, | ||
0, | ||
this.lineNumber, | ||
this.text.length + eol.length, | ||
); | ||
const first = this.text.search(/\S/); | ||
this.firstNonWhitespaceCharacterIndex = | ||
first === -1 ? this.text.length : first; | ||
const all = this.text.match(/\S/g); | ||
this.lastNonWhitespaceCharacterIndex = all | ||
? this.text.lastIndexOf(all[all.length - 1]) | ||
: 0; | ||
this.isEmptyOrWhitespace = | ||
this.firstNonWhitespaceCharacterIndex === this.text.length; | ||
} | ||
} | ||
|
||
export class MockTextDocument implements TextDocument { | ||
readonly uri: URI; | ||
readonly languageId: string; | ||
readonly version: number; | ||
readonly range: Range; | ||
readonly eol: EndOfLine; | ||
private lines: MockTextLine[]; | ||
private contents: string; | ||
|
||
constructor(filename: string, languageId: string, contents: string) { | ||
this.uri = URI.file(filename); | ||
this.languageId = languageId; | ||
this.version = 1; | ||
this.contents = contents; | ||
const rawLines: string[] = contents.match(/[^\n]*\n|[^\n]+/g) ?? []; | ||
this.lines = rawLines.map((line, i) => { | ||
return new MockTextLine(i, line); | ||
}); | ||
if (this.lines.length === 0) { | ||
this.range = new Range(0, 0, 0, 0); | ||
} else { | ||
const lastLineRange = this.lines[this.lines.length - 1].range; | ||
this.range = new Range( | ||
0, | ||
0, | ||
lastLineRange.end.line, | ||
lastLineRange.end.character, | ||
); | ||
} | ||
this.eol = "LF"; | ||
} | ||
|
||
get lineCount(): number { | ||
return this.lines.length; | ||
} | ||
|
||
public lineAt(x: number | Position): TextLine { | ||
if (typeof x === "number") { | ||
return this.lines[x]; | ||
} | ||
return this.lines[x.line]; | ||
} | ||
|
||
public offsetAt(position: Position): number { | ||
let offset = 0; | ||
for (let i = 0; i < position.line; i++) { | ||
offset += this.lineAt(i).rangeIncludingLineBreak.end.character; | ||
} | ||
offset += position.character; | ||
return offset; | ||
} | ||
|
||
public positionAt(offset: number): Position { | ||
let line = 0; | ||
while (offset >= this.lineAt(line).rangeIncludingLineBreak.end.character) { | ||
offset -= this.lineAt(line).rangeIncludingLineBreak.end.character; | ||
line++; | ||
} | ||
return new Position(line, offset); | ||
} | ||
|
||
public getText(range?: Range): string { | ||
if (range === undefined) { | ||
return this.contents; | ||
} | ||
const startOffset = this.offsetAt(range.start); | ||
const endOffset = this.offsetAt(range.end); | ||
return this.contents.slice(startOffset, endOffset); | ||
} | ||
} | ||
|
||
export class MockTextEditor implements TextEditor { | ||
public primarySelection: Selection; | ||
readonly id: string; | ||
readonly document: TextDocument; | ||
readonly options: TextEditorOptions; | ||
readonly isActive: boolean; | ||
|
||
constructor(document: TextDocument, active: boolean) { | ||
this.id = document.uri.toString(); | ||
this.document = document; | ||
this.primarySelection = new Selection(0, 0, 0, 0); | ||
this.options = new DefaultTextEditorOptions(); | ||
this.isActive = active; | ||
// TODO: support visible ranges, multiple selections, options | ||
} | ||
|
||
get visibleRanges(): Range[] { | ||
return [this.document.range]; | ||
} | ||
|
||
get selections(): Selection[] { | ||
return [this.primarySelection]; | ||
} | ||
|
||
isEqual(other: TextEditor): boolean { | ||
return this.id === other.id; | ||
} | ||
} | ||
|
||
class DefaultTextEditorOptions implements TextEditorOptions { | ||
get tabSize(): number | string { | ||
return 4; | ||
} | ||
|
||
get insertSpaces(): boolean | string { | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the attribute is called
eol=lf
, or-text
for "don't mangle this at all"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no not this again 😅. What's the issue you're trying to solve here @josharian?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s solved now, thanks to @auscompgeek .
The issue was that git was adding CR to the golden files on checkout, so the test that the golden files were unmodified was failing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to avoid gitattributes if at all possible. If people want to check things out as CRLF, I don't see why we should get in the way of that. The last time we ran into this problem, we solved it by normalizing when we read the gold file. See
cursorless/packages/cursorless-vscode-e2e/src/suite/testCaseRecorder.vscode.test.ts
Lines 169 to 171 in 5789094
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. If you look at the commit history, I’ve also implemented that, when you gitattributes originally did not work. Although to avoid needless diffs, we also have to normalize at writing time as well…sigh.