Esempio n. 1
0
        def inspect_ctx(prop_name, prop_value, spec_path, schema_path,
                        rolling_ctx, errors):
            ctx_vars = []

            for var in expr_base.extract_vars(prop_value):
                ctx_vars.append(decorate_ctx_var(var, spec_path, schema_path))

            for ctx_var in ctx_vars:
                if ctx_var['name'].startswith('__'):
                    err_msg = (
                        'Variable "%s" that is prefixed with double underscores is considered '
                        'a private variable and cannot be referenced.' %
                        ctx_var['name'])
                    errors.append(decorate_ctx_var_error(ctx_var, err_msg))

                if ctx_var['name'] not in rolling_ctx:
                    err_msg = 'Variable "%s" is referenced before assignment.' % ctx_var[
                        'name']
                    errors.append(decorate_ctx_var_error(ctx_var, err_msg))

            if prop_name in self._context_inputs:
                updated_ctx = get_ctx_inputs(prop_name, prop_value)
                rolling_ctx = list(set(rolling_ctx + updated_ctx))

            return rolling_ctx, errors
    def test_multiple_vars_extraction(self):
        expr = '<% ctx().foobar ctx().foo.get(bar) ctx().fu.bar ctx().fu.bar[0] %>'

        expected_vars = [('yaql', expr, 'foo'), ('yaql', expr, 'foobar'),
                         ('yaql', expr, 'fu')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_empty_extraction(self):
        expr = (
            "<% just_text and $not_a_var and notctx().bar and "
            "ctx(). and ctx().() and ctx().-foobar and ctx().foobar() %>"
        )

        self.assertListEqual([], expr_base.extract_vars(expr))
Esempio n. 4
0
    def test_multiple_vars_extraction(self):
        expr = '{{ ctx("foobar") ctx("foo").get(bar) ctx("fu").bar ctx("fu").bar[0] }}'

        expected_vars = [('jinja', expr, 'foo'), ('jinja', expr, 'foobar'),
                         ('jinja', expr, 'fu')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_multiple_interleaved_vars_extraction(self):
        expr = '{{ Why the ctx("foobar") are you so ctx("fu").bar serious? }}'

        expected_vars = [
            ('jinja', expr, 'foobar'),
            ('jinja', expr, 'fu')
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
Esempio n. 6
0
    def test_multiple_interleaved_vars_extraction(self):
        expr = '<% Why the ctx().foobar are you so ctx().fu.bar serious? %>'

        expected_vars = [
            ('yaql', expr, 'foobar'),
            ('yaql', expr, 'fu')
        ]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
Esempio n. 7
0
    def test_empty_extraction(self):
        expr = ("<% just_text and $not_a_var and "
                "notctx(foo) and notctx(\"bar\") and notctx('fu') "
                'ctx("foo\') and ctx(\'foo") and ctx(foo") and '
                "ctx(\"foo) and ctx(foo') and ctx('foo) and "
                "ctx(-foo) and ctx(\"-bar\") and ctx('-fu') and "
                "ctx(foo.bar) and ctx(\"foo.bar\") and ctx('foo.bar') and "
                "ctx(foo()) and ctx(\"foo()\") and ctx('foo()') %>")

        self.assertListEqual([], expr_base.extract_vars(expr))
Esempio n. 8
0
    def test_multiple_vars_extraction(self):
        expr = '{{ ctx().foobar ctx().foo.get(bar) ctx().fu.bar ctx().fu.bar[0] }}'

        expected_vars = [
            ('jinja', expr, 'foo'),
            ('jinja', expr, 'foobar'),
            ('jinja', expr, 'fu')
        ]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
Esempio n. 9
0
    def test_ignore_ctx_get_func_calls(self):
        expr = (
            "<%ctx().get(foo) and ctx().get(bar) and ctx().get(\"fu\") and ctx().get('baz') and "
            'ctx().get(foo, "bar") and ctx().get("fu", "bar") and ctx().get(\'bar\', \'foo\') and '
            'ctx().get("foo\') and ctx().get(\'foo") and ctx().get("foo) and ctx().get(foo") %>'
        )

        expected_vars = []

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_multiple_vars_extraction(self):
        expr = "<%ctx().fubar ctx().foobar ctx().foo.get(bar) ctx().fu.bar ctx().foobaz.bar[0] %>"

        expected_vars = [
            ("yaql", expr, "foo"),
            ("yaql", expr, "foobar"),
            ("yaql", expr, "foobaz"),
            ("yaql", expr, "fu"),
            ("yaql", expr, "fubar"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_multiple_vars_extraction(self):
        expr = ('{{ctx("fubar") ctx("foobar") ctx("foo").get(bar) '
                'ctx("fu").bar ctx("foobaz").bar[0] }}')

        expected_vars = [
            ("jinja", expr, "foo"),
            ("jinja", expr, "foobar"),
            ("jinja", expr, "foobaz"),
            ("jinja", expr, "fu"),
            ("jinja", expr, "fubar"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
Esempio n. 12
0
        def inspect_ctx(prop_name, prop_value, spec_path, schema_path,
                        rolling_ctx, errors):
            ctx_vars = []

            for var in expr.extract_vars(prop_value):
                ctx_vars.append(decorate_ctx_var(var, spec_path, schema_path))

            for ctx_var in ctx_vars:
                if ctx_var['name'] not in rolling_ctx:
                    errors.append(decorate_ctx_var_error(ctx_var))

            if prop_name in self._context_inputs:
                updated_ctx = get_ctx_inputs(prop_name, prop_value)
                rolling_ctx = list(set(rolling_ctx + updated_ctx))

            return rolling_ctx, errors
Esempio n. 13
0
    def test_vars_extraction_from_list(self):
        expr = [
            '{{ abc }}',
            '{{ Why the ctx().foobar are you so ctx().fu.bar serious? }}',
            'All your base are belong to us.',
            {'{{ ctx().x }}': 123, 'k2': '{{ ctx().y }}', 'k3': ['{{ ctx().z }}']}
        ]

        expected_vars = [
            ('jinja', expr[1], 'foobar'),
            ('jinja', expr[1], 'fu'),
            ('jinja', '{{ ctx().x }}', 'x'),
            ('jinja', '{{ ctx().y }}', 'y'),
            ('jinja', '{{ ctx().z }}', 'z')
        ]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
    def test_vars_extraction_from_list(self):
        expr = [
            "<% abc %>",
            "<% Why the ctx().foobar are you so ctx().fu.bar serious? %>",
            "All your test_base are belong to us.",
            {"<% ctx().x %>": 123, "k2": "<% ctx().y %>", "k3": ["<% ctx().z %>"]},
        ]

        expected_vars = [
            ("yaql", expr[1], "foobar"),
            ("yaql", expr[1], "fu"),
            ("yaql", "<% ctx().x %>", "x"),
            ("yaql", "<% ctx().y %>", "y"),
            ("yaql", "<% ctx().z %>", "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_list(self):
        expr = [
            '<% abc %>',
            '<% Why the ctx().foobar are you so ctx().fu.bar serious? %>',
            'All your test_base are belong to us.', {
                '<% ctx().x %>': 123,
                'k2': '<% ctx().y %>',
                'k3': ['<% ctx().z %>']
            }
        ]

        expected_vars = [('yaql', expr[1], 'foobar'), ('yaql', expr[1], 'fu'),
                         ('yaql', '<% ctx().x %>', 'x'),
                         ('yaql', '<% ctx().y %>', 'y'),
                         ('yaql', '<% ctx().z %>', 'z')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_list(self):
        expr = [
            '{{ abc }}',
            '{{ Why the ctx("foobar") are you so ctx("fu").bar serious? }}',
            'All your test_base are belong to us.',
            {'{{ ctx("x") }}': 123, 'k2': '{{ ctx("y") }}', 'k3': ['{{ ctx("z") }}']}
        ]

        expected_vars = [
            ('jinja', expr[1], 'foobar'),
            ('jinja', expr[1], 'fu'),
            ('jinja', '{{ ctx("x") }}', 'x'),
            ('jinja', '{{ ctx("y") }}', 'y'),
            ('jinja', '{{ ctx("z") }}', 'z')
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
Esempio n. 17
0
    def test_vars_extraction_from_list(self):
        expr = [
            '<% abc %>',
            '<% Why the ctx(foobar) are you so ctx(fu).bar serious? %>',
            'All your base are belong to us.', {
                '<% ctx(x) %>': 123,
                'k2': '<% ctx(y) %>',
                'k3': ['<% ctx(z) %>']
            }
        ]

        expected_vars = [('yaql', expr[1], 'foobar'), ('yaql', expr[1], 'fu'),
                         ('yaql', '<% ctx(x) %>', 'x'),
                         ('yaql', '<% ctx(y) %>', 'y'),
                         ('yaql', '<% ctx(z) %>', 'z')]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
Esempio n. 18
0
    def test_vars_extraction_from_dict(self):
        expr = {
            'k1': '<% abc %>',
            'k2': '<% Why the ctx(foobar) are you so ctx(fu).bar serious? %>',
            'k3': ['<% ctx(z) %>'],
            'k4': {
                'k5': '<% ctx(y) %>'
            },
            '<% ctx(x) %>': 123
        }

        expected_vars = [('yaql', expr['k2'], 'foobar'),
                         ('yaql', expr['k2'], 'fu'),
                         ('yaql', '<% ctx(x) %>', 'x'),
                         ('yaql', '<% ctx(y) %>', 'y'),
                         ('yaql', '<% ctx(z) %>', 'z')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_dict(self):
        expr = {
            "k1": "<% abc %>",
            "k2": "<% Why the ctx().foobar are you so ctx().fu.bar serious? %>",
            "k3": ["<% ctx().z %>"],
            "k4": {"k5": "<% ctx().y %>"},
            "<% ctx().x %>": 123,
        }

        expected_vars = [
            ("yaql", expr["k2"], "foobar"),
            ("yaql", expr["k2"], "fu"),
            ("yaql", "<% ctx().x %>", "x"),
            ("yaql", "<% ctx().y %>", "y"),
            ("yaql", "<% ctx().z %>", "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
Esempio n. 20
0
    def test_vars_extraction_from_dict(self):
        expr = {
            'k1': '{{ abc }}',
            'k2': '{{ Why the ctx().foobar are you so ctx().fu.bar serious? }}',
            'k3': ['{{ ctx().z }}'],
            'k4': {'k5': '{{ ctx().y }}'},
            '{{ ctx().x }}': 123
        }

        expected_vars = [
            ('jinja', expr['k2'], 'foobar'),
            ('jinja', expr['k2'], 'fu'),
            ('jinja', '{{ ctx().x }}', 'x'),
            ('jinja', '{{ ctx().y }}', 'y'),
            ('jinja', '{{ ctx().z }}', 'z')
        ]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
Esempio n. 21
0
    def test_vars_extraction_from_dict(self):
        expr = {
            'k1': '<% abc %>',
            'k2': '<% Why the ctx().foobar are you so ctx().fu.bar serious? %>',
            'k3': ['<% ctx().z %>'],
            'k4': {'k5': '<% ctx().y %>'},
            '<% ctx().x %>': 123
        }

        expected_vars = [
            ('yaql', expr['k2'], 'foobar'),
            ('yaql', expr['k2'], 'fu'),
            ('yaql', '<% ctx().x %>', 'x'),
            ('yaql', '<% ctx().y %>', 'y'),
            ('yaql', '<% ctx().z %>', 'z')
        ]

        self.assertListEqual(expected_vars, expressions.extract_vars(expr))
    def test_vars_extraction_from_dict(self):
        expr = {
            'k1': '{{ abc }}',
            'k2': '{{ Why the ctx("foobar") are you so ctx("fu").bar serious? }}',
            'k3': ['{{ ctx("z") }}'],
            'k4': {'k5': '{{ ctx("y") }}'},
            '{{ ctx("x") }}': 123
        }

        expected_vars = [
            ('jinja', expr['k2'], 'foobar'),
            ('jinja', expr['k2'], 'fu'),
            ('jinja', '{{ ctx("x") }}', 'x'),
            ('jinja', '{{ ctx("y") }}', 'y'),
            ('jinja', '{{ ctx("z") }}', 'z')
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_list(self):
        expr = [
            "{{ abc }}",
            "{{ Why the ctx().foobar are you so ctx().fu.bar serious? }}",
            "All your test_base are belong to us.",
            {
                "{{ ctx().x }}": 123,
                "k2": "{{ ctx().y }}",
                "k3": ["{{ ctx().z }}"]
            },
        ]

        expected_vars = [
            ("jinja", expr[1], "foobar"),
            ("jinja", expr[1], "fu"),
            ("jinja", "{{ ctx().x }}", "x"),
            ("jinja", "{{ ctx().y }}", "y"),
            ("jinja", "{{ ctx().z }}", "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_list(self):
        expr = [
            "{{ abc }}",
            '{{ Why the ctx("foobar") are you so ctx("fu").bar serious? }}',
            "All your test_base are belong to us.",
            {
                '{{ ctx("x") }}': 123,
                "k2": '{{ ctx("y") }}',
                "k3": ['{{ ctx("z") }}']
            },
        ]

        expected_vars = [
            ("jinja", expr[1], "foobar"),
            ("jinja", expr[1], "fu"),
            ("jinja", '{{ ctx("x") }}', "x"),
            ("jinja", '{{ ctx("y") }}', "y"),
            ("jinja", '{{ ctx("z") }}', "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_dict(self):
        expr = {
            "k1": "{{ abc }}",
            "k2":
            '{{ Why the ctx("foobar") are you so ctx("fu").bar serious? }}',
            "k3": ['{{ ctx("z") }}'],
            "k4": {
                "k5": '{{ ctx("y") }}'
            },
            '{{ ctx("x") }}': 123,
        }

        expected_vars = [
            ("jinja", expr["k2"], "foobar"),
            ("jinja", expr["k2"], "fu"),
            ("jinja", '{{ ctx("x") }}', "x"),
            ("jinja", '{{ ctx("y") }}', "y"),
            ("jinja", '{{ ctx("z") }}', "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_vars_extraction_from_dict(self):
        expr = {
            "k1": "{{ abc }}",
            "k2":
            "{{ Why the ctx().foobar are you so ctx().fu.bar serious? }}",
            "k3": ["{{ ctx().z }}"],
            "k4": {
                "k5": "{{ ctx().y }}"
            },
            "{{ ctx().x }}": 123,
        }

        expected_vars = [
            ("jinja", expr["k2"], "foobar"),
            ("jinja", expr["k2"], "fu"),
            ("jinja", "{{ ctx().x }}", "x"),
            ("jinja", "{{ ctx().y }}", "y"),
            ("jinja", "{{ ctx().z }}", "z"),
        ]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_single_functional_var_extraction(self):
        expr = '{{ ctx("foo").get(bar) }}'

        expected_vars = [('jinja', expr, 'foo')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_single_indexing_var_extraction(self):
        expr = '{{ ctx("foo")[0] }}'

        expected_vars = [('jinja', expr, 'foo')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))
    def test_empty_extraction(self):
        expr = '{{ just_text and _not_a_var }}'

        self.assertListEqual([], expr_base.extract_vars(expr))
    def test_single_functional_var_extraction(self):
        expr = '<% ctx().foo.get(bar) %>'

        expected_vars = [('yaql', expr, 'foo')]

        self.assertListEqual(expected_vars, expr_base.extract_vars(expr))