annotate vendor/psy/psysh/test/TabCompletion/AutoCompleterTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Test\TabCompletion;
Chris@0 13
Chris@0 14 use Psy\Command\ListCommand;
Chris@0 15 use Psy\Command\ShowCommand;
Chris@0 16 use Psy\Configuration;
Chris@0 17 use Psy\Context;
Chris@0 18 use Psy\ContextAware;
Chris@0 19 use Psy\TabCompletion\Matcher;
Chris@0 20
Chris@0 21 class AutoCompleterTest extends \PHPUnit\Framework\TestCase
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * @param string $line
Chris@0 25 * @param array $mustContain
Chris@0 26 * @param array $mustNotContain
Chris@0 27 * @dataProvider classesInput
Chris@0 28 */
Chris@0 29 public function testClassesCompletion($line, $mustContain, $mustNotContain)
Chris@0 30 {
Chris@0 31 $context = new Context();
Chris@0 32
Chris@0 33 $commands = [
Chris@0 34 new ShowCommand(),
Chris@0 35 new ListCommand(),
Chris@0 36 ];
Chris@0 37
Chris@0 38 $matchers = [
Chris@0 39 new Matcher\VariablesMatcher(),
Chris@0 40 new Matcher\ClassNamesMatcher(),
Chris@0 41 new Matcher\ConstantsMatcher(),
Chris@0 42 new Matcher\FunctionsMatcher(),
Chris@0 43 new Matcher\ObjectMethodsMatcher(),
Chris@0 44 new Matcher\ObjectAttributesMatcher(),
Chris@0 45 new Matcher\KeywordsMatcher(),
Chris@0 46 new Matcher\ClassAttributesMatcher(),
Chris@0 47 new Matcher\ClassMethodsMatcher(),
Chris@0 48 new Matcher\CommandsMatcher($commands),
Chris@0 49 ];
Chris@0 50
Chris@0 51 $config = new Configuration();
Chris@0 52 $tabCompletion = $config->getAutoCompleter();
Chris@0 53 foreach ($matchers as $matcher) {
Chris@0 54 if ($matcher instanceof ContextAware) {
Chris@0 55 $matcher->setContext($context);
Chris@0 56 }
Chris@0 57 $tabCompletion->addMatcher($matcher);
Chris@0 58 }
Chris@0 59
Chris@0 60 $context->setAll(['foo' => 12, 'bar' => new \DOMDocument()]);
Chris@0 61
Chris@0 62 $code = $tabCompletion->processCallback('', 0, [
Chris@0 63 'line_buffer' => $line,
Chris@0 64 'point' => 0,
Chris@4 65 'end' => \strlen($line),
Chris@0 66 ]);
Chris@0 67
Chris@0 68 foreach ($mustContain as $mc) {
Chris@0 69 $this->assertContains($mc, $code);
Chris@0 70 }
Chris@0 71
Chris@0 72 foreach ($mustNotContain as $mnc) {
Chris@0 73 $this->assertNotContains($mnc, $code);
Chris@0 74 }
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * TODO
Chris@0 79 * ====
Chris@0 80 * draft, open to modifications
Chris@0 81 * - [ ] if the variable is an array, return the square bracket for completion
Chris@0 82 * - [ ] if the variable is a constructor or method, reflect to complete as a function call
Chris@0 83 * - [ ] if the preceding token is a variable, call operators or keywords compatible for completion
Chris@0 84 * - [X] a command always should be the second token after php_open_tag
Chris@0 85 * - [X] keywords are never consecutive
Chris@0 86 * - [X] namespacing completion should work just fine
Chris@0 87 * - [X] after a new keyword, should always be a class constructor, never a function call or keyword, constant,
Chris@0 88 * or variable that does not contain a existing class name.
Chris@0 89 * - [X] on a namespaced constructor the completion must show the classes related, not constants.
Chris@0 90 *
Chris@0 91 * @return array
Chris@0 92 */
Chris@0 93 public function classesInput()
Chris@0 94 {
Chris@0 95 return [
Chris@0 96 // input, must had, must not had
Chris@0 97 ['T_OPE', ['T_OPEN_TAG'], []],
Chris@0 98 ['st', ['stdClass'], []],
Chris@0 99 ['stdCla', ['stdClass'], []],
Chris@0 100 ['new s', ['stdClass'], []],
Chris@0 101 [
Chris@0 102 'new ',
Chris@0 103 ['stdClass', 'Psy\\Context', 'Psy\\Configuration'],
Chris@0 104 ['require', 'array_search', 'T_OPEN_TAG', '$foo'],
Chris@0 105 ],
Chris@0 106 ['new Psy\\C', ['Context'], ['CASE_LOWER']],
Chris@0 107 ['\s', ['stdClass'], []],
Chris@0 108 ['array_', ['array_search', 'array_map', 'array_merge'], []],
Chris@0 109 ['$bar->', ['load'], []],
Chris@0 110 ['$b', ['bar'], []],
Chris@0 111 ['6 + $b', ['bar'], []],
Chris@0 112 ['$f', ['foo'], []],
Chris@0 113 ['l', ['ls'], []],
Chris@0 114 ['ls ', [], ['ls']],
Chris@0 115 ['sho', ['show'], []],
Chris@0 116 ['12 + clone $', ['foo'], []],
Chris@0 117 // array(
Chris@0 118 // '$foo ',
Chris@0 119 // array('+', 'clone'),
Chris@0 120 // array('$foo', 'DOMDocument', 'array_map')
Chris@0 121 // ), requires a operator matcher?
Chris@0 122 ['$', ['foo', 'bar'], ['require', 'array_search', 'T_OPEN_TAG', 'Psy']],
Chris@0 123 [
Chris@0 124 'Psy\\',
Chris@0 125 ['Context', 'TabCompletion\\Matcher\\AbstractMatcher'],
Chris@0 126 ['require', 'array_search'],
Chris@0 127 ],
Chris@0 128 [
Chris@0 129 'Psy\Test\TabCompletion\StaticSample::CO',
Chris@0 130 ['StaticSample::CONSTANT_VALUE'],
Chris@0 131 [],
Chris@0 132 ],
Chris@0 133 [
Chris@0 134 'Psy\Test\TabCompletion\StaticSample::',
Chris@0 135 ['StaticSample::$staticVariable'],
Chris@0 136 [],
Chris@0 137 ],
Chris@0 138 [
Chris@0 139 'Psy\Test\TabCompletion\StaticSample::',
Chris@0 140 ['StaticSample::staticFunction'],
Chris@0 141 [],
Chris@0 142 ],
Chris@0 143 ];
Chris@0 144 }
Chris@0 145 }