1
0
Fork 0

Refactor TextDocument tests into separate classes

pull/49/head
Felix Becker 2016-10-08 14:51:49 +02:00
parent 48c71e5bc1
commit 987308fc0a
4 changed files with 195 additions and 127 deletions

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project};
use LanguageServer\Protocol\{
TextDocumentIdentifier,
TextDocumentItem,
VersionedTextDocumentIdentifier,
TextDocumentContentChangeEvent,
Range,
Position
};
class DidChangeTest extends TestCase
{
public function test()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
$phpDocument = $project->getDocument('whatever');
$phpDocument->updateContent("<?php\necho 'Hello, World'\n");
$identifier = new VersionedTextDocumentIdentifier('whatever');
$changeEvent = new TextDocumentContentChangeEvent();
$changeEvent->range = new Range(new Position(0, 0), new Position(9999, 9999));
$changeEvent->rangeLength = 9999;
$changeEvent->text = "<?php\necho 'Goodbye, World'\n";
$textDocument->didChange($identifier, [$changeEvent]);
$this->assertEquals("<?php\necho 'Goodbye, World'\n", $phpDocument->getContent());
}
}

View File

@ -1,36 +1,38 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server;
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project, PhpDocument};
use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions, VersionedTextDocumentIdentifier, TextDocumentContentChangeEvent, Range, Position};
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody};
use LanguageServer\{Server, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, SymbolKind};
class TextDocumentTest extends TestCase
class DocumentSymbolTest extends TestCase
{
public function testDocumentSymbol()
/**
* @var Server\TextDocument
*/
private $textDocument;
public function setUp()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/symbols.php');
$textDocument->didOpen($textDocumentItem);
$this->textDocument = new Server\TextDocument($project, $client);
$project->getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php'));
}
public function test()
{
// Request symbols
$result = $textDocument->documentSymbol(new TextDocumentIdentifier('whatever'));
$result = $this->textDocument->documentSymbol(new TextDocumentIdentifier('symbols'));
$this->assertEquals([
[
'name' => 'TestNamespace',
'kind' => SymbolKind::NAMESPACE,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 2,
@ -48,7 +50,7 @@ class TextDocumentTest extends TestCase
'name' => 'TEST_CONST',
'kind' => SymbolKind::CONSTANT,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 4,
@ -66,7 +68,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestClass',
'kind' => SymbolKind::CLASS_,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 6,
@ -84,7 +86,7 @@ class TextDocumentTest extends TestCase
'name' => 'TEST_CLASS_CONST',
'kind' => SymbolKind::CONSTANT,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 8,
@ -102,7 +104,7 @@ class TextDocumentTest extends TestCase
'name' => 'staticTestProperty',
'kind' => SymbolKind::PROPERTY,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 9,
@ -120,7 +122,7 @@ class TextDocumentTest extends TestCase
'name' => 'testProperty',
'kind' => SymbolKind::PROPERTY,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 10,
@ -138,7 +140,7 @@ class TextDocumentTest extends TestCase
'name' => 'staticTestMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 12,
@ -156,7 +158,7 @@ class TextDocumentTest extends TestCase
'name' => 'testMethod',
'kind' => SymbolKind::METHOD,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 17,
@ -174,7 +176,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestTrait',
'kind' => SymbolKind::CLASS_,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 23,
@ -192,7 +194,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestInterface',
'kind' => SymbolKind::INTERFACE,
'location' => [
'uri' => 'whatever',
'uri' => 'symbols',
'range' => [
'start' => [
'line' => 28,
@ -208,106 +210,4 @@ class TextDocumentTest extends TestCase
]
], json_decode(json_encode($result), true));
}
public function testParseErrorsArePublishedAsDiagnostics()
{
$args = null;
$client = new LanguageClient(new MockProtocolStream());
$client->textDocument = new class($args) extends Client\TextDocument {
private $args;
public function __construct(&$args)
{
parent::__construct(new MockProtocolStream());
$this->args = &$args;
}
public function publishDiagnostics(string $uri, array $diagnostics)
{
$this->args = func_get_args();
}
};
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/invalid_file.php');
$textDocument->didOpen($textDocumentItem);
$this->assertEquals([
'whatever',
[[
'range' => [
'start' => [
'line' => 2,
'character' => 10
],
'end' => [
'line' => 2,
'character' => 15
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "Syntax error, unexpected T_CLASS, expecting T_STRING"
]]
], json_decode(json_encode($args), true));
}
public function testFormatting()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/format.php');
$textDocument->didOpen($textDocumentItem);
// how code should look after formatting
$expected = file_get_contents(__DIR__ . '/../../fixtures/format_expected.php');
// Request formatting
$result = $textDocument->formatting(new TextDocumentIdentifier('whatever'), new FormattingOptions());
$this->assertEquals([0 => [
'range' => [
'start' => [
'line' => 0,
'character' => 0
],
'end' => [
'line' => PHP_INT_MAX,
'character' => PHP_INT_MAX
]
],
'newText' => $expected
]], json_decode(json_encode($result), true));
}
public function testDidChange()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
$phpDocument = $project->getDocument('whatever');
$phpDocument->updateContent("<?php\necho 'Hello, World'\n");
$identifier = new VersionedTextDocumentIdentifier('whatever');
$changeEvent = new TextDocumentContentChangeEvent();
$changeEvent->range = new Range(new Position(0,0), new Position(9999,9999));
$changeEvent->rangeLength = 9999;
$changeEvent->text = "<?php\necho 'Goodbye, World'\n";
$textDocument->didChange($identifier, [$changeEvent]);
$this->assertEquals("<?php\necho 'Goodbye, World'\n", $phpDocument->getContent());
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, TextDocumentItem, FormattingOptions};
class FormattingTest extends TestCase
{
/**
* @var Server\TextDocument
*/
private $textDocument;
public function setUp()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client);
}
public function test()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../../fixtures/format.php');
$textDocument->didOpen($textDocumentItem);
// how code should look after formatting
$expected = file_get_contents(__DIR__ . '/../../../fixtures/format_expected.php');
// Request formatting
$result = $textDocument->formatting(new TextDocumentIdentifier('whatever'), new FormattingOptions());
$this->assertEquals([0 => [
'range' => [
'start' => [
'line' => 0,
'character' => 0
],
'end' => [
'line' => PHP_INT_MAX,
'character' => PHP_INT_MAX
]
],
'newText' => $expected
]], json_decode(json_encode($result), true));
}
}

View File

@ -0,0 +1,73 @@
<?php
declare(strict_types = 1);
namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentIdentifier, TextDocumentItem, DiagnosticSeverity};
class ParseErrorsTest extends TestCase
{
/**
* @var Server\TextDocument
*/
private $textDocument;
public function setUp()
{
$client = new LanguageClient(new MockProtocolStream());
$project = new Project($client);
$this->textDocument = new Server\TextDocument($project, $client);
}
public function testParseErrorsArePublishedAsDiagnostics()
{
$args = null;
$client = new LanguageClient(new MockProtocolStream());
$client->textDocument = new class($args) extends Client\TextDocument {
private $args;
public function __construct(&$args)
{
parent::__construct(new MockProtocolStream());
$this->args = &$args;
}
public function publishDiagnostics(string $uri, array $diagnostics)
{
$this->args = func_get_args();
}
};
$project = new Project($client);
$textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source
$textDocumentItem = new TextDocumentItem();
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php';
$textDocumentItem->version = 1;
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../../fixtures/invalid_file.php');
$textDocument->didOpen($textDocumentItem);
$this->assertEquals([
'whatever',
[[
'range' => [
'start' => [
'line' => 2,
'character' => 10
],
'end' => [
'line' => 2,
'character' => 15
]
],
'severity' => DiagnosticSeverity::ERROR,
'code' => null,
'source' => 'php',
'message' => "Syntax error, unexpected T_CLASS, expecting T_STRING"
]]
], json_decode(json_encode($args), true));
}
}