ff6d35744a189127f87274442f5e74bfc80e2644
[yaffs-website] / vendor / psy / psysh / test / Input / ShellInputTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test\Input;
13
14 use Psy\Input\CodeArgument;
15 use Psy\Input\ShellInput;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputDefinition;
18 use Symfony\Component\Console\Input\InputOption;
19
20 class ShellInputTest extends \PHPUnit\Framework\TestCase
21 {
22     /**
23      * @dataProvider getTokenizeData
24      */
25     public function testTokenize($input, $tokens, $message)
26     {
27         $input = new ShellInput($input);
28         $r = new \ReflectionClass('Psy\Input\ShellInput');
29         $p = $r->getProperty('tokenPairs');
30         $p->setAccessible(true);
31         $this->assertSame($tokens, $p->getValue($input), $message);
32     }
33
34     public function testInputOptionWithGivenString()
35     {
36         $definition = new InputDefinition([
37             new InputOption('foo', null, InputOption::VALUE_REQUIRED),
38             new CodeArgument('code', null, CodeArgument::REQUIRED),
39         ]);
40
41         $input = new ShellInput('--foo=bar echo "baz\\\\n";');
42         $input->bind($definition);
43         $this->assertSame('bar', $input->getOption('foo'));
44         $this->assertSame('echo "baz\n";', $input->getArgument('code'));
45     }
46
47     public function testInputOptionWithoutCodeArguments()
48     {
49         $definition = new InputDefinition([
50             new InputOption('foo', null, InputOption::VALUE_REQUIRED),
51             new InputArgument('bar', null, InputArgument::REQUIRED),
52             new InputArgument('baz', null, InputArgument::REQUIRED),
53         ]);
54
55         $input = new ShellInput('--foo=foo bar "baz\\\\n"');
56         $input->bind($definition);
57         $this->assertSame('foo', $input->getOption('foo'));
58         $this->assertSame('bar', $input->getArgument('bar'));
59         $this->assertSame('baz\\n', $input->getArgument('baz'));
60     }
61
62     public function getTokenizeData()
63     {
64         // Test all the cases from StringInput test, ensuring they have an appropriate $rest token.
65         return [
66             [
67                 '',
68                 [],
69                 '->tokenize() parses an empty string',
70             ],
71             [
72                 'foo',
73                 [['foo', 'foo']],
74                 '->tokenize() parses arguments',
75             ],
76             [
77                 '  foo  bar  ',
78                 [['foo', 'foo  bar  '], ['bar', 'bar  ']],
79                 '->tokenize() ignores whitespaces between arguments',
80             ],
81             [
82                 '"quoted"',
83                 [['quoted', '"quoted"']],
84                 '->tokenize() parses quoted arguments',
85             ],
86             [
87                 "'quoted'",
88                 [['quoted', "'quoted'"]],
89                 '->tokenize() parses quoted arguments',
90             ],
91             [
92                 "'a\rb\nc\td'",
93                 [["a\rb\nc\td", "'a\rb\nc\td'"]],
94                 '->tokenize() parses whitespace chars in strings',
95             ],
96             [
97                 "'a'\r'b'\n'c'\t'd'",
98                 [
99                     ['a', "'a'\r'b'\n'c'\t'd'"],
100                     ['b', "'b'\n'c'\t'd'"],
101                     ['c', "'c'\t'd'"],
102                     ['d', "'d'"],
103                 ],
104                 '->tokenize() parses whitespace chars between args as spaces',
105             ],
106
107             /*
108              * These don't play nice with unescaping input, but the end result
109              * is correct, so disable the tests for now.
110              *
111              * @todo Sort this out and re-enable these test cases.
112              */
113             // [
114             //     '\"quoted\"',
115             //     [['"quoted"', '\"quoted\"']],
116             //     '->tokenize() parses escaped-quoted arguments',
117             // ],
118             // [
119             //     "\'quoted\'",
120             //     [['\'quoted\'', "\'quoted\'"]],
121             //     '->tokenize() parses escaped-quoted arguments',
122             // ],
123
124             [
125                 '-a',
126                  [['-a', '-a']],
127                  '->tokenize() parses short options',
128              ],
129             [
130                 '-azc',
131                 [['-azc', '-azc']],
132                 '->tokenize() parses aggregated short options',
133             ],
134             [
135                 '-awithavalue',
136                 [['-awithavalue', '-awithavalue']],
137                 '->tokenize() parses short options with a value',
138             ],
139             [
140                 '-a"foo bar"',
141                 [['-afoo bar', '-a"foo bar"']],
142                 '->tokenize() parses short options with a value',
143             ],
144             [
145                 '-a"foo bar""foo bar"',
146                 [['-afoo barfoo bar', '-a"foo bar""foo bar"']],
147                 '->tokenize() parses short options with a value',
148             ],
149             [
150                 '-a\'foo bar\'',
151                 [['-afoo bar', '-a\'foo bar\'']],
152                 '->tokenize() parses short options with a value',
153             ],
154             [
155                 '-a\'foo bar\'\'foo bar\'',
156                 [['-afoo barfoo bar', '-a\'foo bar\'\'foo bar\'']],
157                 '->tokenize() parses short options with a value',
158             ],
159             [
160                 '-a\'foo bar\'"foo bar"',
161                 [['-afoo barfoo bar', '-a\'foo bar\'"foo bar"']],
162                 '->tokenize() parses short options with a value',
163             ],
164             [
165                 '--long-option',
166                 [['--long-option', '--long-option']],
167                 '->tokenize() parses long options',
168             ],
169             [
170                 '--long-option=foo',
171                 [['--long-option=foo', '--long-option=foo']],
172                 '->tokenize() parses long options with a value',
173             ],
174             [
175                 '--long-option="foo bar"',
176                 [['--long-option=foo bar', '--long-option="foo bar"']],
177                 '->tokenize() parses long options with a value',
178             ],
179             [
180                 '--long-option="foo bar""another"',
181                 [['--long-option=foo baranother', '--long-option="foo bar""another"']],
182                 '->tokenize() parses long options with a value',
183             ],
184             [
185                 '--long-option=\'foo bar\'',
186                 [['--long-option=foo bar', '--long-option=\'foo bar\'']],
187                 '->tokenize() parses long options with a value',
188             ],
189             [
190                 "--long-option='foo bar''another'",
191                 [['--long-option=foo baranother', "--long-option='foo bar''another'"]],
192                 '->tokenize() parses long options with a value',
193             ],
194             [
195                 "--long-option='foo bar'\"another\"",
196                 [['--long-option=foo baranother', "--long-option='foo bar'\"another\""]],
197                 '->tokenize() parses long options with a value',
198             ],
199             [
200                 'foo -a -ffoo --long bar',
201                 [
202                     ['foo', 'foo -a -ffoo --long bar'],
203                     ['-a', '-a -ffoo --long bar'],
204                     ['-ffoo', '-ffoo --long bar'],
205                     ['--long', '--long bar'],
206                     ['bar', 'bar'],
207                 ],
208                 '->tokenize() parses when several arguments and options',
209             ],
210         ];
211     }
212 }