forked from nette/tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandLine.phpt
189 lines (129 loc) · 4.26 KB
/
CommandLine.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
use Tester\Assert,
Tester\Runner\CommandLine as Cmd;
require __DIR__ . '/bootstrap.php';
require __DIR__ . '/../Tester/Runner/CommandLine.php';
test(function() {
$cmd = new Cmd('
-p
');
Assert::same( array('-p' => NULL), $cmd->parse(array()) );
Assert::same( array('-p' => TRUE), $cmd->parse(array('-p')) );
$cmd = new Cmd('
-p description
');
Assert::same( array('-p' => NULL), $cmd->parse(array()) );
Assert::same( array('-p' => TRUE), $cmd->parse(array('-p')) );
});
test(function() { // default value
$cmd = new Cmd('
-p (default: 123)
');
Assert::same( array('-p' => '123'), $cmd->parse(array()) );
Assert::same( array('-p' => TRUE), $cmd->parse(array('-p')) );
$cmd = new Cmd('
-p
', array(
'-p' => array(Cmd::VALUE => 123),
));
Assert::same( array('-p' => 123), $cmd->parse(array()) );
Assert::same( array('-p' => TRUE), $cmd->parse(array('-p')) );
});
test(function() { // alias
$cmd = new Cmd('
-p | --param
');
Assert::same( array('--param' => NULL), $cmd->parse(array()) );
Assert::same( array('--param' => TRUE), $cmd->parse(array('-p')) );
Assert::same( array('--param' => TRUE), $cmd->parse(array('--param')) );
Assert::same( array('--param' => TRUE), $cmd->parse(explode(' ', '-p --param')) );
$cmd = new Cmd('
-p --param
');
Assert::same( array('--param' => TRUE), $cmd->parse(array('-p')) );
$cmd = new Cmd('
-p, --param
');
Assert::same( array('--param' => TRUE), $cmd->parse(array('-p')) );
});
test(function() { // argument
$cmd = new Cmd('
-p param
');
Assert::same( array('-p' => NULL), $cmd->parse(array()) );
Assert::same( array('-p' => 'val'), $cmd->parse(explode(' ', '-p val')) );
Assert::same( array('-p' => 'val2'), $cmd->parse(explode(' ', '-p val1 -p val2')) );
Assert::exception(function() use ($cmd) {
$cmd->parse(array('-p'));
}, 'Exception', 'Option -p requires argument.');
Assert::exception(function() use ($cmd) {
$cmd->parse(array('-p', '-a'));
}, 'Exception', 'Option -p requires argument.');
$cmd = new Cmd('
-p=<param>
');
Assert::same( array('-p' => 'val'), $cmd->parse(explode(' ', '-p val')) );
});
test(function() { // optional argument
$cmd = new Cmd('
-p [param]
');
Assert::same( array('-p' => NULL), $cmd->parse(array()) );
Assert::same( array('-p' => TRUE), $cmd->parse(array('-p')) );
Assert::same( array('-p' => 'val'), $cmd->parse(explode(' ', '-p val')) );
});
test(function() { // repeatable argument
$cmd = new Cmd('
-p [param]...
');
Assert::same( array('-p' => array()), $cmd->parse(array()) );
Assert::same( array('-p' => array(TRUE)), $cmd->parse(array('-p')) );
Assert::same( array('-p' => array('val')), $cmd->parse(explode(' ', '-p val')) );
Assert::same( array('-p' => array('val1', 'val2')), $cmd->parse(explode(' ', '-p val1 -p val2')) );
});
test(function() { // realpath
$cmd = new Cmd('
-p <path>
', array(
'-p' => array(Cmd::REALPATH => TRUE),
));
Assert::exception(function() use ($cmd) {
$cmd->parse(array('-p', 'xyz'));
}, 'Exception', "File path 'xyz' not found.");
Assert::same( array('-p' => __FILE__), $cmd->parse(array('-p', __FILE__)) );
});
test(function() { // positional arguments
$cmd = new Cmd('', array(
'pos' => array(),
));
Assert::same( array('pos' => 'val'), $cmd->parse(array('val')) );
Assert::exception(function() use ($cmd) {
$cmd->parse(array());
}, 'Exception', 'Missing required argument <pos>.');
Assert::exception(function() use ($cmd) {
$cmd->parse(array('val1', 'val2'));
}, 'Exception', 'Unexpected parameter val2.');
$cmd = new Cmd('', array(
'pos' => array(Cmd::REPEATABLE => TRUE),
));
Assert::same( array('pos' => array('val1', 'val2')), $cmd->parse(array('val1', 'val2')) );
$cmd = new Cmd('', array(
'pos' => array(Cmd::OPTIONAL => TRUE),
));
Assert::same( array('pos' => NULL), $cmd->parse(array()) );
$cmd = new Cmd('', array(
'pos' => array(Cmd::VALUE => 'default', Cmd::REPEATABLE => TRUE),
));
Assert::same( array('pos' => array('default')), $cmd->parse(array()) );
});
test(function() { // errors
$cmd = new Cmd('
-p
');
Assert::exception(function() use ($cmd) {
$cmd->parse(array('-x'));
}, 'Exception', 'Unknown option -x.');
Assert::exception(function() use ($cmd) {
$cmd->parse(array('val'));
}, 'Exception', 'Unexpected parameter val.');
});