33
44from robot .utils .argumentparser import ArgumentParser
55from robot .utils .asserts import *
6- from robot .errors import *
6+ from robot .errors import Information , DataError , FrameworkError
77
88
9- USAGE = """
10- usage: robot.py [options] datafile
9+ USAGE = """Example Tool -- Stuff before hyphens is considered name
1110
12- options:
11+ Usage: robot.py [options] datafile
12+
13+ Version: <VERSION>
14+
15+ Options:
1316 -d --reportdir dir Explanation
1417 -r --reportfile file This explanation continues ............... 78
1518 ........... to multiple lines.
3134* denotes options that can be set multiple times
3235"""
3336
34- USAGE2 = """
37+ USAGE2 = """Just Name Here
3538usage: robot.py [options] arg1 arg2
3639
3740options:
3841 -v --variable name=value
39- -x --var-able name=v1,v2 Explanation
42+ -x --var-able name=v1,v2 Explanation
43+ --42
4044"""
4145
4246
@@ -120,26 +124,26 @@ def test_single_option_multiple_times(self):
120124 def test_non_ascii_chars (self ):
121125 ap = ArgumentParser (USAGE2 )
122126 inargs = '-x foo=bar --variable a=1,2,3 arg1 arg2' .split ()
123- exp_opts = { 'var-able' :'foo=bar' , 'variable' :'a=1,2,3' }
124- exp_args = [ 'arg1' , 'arg2' ]
127+ exp_opts = {'var-able' :'foo=bar' , 'variable' :'a=1,2,3' , '42' : False }
128+ exp_args = ['arg1' , 'arg2' ]
125129 opts , args = ap .parse_args (inargs )
126130 assert_equals (opts , exp_opts )
127131 assert_equals (args , exp_args )
128132
129133 def test_check_args_with_correct_args (self ):
130134 for args in [ ('hello' ,), ('hello world' ,) ]:
131- self .ap .check_args (args )
135+ self .ap .parse_args (args , check_args = True )
132136
133137 def test_check_args_with_wrong_number_of_args (self ):
134138 for args in [ (), ('arg1' ,'arg2' ,'arg3' ) ]:
135- assert_raises (DataError , self .ap .check_args , args )
139+ assert_raises (DataError , self .ap ._check_args , args )
136140
137141 def test_check_variable_number_of_args (self ):
138142 ap = ArgumentParser ('usage: robot.py [options] args' )
139- ap .check_args (['one_is_ok' ])
140- ap .check_args (['two' , 'ok' ])
141- ap .check_args (['this' , 'should' , 'also' , 'work' , 'pretty' , 'well' ] )
142- assert_raises (DataError , ap .check_args , [])
143+ ap .parse_args (['one_is_ok' ], check_args = True )
144+ ap .parse_args (['two' , 'ok' ], check_args = True )
145+ ap .parse_args (['this' , 'should' , 'also' , 'work' , '!' ], check_args = True )
146+ assert_raises (DataError , ap ._check_args , [])
143147
144148 def test_unescape_options (self ):
145149 cli = '--escape quot:Q -E space:SP -E lt:LT -E gt:GT ' \
@@ -150,7 +154,7 @@ def test_unescape_options(self):
150154 assert_equals (args , ['source with spaces' ])
151155
152156 def test_split_pythonpath (self ):
153- ap = ArgumentParser ('' )
157+ ap = ArgumentParser ('ignored ' )
154158 data = [ (['path' ], ['path' ]),
155159 (['path1' ,'path2' ], ['path1' ,'path2' ]),
156160 (['path1:path2' ], ['path1' ,'path2' ]),
@@ -165,7 +169,7 @@ def test_split_pythonpath(self):
165169 assert_equals (ap ._split_pythonpath (inp ), exp )
166170
167171 def test_get_pythonpath (self ):
168- ap = ArgumentParser ('' )
172+ ap = ArgumentParser ('ignored ' )
169173 p1 = os .path .abspath ('.' )
170174 p2 = os .path .abspath ('..' )
171175 assert_equals (ap ._get_pythonpath (p1 ), [p1 ])
@@ -187,20 +191,46 @@ def test_arguments_with_glob_patterns_arent_removed_if_they_dont_match(self):
187191class TestPrintHelpAndVersion (unittest .TestCase ):
188192
189193 def setUp (self ):
190- self .ap = ArgumentParser (USAGE , version = 'testing 1.0' )
194+ self .ap = ArgumentParser (USAGE , version = '1.0 alpha' )
195+ self .ap2 = ArgumentParser (USAGE2 )
191196
192197 def test_print_help (self ):
193- assert_raises_with_msg (Information , USAGE ,
194- self .ap .parse_args , ['--help' ], help = 'help' )
198+ assert_raises_with_msg (Information , USAGE2 ,
199+ self .ap2 .parse_args , ['--42' ], help = '42' )
200+
201+ def test_name_is_got_from_first_line_of_the_usage (self ):
202+ assert_equals (self .ap ._name , 'Example Tool' )
203+ assert_equals (self .ap2 ._name , 'Just Name Here' )
195204
196205 def test_print_version (self ):
197- assert_raises_with_msg (Information , 'testing 1.0' ,
206+ assert_raises_with_msg (Information , 'Example Tool 1.0 alpha ' ,
198207 self .ap .parse_args , ['--version' ], version = 'version' )
199208
200209 def test_print_version_when_version_not_set (self ):
201- ap = ArgumentParser (USAGE )
202- assert_raises_with_msg (Information , "No version information available" ,
203- ap .parse_args , ['--version' ], version = 'version' )
210+ assert_raises (FrameworkError , self .ap2 .parse_args , ['--42' , '-x a' ], version = '42' )
211+
212+ def test_version_is_replaced_in_help (self ):
213+ assert_raises_with_msg (Information , USAGE .replace ('<VERSION>' , '1.0 alpha' ),
214+ self .ap .parse_args , ['--help' ], help = 'help' )
215+
216+ def test_escapes_are_replaced_in_help (self ):
217+ usage = """Name
218+ --escape x:y blaa blaa .............................................. end
219+ <-----------------------ESCAPES---------------------------->
220+ -- next line --
221+ --he"""
222+ expected = """Name
223+ --escape x:y blaa blaa .............................................. end
224+ Available escapes:
225+ amp (&), apos ('), at (@), bslash (\), colon (:), comma (,),
226+ curly1 ({), curly2 (}), dollar ($), exclam (!), gt (>), hash
227+ (#), lt (<), paren1 ((), paren2 ()), percent (%), pipe (|),
228+ quest (?), quot ("), semic (;), slash (/), space ( ),
229+ square1 ([), square2 (]), star (*)
230+ -- next line --
231+ --he"""
232+ assert_raises_with_msg (Information , expected ,
233+ ArgumentParser (usage ).parse_args , ['--he' ], help = 'he' )
204234
205235
206236if __name__ == "__main__" :
0 commit comments