
import string
from unittest import TestScenario, parse_args, run_scenarios
from quixote import template

class TemplateEvalTest(TestScenario):
    "Test of the PTL template code"
    def setup(self):
        self.namespace = {
            'true_cond': 1, 'false_cond':0,
            'one_value': 1, 'two_value': 2, 'i':0,
            'list_sequence': ['a', 'b', 'c'],
            'tuple_sequence': ('d', 'e', 'f'),
            'string_sequence': '123',
            
            }
        self.template = template.Template()
        
    def check_literals(self):
        "Test trees that only contain simple strings: 2"

        for tree in [('literal text',),
                     ('literal text', 'second block', '  final block') ]:
            self.template.tree = tree
            self.test_val('self.template(self.namespace)',
                          string.join(tree, '') )

    def check_eval(self):
        "Check EVAL child: 1"
        self.template.tree = ( ('EVAL', 'one_value+two_value'), )
        self.test_val('self.template(self.namespace)',
                      '3' )

    def check_stmt(self):
        "Check STMT child: 2"
        self.template.tree = ( ('STMT',
                                'three_value = one_value+two_value'), )
        self.test_val('self.template(self.namespace)',
                      '' )
        self.test_val('self.namespace["three_value"]', 3)
        
    def check_block(self):
        "Check BLOCK child: 3"
        self.template.tree = ( ('BLOCK', """
three_value = one_value+two_value
computed_value = 'a'*5 + chr(99)*( 6-4 )
"""), )
        self.test_val('self.template(self.namespace)',
                      '' )
        self.test_val('self.namespace["three_value"]', 3)
        self.test_val('self.namespace["computed_value"]', 'aaaaacc')


    def check_if(self):
        "Check IF child: 4"
        self.template.tree = ( ('IF',
                                ("true_cond", ("true block",)),
                                "not this else block"),)
        self.test_val('self.template(self.namespace)', 'true block' )

        self.template.tree = ( ('IF',
                                ("false_cond", ("not this if block",)),
                                ("else block",)),)
        self.test_val('self.template(self.namespace)', 'else block' )

        self.template.tree = ( ('IF',
                                ("false_cond", ("not this if block",)),
                                ("true_cond", ("true block",)),
                                ("else block",)),)
        self.test_val('self.template(self.namespace)', 'true block' )

        self.template.tree = ( ('IF',
                                ("false_cond", ("not this if block",)),
                                ("false_cond", ("not this elif block",)),
                                ("else block",)),)
        self.test_val('self.template(self.namespace)', 'else block' )

    def check_while(self):
        "Check WHILE child: 2"
        self.template.tree = ( ('WHILE', 'i<10',
                                ("start ", ('STMT', 'i=i+1'), "end")
                               ), )
        self.test_val('self.template(self.namespace)', 'start end' * 10 )
        self.test_val('self.namespace["i"]', 10)

    def check_for(self):
        "Check FOR child: 3"
        self.template.tree = ( ('FOR', 'element', 'list_sequence',
                                ( ("EVAL", 'element'), )
                               ), )
        self.test_val('self.template(self.namespace)', 'abc' )

        self.template.tree = ( ('FOR', 'element', 'tuple_sequence',
                                ( ("EVAL", 'element'), )
                               ), )
        self.test_val('self.template(self.namespace)', 'def' )

        self.template.tree = ( ('FOR', 'element', 'string_sequence',
                                ( ("EVAL", 'element'), )
                               ), )
        self.test_val('self.template(self.namespace)', '123' )

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

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


