Issue 697939: optparse unit tests + fixes (original) (raw)
Here's a patch that mostly converts the tests from optik 1.4 to the unittest format and makes it usable in the Python library. I've also added some tests, of which five fail with current CVS:
test_opt_string_empty test_opt_string_too_short test_opt_string_long_invalid test_opt_string_short_invalid test_help_long_opts_first
I changed the following to fix the tests:
format_option_strings_short_first and format_option_strings_long_first have been merged into one function, format_options, to eliminate the almost complete duplication. To make this possible, short_first is now an attribute, which conveniently also eases changing short_first after instantiation.
_short_opts and _long_opts are set in the Option constructor, instead of in _check_option_strings, to prevent an AttributeError which would occur when no option strings were passed, making the "at least one option string must be supplied" OptionError useless.
Removed the check that would raise a RuntimeError in Option.str when no option strings existed in _short_opts or _long_opts. A RuntimeError would be raised when an OptionError was raised in _set_opt_strings, because, quite logically, no option strings were set at that point.
I'm not sure why the check was there, because _short_opts and _long_opts are only empty when instantation fails, or when somebody set those internal attributes to false. And the moment you start mucking with internal attributes, you're on your own. :)