def test_context_depend_variables(): push_context(Root()) assert_equals(len(get_context().variables['index']), 0) assert_equals(len(get_context().variables['name']), 0) get_context().add_var(1, Variable(1, value='one')) get_context().add_var('some', Variable('some', value='some')) assert_equals(get_context().get_var(1).value, 'one') assert_equals(get_context().get_var('some').value, 'some') # Checks top context variables are still exists push_context(Root()) assert_equals(get_context().get_var(1).value, 'one') assert_equals(get_context().get_var('some').value, 'some') # Checks variable overriding get_context().add_var('some', Variable('some', value='some_new')) get_context().add_var('foo', Variable('foo', value='foo')) assert_not_equals(get_context().get_var('some').value, 'some') assert_equals(get_context().get_var('some').value, 'some_new') assert_equals(get_context().get_var('foo').value, 'foo') assert_equals(get_context().get_var(1).value, 'one') # Checks variables after restore previous context pop_context() assert_not_equals(get_context().get_var('some').value, 'some_new') assert_equals(get_context().get_var('some').value, 'some') assert_equals(get_context().get_var('foo'), None) assert_equals(get_context().get_var(1).value, 'one')
def builtin_var(name): for builtin, regexp in BUILTIN_VARIABLES.items(): if builtin.endswith('_'): if not name.startswith(builtin): continue elif name != builtin: continue if regexp: return Variable(name=name, value=Regexp(regexp, strict=True, case_sensitive=False)) return Variable(name=name, value='builtin', have_script=False) return None
def test_regexp(): var = Variable(name='simple', value=Regexp('^/.*')) assert_false(var.depends) assert_true(var.regexp) assert_true(var.can_startswith('/')) assert_false(var.can_startswith('a')) assert_true(var.can_contain('a')) assert_false(var.can_contain('\n')) assert_true(var.must_contain('/')) assert_false(var.must_contain('a')) assert_true(var.must_startswith('/')) assert_false(var.must_startswith('a'))
def test_script(): get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'))) var = Variable(name='simple', value='/$foo') assert_true(var.depends) assert_false(var.regexp) assert_false(var.can_startswith('/')) assert_false(var.can_startswith('a')) assert_true(var.can_contain('/')) assert_true(var.can_contain('a')) assert_false(var.can_contain('\n')) assert_true(var.must_contain('/')) assert_false(var.must_contain('a')) assert_true(var.must_startswith('/')) assert_false(var.must_startswith('a'))
def variables(self): if self.operand != '~': return [] regexp = Regexp(self.value, case_sensitive=self.operand == '~') result = [] for name, group in regexp.groups.items(): result.append(Variable(name=name, value=group, boundary=None, provider=self)) return result
def variables(self): if not self.modifier or self.modifier not in ('~', '~*'): return [] regexp = Regexp(self.path, case_sensitive=self.modifier == '~') result = [] for name, group in regexp.groups.items(): result.append(Variable(name=name, value=group, boundary=None, provider=self)) return result
def variables(self): # TODO(buglloc): Finish him! return [ Variable(name=self.variable, value='', boundary=None, provider=self, have_script=False) ]
def test_push_failed_with_regexp_py35_gixy_10(): push_context(Root()) assert_equals(len(get_context().variables['index']), 0) assert_equals(len(get_context().variables['name']), 0) regexp = Regexp('^/some/(.*?)') for name, group in regexp.groups.items(): get_context().add_var(name, Variable(name=name, value=group)) push_context(Root())
def variables(self): regexp = Regexp(self.pattern, case_sensitive=True) result = [] for name, group in regexp.groups.items(): result.append( Variable(name=name, value=group, boundary=self.boundary, provider=self)) return result
def test_add_variables(): context = push_context(Root()) assert_equals(len(context.variables['index']), 0) assert_equals(len(context.variables['name']), 0) one_str_var = Variable('1') context.add_var('1', one_str_var) one_int_var = Variable(1) context.add_var(1, one_int_var) some_var = Variable('some') context.add_var('some', some_var) assert_equals(len(context.variables['index']), 1) assert_equals(context.variables['index'][1], one_int_var) assert_equals(len(context.variables['name']), 1) assert_equals(context.variables['name']['some'], some_var) context.clear_index_vars() assert_equals(len(context.variables['index']), 0) assert_equals(len(context.variables['name']), 1) assert_equals(context.variables['name']['some'], some_var)
def test_get_variables(): context = push_context(Root()) assert_equals(len(context.variables['index']), 0) assert_equals(len(context.variables['name']), 0) one_var = Variable(1) context.add_var(1, one_var) some_var = Variable('some') context.add_var('some', some_var) assert_equals(context.get_var(1), one_var) assert_equals(context.get_var('some'), some_var) # Checks not existed variables, for now context may return None assert_equals(context.get_var(0), None) assert_equals(context.get_var('not_existed'), None) # Checks builtins variables assert_true(context.get_var('uri')) assert_true(context.get_var('document_uri')) assert_true(context.get_var('arg_asdsadasd')) assert_true(context.get_var('args'))
def test_literal(): var = Variable(name='simple', value='$uri', have_script=False) assert_false(var.depends) assert_false(var.regexp) assert_equals(var.value, '$uri') assert_false(var.can_startswith('$')) assert_false(var.can_contain('i')) assert_true(var.must_contain('$')) assert_true(var.must_contain('u')) assert_false(var.must_contain('a')) assert_true(var.must_startswith('$')) assert_false(var.must_startswith('u'))
def test_regexp_boundary(): var = Variable(name='simple', value=Regexp('.*'), boundary=Regexp('/[a-z]', strict=True)) assert_false(var.depends) assert_true(var.regexp) assert_true(var.can_startswith('/')) assert_false(var.can_startswith('a')) assert_false(var.can_contain('/')) assert_true(var.can_contain('a')) assert_false(var.can_contain('0')) assert_false(var.can_contain('\n')) assert_true(var.must_contain('/')) assert_false(var.must_contain('a')) assert_true(var.must_startswith('/')) assert_false(var.must_startswith('a'))
def variables(self): return [Variable(name=self.variable, provider=self, have_script=False)]
def variables(self): return [Variable(name=self.variable, value=self.value, provider=self)]
def variables(self): return [Variable(name='document_root', value=self.path, provider=self)]
def test_script_boundary(): get_context().add_var('foo', Variable(name='foo', value=Regexp('.*'), boundary=Regexp('[a-z]', strict=True))) var = Variable(name='simple', value='/$foo', boundary=Regexp('[/a-z0-9]', strict=True)) assert_true(var.depends) assert_false(var.regexp) assert_false(var.can_startswith('/')) assert_false(var.can_startswith('a')) assert_false(var.can_contain('/')) assert_true(var.can_contain('a')) assert_false(var.can_contain('\n')) assert_false(var.can_contain('0')) assert_true(var.must_contain('/')) assert_false(var.must_contain('a')) assert_true(var.must_startswith('/')) assert_false(var.must_startswith('a'))