def test_no_arguments(self): ap = ArgumentParser('usage: test.py [options]', arg_limits=(0, 0)) ap.parse_args([]) assert_raises_with_msg(DataError, "Expected 0 arguments, got 2.", ap.parse_args, ['1', '2'])
def test_check_args_with_wrong_number_of_args(self): for limits in [1, (1, 1), (1, 2)]: ap = ArgumentParser('usage', arg_limits=limits) for args in [(), ('arg1', 'arg2', 'arg3')]: assert_raises(DataError, ap.parse_args, args)
def test_argument_range(self): ap = ArgumentParser('usage: test.py [options] args', arg_limits=(2,4)) ap.parse_args(['1', '2']) ap.parse_args(['1', '2', '3', '4']) assert_raises_with_msg(DataError, "Expected 2 to 4 arguments, got 1.", ap.parse_args, ['one is not enough'])
def test_check_args_with_correct_args(self): for arg_limits in [None, (1, 1), 1, (1,)]: ap = ArgumentParser(USAGE, arg_limits=arg_limits) assert_equals(ap.parse_args(['hello'])[1], ['hello'])
def test_default_validation(self): ap = ArgumentParser(USAGE) for args in [(), ('1',), ('m', 'a', 'n', 'y')]: assert_equals(ap.parse_args(args)[1], list(args))
def setUp(self): self.ap = ArgumentParser(USAGE)
def test_environment_variable_not_set(self): ap = ArgumentParser('Usage:\n -o --opt value', env_options='NOT_SET') opts, args = ap.parse_args(['arg']) assert_equals(opts['opt'], None) assert_equals(args, ['arg'])
def test_print_version_when_version_not_set(self): ap = ArgumentParser(' --version', name='Kekkonen') msg = assert_raises(Information, ap.parse_args, ['--version']) assert_equals(unicode(msg), 'Kekkonen %s' % get_full_version())
def test_case_insensitive_long_options(self): ap = ArgumentParser(' -f --foo\n -B --BAR\n') assert_equals(ap._short_opts, 'fB') assert_equals(ap._long_opts, ['foo','bar'])
def setUp(self): self.ap = ArgumentParser(USAGE, version='1.0 alpha') self.ap2 = ArgumentParser(USAGE2)
def test_name_and_version_can_be_given(self): ap = ArgumentParser(USAGE, name='Kakkonen', version='2') assert_equals(ap.name, 'Kakkonen') assert_equals(ap.version, '2')
def test_custom_validator_fails(self): def validate(options, args): raise AssertionError ap = ArgumentParser(USAGE2, validator=validate) assert_raises(AssertionError, ap.parse_args, [])
def test_check_args_fails_when_no_args_specified(self): assert_raises(FrameworkError, ArgumentParser('test').parse_args, [], check_args=True)
def test_reading_args_from_usage_when_it_has_just_options(self): ap = ArgumentParser('usage: test.py [options]') ap.parse_args([], check_args=True) assert_raises_with_msg(DataError, "Expected 0 arguments, got 2.", ap._check_args, ['1', '2'])
def test_arg_limits_to_constructor(self): ap = ArgumentParser('usage: test.py [options] args', arg_limits=(2, 4)) assert_raises_with_msg(DataError, "Expected 2 to 4 arguments, got 1.", ap._check_args, ['one is not enough'])
def __init__(self, file=None, encoding='cp1252', dialect='Excel-EU', delimiter=';', quotechar='"', escapechar='\\', doublequote=True, skipinitialspace=False, lineterminator='\r\n', sheet_name=0, reader_class=None, file_search_strategy='PATH', file_regex=r'(?i)(.*?)(\.csv)', include=None, exclude=None): """**Example:** .. code :: robotframework *** Settings *** Library DataDriver Options ~~~~~~~ .. code :: robotframework *** Settings *** Library DataDriver ... file=None ... encoding=cp1252 ... dialect=Excel-EU ... delimiter=; ... quotechar=" ... escapechar=\\\\ ... doublequote=True ... skipinitialspace=False ... lineterminator=\\r\\n ... sheet_name=0 ... reader_class=None ... file_search_strategy=PATH ... file_regex=(?i)(.*?)(\\.csv) ... include=None ... exclude=None | Encoding ^^^^^^^^ ``encoding`` must be set if it shall not be cp1252 **cp1252** is the same like: - Windows-1252 - Latin-1 - ANSI - Windows Western European See `Python Standard Encoding <https://docs.python.org/3/library/codecs.html#standard-encodings>`_ for more encodings | Example Excel (US / comma seperated) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Dialect Defaults: .. code :: python delimiter = ',' quotechar = '"' doublequote = True skipinitialspace = False lineterminator = '\\r\\n' quoting = QUOTE_MINIMAL Usage in Robot Framework .. code :: robotframework *** Settings *** Library DataDriver my_data_file.csv dialect=excel encoding=${None} | Example Excel Tab (\\\\t seperated) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Dialect Defaults: .. code :: python delimiter = '\\t' quotechar = '"' doublequote = True skipinitialspace = False lineterminator = '\\r\\n' quoting = QUOTE_MINIMAL Usage in Robot Framework .. code :: robotframework *** Settings *** Library DataDriver my_data_file.csv dialect=excel_tab | Example Unix Dialect ^^^^^^^^^^^^^^^^^^^^ Dialect Defaults: .. code :: python delimiter = ',' quotechar = '"' doublequote = True skipinitialspace = False lineterminator = '\\n' quoting = QUOTE_ALL Usage in Robot Framework .. code :: robotframework *** Settings *** Library DataDriver my_data_file.csv dialect=unix_dialect | Example User Defined ^^^^^^^^^^^^^^^^^^^^ User may define the format completely free. If an option is not set, the default values are used. To register a userdefined format user have to set the option ``dialect`` to ``UserDefined`` Usage in Robot Framework .. code :: robotframework *** Settings *** Library DataDriver my_data_file.csv ... dialect=UserDefined ... delimiter=. ... lineterminator=\\n """ self.ROBOT_LIBRARY_LISTENER = self try: re.compile(file_regex) except re.error as e: file_regex = r'(?i)(.*?)(\.csv)' BuiltIn().log_to_console( f'[ DataDriver ] invalid Regex! used {file_regex} instead.') BuiltIn().log_to_console(e) arg_parser = ArgumentParser(USAGE, auto_pythonpath=False, auto_argumentfile=True, env_options='ROBOT_OPTIONS') valid_args = self._filter_args(sys.argv[1:], arg_parser._short_opts, arg_parser._long_opts) options, data_sources = arg_parser.parse_args(valid_args) self.include = options['include'] if not include else include self.exclude = options['exclude'] if not exclude else exclude self.reader_config = ReaderConfig( file=file, encoding=encoding, dialect=dialect, delimiter=delimiter, quotechar=quotechar, escapechar=escapechar, doublequote=doublequote, skipinitialspace=skipinitialspace, lineterminator=lineterminator, sheet_name=sheet_name, reader_class=reader_class, file_search_strategy=file_search_strategy.upper(), file_regex=file_regex, include=self.include, exclude=self.exclude) self.suite_source = None self.template_test = None self.template_keyword = None self.data_table = None self.test_case_data = TestCaseData()