def test_patch_twice():
    MockTurtle.monkey_patch()
    try:
        with pytest.raises(RuntimeError,
                           match=r'MockTurtle is already monkey patched\.'):
            MockTurtle.monkey_patch()
    finally:
        MockTurtle.remove_monkey_patch()
def test_is_patched():
    assert not MockTurtle.is_patched()

    MockTurtle.monkey_patch()
    try:
        assert MockTurtle.is_patched()
    finally:
        MockTurtle.remove_monkey_patch()

    assert not MockTurtle.is_patched()
    def test_bounds_after_monkey_patch(self):
        # SETUP
        expected_width = 300
        expected_height = 200

        # EXEC
        MockTurtle.monkey_patch(canvas=Canvas(expected_width, expected_height))
        width = turtle.window_width()  # @UndefinedVariable
        height = turtle.window_height()  # @UndefinedVariable

        # VERIFY
        self.assertEqual(expected_width, width)
        self.assertEqual(expected_height, height)
def test_bounds_after_monkey_patch():
    expected_width = 300
    expected_height = 200

    MockTurtle.monkey_patch(canvas=Canvas(expected_width, expected_height))
    try:
        width = turtle.window_width()
        height = turtle.window_height()
    finally:
        MockTurtle.remove_monkey_patch()

    assert width == expected_width
    assert height == expected_height
    def test_monkey_patch_anonymous_turtle(self):
        MockTurtle.monkey_patch()
        expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
"""

        turtle.fd(100)  # @UndefinedVariable
        report = MockTurtle.get_all_reports()

        self.assertEqual(expected_report.splitlines(), report)
    def test_monkey_patch_multiple_turtles(self):
        MockTurtle.monkey_patch()
        expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
create_line
    100
    0
    100
    100
    fill='black'
    pensize=1
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
create_line
    100
    0
    100
    -100
    fill='black'
    pensize=1
"""

        t1 = turtle.Turtle()
        t1.begin_fill()
        t1.fd(100)
        t1.right(90)
        t1.fd(100)
        t2 = turtle.Turtle()
        t2.begin_fill()
        t2.fd(100)
        t2.left(90)
        t2.fd(100)
        report = MockTurtle.get_all_reports()

        self.maxDiff = None
        self.assertEqual(expected_report.splitlines(), report)
    def test_monkey_patch_new_turtle(self):
        MockTurtle.monkey_patch()
        expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
"""

        t = turtle.Turtle()
        t.fd(100)
        report = MockTurtle.get_all_reports()

        self.assertEqual(expected_report.splitlines(), report)
def test_display_image_position_with_size():
    expected_report = """\
create_image
    110
    180
    image='UE5HX0lNQUdFX0RBVEE='
"""

    MockTurtle.monkey_patch(Canvas(width=200, height=400))
    try:
        t = MockTurtle()
        image_data = b'PNG_IMAGE_DATA'

        LivePng(image_data).display((10, 20))

        report = t.report
    finally:
        MockTurtle.remove_monkey_patch()

    assert report == expected_report.splitlines()
Exemple #9
0
def patched_turtle():
    MockTurtle.monkey_patch()
    yield
    MockTurtle.remove_monkey_patch()