"""
open/DurusWorks/qp/fill/test/utest_directory.py

"""
from qp.fill.directory import Directory
from qp.fill.directory import get_path_directory_list, get_crumb_tree
from qp.pub.common import clear_publisher
from qp.pub.hit import Hit
from qp.pub.publish import Publisher
from sancho.utest import UTest


class CrumbTest (UTest):

    def _pre(self):
        publisher = Publisher()
        hit = Hit(None, dict(PATH_INFO='/', SERVER_NAME='example.org'))
        publisher.process_hit(hit)

    def _post(self):
        clear_publisher()

    def test_get_path_directory_list(self):
        exports = [('', '_q_index', 'crumb', 'title')]
        class Dir(Directory):
            def get_exports(self):
                return exports
            def _q_traverse(self, component):
                return get_path_directory_list()
        assert get_path_directory_list() == []
        directory = Dir()
        assert directory._q_traverse('') == [('./', directory)]

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

    def test_get_crumb_tree(self):
        class Dir3(Directory):
            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(Directory):
            def get_exports(self):
                return [('', '_q_index', 'crumb2', 'title2'),
                        ('c2', 'n2', 'acrumb', 'atitle')]
            n2 = Dir3()
        class Dir1(Directory):
            def get_exports(self):
                return [('', '_q_index', 'crumb', 'title'),
                        ('c1', 'n1', 'acrumb', 'atitle')]
            n1 = Dir2()

        directory = Dir1()
        assert directory._q_traverse(['c1', 'c2', ''])  == [
            [('./../../', 'crumb', 'title', 'menu_'),
             ('./../../c1', 'acrumb', 'atitle', 'menu_c1')],
            [('./../', 'crumb2', 'title2', 'menu_'),
             ('./../c2', 'acrumb', 'atitle', 'menu_c2')]]

    def test_get_crumb_tree_no_index_crumb_still_has_menu(self):
        class Dir2(Directory):
            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(Directory):
            def get_exports(self):
                return [('', 'c', '', 'title'),
                        ('c1', 'n1', 'still see crumb', 'still see title')]
            n1 = Dir2()

        directory = Dir()
        assert directory._q_traverse(['c1', 'c2'])  == [
            [('./../c1', 'still see crumb', 'still see title', 'menu_c1')],
            [('./', 'crumb3', 'title3', 'menu_'),
             ('./c2', 'acrumb', 'atitle', 'menu_c2')]]


if __name__ == "__main__":
    CrumbTest()
