
from mems.tools.unittest import TestScenario, parse_args, run_scenarios
from quixote import template

class TemplateTest(TestScenario):
    "Test of the PTL template code"
    def setup(self):
        self.namespace = {}

    def check_literal(self):
        "Test simple strings: 3"
        for text in ['literal text', 'literal text with <html> tag',
                     'literal text with closing ?>']:
            t = template.compile_template( text )
            self.test_val('t[0].tree', (text,))

    def check_eval(self):
        "Test variable references : 2"
        t = template.compile_template( "<?value?>" )
        self.test_val('t[0].tree', ('', (template.EVAL, 'value'), ''))

        t = template.compile_template( "<?value?> <?v2?>" )
        self.test_val('t[0].tree', ('', (template.EVAL, 'value'), ' ',
                                 (template.EVAL, 'v2'), ''))

    def check_stmt(self):
        "Test statement references : 2"
        t = template.compile_template( "<?stmt x=1?>" )
        self.test_val('t[0].tree', ('', (template.STMT, 'x=1'), ''))

        t = template.compile_template( "<?stmt x=1?> <?stmt import sys?>" )
        self.test_val('t[0].tree', ('', (template.STMT, 'x=1'), ' ',
                                 (template.STMT, 'import sys'), ''))

    def check_if(self):
        "Test 'if' statement: 4"

        t = template.compile_template( "<?if x==1?>clause<?/if?>" )
        self.test_val('t[0].tree', ('', (template.IF,
                                      ('x==1', ('clause',) ),
                                      None),
                                      ''))

        t = template.compile_template( "<?if x==1?>clause<?else?>else clause<?/if?>" )
        self.test_val('t[0].tree', ('', (template.IF,
                                      ('x==1', ('clause',) ),
                                      ('else clause',)),
                                      ''))

        t = template.compile_template( "<?if x==1?>clause<?elif x==2?>subclause<?/if?>" )
        self.test_val('t[0].tree', ('', (template.IF,
                                      ('x==1', ('clause',) ),
                                      ('x==2', ('subclause',) ),
                                      None),
                                      ''))

        t = template.compile_template( "<?if x==1?>clause<?elif x==2?>subclause"
                              "<?else?>else clause<?/if?>" )
        self.test_val('t[0].tree', ('', (template.IF,
                                      ('x==1', ('clause',) ),
                                      ('x==2', ('subclause',) ),
                                      ('else clause',)),
                                      ''))
    def check_while(self):
        "Test 'while' statement: 1"
        t = template.compile_template( "<?while x==1?>clause<?/while?>" )
        self.test_val('t[0].tree', ('', (template.WHILE,
                                      'x==1', ('clause',) ),
                                      ''))

    def check_for(self):
        "Test 'for' statement: 1"
        t = template.compile_template( "<?for x in range(5)?>clause<?/for?>" )
        self.test_val('t[0].tree', ('', (template.FOR,
                                      'x', 'range(5)', ('clause',) ),
                                      ''))

    def check_block(self):
        "Test 'block' statement: 1"
        t = template.compile_template( "<?block?>block body\n\tnextline<?/block?>" )
        self.test_val('t[0].tree', ('',
                                 (template.BLOCK, 'block body\n\tnextline'),
                                 ''))

#        t = template.compile_template( "<?block?>block body\n\tnextline" )

    def check_template(self):
        "Test 'template' statement: 10"
        t = template.compile_template( "<?template f1(sig1)?>block body<?/template?>" )
        self.test_val('t[0].tree', ('block body',) )
        self.test_val('t[0].name', 'f1' )
        self.test_val('t[0].signature', 'sig1' )

        t = template.compile_template( "<?template f1(sig1)?>block body<?/template?>"
                              "<?template f2(sig2)?>block body II<?/template?>")
        self.test_val('t[0].tree', ('block body',) )
        self.test_val('t[0].name', 'f1' )
        self.test_val('t[0].signature', 'sig1' )
        self.test_val('t[1].tree', ('block body II',) )
        self.test_val('t[1].name', 'f2' )
        self.test_val('t[1].signature', 'sig2' )

        # Test a template with a newline in the signature
        t = template.compile_template( "<?template f1(arg1,\n arg2) ?>block body<?/template?>")
        self.test_val('t[0].signature', 'arg1,\n arg2' )


    def check_block_comment(self):
        "Test 'comment' directive: 4"
        t = template.compile_template( "<?comment?>ignore me<?/comment?>" )
        self.test_val('t[0].tree', ('',
                                    (template.BLOCK_COMMENT, ('ignore me',)),
                                    ''))

        # Test missing or incorrect closing directive
        self.test_exc('template.compile_template("<?comment?>ignore me")',
                      template.ParseError)
        self.test_exc('template.compile_template("<?comment?>ignore me<?/if?>")',
                      template.ParseError)

        # Test a directive nested within a comment
        t = template.compile_template( "<?comment?>"
                                       "<?if x==1?>clause<?else?>else clause<?/if?>"
                                       "<?/comment?>" )
        if_tree = ('', (template.IF,
                        ('x==1', ('clause',) ),
                        ('else clause',)),
                   '')
        self.test_val('t[0].tree', ('',
                                    (template.BLOCK_COMMENT, if_tree),
                                    ''))


    def check_comment(self):
        "Test '#' directive: 3"
        t = template.compile_template( "<?#comment text?>real text" )
        self.test_val('t[0].tree',
                      ('',
                       (template.COMMENT, 'comment text'),
                       'real text'))

        t = template.compile_template( "<?# comment text?>real text" )
        self.test_val('t[0].tree',
                      ('',
                       (template.COMMENT, 'comment text'),
                      'real text'))

        # Test with newlines
        t = template.compile_template( "<?#comment\ntext\nline3?>real text" )
        self.test_val('t[0].tree',
                      ('',
                       (template.COMMENT, 'comment\ntext\nline3'),
                      'real text'))



    def shutdown(self):
        del self.namespace
    
test_scenarios = [TemplateTest]

if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios (scenarios, options)


