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)
def test_write(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='black' pensize=1 create_text 100 0 anchor='sw' fill='black' font=('Arial', 8, 'normal') text='Bob' """ # EXEC t = MockTurtle() t.fd(100) t.write('Bob') report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def __init__(self, canvas=None): self.message_limit = 10000 self.max_width = None self.keepalive = False MockTurtle.monkey_patch(canvas) self.environment = {} self.return_code = None
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_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)
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)
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)
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(self): # SETUP expected_width = 300 expected_height = 200 # EXEC MockTurtle.monkey_patch(canvas=Canvas(expected_width, expected_height)) width = turtle.window_width() height = turtle.window_height() # VERIFY self.assertEqual(expected_width, width) self.assertEqual(expected_height, height)
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)
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) report = MockTurtle.get_all_reports() self.assertEqual(expected_report.splitlines(), report)
def main(): parser = argparse.ArgumentParser(description='Trace Python code.') parser.add_argument('-c', '--canvas', action='store_true', help='Should canvas commands be printed?') parser.add_argument('-x', '--width', type=int, default=800, help='width of the canvas in pixels') parser.add_argument('-y', '--height', type=int, default=600, help='height of the canvas in pixels') parser.add_argument('-m', '--module', help='install traced code as a module with this name') args = parser.parse_args() code = sys.stdin.read() canvas = Canvas(args.width, args.height) tracer = CodeTracer(canvas) tracer.max_width = 200000 code_report = tracer.trace_code(code, module_name=args.module) turtle_report = MockTurtle.get_all_reports() if turtle_report and args.canvas: print('start_canvas') print('\n'.join(turtle_report)) print('end_canvas') print('.') print(code_report)
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_bad_attribute(self): # SETUP # EXEC t = MockTurtle() # VERIFY self.assertFalse(hasattr(t, 'bogus'))
def __init__(self, turtle=None): self.message_limit = 10000 self.max_width = None self.keepalive = False self.turtle = turtle if turtle else MockTurtle() self.environment = {'__name__': MODULE_NAME, CANVAS_NAME: self.turtle.screen.cv, TURTLE_NAME: self.turtle}
def test_forward(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='black'""" # EXEC t = MockTurtle() t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_screen_methods_exist(self): # SETUP t = MockTurtle() # EXEC # VERIFY t.screen.tracer() t.screen.update()
def test_offset(self): # SETUP expected_report = """\ create_line 400 300 500 300 fill='black'""" # EXEC t = MockTurtle(canvas=Canvas(800, 600)) t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_color(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#ff0080'""" # EXEC t = MockTurtle() t.color(1.0, 0.0, 0.5) t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_is_filling(self): # SETUP # EXEC t = MockTurtle() is_filling1 = t.fill() t.begin_fill() is_filling2 = t.fill() t.end_fill() is_filling3 = t.fill() # VERIFY self.assertFalse(is_filling1) self.assertTrue(is_filling2) self.assertFalse(is_filling3)
def test_offset(self): # SETUP expected_report = """\ create_line 400 300 500 300 fill='black' pensize=1 """ # EXEC t = MockTurtle(canvas=Canvas(800, 600)) t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_color_name(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#0000ff' pensize=1""" # EXEC t = MockTurtle() t.color('blue') t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_color_bad(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#000000' pensize=1""" # EXEC t = MockTurtle() t.color((1.0, 0.0)) # Only two numbers, fails to black. t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_pen_dict(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#0000ff' pensize=1""" # EXEC t = MockTurtle() t.pen(pencolor=(0, 0, 1.0)) t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_forward(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='black' pensize=1 """ # EXEC t = MockTurtle() t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_color_bad_range(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#000000' pensize=1""" # EXEC t = MockTurtle() t.color(1.0, 0.0, 1.5) # Over 1.0 not allowed, fails to black. t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_repr(self): # SETUP expected_text = "MockTurtle(100, 0, 10)" # EXEC t = MockTurtle(25, 0, -7) t.left(7) t.fd(75) t.left(10) text = repr(t) # VERIFY self.assertEqual(expected_text, text)
def test_write_right(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='black' pensize=1 create_text 100 0 anchor='se' fill='black' font=('Arial', 8, 'normal') text='Bob' """ # EXEC t = MockTurtle() t.fd(100) t.write('Bob', align='right') report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_offset_with_scale(self): """ The offset is applied BEFORE the scale. """ # SETUP expected_report = """\ create_line 400 300 500 300 fill='black' pensize=1 """ # EXEC t = MockTurtle(canvas=Canvas(800, 600)) t.screen.xscale = 100 t.fd(1) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def mock_show(self, *_args, **_kwargs): figure = self.plt.gcf() # noinspection PyProtectedMember turtle_screen = MockTurtle._screen screen_width = turtle_screen.cv.cget('width') screen_height = turtle_screen.cv.cget('height') figure_width = figure.get_figwidth() * figure.dpi figure_height = figure.get_figheight() * figure.dpi if figure_width < screen_width: x = (screen_width - figure_width) // 2 else: x = 0 if figure_height < screen_height: y = (screen_height - figure_height) // 2 else: y = 0 data = BytesIO() self.plt.savefig(data, format='PNG') image = data.getvalue() encoded = standard_b64encode(image) image_text = encoded.decode('UTF-8') MockTurtle.display_image(x, y, image=image_text)
def test_color(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='#ff0080' pensize=1""" # EXEC t = MockTurtle() t.color(1.0, 0.0, 0.5) t.fd(100) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def trace_turtle(self, source): self.trace_code(source) return '\n'.join(MockTurtle.get_all_reports())
def test_write_move(self): """ Not supported yet """ t = MockTurtle() with self.assertRaises(NotImplementedError): t.write('Bob', move=True)
def trace_turtle(self, source): exec(source, self.environment, self.environment) return '\n'.join(MockTurtle.get_all_reports())
def main(): parser = argparse.ArgumentParser( description='Trace Python code.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-c', '--canvas', action='store_true', help='Should canvas commands be printed?') parser.add_argument('-x', '--width', type=int, default=800, help='width of the canvas in pixels') parser.add_argument('-y', '--height', type=int, default=600, help='height of the canvas in pixels') parser.add_argument('-d', '--dump', action='store_true', help='dump source code with report') parser.add_argument('-f', '--filename', help='file name to save in __file__') parser.add_argument( '-b', '--bad_driver', help="message to display if driver doesn't call module") parser.add_argument('-m', '--module', action='store_true', help='driver is an importable module, not a script') parser.add_argument('source', nargs=argparse.OPTIONAL, default='-', help='source file to trace, or - for stdin') parser.add_argument('load_as', nargs=argparse.OPTIONAL, default=SCOPE_NAME, help='load traced code as a module with this name') parser.add_argument('driver', nargs=argparse.REMAINDER, help='script to call traced code, plus any arguments') args = parser.parse_args() if args.driver and args.driver[0] in ('-m', '--module'): args.module = True args.driver = args.driver[1:] if args.source == '-': code = sys.stdin.read() else: with open(args.source, 'r') as source: code = source.read() canvas = Canvas(args.width, args.height) tracer = CodeTracer(canvas) tracer.max_width = 200000 code_report = tracer.trace_code(code, dump=args.dump, load_as=args.load_as, is_module=args.module, driver=args.driver, filename=args.filename, bad_driver=args.bad_driver) turtle_report = MockTurtle.get_all_reports() if turtle_report and args.canvas: print('start_canvas') print('\n'.join(turtle_report)) print('end_canvas') print('.') print(code_report) if tracer.return_code: exit(tracer.return_code)
def tearDown(self): MockTurtle.remove_monkey_patch()
def __init__(self, canvas=None): self.message_limit = 10000 self.max_width = None self.keepalive = False MockTurtle.monkey_patch(canvas) self.environment = {'__name__': SCOPE_NAME}
def test_penup(self): # SETUP expected_report = """\ create_line 0 0 100 0 fill='black' pensize=1 create_line 150 0 350 0 fill='black' pensize=1 """ # EXEC t = MockTurtle() t.fd(100) t.penup() t.fd(50) t.pendown() t.fd(200) report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
def test_get_color_names(self): t = MockTurtle() t.color('blue') color = t.color() self.assertIn(color, (('blue', 'blue'), ('blue1', 'blue1')))
def test_get_default_color(self): t = MockTurtle() color = t.color() self.assertEqual(('black', 'black'), color)
def test_fill(self): # SETUP expected_report = """\ create_polygon 0 0 100 0 100 100 outline='' fill='#0000ff' create_line 0 0 100 0 fill='#ff0000' create_line 100 0 100 100 fill='#ff0000'""" # EXEC t = MockTurtle() t.color('red', 'blue') t.begin_fill() for _ in range(2): t.fd(100) t.right(90) t.end_fill() report = t.report # VERIFY self.assertEqual(expected_report.splitlines(), report)
parser.add_argument('-y', '--height', type=int, default=600, help='height of the canvas in pixels') parser.add_argument('-m', '--module', help='install traced code as a module with this name') args = parser.parse_args() code = sys.stdin.read() canvas = Canvas(args.width, args.height) tracer = CodeTracer(canvas) tracer.max_width = 200000 code_report = tracer.trace_code(code, module_name=args.module) turtle_report = MockTurtle.get_all_reports() if turtle_report and args.canvas: print('start_canvas') print('\n'.join(turtle_report)) print('end_canvas') print('.') print(code_report) elif __name__ == '__live_coding__': import unittest def test_something(self): # SETUP code = """\ a = [1, 2, [3, 4]] a[0] += 1