forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynopsisParserTest.php
More file actions
194 lines (150 loc) · 5.24 KB
/
SynopsisParserTest.php
File metadata and controls
194 lines (150 loc) · 5.24 KB
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
190
191
192
193
194
<?php
use WP_CLI\SynopsisParser;
use WP_CLI\Tests\TestCase;
class SynopsisParserTest extends TestCase {
public function testEmpty() {
$r = SynopsisParser::parse( ' ' );
$this->assertEmpty( $r );
}
public function testPositional() {
$r = SynopsisParser::parse( '<plugin|zip> [<bar>]' );
$this->assertCount( 2, $r );
$param = $r[0];
$this->assertEquals( 'positional', $param['type'] );
$this->assertFalse( $param['optional'] );
$param = $r[1];
$this->assertEquals( 'positional', $param['type'] );
$this->assertTrue( $param['optional'] );
}
public function testFlag() {
$r = SynopsisParser::parse( '[--foo]' );
$this->assertCount( 1, $r );
$param = $r[0];
$this->assertEquals( 'flag', $param['type'] );
$this->assertTrue( $param['optional'] );
// Flags can't be mandatory.
$r = SynopsisParser::parse( '--foo' );
$this->assertCount( 1, $r );
$param = $r[0];
$this->assertEquals( 'unknown', $param['type'] );
}
public function testGeneric() {
$r = SynopsisParser::parse( '--<field>=<value> [--<field>=<value>] --<field>[=<value>] [--<field>[=<value>]]' );
$this->assertCount( 4, $r );
$param = $r[0];
$this->assertEquals( 'generic', $param['type'] );
$this->assertFalse( $param['optional'] );
$param = $r[1];
$this->assertEquals( 'generic', $param['type'] );
$this->assertTrue( $param['optional'] );
$param = $r[2];
$this->assertEquals( 'unknown', $param['type'] );
$param = $r[3];
$this->assertEquals( 'unknown', $param['type'] );
}
public function testAssoc() {
$r = SynopsisParser::parse( '--foo=<value> [--bar=<value>] [--bar[=<value>]]' );
$this->assertCount( 3, $r );
$param = $r[0];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertFalse( $param['optional'] );
$param = $r[1];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertTrue( $param['optional'] );
$param = $r[2];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertTrue( $param['optional'] );
$this->assertTrue( $param['value']['optional'] );
}
public function testInvalidAssoc() {
$r = SynopsisParser::parse( '--bar[=<value>] --bar=[<value>] --count=100' );
$this->assertCount( 3, $r );
$this->assertEquals( 'unknown', $r[0]['type'] );
$this->assertEquals( 'unknown', $r[1]['type'] );
$this->assertEquals( 'unknown', $r[2]['type'] );
}
public function testRepeating() {
$r = SynopsisParser::parse( '<positional>... [--<field>=<value>...]' );
$this->assertCount( 2, $r );
$param = $r[0];
$this->assertEquals( 'positional', $param['type'] );
$this->assertTrue( $param['repeating'] );
$param = $r[1];
$this->assertEquals( 'generic', $param['type'] );
$this->assertTrue( $param['repeating'] );
}
public function testCombined() {
$r = SynopsisParser::parse( '<positional> --assoc=<someval> --<field>=<value> [--flag]' );
$this->assertCount( 4, $r );
$this->assertEquals( 'positional', $r[0]['type'] );
$this->assertEquals( 'assoc', $r[1]['type'] );
$this->assertEquals( 'generic', $r[2]['type'] );
$this->assertEquals( 'flag', $r[3]['type'] );
}
public function testAllowedValueCharacters() {
$r = SynopsisParser::parse( '--capitals=<VALUE> --hyphen=<val-ue> --combined=<VAL-ue> --disallowed=<wrong:char>' );
$this->assertCount( 4, $r );
$param = $r[0];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertFalse( $param['optional'] );
$param = $r[1];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertFalse( $param['optional'] );
$param = $r[2];
$this->assertEquals( 'assoc', $param['type'] );
$this->assertFalse( $param['optional'] );
$this->assertEquals( 'unknown', $r[3]['type'] );
}
public function testRender() {
$a = [
[
'name' => 'message',
'type' => 'positional',
'description' => 'A short message to display to the user.',
],
[
'name' => 'secrets',
'type' => 'positional',
'description' => 'You may tell secrets, or you may not',
'optional' => true,
'repeating' => true,
],
[
'name' => 'meal',
'type' => 'assoc',
'description' => 'A meal during the day or night.',
],
[
'name' => 'snack',
'type' => 'assoc',
'description' => 'If you are hungry between meals, you should snack.',
'optional' => true,
],
[
'name' => 'skip',
'type' => 'assoc',
'description' => 'Skip all meals, or skip a single meal by name.',
'optional' => true,
'value' => [
'optional' => true,
],
],
];
$this->assertEquals( '<message> [<secrets>...] --meal=<meal> [--snack=<snack>] [--skip[=<skip>]]', SynopsisParser::render( $a ) );
}
public function testParseThenRender() {
$o = '<positional> --assoc=<assoc> [--double[=<optional>]] --<field>=<value> [--flag]';
$a = SynopsisParser::parse( $o );
$r = SynopsisParser::render( $a );
$this->assertEquals( $o, $r );
}
public function testParseThenRenderNumeric() {
$o = '<p1ositional> --a2ssoc=<assoc> --<field>=<value> [--f3lag]';
$a = SynopsisParser::parse( $o );
$this->assertEquals( 'p1ositional', $a[0]['name'] );
$this->assertEquals( 'a2ssoc', $a[1]['name'] );
$this->assertEquals( 'f3lag', $a[3]['name'] );
$r = SynopsisParser::render( $a );
$this->assertEquals( $o, $r );
}
}