def test_equals(self): var = VarLoader().parse_variables('foo=${bar}=baz', DUMMY_WHERE) data = var.split('=', 1) self.assertEqual(len(data), 2) variable, value = data self.assertTrue(isinstance(variable, str)) self.assertTrue(isinstance(value, Var)) self.assertEqual(value.format(bar='BAR'), 'BAR=baz')
def test_var_endmiddle(self): var = VarLoader().parse_variables('${foo:-3:2}', DUMMY_WHERE) self.assertEqual( var, Var('foo', start=-3, length=2)) self.assertEqual( str(var), '${foo:-3:2}') self.assertEqual( var.format(foo='ABCDEF'), 'DE')
def test_var_offset(self): var = VarLoader().parse_variables('${foo:2}', DUMMY_WHERE) self.assertEqual( var, Var('foo', start=2)) self.assertEqual( str(var), '${foo:2}') self.assertEqual( var.format(foo='ABCDEF'), 'CDEF')
def test_var_neg2_end(self): var = VarLoader().parse_variables('${foo:1:-2}', DUMMY_WHERE) self.assertEqual( var, Var('foo', start=1, length=-2)) self.assertEqual( str(var), '${foo:1:-2}') self.assertEqual( var.format(foo='ABCDEF'), 'BCD')
def test_joined_var_in_var(self): var = VarLoader().parse_variables('${${a}a${c}}', DUMMY_WHERE) self.assertEqual( var, Var(Var.join([Var('a'), 'a', Var('c')]))) self.assertEqual( str(var), '${${a}a${c}}') self.assertEqual( var.format(a='b', c='r', bar='DEF'), 'DEF')
def test_var_begin(self): var = VarLoader().parse_variables('${foo:0:2}', DUMMY_WHERE) self.assertEqual( var, Var('foo', start=0, length=2)) self.assertEqual( str(var), '${foo:0:2}') self.assertEqual( var.format(foo='ABCDEF'), 'AB')
def test_simple_var_in_var(self): var = VarLoader().parse_variables('${${foo}}', DUMMY_WHERE) self.assertEqual( var, Var(Var('foo'))) self.assertEqual( str(var), '${${foo}}') self.assertEqual( var.format(foo='bar', bar='DEF'), 'DEF')
def test_var_foo_bar_with_otherdata(self): var = VarLoader().parse_variables('//${foo}[$]${bar}$$', DUMMY_WHERE) self.assertEqual( var, Var.join(['//', Var('foo'), '[$]', Var('bar'), '$$'])) self.assertEqual( str(var), '//${foo}[$]${bar}$$') self.assertEqual( var.format(foo='ABC', bar='DEF'), '//ABC[$]DEF$$')
def test_var_foo_bar(self): var = VarLoader().parse_variables('${foo}${bar}', DUMMY_WHERE) self.assertEqual( var, Var.join([Var('foo'), Var('bar')])) self.assertEqual( str(var), '${foo}${bar}') self.assertEqual( var.format(foo='ABC', **{'bar': 'DEF'}), 'ABCDEF')
def test_var_foo(self): var = VarLoader().parse_variables('${foo}', DUMMY_WHERE) self.assertEqual( var, Var('foo')) self.assertEqual( str(var), '${foo}') self.assertEqual( var.format(foo='ABC'), 'ABC')
def test_var_foo_bar_with_otherdata(self): var = VarLoader().parse_variables('//${foo}[$]${bar}$$', DUMMY_WHERE) self.assertEqual(var, Var.join(['//', Var('foo'), '[$]', Var('bar'), '$$'])) self.assertEqual(str(var), '//${foo}[$]${bar}$$') self.assertEqual(var.format(foo='ABC', bar='DEF'), '//ABC[$]DEF$$')
def test_writefunc(self): # TODO: overly complicated internals here; please fix var = VarLoader().parse_variables('FUNC(${bar},baz)', DUMMY_WHERE) data = var.split('(', 1) self.assertEqual(len(data), 2) variable, value = data self.assertTrue(isinstance(variable, str)) self.assertTrue(isinstance(value, Var)) self.assertEqual(value[-1], ')') value = Var.join(value[0:-1]) self.assertEqual(value.format(bar='BAR'), 'BAR,baz')
def test_invalid_start(self): # blank VarLoader().parse_variables('${foo:}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_START': 1}) # garbage VarLoader().parse_variables('${foo:x}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_START': 1}) # 0-offset VarLoader().parse_variables('${foo:0}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_START': 1}) # blank with length VarLoader().parse_variables('${foo::2}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_START': 1})
def test_invalid_length(self): # blank VarLoader().parse_variables('${foo:0:}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_LENGTH': 1}) # garbage VarLoader().parse_variables('${foo:0:x}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_LENGTH': 1}) # 0-length VarLoader().parse_variables('${foo:2:0}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_LENGTH': 1}) # length as large as negative offset VarLoader().parse_variables('${foo:-2:1}', DUMMY_WHERE) # ok self.assertLinted({}) VarLoader().parse_variables('${foo:-2:2}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_LENGTH': 1})
def handle_args(self, args): MessageDefManager.muted = True loader = VarLoader() parser = FileDialplanParser() parser.include(args.dialplan) dialplan = next(iter(parser)) contexts_by_name = list(sorted( (context.name for context in dialplan.contexts), key=(lambda x: x.lower()))) # TODO: dialplan.all_labels is not a public interface.. labels_by_name = list(sorted( dialplan.all_labels, key=(lambda x: x.lower()))) # TODO: loader._variables is *not* a public interface.. varlist_by_name = list(sorted( loader._variables.items(), key=(lambda x: x[0].lower()))) if args.verbose: self.print_contexts(contexts_by_name) self.print_labels(labels_by_name) self.print_variables(varlist_by_name) # Calculate Levenshtein distance if available. Complain if # module wasn't loaded and the user did not ask for verbose # printing either. if editdistance: identifiers = ( set(contexts_by_name) | set(labels_by_name) | set([i[0] for i in varlist_by_name])) ret = self.print_distance(identifiers) elif args.verbose: ret = 0 else: raise ImportError( 'Loading editdistance failed. Using this command without ' 'the editdistance and without verbose mode is a no-op.') return ret
def test_var_neg2_end(self): var = VarLoader().parse_variables('${foo:1:-2}', DUMMY_WHERE) self.assertEqual(var, Var('foo', start=1, length=-2)) self.assertEqual(str(var), '${foo:1:-2}') self.assertEqual(var.format(foo='ABCDEF'), 'BCD')
def test_var_offset(self): var = VarLoader().parse_variables('${foo:2}', DUMMY_WHERE) self.assertEqual(var, Var('foo', start=2)) self.assertEqual(str(var), '${foo:2}') self.assertEqual(var.format(foo='ABCDEF'), 'CDEF')
def test_var_begin(self): var = VarLoader().parse_variables('${foo:0:2}', DUMMY_WHERE) self.assertEqual(var, Var('foo', start=0, length=2)) self.assertEqual(str(var), '${foo:0:2}') self.assertEqual(var.format(foo='ABCDEF'), 'AB')
def test_sliced_function(self): var = VarLoader().parse_variables('${CALLERID(num):0:-6}xxxxxx', DUMMY_WHERE) self.assertEqual(str(var), '${CALLERID(num):0:-6}xxxxxx')
def test_slice_by_expression(self): var = VarLoader().parse_variables('${foo:$[1+1]:$[2+2]}', DUMMY_WHERE) self.assertEqual(str(var), '${foo:$[1+1]:$[2+2]}')
def test_var_foo_bar(self): var = VarLoader().parse_variables('${foo}${bar}', DUMMY_WHERE) self.assertEqual(var, Var.join([Var('foo'), Var('bar')])) self.assertEqual(str(var), '${foo}${bar}') self.assertEqual(var.format(foo='ABC', **{'bar': 'DEF'}), 'ABCDEF')
def test_slice(self): var = VarLoader().parse_variables('${${foo}:${start}:${len}}', DUMMY_WHERE) self.assertEqual(str(var), '${${foo}:${start}:${len}}')
def call_app(self, appclass, data): where = DUMMY_WHERE app = appclass() var = VarLoader().parse_variables(data, where) ignored = [] app(var, jump_destinations=ignored, where=where)
def __call__(self, data, where): VarLoader().parse_assignment(data, where) super().__call__(data, where)
def __call__(self, data, where, jump_destinations): ret = VarLoader().parse_assignment(data, where) super().__call__(data, where, jump_destinations) return ret
def test_joined_var_in_var(self): var = VarLoader().parse_variables('${${a}a${c}}', DUMMY_WHERE) self.assertEqual(var, Var(Var.join([Var('a'), 'a', Var('c')]))) self.assertEqual(str(var), '${${a}a${c}}') self.assertEqual(var.format(a='b', c='r', bar='DEF'), 'DEF')
def test_simple_var_in_var(self): var = VarLoader().parse_variables('${${foo}}', DUMMY_WHERE) self.assertEqual(var, Var(Var('foo'))) self.assertEqual(str(var), '${${foo}}') self.assertEqual(var.format(foo='bar', bar='DEF'), 'DEF')
def funcarg(self, arg): func = GotoIf() parsed = VarLoader().parse_variables(arg, DUMMY_WHERE) ignored = [] func(parsed, jump_destinations=ignored, where=DUMMY_WHERE)
def test_var_endmiddle(self): var = VarLoader().parse_variables('${foo:-3:2}', DUMMY_WHERE) self.assertEqual(var, Var('foo', start=-3, length=2)) self.assertEqual(str(var), '${foo:-3:2}') self.assertEqual(var.format(foo='ABCDEF'), 'DE')
def test_invalid_args(self): VarLoader().parse_variables('${foo:1:2:3}', DUMMY_WHERE) self.assertLinted({'E_VAR_SUBSTR_ARGS': 1})
def test_slice_by_variable_neglength(self): var = VarLoader().parse_variables('${foo:1:-${tmp}}', DUMMY_WHERE) self.assertEqual(str(var), '${foo:1:-${tmp}}')
def test_slice_by_two_variables(self): var = VarLoader().parse_variables('${foo:${x}:-${y}}', DUMMY_WHERE) self.assertEqual(str(var), '${foo:${x}:-${y}}')
def test_var_foo(self): var = VarLoader().parse_variables('${foo}', DUMMY_WHERE) self.assertEqual(var, Var('foo')) self.assertEqual(str(var), '${foo}') self.assertEqual(var.format(foo='ABC'), 'ABC')