Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Console GetoptPlus/Help tests
4
 *
5
 * PHP version 5
6
 *
7
 * All rights reserved.
8
 * Redistribution and use in source and binary forms, with or without modification,
9
 * are permitted provided that the following conditions are met:
10
 * + Redistributions of source code must retain the above copyright notice,
11
 * this list of conditions and the following disclaimer.
12
 * + Redistributions in binary form must reproduce the above copyright notice,
13
 * this list of conditions and the following disclaimer in the documentation and/or
14
 * other materials provided with the distribution.
15
 * + The names of its contributors may not be used to endorse or promote
16
 * products derived from this software without specific prior written permission.
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * @category  PHP
30
 * @package   Console_GetoptPlus
31
 * @author    Michel Corne <mcorne@yahoo.com>
32
 * @copyright 2008 Michel Corne
33
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
34
 * @version   SVN: $Id: HelpTest.php 47 2008-01-10 11:03:38Z mcorne $
35
 * @link      http://pear.php.net/package/Console_GetoptPlus
36
 */
37
// Call tests_GetoptPlus_HelpTest::main() if this source file is executed directly.
38
if (!defined("PHPUnit_MAIN_METHOD")) {
39
    define("PHPUnit_MAIN_METHOD", "tests_GetoptPlus_HelpTest::main");
40
}
41
 
42
require_once "PHPUnit/Framework/TestCase.php";
43
require_once "PHPUnit/Framework/TestSuite.php";
44
 
45
require_once 'Console/GetoptPlus/Help.php';
46
 
47
/**
48
 * Test class for Console_GetoptPlus_Help.
49
 * Generated by PHPUnit_Util_Skeleton on 2007-05-17 at 11:00:39.
50
 *
51
 * @category  PHP
52
 * @package   Console_GetoptPlus
53
 * @author    Michel Corne <mcorne@yahoo.com>
54
 * @copyright 2008 Michel Corne
55
 * @license   http://www.opensource.org/licenses/bsd-license.php The BSD License
56
 * @version   Release:@package_version@
57
 * @link      http://pear.php.net/package/Console_GetoptPlus
58
 */
59
class tests_GetoptPlus_HelpTest extends PHPUnit_Framework_TestCase
60
{
61
    private $help;
62
 
63
    /**
64
     * Runs the test methods of this class.
65
     *
66
     * @access public
67
     * @static
68
     */
69
    public static function main()
70
    {
71
        require_once "PHPUnit/TextUI/TestRunner.php";
72
 
73
        $suite = new PHPUnit_Framework_TestSuite("Console_GetoptPlus_HelpTest");
74
        $result = PHPUnit_TextUI_TestRunner::run($suite);
75
    }
76
 
77
    /**
78
     * Sets up the fixture, for example, open a network connection.
79
     * This method is called before a test is executed.
80
     *
81
     * @access protected
82
     */
83
    protected function setUp()
84
    {
85
        $this->help = new Console_GetoptPlus_Help();
86
    }
87
 
88
    /**
89
     * Tears down the fixture, for example, close a network connection.
90
     * This method is called after a test is executed.
91
     *
92
     * @access protected
93
     */
94
    protected function tearDown()
95
    {
96
    }
97
 
98
    /**
99
     * Tests tidyString()
100
     */
101
    public function testTidyString()
102
    {
103
        // format: <string>, <expected tidied string>
104
        $test = array(// /
105
 
106
            1 => array('', ''),
107
            2 => array(array(), ''),
108
            3 => array(array('foo'), 'foo'),
109
            );
110
 
111
        foreach($test as $idx => $values) {
112
            list($string, $expected) = $values;
113
            // tidies the string
114
            $tidied = $this->help->tidyString($string);
115
            $this->assertEquals($expected, $tidied , 'test #' . $idx);
116
        }
117
    }
118
 
119
    /**
120
     * Tests tidyArray()
121
     */
122
    public function testTidyArray()
123
    {
124
        // format: <array>, <expected tidied array>
125
        $test = array(// /
126
 
127
            1 => array('', array('')),
128
            2 => array('foo', array('foo')),
129
            3 => array(array(), array()),
130
            4 => array(array('foo'), array('foo')),
131
            );
132
 
133
        foreach($test as $idx => $values) {
134
            list($array, $expected) = $values;
135
            // tidies the string
136
            $tidied = $this->help->tidyArray($array);
137
            $this->assertEquals($expected, $tidied , 'test #' . $idx);
138
        }
139
    }
140
 
141
    /**
142
     * Tests alignLines()
143
     */
144
    public function testAlignLines()
145
    {
146
        // format: <lines>, <first line addon>, <padding length>, <expected tidied lines>
147
        $test = array(// /
148
            // no line or empty line + no addon
149
 
150
            1 => array(null, null, 0, array('')),
151
            2 => array(null, null, 1, array('')),
152
            3 => array('', '', null, array('')),
153
            4 => array('', '', 0, array('')),
154
            5 => array('', '', 1, array('')),
155
            // no line or empty line + addon
156
            10 => array(null, 'addon', null, array('addon')),
157
            11 => array(null, 'addon', 0, array('addon')),
158
            12 => array(null, 'addon', 1, array('addon')),
159
            13 => array(null, 'addon', 5, array('addon')),
160
            14 => array(null, 'addon', 6, array('addon')),
161
            15 => array('', 'addon', null, array('addon')),
162
            16 => array('', 'addon', 0, array('addon')),
163
            17 => array('', 'addon', 1, array('addon')),
164
            18 => array('', 'addon', 5, array('addon')),
165
            19 => array('', 'addon', 6, array('addon')),
166
            // 1 lines + no addon
167
            20 => array('some text', '', null, array('some text')),
168
            21 => array('some text', '', 0, array('some text')),
169
            22 => array('some text', null, 1, array(' some text')),
170
            // 1 line + addon
171
            30 => array('some text', 'addon', null, array('addon some text')),
172
            31 => array('some text', 'addon', 0, array('addon', 'some text')),
173
            32 => array('some text', 'addon', 1, array('addon', ' some text')),
174
            33 => array('some text', 'addon', 5, array('addon', '     some text')),
175
            34 => array('some text', 'addon', 6, array('addon some text')),
176
            // 2 lines + addon
177
            40 => array(array('some text', 'second line'), 'addon', null,
178
                array('addon some text', '      second line')),
179
            41 => array(array('some text', 'second line'), 'addon', 0,
180
                array('addon', 'some text', 'second line')),
181
            42 => array(array('some text', 'second line'), 'addon', 1,
182
                array('addon', ' some text', ' second line')),
183
            43 => array(array('some text', 'second line'), 'addon', 5,
184
                array('addon', '     some text', '     second line')),
185
            44 => array(array('some text', 'second line'), 'addon', 6,
186
                array('addon some text', '      second line')),
187
            );
188
 
189
        foreach($test as $idx => $values) {
190
            list($lines, $addon, $paddingLength, $expected) = $values;
191
            // tidies the string
192
            $tidied = $this->help->alignLines($lines, $addon, $paddingLength);
193
            $this->assertEquals($expected, $tidied , 'test #' . $idx);
194
        }
195
    }
196
 
197
    /**
198
     * Tests setUsage()
199
     */
200
    public function testSetUsage()
201
    {
202
        // format: <config>, <expected usage text>
203
        $test = array(// /
204
 
205
            1 => array(array('options' => true), array('Usage: command [options]')),
206
            2 => array(array('parameters' => true), array('Usage: command [parameters]')),
207
            3 => array(// /
208
                array('options' => true, 'parameters' => true),
209
                array('Usage: command [options] [parameters]')),
210
            4 => array(// /
211
                array('usage' => 'blabla...'),
212
                array('Usage: command blabla...')),
213
            5 => array(// /
214
                array('usage' => array(array('first line...', 'second line...'))),
215
                array(// /
216
                    'Usage: command first line...',
217
                    '               second line...')),
218
            6 => array(// /
219
                array('usage' => array(// /
220
                        array('first usage first line...', 'first usage second line...'),
221
                        array('second usage first line...', 'second usage second line...'))),
222
                array(// /
223
                    'Usage: command first usage first line...',
224
                    '               first usage second line...',
225
                    '       command second usage first line...',
226
                    '               second usage second line...')),
227
            );
228
 
229
        foreach($test as $idx => $values) {
230
            list($config, $expected) = $values;
231
            // sets the command usages
232
            $tidied = $this->help->setUsage($config, 'command');
233
            $this->assertEquals($expected, $tidied , 'test #' . $idx);
234
        }
235
    }
236
 
237
    /**
238
     * Tests setOptions()
239
     */
240
    public function testSetOptions()
241
    {
242
        // format: <options configuration>, <expected options usage>
243
        $test = array(// /
244
            // no description
245
 
246
            1 => array(// /
247
                array(array('long' => 'foo', 'type' => 'noarg')),
248
                array('Options:', '--foo')),
249
            2 => array(// /
250
                array(array('short' => 'f', 'type' => 'mandatory', 'desc' => 'name')),
251
                array('Options:', '-f <name>')),
252
            3 => array(// /
253
                array(array('long' => 'foo', 'short' => 'f', 'type' => 'optional', 'desc' => 'name')),
254
                array('Options:', '-f --foo [name]')),
255
            // 1 line description
256
            10 => array(// /
257
                array(array('long' => 'foo', 'type' => 'noarg',
258
                        'desc' => 'option description...')),
259
                array('Options:', '--foo                         option description...')),
260
            11 => array(// /
261
                array(array('long' => 'foo', 'type' => 'mandatory',
262
                        'desc' => array('name', 'option description...'))),
263
                array('Options:', '--foo <name>                  option description...')),
264
            12 => array(// /
265
                array(array('long' => 'foo', 'type' => 'optional',
266
                        'desc' => array('name', 'option description...'))),
267
                array('Options:', '--foo [name]                  option description...')),
268
            // 2 line description
269
            20 => array(// /
270
                array(array('long' => 'foo', 'type' => 'noarg', 'desc' => array(// /
271
                            'option description first line...',
272
                            'option description second line...'))),
273
                array(// /
274
                    'Options:',
275
                    '--foo                         option description first line...',
276
                    '                              option description second line...')),
277
            21 => array(// /
278
                array(array('long' => 'foo', 'type' => 'mandatory', 'desc' => array(// /
279
                            'name',
280
                            'option description first line...',
281
                            'option description second line...'))),
282
                array(// /
283
                    'Options:',
284
                    '--foo <name>                  option description first line...',
285
                    '                              option description second line...')),
286
            22 => array(// /
287
                array(array('long' => 'foo', 'type' => 'optional', 'desc' => array(// /
288
                            'name',
289
                            'option description first line...',
290
                            'option description second line...'))),
291
                array(// /
292
                    'Options:',
293
                    '--foo [name]                  option description first line...',
294
                    '                              option description second line...')),
295
            // integrated
296
            30 => array(// /
297
                array(// /
298
                    array('long' => 'foo', 'type' => 'noarg', 'desc' => array(// /
299
                            'option foo description first line...',
300
                            'option foo description second line...')),
301
                    array('long' => 'bar', 'type' => 'mandatory', 'short' => 'b', 'desc' => array(// /
302
                            'name',
303
                            'option bar description first line...',
304
                            'option bar description second line...')),
305
                    array('short' => 'c', 'type' => 'optional', 'desc' => array(// /
306
                            'value',
307
                            'option c description first line...',
308
                            'option c description second line...'))),
309
                array(// /
310
                    'Options:',
311
                    '--foo                         option foo description first line...',
312
                    '                              option foo description second line...',
313
                    '-b --bar <name>               option bar description first line...',
314
                    '                              option bar description second line...',
315
                    '-c [value]                    option c description first line...',
316
                    '                              option c description second line...')),
317
            );
318
 
319
        foreach($test as $idx => $values) {
320
            list($optionsConfig, $expected) = $values;
321
            // sets the command usages
322
            $tidied = $this->help->setOptions($optionsConfig);
323
            $this->assertEquals($expected, $tidied , 'test #' . $idx);
324
        }
325
    }
326
 
327
    /**
328
     * Tests set()
329
     */
330
    public function testSet()
331
    {
332
        $config = array(// /
333
            'header' => array('Note: some text ...', '... more text.', ''),
334
            'options' => array(// /
335
                array('long' => 'foo', 'type' => 'noarg', 'desc' => array(// /
336
                        'option foo description first line...',
337
                        'option foo description second line...')),
338
                array('long' => 'bar', 'type' => 'mandatory', 'short' => 'b', 'desc' => array(// /
339
                        'name',
340
                        'option bar description first line...',
341
                        'option bar description second line...')),
342
                array('short' => 'c', 'type' => 'optional', 'desc' => array(// /
343
                        'value',
344
                        'option c description first line...',
345
                        'option c description second line...'))),
346
            'parameters' => array('[param1] [,param2]', 'The parameters decription.'),
347
            'footer' => array('Note: some text ...', '... more text.'),
348
            );
349
 
350
        $expected = array(// /
351
            'Note: some text ...',
352
            '... more text.',
353
            '',
354
            'Usage: command [options] [parameters]',
355
            'Options:',
356
            '--foo                         option foo description first line...',
357
            '                              option foo description second line...',
358
            '-b --bar <name>               option bar description first line...',
359
            '                              option bar description second line...',
360
            '-c [value]                    option c description first line...',
361
            '                              option c description second line...',
362
            'Parameters: [param1] [,param2]',
363
            '            The parameters decription.',
364
            'Note: some text ...',
365
            '... more text.'
366
            );
367
 
368
        $help = $this->help->set($config, 'command');
369
        is_string($help) and $help = explode("\n", $help);
370
 
371
        $this->assertEquals($expected, $help);
372
    }
373
 
374
    /**
375
     * Tests get()
376
     */
377
    public function testget()
378
    {
379
        $expected = array('Usage: command');
380
 
381
        $help = Console_GetoptPlus_Help::get(array(), 'command');
382
        is_string($help) and $help = explode("\n", $help);
383
 
384
        $this->assertEquals($expected, $help);
385
    }
386
}
387
// Call tests_GetoptPlus_HelpTest::main() if this source file is executed directly.
388
if (PHPUnit_MAIN_METHOD == "tests_GetoptPlus_HelpTest::main") {
389
    tests_GetoptPlus_HelpTest::main();
390
}
391
 
392
?>