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 <?php
declare(strict_types = 1); declare(strict_types = 1);
namespace LanguageServer\Tests\Server; namespace LanguageServer\Tests\Server\TextDocument;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\Tests\MockProtocolStream; use LanguageServer\Tests\MockProtocolStream;
use LanguageServer\{Server, Client, LanguageClient, Project, PhpDocument}; use LanguageServer\{Server, LanguageClient, Project};
use LanguageServer\Protocol\{TextDocumentItem, TextDocumentIdentifier, SymbolKind, DiagnosticSeverity, FormattingOptions, VersionedTextDocumentIdentifier, TextDocumentContentChangeEvent, Range, Position}; use LanguageServer\Protocol\{TextDocumentIdentifier, SymbolKind};
use AdvancedJsonRpc\{Request as RequestBody, Response as ResponseBody};
class TextDocumentTest extends TestCase class DocumentSymbolTest extends TestCase
{ {
public function testDocumentSymbol() /**
* @var Server\TextDocument
*/
private $textDocument;
public function setUp()
{ {
$client = new LanguageClient(new MockProtocolStream()); $client = new LanguageClient(new MockProtocolStream());
$project = new Project($client); $project = new Project($client);
$textDocument = new Server\TextDocument($project, $client); $this->textDocument = new Server\TextDocument($project, $client);
// Trigger parsing of source $project->getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php'));
$textDocumentItem = new TextDocumentItem(); }
$textDocumentItem->uri = 'whatever';
$textDocumentItem->languageId = 'php'; public function test()
$textDocumentItem->version = 1; {
$textDocumentItem->text = file_get_contents(__DIR__ . '/../../fixtures/symbols.php');
$textDocument->didOpen($textDocumentItem);
// Request symbols // Request symbols
$result = $textDocument->documentSymbol(new TextDocumentIdentifier('whatever')); $result = $this->textDocument->documentSymbol(new TextDocumentIdentifier('symbols'));
$this->assertEquals([ $this->assertEquals([
[ [
'name' => 'TestNamespace', 'name' => 'TestNamespace',
'kind' => SymbolKind::NAMESPACE, 'kind' => SymbolKind::NAMESPACE,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 2, 'line' => 2,
@ -48,7 +50,7 @@ class TextDocumentTest extends TestCase
'name' => 'TEST_CONST', 'name' => 'TEST_CONST',
'kind' => SymbolKind::CONSTANT, 'kind' => SymbolKind::CONSTANT,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 4, 'line' => 4,
@ -66,7 +68,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestClass', 'name' => 'TestClass',
'kind' => SymbolKind::CLASS_, 'kind' => SymbolKind::CLASS_,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 6, 'line' => 6,
@ -84,7 +86,7 @@ class TextDocumentTest extends TestCase
'name' => 'TEST_CLASS_CONST', 'name' => 'TEST_CLASS_CONST',
'kind' => SymbolKind::CONSTANT, 'kind' => SymbolKind::CONSTANT,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 8, 'line' => 8,
@ -102,7 +104,7 @@ class TextDocumentTest extends TestCase
'name' => 'staticTestProperty', 'name' => 'staticTestProperty',
'kind' => SymbolKind::PROPERTY, 'kind' => SymbolKind::PROPERTY,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 9, 'line' => 9,
@ -120,7 +122,7 @@ class TextDocumentTest extends TestCase
'name' => 'testProperty', 'name' => 'testProperty',
'kind' => SymbolKind::PROPERTY, 'kind' => SymbolKind::PROPERTY,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 10, 'line' => 10,
@ -138,7 +140,7 @@ class TextDocumentTest extends TestCase
'name' => 'staticTestMethod', 'name' => 'staticTestMethod',
'kind' => SymbolKind::METHOD, 'kind' => SymbolKind::METHOD,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 12, 'line' => 12,
@ -156,7 +158,7 @@ class TextDocumentTest extends TestCase
'name' => 'testMethod', 'name' => 'testMethod',
'kind' => SymbolKind::METHOD, 'kind' => SymbolKind::METHOD,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 17, 'line' => 17,
@ -174,7 +176,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestTrait', 'name' => 'TestTrait',
'kind' => SymbolKind::CLASS_, 'kind' => SymbolKind::CLASS_,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 23, 'line' => 23,
@ -192,7 +194,7 @@ class TextDocumentTest extends TestCase
'name' => 'TestInterface', 'name' => 'TestInterface',
'kind' => SymbolKind::INTERFACE, 'kind' => SymbolKind::INTERFACE,
'location' => [ 'location' => [
'uri' => 'whatever', 'uri' => 'symbols',
'range' => [ 'range' => [
'start' => [ 'start' => [
'line' => 28, 'line' => 28,
@ -208,106 +210,4 @@ class TextDocumentTest extends TestCase
] ]
], json_decode(json_encode($result), true)); ], 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));
}
}