Skip to content

Commit

Permalink
Add env unit tests on \\end{ suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Oct 3, 2024
1 parent 05a4be3 commit e27630d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
58 changes: 56 additions & 2 deletions test/units/17_completion_environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode'
import * as path from 'path'
import * as sinon from 'sinon'
import { lw } from '../../src/lw'
import { assert, get, mock, set } from './utils'
import { assert, get, mock, set, TextEditor } from './utils'
import { provider } from '../../src/completion/completer/environment'
import { provider as macro } from '../../src/completion/completer/macro'

Expand Down Expand Up @@ -45,7 +45,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should provide environments in the form of macros', () => {
const labels = macro
const labels = provider
.from(['', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
Expand All @@ -57,6 +57,60 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
assert.ok(labels.includes('document'))
})

it('should provide environment snippet when the cursor is `\\begin{|`', () => {
const stub = mock.activeTextEditor(texPath, '\\begin{')
const suggestion = provider
.from(['\\begin{', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
line: '\\begin{',
position: new vscode.Position(0, 7),
})
.find(s => s.label === 'itemize')
stub.restore()

assert.ok(suggestion)
assert.ok(suggestion.insertText instanceof vscode.SnippetString)
assert.ok(suggestion.insertText.value.startsWith('itemize}\n'), suggestion.insertText.value)
})

it('should provide environment name when the cursor is `\\begin{|}\\end{|}`', () => {
const editor = new TextEditor(texPath, '\\begin{}\\end{}', {})
editor.setSelections([
new vscode.Selection(new vscode.Position(0, 7), new vscode.Position(0, 7)),
new vscode.Selection(new vscode.Position(0, 13), new vscode.Position(0, 13))
])
const stub = sinon.stub(vscode.window, 'activeTextEditor').value(editor)
const suggestion = provider
.from(['\\begin{', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
line: '\\begin{',
position: new vscode.Position(0, 7),
})
.find(s => s.label === 'itemize')
stub.restore()

assert.ok(suggestion)
assert.strictEqual(suggestion.insertText, undefined)
})

it('should provide environment name when the cursor is `\\end{|`', () => {
const stub = mock.activeTextEditor(texPath, '\\begin{itemize}\\end{')
const suggestion = provider
.from(['\\end{', ''], {
uri: vscode.Uri.file(texPath),
langId: 'latex',
line: '\\begin{itemize}\\end{',
position: new vscode.Position(0, 20),
})
.find(s => s.label === 'itemize')
stub.restore()

assert.ok(suggestion)
assert.strictEqual(suggestion.insertText, undefined)
})

it('should provide environments defined in packages', async () => {
assert.ok(!getEnvs().includes('algorithm'))

Expand Down
6 changes: 5 additions & 1 deletion test/units/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export class TextDocument implements vscode.TextDocument {
validatePosition(_: vscode.Position): vscode.Position { throw new Error('Not implemented.') }
}

class TextEditor implements vscode.TextEditor {
export class TextEditor implements vscode.TextEditor {
document: TextDocument
selection: vscode.Selection = new vscode.Selection(new vscode.Position(0, 0), new vscode.Position(0, 0))
selections: vscode.Selection[] = [ this.selection ]
Expand All @@ -281,6 +281,10 @@ class TextEditor implements vscode.TextEditor {
}
}

setSelections(selections: vscode.Selection[]) {
this.selection = selections[0]
this.selections = selections
}
edit(_: (_: vscode.TextEditorEdit) => void): Thenable<boolean> { throw new Error('Not implemented.') }
insertSnippet(_: vscode.SnippetString): Thenable<boolean> { throw new Error('Not implemented.') }
setDecorations(_d: vscode.TextEditorDecorationType, _r: vscode.Range[] | vscode.DecorationOptions[]): void { throw new Error('Not implemented.') }
Expand Down

0 comments on commit e27630d

Please sign in to comment.