Add tests for synaxTreeScopeDescriptor

This commit is contained in:
Linus Eriksson
2018-10-18 17:49:37 +02:00
parent 0e2501d6e4
commit ad41476cbe
2 changed files with 135 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ const {clipboard} = require('electron')
const TextEditor = require('../src/text-editor')
const TextBuffer = require('text-buffer')
const TextMateLanguageMode = require('../src/text-mate-language-mode')
const TreeSitterLanguageMode = require('../src/tree-sitter-language-mode')
describe('TextEditor', () => {
let buffer, editor, lineLengths
@@ -7077,6 +7078,65 @@ describe('TextEditor', () => {
})
})
describe('.syntaxTreeScopeDescriptorForBufferPosition(position)', () => {
it('returns the result of scopeDescriptorForBufferPosition() when textmate language mode is used', async () => {
atom.config.set('core.useTreeSitterParsers', false)
editor = await atom.workspace.open('sample.js', {autoIndent: false})
await atom.packages.activatePackage('language-javascript')
let buffer = editor.getBuffer()
let languageMode = new TextMateLanguageMode({
buffer,
grammar: atom.grammars.grammarForScopeName('source.js')
})
buffer.setLanguageMode(languageMode)
languageMode.startTokenizing()
while (languageMode.firstInvalidRow() != null) {
advanceClock()
}
const syntaxTreeeScopeDescriptor = editor.syntaxTreeScopeDescriptorForBufferPosition([4, 17])
expect(syntaxTreeeScopeDescriptor.getScopesArray()).toEqual([
'source.js',
'support.variable.property.js'
])
})
it('returns the result of syntaxTreeScopeDescriptorForBufferPosition() when tree-sitter language mode is used', async () => {
editor = await atom.workspace.open('sample.js', {autoIndent: false})
await atom.packages.activatePackage('language-javascript')
let buffer = editor.getBuffer()
buffer.setLanguageMode(new TreeSitterLanguageMode({
buffer,
grammar: atom.grammars.grammarForScopeName('source.js')
}))
const syntaxTreeeScopeDescriptor = editor.syntaxTreeScopeDescriptorForBufferPosition([4, 17])
expect(syntaxTreeeScopeDescriptor.getScopesArray()).toEqual([
'source.js',
'program',
'variable_declaration',
'variable_declarator',
'function',
'statement_block',
'variable_declaration',
'variable_declarator',
'function',
'statement_block',
'while_statement',
'parenthesized_expression',
'binary_expression',
'member_expression',
'property_identifier'
])
})
})
describe('.shouldPromptToSave()', () => {
beforeEach(async () => {
editor = await atom.workspace.open('sample.js')

View File

@@ -1424,6 +1424,81 @@ describe('TreeSitterLanguageMode', () => {
})
})
describe('.syntaxTreeScopeDescriptorForPosition', () => {
it('returns a scope descriptor representing the given position in the syntax tree', async () => {
const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
scopeName: 'source.js',
parser: 'tree-sitter-javascript'
})
buffer.setText('foo({bar: baz});')
buffer.setLanguageMode(new TreeSitterLanguageMode({buffer, grammar}))
expect(editor.syntaxTreeScopeDescriptorForBufferPosition([0, 6]).getScopesArray()).toEqual([
'source.js',
'program',
'expression_statement',
'call_expression',
'arguments',
'object',
'pair',
'property_identifier'
])
})
it('includes nodes in injected syntax trees', async () => {
const jsGrammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, {
scopeName: 'source.js',
parser: 'tree-sitter-javascript',
scopes: {},
injectionRegExp: 'javascript',
injectionPoints: [HTML_TEMPLATE_LITERAL_INJECTION_POINT]
})
const htmlGrammar = new TreeSitterGrammar(atom.grammars, htmlGrammarPath, {
scopeName: 'text.html',
parser: 'tree-sitter-html',
scopes: {},
injectionRegExp: 'html',
injectionPoints: [SCRIPT_TAG_INJECTION_POINT]
})
atom.grammars.addGrammar(jsGrammar)
atom.grammars.addGrammar(htmlGrammar)
buffer.setText(`
<div>
<script>
html \`
<span>\${person.name}</span>
\`
</script>
</div>
`)
const languageMode = new TreeSitterLanguageMode({buffer, grammar: htmlGrammar, grammars: atom.grammars})
buffer.setLanguageMode(languageMode)
const position = buffer.findSync('name').start
expect(editor.syntaxTreeScopeDescriptorForBufferPosition(position).getScopesArray()).toEqual([
'text.html',
'fragment',
'element',
'raw_element',
'raw_text',
'program',
'expression_statement',
'call_expression',
'template_string',
'fragment',
'element',
'template_substitution',
'member_expression',
'property_identifier'
])
})
})
describe('.bufferRangeForScopeAtPosition(selector?, position)', () => {
describe('when selector = null', () => {
it('returns the range of the smallest node at position', async () => {