Esempio n. 1
0
 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')
Esempio n. 2
0
 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')
Esempio n. 3
0
 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')
Esempio n. 4
0
 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')
Esempio n. 5
0
 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')
Esempio n. 6
0
 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')
Esempio n. 7
0
 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')
Esempio n. 8
0
 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$$')
Esempio n. 9
0
 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')
Esempio n. 10
0
 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')
Esempio n. 11
0
 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$$')
Esempio n. 12
0
 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')
Esempio n. 13
0
    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})
Esempio n. 14
0
    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})
Esempio n. 15
0
    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
Esempio n. 16
0
 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')
Esempio n. 17
0
 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')
Esempio n. 18
0
 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')
Esempio n. 19
0
 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')
Esempio n. 20
0
 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]}')
Esempio n. 21
0
 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')
Esempio n. 22
0
 def test_slice(self):
     var = VarLoader().parse_variables('${${foo}:${start}:${len}}',
                                       DUMMY_WHERE)
     self.assertEqual(str(var), '${${foo}:${start}:${len}}')
Esempio n. 23
0
 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)
Esempio n. 24
0
 def __call__(self, data, where):
     VarLoader().parse_assignment(data, where)
     super().__call__(data, where)
Esempio n. 25
0
 def __call__(self, data, where, jump_destinations):
     ret = VarLoader().parse_assignment(data, where)
     super().__call__(data, where, jump_destinations)
     return ret
Esempio n. 26
0
 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')
Esempio n. 27
0
 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')
Esempio n. 28
0
 def funcarg(self, arg):
     func = GotoIf()
     parsed = VarLoader().parse_variables(arg, DUMMY_WHERE)
     ignored = []
     func(parsed, jump_destinations=ignored, where=DUMMY_WHERE)
Esempio n. 29
0
 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')
Esempio n. 30
0
 def test_invalid_args(self):
     VarLoader().parse_variables('${foo:1:2:3}', DUMMY_WHERE)
     self.assertLinted({'E_VAR_SUBSTR_ARGS': 1})
Esempio n. 31
0
 def test_slice_by_variable_neglength(self):
     var = VarLoader().parse_variables('${foo:1:-${tmp}}', DUMMY_WHERE)
     self.assertEqual(str(var), '${foo:1:-${tmp}}')
Esempio n. 32
0
 def test_slice_by_two_variables(self):
     var = VarLoader().parse_variables('${foo:${x}:-${y}}', DUMMY_WHERE)
     self.assertEqual(str(var), '${foo:${x}:-${y}}')
Esempio n. 33
0
 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')