self.assertEqual(('abc', 'defg'), value_comment_from_str('abc (defg)', str_from_str, str_from_str))
self.assertEqual(('abc', ''), value_comment_from_str('abc ()', str_from_str, str_from_str))
self.assertEqual(('', 'def'), value_comment_from_str('(def)', str_from_str, str_from_str))
- self.assertEqual(('ab', '(cd)'), value_comment_from_str('ab((cd))', str_from_str, str_from_str))
- self.assertEqual(('ab', '(c(d)[(]))'), value_comment_from_str('ab((c(d)[(])))', str_from_str, str_from_str))
- self.assertEqual(('ab(', 'cd'), value_comment_from_str('ab((cd)', str_from_str, str_from_str))
- self.assertEqual(('abcd', 'ef'), value_comment_from_str('abcd (ef) ', str_from_str, str_from_str))
+ self.assertEqual(('ab', '(cd)'), value_comment_from_str('ab ((cd))', str_from_str, str_from_str))
+ self.assertEqual(('ab', '(c(d)[(]))'), value_comment_from_str('ab ((c(d)[(])))', str_from_str, str_from_str))
+ self.assertEqual(('ab(', 'cd'), value_comment_from_str('ab( (cd)', str_from_str, str_from_str))
+ self.assertEqual(('abcd ', 'ef'), value_comment_from_str('abcd (ef)', str_from_str, str_from_str))
self.assertEqual(('abc', ''), value_comment_from_str('abc', str_from_str, str_from_str, comment_optional=True))
+ self.assertEqual(('a(bc)de', 'fg'), value_comment_from_str('a(bc)de (fg)', str_from_str, str_from_str))
+ self.assertEqual(('a(bc)de', 'fg'), value_comment_from_str('a(bc)de (fg)', str_from_str, str_from_str, comment_optional=True))
+ self.assertEqual(('a(bc)de', 'f(g)'), value_comment_from_str('a(bc)de (f(g))', str_from_str, str_from_str))
+ self.assertEqual(('a(bc)de', 'f(g)'), value_comment_from_str('a(bc)de (f(g))', str_from_str, str_from_str, comment_optional=True))
+ self.assertEqual(('a(bc)de', None), value_comment_from_str('a(bc)de', str_from_str, opt_str_from_str, comment_optional=True))
with self.assertRaises(ValueError):
value_comment_from_str('abc (', str_from_str, str_from_str)
with self.assertRaises(ValueError):
value_comment_from_str('abc (b))', str_from_str, str_from_str)
with self.assertRaises(ValueError):
value_comment_from_str('abc', str_from_str, str_from_str)
+ with self.assertRaises(ValueError):
+ value_comment_from_str('abc(def)', str_from_str, str_from_str)
def test_to_str(self):
self.assertEqual('abc (defg)', value_comment_to_str(('abc', 'defg'), str_to_str, str_to_str))
self.assertEqual('ab( (cd)', value_comment_to_str(('ab(', 'cd'), str_to_str, str_to_str))
self.assertEqual('abcd (ef)', value_comment_to_str(('abcd', 'ef'), str_to_str, str_to_str))
self.assertEqual('abc', value_comment_to_str(('abc', ''), str_to_str, str_to_str, comment_optional=True))
+ self.assertEqual('a(bc)de (fg)', value_comment_to_str(('a(bc)de', 'fg'), str_to_str, str_to_str))
+ self.assertEqual('a(bc)de (fg)', value_comment_to_str(('a(bc)de', 'fg'), str_to_str, str_to_str, comment_optional=True))
+ self.assertEqual('a(bc)de (f(g))', value_comment_to_str(('a(bc)de', 'f(g)'), str_to_str, str_to_str))
+ self.assertEqual('a(bc)de (f(g))', value_comment_to_str(('a(bc)de', 'f(g)'), str_to_str, str_to_str, comment_optional=True))
+ self.assertEqual('a(bc)de', value_comment_to_str(('a(bc)de', None), str_to_str, opt_str_to_str, comment_optional=True))
# string converter
def value_comment_from_str(value, value_from_str, comment_from_str, comment_optional=False):
"""Makes it possible to have a mandatory comment in parenthesis at the end of the string."""
- open_brackets = 0
comment = ''
- comment_end_pos = None
- for i, char in enumerate(value[::-1]):
- if char == ')':
- open_brackets += 1
- if open_brackets == 1:
- comment_end_pos = i
- if len(value[-1-comment_end_pos:].rstrip()) > 1:
- raise ValueError('invalid characters after comment')
- elif char == '(':
- open_brackets -= 1
- if open_brackets == 0:
- comment = value[-i:-1-comment_end_pos]
- value = value[:-i-1].rstrip()
- break
+ if value.endswith(')'):
+ open_brackets = 0
+ for i, char in enumerate(value[::-1]):
+ if char == ')':
+ open_brackets += 1
+ elif char == '(':
+ open_brackets -= 1
+ if open_brackets == 0:
+ comment = value[-i:-1]
+ value = value[:-i-1]
+ if len(value) > 1 and value[-1] != ' ':
+ raise ValueError('there has to be a space before the opening bracket of the comment')
+ value = value[:-1]
+ break
+ else:
+ if open_brackets > 0:
+ raise ValueError('bracket mismatch')
+ if not comment_optional:
+ raise ValueError('mandatory comment not found')
else:
- if open_brackets > 0:
- raise ValueError('bracket mismatch')
if not comment_optional:
- raise ValueError('mandatory comment not found')
+ raise ValueError('mandatory comment not found in "{}"'.format(value))
return value_from_str(value), comment_from_str(comment)