Example #1
0
  def test_should_not_hoist_if_do(self):
    code = """
#def f
#end def
#def foo($bar)
  #if True
    #do $f($bar)
    $reg_f($bar)
  #else
    $reg_f($bar)
  #end if
#end def
    """

    final_tree = self._get_final_tree(code)
    def pred_if(node):
      return type(node) == ast.IfNode

    if_node = walker.find_node(final_tree, pred_if)

    def pred(node):
      return type(node) == ast.AssignNode

    alias = walker.find_node(if_node, pred)
    if not alias:
      self.fail('ast.AssignNode should be present in the If block.')
Example #2
0
    def test_should_not_hoist_if_do(self):
        code = """
        #def f
        #end def
        #def foo($bar)
          #if True
            #do $f($bar)
            $reg_f($bar)
          #else
            $reg_f($bar)
          #end if
        #end def
        """

        final_tree = self._get_final_tree(code)

        def pred_if(node):
            return type(node) == ast.IfNode

        if_node = walker.find_node(final_tree, pred_if)

        def pred(node):
            return type(node) == ast.AssignNode

        alias = walker.find_node(if_node, pred)
        if not alias:
            self.fail('ast.AssignNode should be present in the If block.')
Example #3
0
    def test_private_filter_function_macro(self):
        code = """
        #def foo
          $my_macro()
        #end def
        """

        def macro_function(macro_node, arg_map, compiler):
            return '#set $bar = self._filter_function("test")\n'

        self.compiler.register_macro('macro_function_my_macro',
                                     macro_function,
                                     parse_rule='fragment_goal')

        ast_root = self._compile(code)
        semantic_analyzer = analyzer.SemanticAnalyzer(
            'TestTemplate', ast_root, self.compiler.analyzer_options,
            self.compiler)
        analyzed_tree = semantic_analyzer.get_ast()

        optimization_analyzer = self._get_analyzer(analyzed_tree)
        optimized_tree = optimization_analyzer.optimize_ast()

        def pred(node):
            return bool(type(node) == ast.IdentifierNode and node.name ==
                        '_self_private_filter_function')

        filter_node = walker.find_node(optimized_tree, pred)
        if not filter_node:
            self.fail('Expected _self_private_filter_function in ast')
Example #4
0
    def test_template_method_direct(self):
        code = """
    #def foo
      Hello
    #end def

    #def bar
      $foo()
    #end def
    """
        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (
                type(node) == ast.CallFunctionNode
                and type(node.expression) == ast.PlaceholderNode
                and node.expression.name == "foo"
            )

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail("Expected foo() in ast")
        self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
Example #5
0
    def test_external_function_maybe(self):
        code = """
        #from module import my_lib

        #def bar
          $my_lib.foo()
        #end def
        """

        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (type(node) == ast.CallFunctionNode and
                    type(node.expression) == ast.GetUDNNode and
                    type(node.expression.expression) == ast.PlaceholderNode and
                    node.expression.expression.name == 'my_lib' and
                    node.expression.name == 'foo')

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail('Expected my_libfoo() in ast')
        self.assertEqual(foo_call.sanitization_state,
                         ast.SanitizedState.UNKNOWN)
Example #6
0
    def test_template_method_direct(self):
        code = """
        #def foo
          Hello
        #end def

        #def bar
          $foo()
        #end def
        """

        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (type(node) == ast.CallFunctionNode and
                    type(node.expression) == ast.PlaceholderNode and
                    node.expression.name == 'foo')

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail('Expected foo() in ast')
        self.assertEqual(foo_call.sanitization_state,
                         ast.SanitizedState.SANITIZED_STRING)
Example #7
0
  def test_private_filter_function_macro(self):
    code = """
#def foo
  $my_macro()
#end def
    """

    def macro_function(macro_node, arg_map, compiler):
      return '#set $bar = self._filter_function("test")\n'

    self.compiler.register_macro('macro_function_my_macro', macro_function,
                                 parse_rule='fragment_goal')

    ast_root = self._compile(code)
    semantic_analyzer = analyzer.SemanticAnalyzer(
        'TestTemplate',
        ast_root,
        self.compiler.analyzer_options,
        self.compiler)
    analyzed_tree = semantic_analyzer.get_ast()

    optimization_analyzer = self._get_analyzer(analyzed_tree)
    optimized_tree = optimization_analyzer.optimize_ast()

    def pred(node):
      return bool(type(node) == ast.IdentifierNode and
                  node.name == '_self_private_filter_function')

    filter_node = walker.find_node(optimized_tree, pred)
    if not filter_node:
      self.fail('Expected _self_private_filter_function in ast')
Example #8
0
    def test_external_function_maybe(self):
        code = """
    #from module import my_lib

    #def bar
      $my_lib.foo()
    #end def
    """
        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (
                type(node) == ast.CallFunctionNode
                and type(node.expression) == ast.GetUDNNode
                and type(node.expression.expression) == ast.PlaceholderNode
                and node.expression.expression.name == "my_lib"
                and node.expression.name == "foo"
            )

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail("Expected my_libfoo() in ast")
        self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.UNKNOWN)
Example #9
0
    def test_do_syntax(self):
        code = """
        #def foo
        #do $bar()
        #end def
        """

        template = self._compile(code)
        do_node = walker.find_node(template, TestDo._do_pred)
        if not do_node:
            self.fail('Do node should be present in AST')
Example #10
0
    def test_do_syntax(self):
        code = """
        #def foo
        #do $bar()
        #end def
        """

        template = self._compile(code)
        do_node = walker.find_node(template, TestDo._do_pred)
        if not do_node:
            self.fail('Do node should be present in AST')
Example #11
0
    def test_allow_raw(self):
        code = """
        #allow_raw

        #def foo
        #end def
        """

        template = self._compile(code)

        allow_raw_node = walker.find_node(template,
                                          TestAllowRaw._allow_raw_pred)
        if not allow_raw_node:
            self.fail('ast.AllowRawNode should be present in AST')
Example #12
0
    def test_allow_raw(self):
        code = """
        #allow_raw

        #def foo
        #end def
        """

        template = self._compile(code)

        allow_raw_node = walker.find_node(template,
                                          TestAllowRaw._allow_raw_pred)
        if not allow_raw_node:
            self.fail('ast.AllowRawNode should be present in AST')
Example #13
0
    def test_escape_needed(self):
        code = """
        #def foo
        \#if
        #end def
        """

        template = self._compile(code)

        def_node = walker.find_node(template, TestEscapeHash._def_foo_pred)
        text = ''.join([node.value
                        for node in def_node.child_nodes
                        if type(node) == ast.TextNode])
        self.assertEqual(text, '#if')
Example #14
0
  def test_should_hoist_for(self):
    code = """
#def foo($bar)
  #for $i in []
    $reg_f($bar)
  #end for
#end def
    """

    final_tree = self._get_final_tree(code)
    def pred(node):
      return type(node) == ast.AssignNode and type(node.parent) == ast.FunctionNode
    alias = walker.find_node(final_tree, pred)
    if not alias:
      self.fail('Expected to find ast.AssignNode hoisted to function scope.')
Example #15
0
    def test_escape_needed(self):
        code = """
        #def foo
        \#if
        #end def
        """

        template = self._compile(code)

        def_node = walker.find_node(template, TestEscapeHash._def_foo_pred)
        text = ''.join([
            node.value for node in def_node.child_nodes
            if type(node) == ast.TextNode
        ])
        self.assertEqual(text, '#if')
Example #16
0
    def test_should_not_need_sanitization_filter(self):
        code = """
        #def foo($bar)
          $bar()
        #end def
        """

        optimized_tree = self._get_optimized_tree(code)

        def pred(node):
            return type(node) == ast.CallFunctionNode and type(node.parent) == ast.FilterNode

        call_node = walker.find_node(optimized_tree, pred)
        if not call_node:
            self.fail("Expected to find a ast.CallFunctionNode.")
        if call_node.sanitization_state != ast.SanitizedState.OUTPUTTED_IMMEDIATELY:
            self.fail("Expected node in ast.FilterNode to not need sanitization.")
Example #17
0
    def test_should_not_need_sanitization_do(self):
        code = """
        #def foo($bar)
          #do $bar.baz()
        #end def
        """

        optimized_tree = self._get_optimized_tree(code)

        def pred(node):
            return type(node) == ast.CallFunctionNode

        call_node = walker.find_node(optimized_tree, pred)
        if not call_node:
            self.fail('Expected to find a ast.CallFunctionNode.')
        if call_node.sanitization_state != ast.SanitizedState.NOT_OUTPUTTED:
            self.fail('Expected node in #do to not need sanitization.')
Example #18
0
  def test_should_not_need_sanitization_do(self):
    code = """
#def foo($bar)
  #do $bar.baz()
#end def
    """

    optimized_tree = self._get_optimized_tree(code)

    def pred(node):
      return type(node) == ast.CallFunctionNode

    call_node = walker.find_node(optimized_tree, pred)
    if not call_node:
      self.fail('Expected to find a ast.CallFunctionNode.')
    if call_node.sanitization_state != ast.SanitizedState.NOT_OUTPUTTED:
      self.fail('Expected node in #do to not need sanitization.')
Example #19
0
    def test_should_not_hoist_for(self):
        code = """
        #def foo($bar)
          #for $i in []
            #set $bar["1"] = 1
            $reg_f($bar)
          #end for
        #end def
        """

        final_tree = self._get_final_tree(code)

        def pred(node):
            return type(node) == ast.AssignNode and type(
                node.parent) == ast.FunctionNode

        alias = walker.find_node(final_tree, pred)
        if alias:
            self.fail('ast.AssignNode should not be hoisted to function scope.')
Example #20
0
  def test_should_not_hoist_if_set_output(self):
    code = """
#def foo($bar)
  #if True
    #set $bar[1] = 1
    $bar
  #else
    $bar
  #end if
#end def
    """

    final_tree = self._get_final_tree(code)

    def pred(node):
      return type(node) == ast.AssignNode and type(node.parent) == ast.FunctionNode

    alias = walker.find_node(final_tree, pred)
    if alias:
      self.fail('ast.AssignNode should not be hoisted to the ast.FunctionNode.')
Example #21
0
    def test_should_not_need_sanitization_filter(self):
        code = """
        #def foo($bar)
          $bar()
        #end def
        """

        optimized_tree = self._get_optimized_tree(code)

        def pred(node):
            return (type(node) == ast.CallFunctionNode and
                    type(node.parent) == ast.FilterNode)

        call_node = walker.find_node(optimized_tree, pred)
        if not call_node:
            self.fail('Expected to find a ast.CallFunctionNode.')
        if (call_node.sanitization_state !=
                ast.SanitizedState.OUTPUTTED_IMMEDIATELY):
            self.fail(
                'Expected node in ast.FilterNode to not need sanitization.')
Example #22
0
    def test_library_function_registry_yes(self):
        code = """
    #def bar
      $reg_f()
    #end def
    """
        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (
                type(node) == ast.CallFunctionNode
                and type(node.expression) == ast.PlaceholderNode
                and node.expression.name == "reg_f"
            )

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail("Expected reg_f() in ast")
        self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED)
Example #23
0
    def test_library_function_registry_yes(self):
        code = """
        #def bar
          $reg_f()
        #end def
        """

        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (type(node) == ast.CallFunctionNode and
                    type(node.expression) == ast.PlaceholderNode and
                    node.expression.name == 'reg_f')

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail('Expected reg_f() in ast')
        self.assertEqual(foo_call.sanitization_state,
                         ast.SanitizedState.SANITIZED)
Example #24
0
    def test_should_hoist_if(self):
        code = """
        #def foo($bar)
          #if True
            $reg_f($bar)
          #else
            $reg_f($bar)
          #end if
        #end def
          """

        final_tree = self._get_final_tree(code)

        def pred(node):
            return type(node) == ast.AssignNode and type(
                node.parent) == ast.FunctionNode

        alias = walker.find_node(final_tree, pred)
        if not alias:
            self.fail(
                'Expected to find ast.AssignNode hoisted to function scope.')
Example #25
0
    def test_should_not_hoist_if_set_output(self):
        code = """
        #def foo($bar)
          #if True
            #set $bar[1] = 1
            $bar
          #else
            $bar
          #end if
        #end def
        """

        final_tree = self._get_final_tree(code)

        def pred(node):
            return type(node) == ast.AssignNode and type(
                node.parent) == ast.FunctionNode

        alias = walker.find_node(final_tree, pred)
        if alias:
            self.fail(
                'ast.AssignNode should not be hoisted to the ast.FunctionNode.')
Example #26
0
    def test_library_function_direct(self):
        code = """
    #from module import library my_lib

    #def bar
      $my_lib.foo()
    #end def
    """
        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (
                type(node) == ast.CallFunctionNode
                and type(node.expression) == ast.IdentifierNode
                and node.expression.name == "my_lib.foo"
            )

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail("Expected my_lib.foo() in ast")
        self.assertEqual(foo_call.sanitization_state, ast.SanitizedState.SANITIZED_STRING)
Example #27
0
    def test_library_function_direct(self):
        code = """
        #from module import library my_lib

        #def bar
          $my_lib.foo()
        #end def
        """

        template = self._compile(code)
        semantic_analyzer = self._get_analyzer(template)
        analyzed_ast = semantic_analyzer.get_ast()

        def pred(node):
            return (type(node) == ast.CallFunctionNode and
                    type(node.expression) == ast.IdentifierNode and
                    node.expression.name == 'my_lib.foo')

        foo_call = walker.find_node(analyzed_ast, pred)
        if not foo_call:
            self.fail('Expected my_lib.foo() in ast')
        self.assertEqual(foo_call.sanitization_state,
                         ast.SanitizedState.SANITIZED_STRING)
Example #28
0
    def test_cache_filter_args_udn(self):
        code = """
        #from foo import bar

        #def func
          $bar.baz('arg')
        #end def
        """

        ast_root = self._compile(code)
        semantic_analyzer = analyzer.SemanticAnalyzer(
            'TestTemplate', ast_root, self.compiler.analyzer_options,
            self.compiler)
        analyzed_tree = semantic_analyzer.get_ast()

        optimization_analyzer = self._get_analyzer(analyzed_tree)
        optimized_tree = optimization_analyzer.optimize_ast()

        def pred(node):
            return type(node) == ast.AssignNode

        alias_assign = walker.find_node(optimized_tree, pred)
        if not alias_assign:
            self.fail('There should be an ast.AssignNode due to caching')
Example #29
0
  def test_cache_filter_args_udn(self):
    code = """
#from foo import bar

#def func
  $bar.baz('arg')
#end def
    """
    ast_root = self._compile(code)
    semantic_analyzer = analyzer.SemanticAnalyzer(
        'TestTemplate',
        ast_root,
        self.compiler.analyzer_options,
        self.compiler)
    analyzed_tree = semantic_analyzer.get_ast()

    optimization_analyzer = self._get_analyzer(analyzed_tree)
    optimized_tree = optimization_analyzer.optimize_ast()

    def pred(node):
      return type(node) == ast.AssignNode
    alias_assign = walker.find_node(optimized_tree, pred)
    if not alias_assign:
      self.fail('There should be an ast.AssignNode due to caching')
Example #30
0
 def pred(node):
   return type(node) == ast.AssignNode
   alias_assign = walker.find_node(optimized_tree, pred)
   if not alias_assign:
     self.fail('There should be an ast.AssignNode due to caching')
Example #31
0
 def pred(node):
     return type(node) == ast.AssignNode
     alias_assign = walker.find_node(optimized_tree, pred)
     if not alias_assign:
         self.fail('There should be an ast.AssignNode due to caching')