[lsp] some more tests and fixes for document operations

This commit is contained in:
Sven Efftinge 2016-05-24 20:20:34 +02:00
parent 31181f577c
commit 9c51ff724c
3 changed files with 175 additions and 4 deletions

View file

@ -27,15 +27,18 @@ import io.typefox.lsapi.TextDocumentContentChangeEvent
var column = 0
for (var i = 0; i < l; i++) {
val ch = contents.charAt(i)
if (position.line === line && position.character === column) {
return i
}
if (ch === NL) {
line++
column = 0
} else {
column++
}
if (position.line === line && position.character === column) {
return i
}
}
if (position.line === line && position.character === column) {
return l
}
throw new IndexOutOfBoundsException(position.toString + " text was : "+contents)
}

View file

@ -0,0 +1,113 @@
/*******************************************************************************
* Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.ide.tests.server
import io.typefox.lsapi.PositionImpl
import io.typefox.lsapi.RangeImpl
import io.typefox.lsapi.TextDocumentContentChangeEventImpl
import org.eclipse.xtext.ide.server.Document
import org.junit.Test
import static org.junit.Assert.*
/**
* @author efftinge - Initial contribution and API
*/
class DocumentTest {
@Test def void testOffSet() {
new Document(1, '''
hello world
foo
bar
''') => [
assertEquals(0, getOffSet(position(0,0)))
assertEquals(11, getOffSet(position(0,11)))
try {
getOffSet(position(0, 12))
fail()
} catch (IndexOutOfBoundsException e) {
//expected
}
assertEquals(12, getOffSet(position(1,0)))
assertEquals(13, getOffSet(position(1,1)))
assertEquals(14, getOffSet(position(1,2)))
assertEquals(16, getOffSet(position(2,0)))
assertEquals(19, getOffSet(position(2,3)))
]
}
@Test def void testOffSet_empty() {
new Document(1, "") => [
assertEquals(0, getOffSet(position(0,0)))
try {
getOffSet(position(0, 12))
fail()
} catch (IndexOutOfBoundsException e) {
//expected
}
]
}
@Test def void testUpdate_01() {
new Document(1, '''
hello world
foo
bar
''') => [
assertEquals('''
hello world
bar
'''.toString, applyChanges(#[
change(position(1,0), position(2,0), "")
]).contents)
]
}
@Test def void testUpdate_02() {
new Document(1, '''
hello world
foo
bar
''') => [
assertEquals('''
hello world
future
bar
'''.toString, applyChanges(#[
change(position(1,1), position(1,3), "uture")
]).contents)
]
}
@Test def void testUpdate_03() {
new Document(1, '''
hello world
foo
bar''') => [
assertEquals('', applyChanges(#[
change(position(0,0), position(2,3), "")
]).contents)
]
}
private def change(PositionImpl startPos, PositionImpl endPos, String newText) {
new TextDocumentContentChangeEventImpl => [
range = new RangeImpl => [
start = startPos
end = endPos
]
text = newText
]
}
private def position(int l, int c) {
new PositionImpl => [line=l character=c]
}
}

View file

@ -18,6 +18,11 @@ import io.typefox.lsapi.TextDocumentItemImpl
import org.junit.Test
import static org.junit.Assert.*
import io.typefox.lsapi.DidChangeTextDocumentParamsImpl
import io.typefox.lsapi.VersionedTextDocumentIdentifierImpl
import io.typefox.lsapi.TextDocumentContentChangeEventImpl
import io.typefox.lsapi.RangeImpl
import io.typefox.lsapi.PositionImpl
/**
* @author Sven Efftinge - Initial contribution and API
@ -25,7 +30,7 @@ import static org.junit.Assert.*
class OpenDocumentTest extends AbstractLanguageServerTest {
@Test
def void testOpenDocument() {
def void testOpenedDocumentShadowsPersistedFile() {
val firstFile = 'MyType1.testlang' -> '''
type Test {
NonExisting foo
@ -73,4 +78,54 @@ class OpenDocumentTest extends AbstractLanguageServerTest {
assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", diagnostics.get(firstFile).head.message)
}
@Test
def void testDidChange() {
val firstFile = 'MyType1.testlang' -> '''
type Test {
NonExisting foo
}
'''
languageServer.initialize(new InitializeParamsImpl => [
rootPath = root.absolutePath
])
assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", diagnostics.get(firstFile).head.message)
languageServer.didOpen(new DidOpenTextDocumentParamsImpl => [
textDocument = new TextDocumentItemImpl => [
uri = firstFile
version = 1
text = '''
type Test {
NonExisting foo
}
'''
]
])
assertEquals("Couldn't resolve reference to TypeDeclaration 'NonExisting'.", diagnostics.get(firstFile).head.message)
languageServer.didChange(new DidChangeTextDocumentParamsImpl => [
textDocument = new VersionedTextDocumentIdentifierImpl => [
uri = firstFile
version = 2
]
contentChanges = #[
new TextDocumentContentChangeEventImpl => [
range = new RangeImpl => [
start = new PositionImpl => [
line = 1
character = 4
]
end = new PositionImpl => [
line = 1
character = 15
]
]
text = "Test"
]
]
])
assertNull(diagnostics.get(firstFile).head)
}
}