1
0
Fork 0

Add tests for xglob/xcontent mode

pull/136/head
Felix Becker 2016-11-07 13:16:14 +01:00
parent 745fec4d92
commit 898349b69f
5 changed files with 71 additions and 10 deletions

View File

@ -4,7 +4,7 @@ declare(strict_types = 1);
namespace LanguageServer\Client; namespace LanguageServer\Client;
use LanguageServer\ClientHandler; use LanguageServer\ClientHandler;
use LanguageServer\Protocol\{Message, Content}; use LanguageServer\Protocol\{Message, Content, TextDocumentIdentifier};
use Sabre\Event\Promise; use Sabre\Event\Promise;
use JsonMapper; use JsonMapper;

View File

@ -191,7 +191,7 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
} catch (Exception $e) { } catch (Exception $e) {
$this->client->window->logMessage( $this->client->window->logMessage(
MessageType::ERROR, MessageType::ERROR,
"Error parsing file $shortName: " . (string)$e "Error parsing file {$textDocument->uri}: " . (string)$e
); );
} }
}); });

View File

@ -10,4 +10,12 @@ class Content
* @var string * @var string
*/ */
public $text; public $text;
/**
* @param string $text The content of the text document
*/
public function __construct(string $text = null)
{
$this->text = $text;
}
} }

View File

@ -5,9 +5,12 @@ namespace LanguageServer\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use LanguageServer\LanguageServer; use LanguageServer\LanguageServer;
use LanguageServer\Protocol\{Message, ClientCapabilities, TextDocumentSyncKind, MessageType}; use LanguageServer\Protocol\{Message, ClientCapabilities, TextDocumentSyncKind, MessageType, Content};
use AdvancedJsonRpc; use AdvancedJsonRpc;
use Webmozart\Glob\Glob;
use Webmozart\PathUtil\Path;
use Sabre\Event\Promise; use Sabre\Event\Promise;
use Exception;
use function LanguageServer\pathToUri; use function LanguageServer\pathToUri;
class LanguageServerTest extends TestCase class LanguageServerTest extends TestCase
@ -17,15 +20,14 @@ class LanguageServerTest extends TestCase
$reader = new MockProtocolStream(); $reader = new MockProtocolStream();
$writer = new MockProtocolStream(); $writer = new MockProtocolStream();
$server = new LanguageServer($reader, $writer); $server = new LanguageServer($reader, $writer);
$msg = null; $promise = new Promise;
$writer->on('message', function (Message $message) use (&$msg) { $writer->once('message', [$promise, 'fulfill']);
$msg = $message;
});
$reader->write(new Message(new AdvancedJsonRpc\Request(1, 'initialize', [ $reader->write(new Message(new AdvancedJsonRpc\Request(1, 'initialize', [
'rootPath' => __DIR__, 'rootPath' => __DIR__,
'processId' => getmypid(), 'processId' => getmypid(),
'capabilities' => new ClientCapabilities() 'capabilities' => new ClientCapabilities()
]))); ])));
$msg = $promise->wait();
$this->assertNotNull($msg, 'message event should be emitted'); $this->assertNotNull($msg, 'message event should be emitted');
$this->assertInstanceOf(AdvancedJsonRpc\SuccessResponse::class, $msg->body); $this->assertInstanceOf(AdvancedJsonRpc\SuccessResponse::class, $msg->body);
$this->assertEquals((object)[ $this->assertEquals((object)[
@ -49,7 +51,7 @@ class LanguageServerTest extends TestCase
], $msg->body->result); ], $msg->body->result);
} }
public function testIndexing() public function testIndexingWithDirectFileAccess()
{ {
$promise = new Promise; $promise = new Promise;
$input = new MockProtocolStream; $input = new MockProtocolStream;
@ -68,4 +70,53 @@ class LanguageServerTest extends TestCase
$server->initialize(getmypid(), $capabilities, realpath(__DIR__ . '/../fixtures')); $server->initialize(getmypid(), $capabilities, realpath(__DIR__ . '/../fixtures'));
$promise->wait(); $promise->wait();
} }
public function testIndexingWithGlobAndContentRequests()
{
$promise = new Promise;
$globCalled = false;
$contentCalled = false;
$rootPath = realpath(__DIR__ . '/../fixtures');
$input = new MockProtocolStream;
$output = new MockProtocolStream;
$output->on('message', function (Message $msg) use ($promise, $input, $rootPath, &$globCalled, &$contentCalled) {
if ($msg->body->method === 'textDocument/xcontent') {
// Document content requested
$contentCalled = true;
$input->write(new Message(new AdvancedJsonRpc\SuccessResponse(
$msg->body->id,
new Content(file_get_contents($msg->body->params->textDocument->uri))
)));
} else if ($msg->body->method === 'workspace/xglob') {
// Glob requested
$globCalled = true;
$files = array_map(
'\\LanguageServer\\pathToUri',
array_merge(...array_map(function (string $pattern) use ($rootPath) {
return Glob::glob(Path::makeAbsolute($pattern, $rootPath));
}, $msg->body->params->patterns))
);
$input->write(new Message(new AdvancedJsonRpc\SuccessResponse($msg->body->id, $files)));
} else if ($msg->body->method === 'window/logMessage') {
// Message logged
if ($msg->body->params->type === MessageType::ERROR) {
// Error happened during indexing, fail test
if ($promise->state === Promise::PENDING) {
$promise->reject(new Exception($msg->body->params->message));
}
} else if (strpos($msg->body->params->message, 'All 10 PHP files parsed') !== false) {
// Indexing finished
$promise->fulfill();
}
}
});
$server = new LanguageServer($input, $output);
$capabilities = new ClientCapabilities;
$capabilities->xglobProvider = true;
$capabilities->xcontentProvider = true;
$server->initialize(getmypid(), $capabilities, $rootPath);
$promise->wait();
$this->assertTrue($globCalled);
$this->assertTrue($contentCalled);
}
} }

View File

@ -5,7 +5,7 @@ namespace LanguageServer\Tests;
use LanguageServer\{ProtocolReader, ProtocolWriter}; use LanguageServer\{ProtocolReader, ProtocolWriter};
use LanguageServer\Protocol\Message; use LanguageServer\Protocol\Message;
use Sabre\Event\{Emitter, Promise}; use Sabre\Event\{Loop, Emitter, Promise};
/** /**
* A fake duplex protocol stream * A fake duplex protocol stream
@ -20,7 +20,9 @@ class MockProtocolStream extends Emitter implements ProtocolReader, ProtocolWrit
*/ */
public function write(Message $msg): Promise public function write(Message $msg): Promise
{ {
$this->emit('message', [Message::parse((string)$msg)]); Loop\nextTick(function () use ($msg) {
$this->emit('message', [Message::parse((string)$msg)]);
});
return Promise\resolve(null); return Promise\resolve(null);
} }
} }