CmdLineParserTest.st
author Claus Gittinger <cg@exept.de>
Wed, 07 Sep 2011 09:29:58 +0200
changeset 13639 01a74c51c29a
parent 13481 7cd9d08941d5
child 13939 eda69dfb6133
permissions -rw-r--r--
added: #isExtension

"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
              All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libbasic' }"

nil subclass:#CmdLineParserTest
	instanceVariableNames:'optionA optionB optionBValue'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support-Command line'
!

!CmdLineParserTest class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
              All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
! !

!CmdLineParserTest methodsFor:'accessing'!

cmdlineOptionA

    ^CmdLineOption new
        short: $a;
        long: 'option-a';
        description: 'option a with no arg';
        action:[optionA := true]

    "Created: / 28-01-2009 / 11:56:28 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 08-06-2009 / 14:34:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

cmdlineOptionB

    ^CmdLineOption new
        short: $b;
        long: 'option-b';
        description: 'option b with one arg';
        action:[:value | optionB := true. optionBValue := value]

    "Created: / 28-01-2009 / 12:01:43 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 08-06-2009 / 14:33:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineParserTest methodsFor:'initialization'!

setUp

    optionA := optionB := false.
    optionBValue := nil

    "Created: / 28-01-2009 / 12:01:01 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineParserTest methodsFor:'tests'!

test_01

    CmdLineParser 
        parse: #('-a')
        for: self.

    self assert: optionA.
    self deny: optionB.

    "Created: / 28-01-2009 / 14:19:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:19 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_02

    CmdLineParser
        parse: #('--option-a')
        for: self.

    self assert: optionA.
    self deny: optionB.

    "Created: / 28-01-2009 / 14:19:38 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:30 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_04a

    CmdLineParser
        parse: #('--option-b= value')
        for: self.

    self deny: optionA.
    self assert: optionB.
    self assert: optionBValue = ' value'

    "Created: / 30-01-2009 / 10:43:40 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:34 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_04b

    self 
        should:[CmdLineParser  parse: #('--option-b') for: self]
        raise: CmdLineOptionError

    "Created: / 30-01-2009 / 10:43:37 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:39 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_04c

    CmdLineParser parse: #('--option-b=') for: self.

    self deny: optionA.
    self assert: optionB.
    self assert: optionBValue = ''

    "Created: / 30-01-2009 / 10:44:20 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:44 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_05

    CmdLineParser
        parse: #('-b' 'value')
        for: self.

    self deny: optionA.
    self assert: optionB.
    self assert: optionBValue = 'value'.

    "Created: / 03-02-2009 / 17:38:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_06

    CmdLineParser
        parse: #('-a' '-b' 'value')
        for: self.

    self assert: optionA.
    self assert: optionB.
    self assert: optionBValue = 'value'.

    "Created: / 03-02-2009 / 17:39:54 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:47:56 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_07

    CmdLineParser
        parse: #('-ab' 'value')
        for: self.

    self assert: optionA.
    self assert: optionB.
    self assert: optionBValue = 'value'.

    "Created: / 03-02-2009 / 17:40:04 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 29-05-2009 / 15:48:02 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_08

    | rest |
    rest := CmdLineParser
                parse: #('-a' '/tmp/abc.txt')
                for: self.

    self assert: optionA.
    self assert: optionB not.
    self assert: rest asArray = #('/tmp/abc.txt')

    "Created: / 08-06-2009 / 14:51:54 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

test_09

    | rest |
    rest := CmdLineParser
                parse: #('/tmp/abc.txt')
                for: self.

    self assert: optionA not.
    self assert: optionB not.
    self assert: rest asArray = #('/tmp/abc.txt')

    "Created: / 08-06-2009 / 14:52:13 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineParserTest class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libbasic/CmdLineParserTest.st,v 1.2 2011-07-03 15:06:55 cg Exp $'
!

version_SVN
    ^'§Id: CmdLineParserTest.st,v 1.1 2011/06/28 10:54:52 vrany Exp §'
! !