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