"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/qpy/test/utest_translate.py $
$Id: utest_translate.py 30201 2007-11-02 15:25:24Z dbinger $
"""
from sancho.utest import UTest, raises
from qpy.translate import translate_tokens, get_parse_tree, ptree, symtree
from qpy.translate import statement_tree, expr_tree


class TestTranslate(UTest):

    def test_translate(self):
        src = (
            'def a:xml():\n'
            '    "ok"\n')
        translated = translate_tokens(src)
        assert 'def a__xml_template__(' in translated, translated

        src = ('def a [html] ():\n'
               '    "ok"\n')
        assert translated == translate_tokens(src)
        assert 'def a__xml_template__(' in translated

        src = (
            'def a:str():\n'
            '    "ok"\n')
        translated = translate_tokens(src)
        assert 'def a__str_template__(' in translated

        src = ('def a [plain] ():\n'
               '    "ok"\n')
        assert translated == translate_tokens(src)
        assert 'def a__str_template__(' in translated

    def test_tree_utils(self):
        src = ('def a():\n'
               '    "ok"\n')
        tree = get_parse_tree(src, 'fake')
        assert type(tree) is list
        raises(SyntaxError, get_parse_tree, 'bogus' + src)
        raises(SyntaxError, get_parse_tree, 'bogus' + src, 'fake')
        statement_tree(src)
        expr_tree('x+1')
        symtree(tree)
        ptree(tree)
        raises(AssertionError, symtree, [-1])

if __name__ == '__main__':
    TestTranslate()
