Ejemplo n.º 1
0
 def test_is_dict_variable(self):
     for no in [
             '', 'xxx', '&{var} not alone', r'\@{notvar}', r'\\&{var}',
             '&{var}xx}', '&{x}&{y}', '${scalar}', '@{list}', '&{x}[k]'
     ]:
         assert_false(search_variable(no).is_dict_variable)
     assert_true(search_variable('&{dict}').is_dict_variable)
Ejemplo n.º 2
0
 def test_is_dict_variable(self):
     for ok in DICTS:
         assert is_dict_variable(ok)
         assert search_variable(ok).is_dict_variable()
         assert is_dict_variable(ok + '[item]')
         assert not is_dict_variable(' ' + ok)
         assert not is_dict_variable(ok + '=')
     for nok in NOKS + SCALARS + LISTS:
         assert not is_dict_variable(nok)
         assert not search_variable(nok, ignore_errors=True).is_dict_variable()
Ejemplo n.º 3
0
 def _escape_variables(self, value):
     result = ''
     match = search_variable(value)
     while match:
         result += r'%s\%s{%s}' % (match.before, match.identifier,
                                   self._escape_variables(match.base))
         for item in match.items:
             result += '[%s]' % self._escape_variables(item)
         match = search_variable(match.after)
     return result + match.string
Ejemplo n.º 4
0
 def test_is_scalar_assign(self):
     for ok in SCALARS:
         assert is_scalar_assign(ok)
         assert search_variable(ok).is_scalar_assign()
         assert is_scalar_assign(ok + '=', allow_assign_mark=True)
         assert is_scalar_assign(ok + ' =', allow_assign_mark=True)
         assert not is_scalar_assign(ok + '[item]')
         assert not is_scalar_assign(' ' + ok)
     for nok in NOK_ASSIGNS + LISTS + DICTS:
         assert not is_scalar_assign(nok)
         assert not search_variable(nok, ignore_errors=True).is_scalar_assign()
Ejemplo n.º 5
0
 def test_is_variable(self):
     for ok in SCALARS + LISTS + DICTS:
         assert is_variable(ok)
         assert is_variable(ok + '[item]')
         assert search_variable(ok).is_variable()
         assert not is_variable(' ' + ok)
         assert not is_variable(ok + '=')
     for nok in NOKS:
         assert not is_variable(nok)
         assert not search_variable(nok, identifiers='$@&',
                                    ignore_errors=True).is_variable()
Ejemplo n.º 6
0
 def test_is_variable(self):
     for no in [
             '', 'xxx', '${var} not alone', r'\${notvar}', r'\\${var}',
             '${var}xx}', '${x}${y}'
     ]:
         assert_false(search_variable(no).is_variable, no)
     for yes in [
             '${var}', r'${var$\{}', '${var${internal}}', '@{var}',
             '@{var}[0]'
     ]:
         assert_true(search_variable(yes).is_variable, yes)
Ejemplo n.º 7
0
 def validate(self):
     name = self.get_value(Token.VARIABLE)
     match = search_variable(name, ignore_errors=True)
     if not match.is_assign(allow_assign_mark=True):
         self.errors += ("Invalid variable name '%s'." % name, )
     if match.is_dict_assign(allow_assign_mark=True):
         self._validate_dict_items()
 def lex(self):
     name = self.statement[0]
     values = self.statement[1:]
     match = search_variable(name.value, ignore_errors=True)
     if match.is_assign(allow_assign_mark=True):
         self._valid_variable(name, values)
     else:
         self._invalid_variable(name, values)
     if match.is_dict_assign(allow_assign_mark=True):
         self._validate_dict_items(values)
Ejemplo n.º 9
0
 def _test(self,
           inp,
           variable=None,
           start=0,
           items=None,
           internal=False,
           identifiers=_identifiers,
           ignore_errors=False):
     if isinstance(items, str):
         items = (items, )
     elif items is None:
         items = ()
     else:
         items = tuple(items)
     if variable is None or ignore_errors:
         identifier = base = None
         start = end = -1
         is_var = is_list_var = is_dict_var = False
     else:
         identifier = variable[0]
         base = variable[2:-1]
         end = start + len(variable)
         is_var = inp == variable
         is_list_var = is_var and inp[0] == '@'
         is_dict_var = is_var and inp[0] == '&'
         if items:
             items_str = ''.join('[%s]' % i for i in items)
             end += len(items_str)
             is_var = inp == '%s%s' % (variable, items_str)
     match = search_variable(inp, identifiers, ignore_errors)
     assert_equal(match.base, base, '%r base' % inp)
     assert_equal(match.start, start, '%r start' % inp)
     assert_equal(match.end, end, '%r end' % inp)
     assert_equal(match.before, inp[:start] if start != -1 else inp)
     assert_equal(match.match, inp[start:end] if end != -1 else None)
     assert_equal(match.after, inp[end:] if end != -1 else None)
     assert_equal(match.identifier, identifier, '%r identifier' % inp)
     assert_equal(match.items, items, '%r item' % inp)
     assert_equal(match.internal_variables, internal, '%r internal' % inp)
     assert_equal(match.is_variable, is_var)
     assert_equal(match.is_list_variable, is_list_var)
     assert_equal(match.is_dict_variable, is_dict_var)
 def _is_valid_dict_item(self, item):
     name, value = split_from_equals(item)
     return value is not None or search_variable(item).is_dict_variable
Ejemplo n.º 11
0
 def _is_dict_var(self, arg):
     return (is_string(arg) and arg[:2] == '&{' and arg[-1] == '}' and
             search_variable(arg).is_dict_variable)