Ejemplo n.º 1
0
def test_get_color_rgb():
    t = MockTurtle()
    expected_color = (1.0, 0.0, 0.5)
    t.color(expected_color)
    color = t.color()

    assert color == (expected_color, expected_color)
Ejemplo n.º 2
0
def test_forgotten_end_fill(patched_turtle):
    expected_report = """\
create_line
    0
    0
    100
    0
    fill='#ff0000'
    pensize=1
create_line
    100
    0
    100
    100
    fill='#ff0000'
    pensize=1
"""

    t = MockTurtle()
    t.color('red', 'blue')
    t.begin_fill()
    for _ in range(2):
        t.fd(100)
        t.right(90)
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 3
0
    def test_get_color_rgb(self):
        t = MockTurtle()
        expected_color = (1.0, 0.0, 0.5)
        t.color(expected_color)
        color = t.color()

        self.assertEqual((expected_color, expected_color), color)
Ejemplo n.º 4
0
def test_get_color_names():
    t = MockTurtle()
    t.color('blue')
    color = t.color()

    # Before Python 3.6, dicts were not ordered, and either name was possible.
    assert color in (('blue', 'blue'), ('blue1', 'blue1'))
Ejemplo n.º 5
0
    def test_bgcolor(self):
        # SETUP
        expected_report = """\
bgcolor
    fill='#00ff00'
    outline=''
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1"""

        # EXEC
        t = MockTurtle()
        t.fd(100)
        color1 = t.screen.bgcolor()
        t.screen.bgcolor('green')
        color2 = t.screen.bgcolor()
        report = t.report

        # VERIFY
        self.assertEqual(expected_report.splitlines(), report)
        self.assertEqual('white', color1)
        self.assertEqual('green', color2)
Ejemplo n.º 6
0
def test_get_colour(colour_in, colour_out):
    t = MockTurtle()
    t.pencolor(colour_in)

    c = t.pencolor()

    assert c == colour_out
Ejemplo n.º 7
0
    def test_forgotten_end_fill(self):
        # SETUP
        expected_report = """\
create_line
    0
    0
    100
    0
    fill='#ff0000'
    pensize=1
create_line
    100
    0
    100
    100
    fill='#ff0000'
    pensize=1
"""

        # EXEC
        t = MockTurtle()
        t.color('red', 'blue')
        t.begin_fill()
        for _ in range(2):
            t.fd(100)
            t.right(90)
        report = t.report

        # VERIFY
        self.assertEqual(expected_report.splitlines(), report)
def test_bounds():
    expected_width = 800
    expected_height = 600

    t = MockTurtle(canvas=Canvas(expected_width, expected_height))
    width = t.window_width()
    height = t.window_height()

    assert width == expected_width
    assert height == expected_height
Ejemplo n.º 9
0
def test_bounds():
    expected_width = 800
    expected_height = 600

    t = MockTurtle(canvas=Canvas(expected_width, expected_height))
    width = t.getscreen().window_width()
    height = t.getscreen().window_height()
    size = t.getscreen().screensize()

    assert width == expected_width
    assert height == expected_height
    assert size == (expected_width, expected_height)
Ejemplo n.º 10
0
def test_clearstamp(patched_turtle):
    expected_report = """\
create_polygon
    0
    0
    -9
    -5
    -7
    0
    -9
    5
    fill='black'
    outline=''
create_line
    0
    0
    -9
    -5
    fill='black'
    pensize=1
create_line
    -9
    -5
    -7
    0
    fill='black'
    pensize=1
create_line
    -7
    0
    -9
    5
    fill='black'
    pensize=1
create_line
    -9
    5
    0
    0
    fill='black'
    pensize=1
"""

    t = MockTurtle()
    stamp1 = t.stamp()
    t.up()
    t.forward(100)
    stamp2 = t.stamp()
    t.clearstamp(stamp2)
    report = t.report

    assert report == expected_report.splitlines()
    assert stamp1 != stamp2
Ejemplo n.º 11
0
    def test_stamp(self):
        # SETUP
        expected_report = """\
create_polygon
    0
    0
    -9
    -5
    -7
    0
    -9
    5
    0
    0
    fill='#000000'
    outline=''
create_line
    0
    0
    -9
    -5
    fill='#000000'
    pensize=1
create_line
    -9
    -5
    -7
    0
    fill='#000000'
    pensize=1
create_line
    -7
    0
    -9
    5
    fill='#000000'
    pensize=1
create_line
    -9
    5
    0
    0
    fill='#000000'
    pensize=1
"""

        # EXEC
        t = MockTurtle()
        t.stamp()
        report = t.report

        # VERIFY
        self.assertEqual(expected_report.splitlines(), report)
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
    def test_bounds(self):
        # SETUP
        expected_width = 800
        expected_height = 600

        # EXEC
        t = MockTurtle(canvas=Canvas(expected_width, expected_height))
        width = t.window_width()
        height = t.window_height()

        # VERIFY
        self.assertEqual(expected_width, width)
        self.assertEqual(expected_height, height)
Ejemplo n.º 15
0
    def display(self, position: Position = None, align: str = 'topleft'):
        """ Display this image on the mock turtle's canvas.

        :param position: (x, y) coordinates on the canvas, or None for the
            top-left corner
        :param align: which point in the image to line up with (x, y) - a
            combination of 'top', 'center', or 'bottom' plus 'left', 'center', or
            'right'. If one of the words is missing, it defaults to 'center'.
        """

        png_bytes = self.convert_to_png()
        b64_bytes = standard_b64encode(png_bytes)
        b64_string = b64_bytes.decode('UTF-8')
        MockTurtle.display_image(b64_string, position, align)
Ejemplo n.º 16
0
def test_stamp(patched_turtle):
    expected_report = """\
create_polygon
    0
    0
    -9
    -5
    -7
    0
    -9
    5
    0
    0
    fill='#000000'
    outline=''
create_line
    0
    0
    -9
    -5
    fill='#000000'
    pensize=1
create_line
    -9
    -5
    -7
    0
    fill='#000000'
    pensize=1
create_line
    -7
    0
    -9
    5
    fill='#000000'
    pensize=1
create_line
    -9
    5
    0
    0
    fill='#000000'
    pensize=1
"""

    t = MockTurtle()
    t.stamp()
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 17
0
    def test_scale(self):
        # SETUP
        expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
create_line
    100
    0
    100
    150
    fill='black'
    pensize=1
"""

        # EXEC
        t = MockTurtle(canvas=Canvas())
        t.screen.xscale = 100.0
        t.screen.yscale = 50
        t.fd(1)
        t.right(90)
        t.fd(3)
        report = t.report

        # VERIFY
        self.assertEqual(expected_report.splitlines(), report)
Ejemplo n.º 18
0
def test_scale(patched_turtle):
    expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
create_line
    100
    0
    100
    150
    fill='black'
    pensize=1
"""

    t = MockTurtle(canvas=Canvas())
    t.screen.xscale = 100.0
    t.screen.yscale = 50
    t.fd(1)
    t.right(90)
    t.fd(3)
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 19
0
def test_is_filling():
    t = MockTurtle()
    is_filling1 = t.fill()
    t.begin_fill()
    is_filling2 = t.fill()
    t.end_fill()
    is_filling3 = t.fill()

    assert not is_filling1
    assert is_filling2
    assert not is_filling3
Ejemplo n.º 20
0
def test_dot(patched_turtle):
    """ Draw dot with the rounded ends of a zero-length line. """
    expected_report = """\
create_line
    0
    0
    0
    0
    fill='black'
    pensize=5"""

    t = MockTurtle()
    t.dot()
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 21
0
    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)
Ejemplo n.º 22
0
def test_forward(patched_turtle):
    expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
"""

    t = MockTurtle()
    t.fd(100)
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 23
0
def test_update(patched_turtle):
    expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1
create_line
    100
    0
    100
    200
    fill='black'
    pensize=1
"""
    t = turtle

    t.forward(100)
    t.tracer(10)
    t.right(90)
    t.forward(200)
    t.update()

    assert MockTurtle.get_all_reports() == expected_report.splitlines()
Ejemplo n.º 24
0
    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)
Ejemplo n.º 25
0
def test_offset():
    assert not MockTurtle.is_patched()
    expected_report = """\
create_line
    400
    300
    500
    300
    fill='black'
    pensize=1
"""

    t = MockTurtle(canvas=Canvas(800, 600))
    t.fd(100)
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 26
0
    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)
Ejemplo n.º 27
0
def test_bad_colour(patched_turtle, colour_in, expected_error):
    expected_report = """\
create_line
    0
    0
    100
    0
    fill='black'
    pensize=1"""

    t = MockTurtle()
    t.fd(100)
    with pytest.raises(TurtleGraphicsError, match=re.escape(expected_error)):
        turtle.color(colour_in)
    report = t.report

    assert report == expected_report.splitlines()
Ejemplo n.º 28
0
    def test_bad_attribute(self):
        # SETUP

        # EXEC
        t = MockTurtle()

        # VERIFY
        self.assertFalse(hasattr(t, 'bogus'))
Ejemplo n.º 29
0
def test_repr():
    expected_text = "MockTurtle(100, 0, 10)"

    t = MockTurtle(25, 0, -7)
    t.left(7)
    t.fd(75)
    t.left(10)
    text = repr(t)

    assert text == expected_text
Ejemplo n.º 30
0
    def test_screen_methods_exist(self):
        # SETUP
        t = MockTurtle()

        # EXEC

        # VERIFY
        t.screen.tracer()
        t.screen.update()