def setup(self):
     """
     Setup the test cases
     """
     self.rng = random.Random()
     self.connector = RandomConnector(self.rng)
     self.level = (LevelBuilder().build())
 def setup(self):
     """
     Setup the test cases
     """
     self.rng = random.Random()
     self.connector = RandomConnector(self.rng)
     self.level = (LevelBuilder().build())
class TestRandomConnector:
    """
    Tests for RandomConnector class
    """
    def __init__(self):
        """
        Default constructor
        """
        self.connector = None
        self.level = None
        self.rng = None

    def setup(self):
        """
        Setup the test cases
        """
        self.rng = random.Random()
        self.connector = RandomConnector(self.rng)
        self.level = (LevelBuilder().build())

    def test_connect_two_sections(self):
        """
        Test that two adjacent sections can be connected
        """
        section1 = new_section((0, 0), (10, 5), self.level, self.rng)
        section2 = new_section((0, 6), (10, 10), self.level, self.rng)

        mark_neighbours(section1, section2)

        sections = [section1, section2]

        connected_sections = self.connector.connect_sections(sections)

        assert_that(connected_sections[1],
                    is_(equal_to(
                        list(section_connections(connected_sections[0]))[0].connection)))

    def test_connecting_2x2_grid(self):
        """
        Test that 2x2 grid is fully connected
        """
        section00 = new_section((0, 0), (5, 5), self.level, self.rng)
        section10 = new_section((6, 0), (10, 5), self.level, self.rng)
        section01 = new_section((0, 6), (5, 10), self.level, self.rng)
        section11 = new_section((6, 6), (10, 10), self.level, self.rng)

        mark_neighbours(section00, section10)
        mark_neighbours(section00, section01)
        mark_neighbours(section10, section11)
        mark_neighbours(section01, section11)

        sections = [section00, section10, section01, section11]

        connected_sections = self.connector.connect_sections(sections)

        assert_that(connected_sections, has_length(4))

        for section in connected_sections:
            assert_that(list(section_connections(section)),
                        has_length(greater_than(0)))
            assert_that(is_connected(section))

    def test_connect_row_of_sections(self):
        """
        Test special case where connections have to branch

        Row of Sections is connected, starting from the middle
        RandomConnector can not connect this in one path, but has to branch
        """
        section0 = new_section((0, 0), (10, 10), self.level, self.rng)
        section1 = new_section((11, 0), (20, 10), self.level, self.rng)
        section2 = new_section((21, 0), (30, 10), self.level, self.rng)
        section3 = new_section((31, 0), (40, 10), self.level, self.rng)
        section4 = new_section((41, 0), (50, 10), self.level, self.rng)

        mark_neighbours(section0, section1)
        mark_neighbours(section1, section2)
        mark_neighbours(section2, section3)
        mark_neighbours(section3, section4)

        sections = [section0, section1, section2, section3, section4]

        connected_sections = self.connector.connect_sections(sections, section2)  # noqa

        for section in connected_sections:
            assert_that(list(section_connections(section)),
                        has_length(greater_than(0)))
            assert_that(is_connected(section))
class TestRandomConnector:
    """
    Tests for RandomConnector class
    """
    def __init__(self):
        """
        Default constructor
        """
        self.connector = None
        self.level = None
        self.rng = None

    def setup(self):
        """
        Setup the test cases
        """
        self.rng = random.Random()
        self.connector = RandomConnector(self.rng)
        self.level = (LevelBuilder().build())

    def test_connect_two_sections(self):
        """
        Test that two adjacent sections can be connected
        """
        section1 = new_section((0, 0), (10, 5), self.level, self.rng)
        section2 = new_section((0, 6), (10, 10), self.level, self.rng)

        mark_neighbours(section1, section2)

        sections = [section1, section2]

        connected_sections = self.connector.connect_sections(sections)

        assert_that(
            connected_sections[1],
            is_(
                equal_to(
                    list(section_connections(
                        connected_sections[0]))[0].connection)))

    def test_connecting_2x2_grid(self):
        """
        Test that 2x2 grid is fully connected
        """
        section00 = new_section((0, 0), (5, 5), self.level, self.rng)
        section10 = new_section((6, 0), (10, 5), self.level, self.rng)
        section01 = new_section((0, 6), (5, 10), self.level, self.rng)
        section11 = new_section((6, 6), (10, 10), self.level, self.rng)

        mark_neighbours(section00, section10)
        mark_neighbours(section00, section01)
        mark_neighbours(section10, section11)
        mark_neighbours(section01, section11)

        sections = [section00, section10, section01, section11]

        connected_sections = self.connector.connect_sections(sections)

        assert_that(connected_sections, has_length(4))

        for section in connected_sections:
            assert_that(list(section_connections(section)),
                        has_length(greater_than(0)))
            assert_that(is_connected(section))

    def test_connect_row_of_sections(self):
        """
        Test special case where connections have to branch

        Row of Sections is connected, starting from the middle
        RandomConnector can not connect this in one path, but has to branch
        """
        section0 = new_section((0, 0), (10, 10), self.level, self.rng)
        section1 = new_section((11, 0), (20, 10), self.level, self.rng)
        section2 = new_section((21, 0), (30, 10), self.level, self.rng)
        section3 = new_section((31, 0), (40, 10), self.level, self.rng)
        section4 = new_section((41, 0), (50, 10), self.level, self.rng)

        mark_neighbours(section0, section1)
        mark_neighbours(section1, section2)
        mark_neighbours(section2, section3)
        mark_neighbours(section3, section4)

        sections = [section0, section1, section2, section3, section4]

        connected_sections = self.connector.connect_sections(
            sections, section2)  # noqa

        for section in connected_sections:
            assert_that(list(section_connections(section)),
                        has_length(greater_than(0)))
            assert_that(is_connected(section))