Example #1
0
 def setUp(self):
     self.separator = ','
     self.quote = '"'
     self.argument_parser = ArgumentParserWithQuoting(self.quote,
                                                      self.separator)
     self.safe_arguments = list()
     self.unsafe_arguments = list()
     for i in range(5):
         self.safe_arguments.append("arg{0}".format(i))
         self.unsafe_arguments.append('ar,\\"g{0}'.format(i))
Example #2
0
class TestArgumentParserWithQuoting():
    def setUp(self):
        self.separator = ','
        self.quote = '"'
        self.argument_parser = ArgumentParserWithQuoting(self.quote,
                                                         self.separator)
        self.safe_arguments = list()
        self.unsafe_arguments = list()
        for i in range(5):
            self.safe_arguments.append("arg{0}".format(i))
            self.unsafe_arguments.append('ar,\\"g{0}'.format(i))

    def _quote(self, s):
        return '{quote}{s}{quote}"'.format(s=s, quote=self.quote)

    def test_single_unquoted_argument(self):
        arguments = self.argument_parser.parse(self.safe_arguments[0])
        assert_equal([self.safe_arguments[0]], arguments)

    def test_unquoted_arguments(self):
        arguments = self.argument_parser.parse(self.separator.join(
            self.safe_arguments))
        assert_equal(self.safe_arguments, arguments)

    def test_single_quoted_argument(self):
        arguments = self.argument_parser.parse(self._quote(
            self.safe_arguments[0]))
        assert_equal([self.safe_arguments[0]], arguments)

    def test_quoted_arguments(self):
        arguments = self.argument_parser.parse(self.separator.join(
            [self._quote(a) for a in self.safe_arguments]))
        assert_equal(self.safe_arguments, arguments)

    def test_unsafe_quoted_arguments(self):
        arguments = self.argument_parser.parse(self.separator.join(
            [self._quote(a) for a in self.unsafe_arguments]))
        assert_equal([a.replace('\\' + self.quote, self.quote)
                      for a in self.unsafe_arguments], arguments)

    def test_raises_exception_if_quoted_argument_is_not_terminated(self):
        assert_raises(WrongArgumentError, self.argument_parser.parse, '"arg')

    def test_skipped_unquoted_arguments_are_parsed_as_empty_strings(self):
        assert_equal(['', self.safe_arguments[0]], self.argument_parser.parse(
            self.separator + self.safe_arguments[0]))

    def test_skipped_quoted_arguments_are_parsed_as_empty_strings(self):
        assert_equal(['', self.safe_arguments[0]], self.argument_parser.parse(
            self.quote + self.quote + self.separator +
            self._quote(self.safe_arguments[0])))