#!/www/python/bin/python
"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/ui/test/utest_crumbs.py $
$Id: utest_crumbs.py 26245 2005-02-25 14:45:59Z dbinger $
"""
from quixote import enable_ptl; enable_ptl()

from dulcinea.ui.crumbs import get_crumb_tree, format_crumbs
from dulcinea.ui.crumbs import get_path_directory_list, get_exports
from dulcinea.ui.directory import DynamicExportingDirectory
from quixote.directory import Directory
from quixote.http_request import HTTPRequest
from quixote.logger import DefaultLogger
from quixote.publish import Publisher
from sancho.utest import UTest
import dulcinea.ui.crumbs
import quixote.publish
import sys


class PrintLogger(DefaultLogger):

    def __init__(self):
        self.error_log = sys.stdout
        self.access_log = None
        self.error_email = None

class CrumbTest (UTest):

    def _pre(self):
        quixote.publish._publisher = None

    def _post(self):
        quixote.publish._publisher = None

    def test_get_path_directory_list(self):
        class Dir(DynamicExportingDirectory):
            def _q_traverse(self, component):
                return get_path_directory_list()
        publisher = Publisher(Dir(), logger=PrintLogger())
        publisher._set_request(HTTPRequest(
            None, dict(SCRIPT_NAME='',
                       PATH_INFO='/')))
        assert get_path_directory_list() == []
        directory = Dir()
        assert directory._q_traverse('') == [('./', directory)]

    def test_get_exports1(self):
        exports = [('', '_q_index', 'crumb', 'title')]
        class Dir(DynamicExportingDirectory):
            def get_exports(self):
                return exports
        assert get_exports(Dir()) == exports

    def test_get_exports_fallback(self):
        class Dir(Directory):
            _q_exports = ['', ('a','b'), 'foo']
        print get_exports(Dir())
        assert get_exports(Dir()) == [('', '_q_index', None, ''),
                                      ('a', 'b', '', ''),
                                      ('foo', 'foo', '', '')]

    def test_get_crumb_tree(self):
        class Dir3(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb3', 'title3'),
                        ('c3', 'n3', 'acrumb', 'atitle')]

            def _q_traverse(self, components):
                return get_crumb_tree()
            n3 = 'ok'
        class Dir2(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb2', 'title2'),
                        ('c2', 'n2', 'acrumb', 'atitle')]
            n2 = Dir3()
        class Dir1(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb', 'title'),
                        ('c1', 'n1', 'acrumb', 'atitle')]
            n1 = Dir2()

        directory = Dir1()
        publisher = Publisher(directory, logger=PrintLogger())
        publisher._set_request(HTTPRequest(
            None, dict(SCRIPT_NAME='',
                       PATH_INFO='/')))
        print directory._q_traverse(['c1', 'c2', ''])
        assert directory._q_traverse(['c1', 'c2', ''])  == [
            [('./../../', 'crumb', 'title'),
             ('./../../c1', 'acrumb', 'atitle')],
            [('./../', 'crumb2', 'title2'),
             ('./../c2', 'acrumb', 'atitle')]]

    def test_get_crumb_tree_no_menu(self):
        class Dir2(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb3', 'title3'),
                        ('c2', 'n2', 'acrumb', 'atitle')]

            def _q_traverse(self, components):
                return get_crumb_tree()
            n2 = 'ok'
        class Dir(DynamicExportingDirectory):
            def get_exports(self):
                return [('', 'c', '', 'title'),
                        ('c1', 'n1', 'no see crumb', 'no see title')]
            n1 = Dir2()

        directory = Dir()
        publisher = Publisher(directory, logger=PrintLogger())
        publisher._set_request(HTTPRequest(
            None, dict(SCRIPT_NAME='',
                       PATH_INFO='/')))
        print directory._q_traverse(['c1', 'c2'])
        assert directory._q_traverse(['c1', 'c2'])  == [
            [('./', 'crumb3', 'title3'), ('./c2', 'acrumb', 'atitle')]]

    def test_format_crumbs(self):
        class Dir3(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb', 'title'),
                        ('c3', 'n3', 'acrumb', 'atitle')]

            def _q_traverse(self, components):
                return format_crumbs()
            n3 = 'ok'
        class Dir2(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb', 'title'),
                        ('c2', 'n2', 'acrumb', 'atitle')]
            n2 = Dir3()
        class Dir1(DynamicExportingDirectory):
            def get_exports(self):
                return [('', '_q_index', 'crumb', 'title'),
                        ('c1', 'n1', 'acrumb', 'atitle')]
            n1 = Dir2()

        directory = Dir1()
        publisher = Publisher(directory, logger=PrintLogger())
        publisher._set_request(HTTPRequest(
            None, dict(SCRIPT_NAME='',
                       PATH_INFO='/')))
        crumb_menus = False
        def get_config_value(name):
            return crumb_menus
        dulcinea.ui.crumbs.get_config_value = get_config_value
        print repr(str(directory._q_traverse(['c1', 'c2', ''])))
        crumb_menus = True
        print repr(str(directory._q_traverse(['c1', 'c2', ''])))

if __name__ == "__main__":
    CrumbTest()
