def test_sub_routes_with_duplicated_sub_route_names(self):
        generalized_sub_route_1 = Route(None, 'sub_route_1')
        generalized_sub_route_2 = Route(None, 'sub_route_2')
        generalized_route = Route(
            FAKE_VIEW,
            None,
            [generalized_sub_route_1, generalized_sub_route_2],
            )

        duplicated_sub_route_name = 'duplicated_sub_route_name'
        specialized_sub_route_1 = generalized_sub_route_1.create_specialization(
            additional_sub_routes=[Route(None, duplicated_sub_route_name)],
            )
        specialized_sub_route_2 = generalized_sub_route_2.create_specialization(
            additional_sub_routes=[Route(None, duplicated_sub_route_name)],
            )

        with assert_raises_substring(
            DuplicatedRouteError,
            duplicated_sub_route_name,
            ):
            generalized_route.create_specialization(
                specialized_sub_routes=[
                    specialized_sub_route_1,
                    specialized_sub_route_2,
                    ],
                )
    def test_sub_route_specializing_current_route(self):
        generalized_route = Route(None, FAKE_ROUTE_NAME)

        sub_route = generalized_route.create_specialization()

        with assert_raises_substring(DuplicatedRouteError, FAKE_ROUTE_NAME):
            generalized_route.create_specialization(
                specialized_sub_routes=[sub_route],
                )
 def test_sub_route_duplicating_route_name(self):
     duplicated_route_name = 'route_name'
     generalized_route = Route(FAKE_VIEW, duplicated_route_name)
     sub_route = Route(None, duplicated_route_name)
     with assert_raises_substring(
         DuplicatedRouteError,
         duplicated_route_name,
         ):
         generalized_route.create_specialization(
             additional_sub_routes=[sub_route],
             )
    def test_specializing_sub_route(self):
        generalized_sub_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)
        generalized_route = Route(None, None, [generalized_sub_route])

        intermediate_route = generalized_route.create_specialization()

        specialized_sub_route = generalized_sub_route.create_specialization()
        specialized_route = intermediate_route.create_specialization(
            specialized_sub_routes=[specialized_sub_route],
            )

        eq_((specialized_sub_route,), tuple(specialized_route.sub_routes))
    def test_sibling_specializations_with_different_attributes(self):
        generalized_route = Route(
            None,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )

        specialized_route_1 = \
            generalized_route.create_specialization(FAKE_VIEW)
        specialized_route_2 = generalized_route.create_specialization()

        assert_non_equivalent(specialized_route_1, specialized_route_2)
    def test_non_existing_named_sub_route(self):
        generalized_route = Route(None, None)

        non_existing_sub_route_name = 'non_existing_route'
        generalized_sub_route = Route(None, non_existing_sub_route_name)
        specialized_sub_route = generalized_sub_route.create_specialization()

        with assert_raises_substring(
            InvalidSpecializationError,
            non_existing_sub_route_name,
            ):
            generalized_route.create_specialization(
                specialized_sub_routes=[specialized_sub_route],
                )
    def test_non_existing_unnamed_sub_route(self):
        unnamed_sub_route = Route(None, None)
        generalized_route = Route(None, None, [unnamed_sub_route])

        unnamed_generalized_sub_route = Route(FAKE_VIEW, None)
        unnamed_specialized_sub_route = \
            unnamed_generalized_sub_route.create_specialization()

        with assert_raises_substring(
            InvalidSpecializationError,
            repr(unnamed_generalized_sub_route),
            ):
            generalized_route.create_specialization(
                specialized_sub_routes=[unnamed_specialized_sub_route],
                )
    def test_sub_route_collection_against_different_sub_route_collection(self):
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )
        specialized_route_1 = generalized_route.create_specialization()
        specialized_route_2 = generalized_route.create_specialization(
            additional_sub_routes=[Route(None, None)],
            )

        assert_non_equivalent(
            specialized_route_1.sub_routes,
            specialized_route_2.sub_routes,
            )
def test_no_change():
    generalized_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME, FAKE_SUB_ROUTES)
    specialized_route = generalized_route.create_specialization()

    eq_(FAKE_ROUTE_NAME, specialized_route.name)
    eq_(FAKE_VIEW, specialized_route.view)
    eq_(FAKE_SUB_ROUTES, tuple(specialized_route.sub_routes))
    def test_no_change(self):
        generalized_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)
        intermediate_route = generalized_route.create_specialization()
        specialized_route = intermediate_route.create_specialization()

        eq_(FAKE_ROUTE_NAME, specialized_route.name)
        eq_(FAKE_VIEW, specialized_route.view)
    def test_unnamed_sub_routes_specializing_common_generalization(self):
        generalized_sub_route = Route(None, None)
        generalized_route = Route(None, None, [generalized_sub_route])

        specialized_sub_route_1 = \
            generalized_sub_route.create_specialization(FAKE_VIEW)
        specialized_sub_route_2 = generalized_sub_route.create_specialization()

        with assert_raises_substring(
            InvalidSpecializationError,
            'cannot be specialized twice',
            ):
            generalized_route.create_specialization(specialized_sub_routes=[
                specialized_sub_route_1,
                specialized_sub_route_2,
                ])
    def test_specialization_vs_non_specialization(self):
        generalized_route = Route(None, FAKE_ROUTE_NAME, FAKE_SUB_ROUTES)
        specialized_route = generalized_route.create_specialization()

        route = Route(None, FAKE_ROUTE_NAME, FAKE_SUB_ROUTES)

        assert_non_equivalent(specialized_route, route)
    def test_specializations_from_equivalent_generalizations(self):
        generalized_route_1 = Route(
            None,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )
        specialized_route_1 = generalized_route_1.create_specialization()

        generalized_route_2 = Route(
            None,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )
        specialized_route_2 = generalized_route_2.create_specialization()

        assert_equivalent(specialized_route_1, specialized_route_2)
    def test_previously_unset(self):
        generalized_route = Route(None, FAKE_ROUTE_NAME)

        view = object()
        specialized_route = generalized_route.create_specialization(view=view)

        eq_(view, specialized_route.view)
    def test_getting_additional_sub_route_by_name(self):
        sub_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)
        generalized_route = Route(None, None, [sub_route])
        specialized_route = generalized_route.create_specialization()

        retrieved_route = specialized_route.get_route_by_name(FAKE_ROUTE_NAME)
        eq_(sub_route, retrieved_route)
    def test_existing_unnamed_sub_routes(self):
        generalized_sub_route = Route(FAKE_VIEW, None)
        sub_route = Route(None, None)
        generalized_route = Route(
            None,
            FAKE_ROUTE_NAME,
            [generalized_sub_route, sub_route],
            )

        specialized_sub_route = generalized_sub_route.create_specialization()
        specialized_route = generalized_route.create_specialization(
            specialized_sub_routes=[specialized_sub_route],
            )

        # A second sub-route without name shouldn't override the first sub-route
        eq_((specialized_sub_route, sub_route), tuple(specialized_route.sub_routes))
    def test_existing_named_sub_routes(self):
        generalized_sub_route_name = 'sub_route_name'
        generalized_sub_route = Route(None, generalized_sub_route_name)
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            [generalized_sub_route],
            )

        specialized_sub_route = generalized_sub_route.create_specialization(
            view=FAKE_VIEW,
            )
        specialized_route = generalized_route.create_specialization(
            specialized_sub_routes=[specialized_sub_route],
            )

        eq_((specialized_sub_route,), tuple(specialized_route.sub_routes))
    def test_getting_specialized_sub_route_by_name(self):
        generalized_route = Route(None, FAKE_ROUTE_NAME)
        specialized_route = generalized_route.create_specialization(
            specialized_sub_routes=[]
            )

        retrieved_route = specialized_route.get_route_by_name(FAKE_ROUTE_NAME)
        eq_(specialized_route, retrieved_route)
    def test_previously_set(self):
        generalized_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)

        overridden_view = object()
        specialized_route = \
            generalized_route.create_specialization(view=overridden_view)

        eq_(overridden_view, specialized_route.view)
    def test_sub_route_with_non_specialized_route(self):
        sub_route_name = 'sub_route_name'
        generalized_sub_route = Route(None, sub_route_name)
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            [generalized_sub_route],
            )
        unspecialized_route = Route(None, sub_route_name)

        with assert_raises_substring(
            InvalidSpecializationError,
            sub_route_name,
            ):
            generalized_route.create_specialization(
                specialized_sub_routes=[unspecialized_route],
                )
    def test_sub_route_collection_against_non_sub_route_collection(self):
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )
        specialized_route = generalized_route.create_specialization()

        assert_non_equivalent(specialized_route.sub_routes, None)
    def test_sibling_specializations_with_same_sub_routes(self):
        generalized_sub_route = Route(None, None, FAKE_SUB_ROUTES)
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            [generalized_sub_route],
            )

        specialized_sub_route_1 = generalized_sub_route.create_specialization()
        specialized_route_1 = generalized_route.create_specialization(
            specialized_sub_routes=[specialized_sub_route_1],
            )

        specialized_sub_route_2 = generalized_sub_route.create_specialization()
        specialized_route_2 = generalized_route.create_specialization(
            specialized_sub_routes=[specialized_sub_route_2],
            )

        assert_equivalent(specialized_route_1, specialized_route_2)
    def test_adding_duplicate_sub_route_name(self):
        generalized_sub_route = Route(None, FAKE_ROUTE_NAME)
        generalized_route = Route(FAKE_VIEW, None, [generalized_sub_route])

        duplicated_sub_route_name = 'duplicated_sub_route_name'
        specialized_sub_route = generalized_sub_route.create_specialization(
            additional_sub_routes=[Route(None, duplicated_sub_route_name)],
            )

        additional_sub_route = Route(None, duplicated_sub_route_name)

        with assert_raises_substring(
            DuplicatedRouteError,
            duplicated_sub_route_name,
            ):
            generalized_route.create_specialization(
                additional_sub_routes=[additional_sub_route],
                specialized_sub_routes=[specialized_sub_route],
                )
    def test_specializing_with_invalid_sub_route(self):
        """
        The same validation routines as in the single inheritance case should be
        used.

        """
        sub_route_1 = Route(None, FAKE_ROUTE_NAME)
        generalized_route = Route(None, None, [sub_route_1])

        intermediate_route = generalized_route.create_specialization()

        sub_route_2 = Route(FAKE_VIEW, FAKE_ROUTE_NAME)
        with assert_raises_substring(
            InvalidSpecializationError,
            FAKE_ROUTE_NAME
            ):
            intermediate_route.create_specialization(
                specialized_sub_routes=[sub_route_2.create_specialization()],
                )
    def test_length_with_additional_sub_routes(self):
        generalized_route = \
            Route(FAKE_VIEW, FAKE_ROUTE_NAME, FAKE_SUB_ROUTES)

        additional_sub_routes = [Route(None, 'additional_sub_route')]
        specialized_route = generalized_route.create_specialization(
            additional_sub_routes=additional_sub_routes,
            )

        expected_length = len(FAKE_SUB_ROUTES) + len(additional_sub_routes)
        eq_(expected_length, len(specialized_route.sub_routes))
    def test_sibling_specializations_with_same_attributes(self):
        generalized_route = Route(
            FAKE_VIEW,
            FAKE_ROUTE_NAME,
            FAKE_SUB_ROUTES,
            )

        specialized_route_1 = generalized_route.create_specialization()
        specialized_route_2 = generalized_route.create_specialization()

        assert_equivalent(specialized_route_1, specialized_route_2)
    def test_additional_sub_route(self):
        generalized_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)

        intermediate_route = generalized_route.create_specialization()

        sub_route = Route(None, None)
        specialized_route = intermediate_route.create_specialization(
            additional_sub_routes=[sub_route],
            )

        eq_((sub_route,), tuple(specialized_route.sub_routes))
    def test_additional_duplicated_sub_route(self):
        sub_route_1 = Route(None, FAKE_ROUTE_NAME)
        generalized_route = Route(FAKE_VIEW, None, [sub_route_1])

        intermediate_route = generalized_route.create_specialization()

        sub_route_2 = Route(None, FAKE_ROUTE_NAME)
        with assert_raises_substring(DuplicatedRouteError, FAKE_ROUTE_NAME):
            intermediate_route.create_specialization(
                additional_sub_routes=[sub_route_2],
                )
def test_repr():
    generalized_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME, FAKE_SUB_ROUTES)

    view = object()
    specialized_route = generalized_route.create_specialization(view)

    expected_repr = '<Specialization of {!r} with view {!r}>'.format(
        generalized_route,
        view,
        )
    eq_(expected_repr, repr(specialized_route))
    def test_repr(self):
        sub_route = Route(None, 'sub_route_1')
        generalized_sub_route = Route(FAKE_VIEW, FAKE_ROUTE_NAME)
        generalized_route = \
            Route(None, None, [sub_route, generalized_sub_route])

        additional_sub_routes = [Route(None, 'sub_route_2')]
        specialized_sub_routes = \
            [generalized_sub_route.create_specialization(object())]
        specialized_route = generalized_route.create_specialization(
            additional_sub_routes=additional_sub_routes,
            specialized_sub_routes=specialized_sub_routes,
            )

        expected_repr = \
            '_RouteSpecializationCollection({!r}, {!r}, {!r})'.format(
                generalized_route.sub_routes,
                specialized_sub_routes,
                additional_sub_routes,
                )
        eq_(expected_repr, repr(specialized_route.sub_routes))