From db95eafda082a57fb2ab5c3ecfb70ad894af4961 Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sun, 9 Oct 2016 15:48:08 +0200 Subject: [PATCH] Extend symbol search tests --- src/PhpDocument.php | 5 +- tests/NodeVisitor/DefinitionCollectorTest.php | 22 +- tests/Server/Workspace/SymbolTest.php | 246 ++++++++++++++++-- 3 files changed, 249 insertions(+), 24 deletions(-) diff --git a/src/PhpDocument.php b/src/PhpDocument.php index 9979d6a..fdb4609 100644 --- a/src/PhpDocument.php +++ b/src/PhpDocument.php @@ -133,6 +133,9 @@ class PhpDocument if ($symbols === null) { return null; } + if ($query === '') { + return $symbols; + } return array_filter($symbols, function($symbol) use ($query) { return stripos($symbol->name, $query) !== false; }); @@ -316,7 +319,7 @@ class PhpDocument if ($node instanceof Node\Stmt\ClassLike && !isset($nameNode) && isset($node->name)) { // Class, interface or trait declaration return (string)$node->namespacedName; - } else if ($node instanceof Node\Stmt\Function_) { + } else if ($node instanceof Node\Stmt\Function_ && !isset($nameNode)) { // Function: use functionName() as the name return (string)$node->namespacedName . '()'; } else if ($node instanceof Node\Stmt\ClassMethod) { diff --git a/tests/NodeVisitor/DefinitionCollectorTest.php b/tests/NodeVisitor/DefinitionCollectorTest.php index cbd82f2..554ba2e 100644 --- a/tests/NodeVisitor/DefinitionCollectorTest.php +++ b/tests/NodeVisitor/DefinitionCollectorTest.php @@ -12,12 +12,12 @@ use LanguageServer\NodeVisitor\{ReferencesAdder, DefinitionCollector}; class DefinitionCollectorTest extends TestCase { - public function test() + public function testCollectsSymbols() { $client = new LanguageClient(new MockProtocolStream()); $project = new Project($client); $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - $document = new PhpDocument('whatever', $project, $client, $parser); + $document = new PhpDocument('symbols', $project, $client, $parser); $traverser = new NodeTraverser; $traverser->addVisitor(new NameResolver); $traverser->addVisitor(new ReferencesAdder($document)); @@ -49,4 +49,22 @@ class DefinitionCollectorTest extends TestCase $this->assertInstanceOf(Node\Stmt\Interface_::class, $defs['TestNamespace\\TestInterface']); $this->assertInstanceOf(Node\Stmt\Function_::class, $defs['TestNamespace\\test_function()']); } + + public function testDoesNotCollectReferences() + { + $client = new LanguageClient(new MockProtocolStream()); + $project = new Project($client); + $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + $document = new PhpDocument('references', $project, $client, $parser); + $traverser = new NodeTraverser; + $traverser->addVisitor(new NameResolver); + $traverser->addVisitor(new ReferencesAdder($document)); + $definitionCollector = new DefinitionCollector; + $traverser->addVisitor($definitionCollector); + $stmts = $parser->parse(file_get_contents(__DIR__ . '/../../fixtures/references.php')); + $traverser->traverse($stmts); + $defs = $definitionCollector->definitions; + $this->assertEquals(['TestNamespace\\whatever()'], array_keys($defs)); + $this->assertInstanceOf(Node\Stmt\Function_::class, $defs['TestNamespace\\whatever()']); + } } diff --git a/tests/Server/Workspace/SymbolTest.php b/tests/Server/Workspace/SymbolTest.php index 3cca23f..345b142 100644 --- a/tests/Server/Workspace/SymbolTest.php +++ b/tests/Server/Workspace/SymbolTest.php @@ -21,52 +21,256 @@ class SymbolTest extends TestCase $client = new LanguageClient(new MockProtocolStream()); $project = new Project($client); $this->workspace = new Server\Workspace($project, $client); - - // create two documents - $project->getDocument('file:///document1.php')->updateContent("getDocument('file:///document2.php')->updateContent("getDocument('symbols')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/symbols.php')); + $project->getDocument('references')->updateContent(file_get_contents(__DIR__ . '/../../../fixtures/references.php')); } - public function testSymbol() + public function testEmptyQueryReturnsAllSymbols() { // Request symbols - $result = $this->workspace->symbol('f'); + $result = $this->workspace->symbol(''); $this->assertEquals([ [ - 'name' => 'foo', - 'kind' => SymbolKind::FUNCTION, + 'name' => 'TEST_CONST', + 'kind' => SymbolKind::CONSTANT, 'location' => [ - 'uri' => 'file:///document1.php', + 'uri' => 'symbols', 'range' => [ 'start' => [ - 'line' => 1, - 'character' => 0 + 'line' => 4, + 'character' => 6 ], 'end' => [ - 'line' => 1, - 'character' => 17 + 'line' => 4, + 'character' => 22 ] ] ], - 'containerName' => null + 'containerName' => 'TestNamespace' ], [ - 'name' => 'frob', - 'kind' => SymbolKind::FUNCTION, + 'name' => 'TestClass', + 'kind' => SymbolKind::CLASS_, 'location' => [ - 'uri' => 'file:///document2.php', + 'uri' => 'symbols', 'range' => [ 'start' => [ - 'line' => 2, + 'line' => 6, 'character' => 0 ], 'end' => [ - 'line' => 2, - 'character' => 18 + 'line' => 21, + 'character' => 1 ] ] ], - 'containerName' => null + 'containerName' => 'TestNamespace' + ], + [ + 'name' => 'TEST_CLASS_CONST', + 'kind' => SymbolKind::CONSTANT, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 8, + 'character' => 10 + ], + 'end' => [ + 'line' => 8, + 'character' => 32 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'staticTestProperty', + 'kind' => SymbolKind::PROPERTY, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 9, + 'character' => 18 + ], + 'end' => [ + 'line' => 9, + 'character' => 37 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'testProperty', + 'kind' => SymbolKind::PROPERTY, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 10, + 'character' => 11 + ], + 'end' => [ + 'line' => 10, + 'character' => 24 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'staticTestMethod', + 'kind' => SymbolKind::METHOD, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 12, + 'character' => 4 + ], + 'end' => [ + 'line' => 15, + 'character' => 5 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'testMethod', + 'kind' => SymbolKind::METHOD, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 17, + 'character' => 4 + ], + 'end' => [ + 'line' => 20, + 'character' => 5 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'TestTrait', + 'kind' => SymbolKind::CLASS_, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 23, + 'character' => 0 + ], + 'end' => [ + 'line' => 26, + 'character' => 1 + ] + ] + ], + 'containerName' => 'TestNamespace' + ], + [ + 'name' => 'TestInterface', + 'kind' => SymbolKind::INTERFACE, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 28, + 'character' => 0 + ], + 'end' => [ + 'line' => 31, + 'character' => 1 + ] + ] + ], + 'containerName' => 'TestNamespace' + ], + [ + 'name' => 'test_function', + 'kind' => SymbolKind::FUNCTION, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 33, + 'character' => 0 + ], + 'end' => [ + 'line' => 36, + 'character' => 1 + ] + ] + ], + 'containerName' => 'TestNamespace' + ], + [ + 'name' => 'whatever', + 'kind' => SymbolKind::FUNCTION, + 'location' => [ + 'uri' => 'references', + 'range' => [ + 'start' => [ + 'line' => 15, + 'character' => 0 + ], + 'end' => [ + 'line' => 17, + 'character' => 1 + ] + ] + ], + 'containerName' => 'TestNamespace' + ] + ], json_decode(json_encode($result), true)); + } + + public function testQueryFiltersResults() + { + // Request symbols + $result = $this->workspace->symbol('testmethod'); + $this->assertEquals([ + [ + 'name' => 'staticTestMethod', + 'kind' => SymbolKind::METHOD, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 12, + 'character' => 4 + ], + 'end' => [ + 'line' => 15, + 'character' => 5 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' + ], + [ + 'name' => 'testMethod', + 'kind' => SymbolKind::METHOD, + 'location' => [ + 'uri' => 'symbols', + 'range' => [ + 'start' => [ + 'line' => 17, + 'character' => 4 + ], + 'end' => [ + 'line' => 20, + 'character' => 5 + ] + ] + ], + 'containerName' => 'TestNamespace\\TestClass' ] ], json_decode(json_encode($result), true)); }