CmdLineParserTest.st
author Stefan Vogel <sv@exept.de>
Mon, 22 Jun 2015 11:33:37 +0200
branchexpecco_2_7_5_branch
changeset 18499 b132ac7c9d6a
parent 13939 eda69dfb6133
child 17340 3d51dbb5a9bb
child 18011 deb0c3355881
permissions -rw-r--r--
GLIBC 2.12 compatibility

"{ Package: 'stx:libbasic' }"

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


!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
    ^'$Header: /cvs/stx/stx/libbasic/CmdLineParserTest.st,v 1.3 2012-01-13 10:58:40 vrany Exp $'
! !