3 namespace Stecman\Component\Symfony\Console\BashCompletion\Tests;
5 use PHPUnit\Framework\TestCase;
6 use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
7 use Stecman\Component\Symfony\Console\BashCompletion\EnvironmentCompletionContext;
9 class CompletionContextTest extends TestCase
12 public function testWordBreakSplit()
14 $context = new CompletionContext();
15 $context->setCommandLine('console config:application --direction="west" --with-bruce --repeat 3');
17 // Cursor at the end of the first word
18 $context->setCharIndex(7);
19 $words = $context->getWords();
21 $this->assertEquals(array(
32 public function testCursorPosition()
34 $context = new CompletionContext();
35 $context->setCommandLine('make horse --legs 4 --colour black ');
37 // Cursor at the start of the line
38 $context->setCharIndex(0);
39 $this->assertEquals(0, $context->getWordIndex());
41 // Cursor at the end of the line
42 $context->setCharIndex(34);
43 $this->assertEquals(5, $context->getWordIndex());
44 $this->assertEquals('black', $context->getCurrentWord());
46 // Cursor after space at the end of the string
47 $context->setCharIndex(35);
48 $this->assertEquals(6, $context->getWordIndex());
49 $this->assertEquals('', $context->getCurrentWord());
51 // Cursor in the middle of 'horse'
52 $context->setCharIndex(8);
53 $this->assertEquals(1, $context->getWordIndex());
54 $this->assertEquals('hor', $context->getCurrentWord());
56 // Cursor at the end of '--legs'
57 $context->setCharIndex(17);
58 $this->assertEquals(2, $context->getWordIndex());
59 $this->assertEquals('--legs', $context->getCurrentWord());
62 public function testWordBreakingWithSmallInputs()
64 $context = new CompletionContext();
66 // Cursor at the end of a word and not in the following space has no effect
67 $context->setCommandLine('cmd a');
68 $context->setCharIndex(5);
69 $this->assertEquals(array('cmd', 'a'), $context->getWords());
70 $this->assertEquals(1, $context->getWordIndex());
71 $this->assertEquals('a', $context->getCurrentWord());
73 // As above, but in the middle of the command line string
74 $context->setCommandLine('cmd a');
75 $context->setCharIndex(3);
76 $this->assertEquals(array('cmd', 'a'), $context->getWords());
77 $this->assertEquals(0, $context->getWordIndex());
78 $this->assertEquals('cmd', $context->getCurrentWord());
80 // Cursor at the end of the command line with a space appends an empty word
81 $context->setCommandLine('cmd a ');
82 $context->setCharIndex(8);
83 $this->assertEquals(array('cmd', 'a', ''), $context->getWords());
84 $this->assertEquals(2, $context->getWordIndex());
85 $this->assertEquals('', $context->getCurrentWord());
87 // Cursor in break space before a word appends an empty word in that position
88 $context->setCommandLine('cmd a');
89 $context->setCharIndex(4);
90 $this->assertEquals(array('cmd', '', 'a',), $context->getWords());
91 $this->assertEquals(1, $context->getWordIndex());
92 $this->assertEquals('', $context->getCurrentWord());
95 public function testConfigureFromEnvironment()
97 putenv("CMDLINE_CONTENTS=beam up li");
98 putenv('CMDLINE_CURSOR_INDEX=10');
100 $context = new EnvironmentCompletionContext();
111 $this->assertEquals('li', $context->getCurrentWord());