def test_syntaxerror(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a i.runsource('1.1.1.1') if pypy: expected = ( ' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') else: expected = ( ' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_unhighlight_paren_bugs(self): """two previous bugs, parent didn't highlight until next render and paren didn't unhighlight until enter""" self.assertEqual(self.repl.rl_history.entries, [""]) self.enter("(") self.assertEqual(self.repl.rl_history.entries, [""]) screen = [">>> (", "... "] self.assertEqual(self.repl.rl_history.entries, [""]) self.assert_paint_ignoring_formatting(screen) self.assertEqual(self.repl.rl_history.entries, [""]) with output_to_repl(self.repl): self.assertEqual(self.repl.rl_history.entries, [""]) self.repl.process_event(")") self.assertEqual(self.repl.rl_history.entries, [""]) screen = fsarray( [ cyan(">>> ") + on_magenta(bold(red("("))), green("... ") + on_magenta(bold(red(")"))), ] ) self.assert_paint(screen, (1, 5)) with output_to_repl(self.repl): self.repl.process_event(" ") screen = fsarray( [ cyan(">>> ") + yellow("("), green("... ") + yellow(")") + bold(cyan(" ")), ] ) self.assert_paint(screen, (1, 6))
def test_traceback(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a def f(): return 1/0 def g(): return f() i.runsource('g()', encode=False) if pypy: global_not_found = "global name 'g' is not defined" else: global_not_found = "name 'g' is not defined" expected = 'Traceback (most recent call last):\n File ' + \ green('"%s"' % _last_console_filename()) + ', line ' + bold(magenta('1')) + ', in ' + \ cyan('<module>') + '\n g()\n' + bold(red('NameError')) + ': ' + \ cyan(global_not_found) + '\n' self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_traceback(self): i, a = self.interp_errlog() def f(): return 1 / 0 def gfunc(): return f() i.runsource("gfunc()") global_not_found = "name 'gfunc' is not defined" expected = ( "Traceback (most recent call last):\n File " + green('"<input>"') + ", line " + bold(magenta("1")) + ", in " + cyan("<module>") + "\n gfunc()\n" + bold(red("NameError")) + ": " + cyan(global_not_found) + "\n" ) self.assertMultiLineEqual(str(plain("").join(a)), str(expected)) self.assertEqual(plain("").join(a), expected)
def test_unhighlight_paren_bugs(self): """two previous bugs, parent didn't highlight until next render and paren didn't unhighlight until enter""" self.assertEqual(self.repl.rl_history.entries, ['']) self.enter('(') self.assertEqual(self.repl.rl_history.entries, ['']) screen = [">>> (", "... "] self.assertEqual(self.repl.rl_history.entries, ['']) self.assert_paint_ignoring_formatting(screen) self.assertEqual(self.repl.rl_history.entries, ['']) with output_to_repl(self.repl): self.assertEqual(self.repl.rl_history.entries, ['']) self.repl.process_event(')') self.assertEqual(self.repl.rl_history.entries, ['']) screen = fsarray([cyan(">>> ") + on_magenta(bold(red('('))), green("... ") + on_magenta(bold(red(')')))]) self.assert_paint(screen, (1, 5)) with output_to_repl(self.repl): self.repl.process_event(' ') screen = fsarray([cyan(">>> ") + yellow('('), green("... ") + yellow(')') + bold(cyan(" "))]) self.assert_paint(screen, (1, 6))
def test_traceback(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a def f(): return 1 / 0 def g(): return f() i.runsource('g()', encode=False) if pypy: global_not_found = "global name 'g' is not defined" else: global_not_found = "name 'g' is not defined" expected = 'Traceback (most recent call last):\n File ' + \ green('"%s"' % _last_console_filename()) + ', line ' + bold(magenta('1')) + ', in ' + \ cyan('<module>') + '\n g()\n' + bold(red('NameError')) + ': ' + \ cyan(global_not_found) + '\n' self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_enter_text(self): [self.repl.add_normal_character(c) for c in '1 + 1'] screen = fsarray([ cyan('>>> ') + bold( green('1') + cyan(' ') + yellow('+') + cyan(' ') + green('1')), cyan('Welcome to') ]) self.assert_paint(screen, (0, 9))
def test_parse(self): self.assertEquals(parse.parse('\x01y\x03print\x04'), yellow('print')) self.assertEquals( parse.parse('\x01y\x03print\x04\x01c\x03 \x04\x01g\x031\x04\x01c' '\x03 \x04\x01Y\x03+\x04\x01c\x03 \x04\x01g\x032\x04'), yellow('print') + cyan(' ') + green('1') + cyan(' ') + bold(yellow('+')) + cyan(' ') + green(u'2'))
def test_parse(self): self.assertEqual(parse.parse('\x01y\x03print\x04'), yellow('print')) self.assertEqual( parse.parse('\x01y\x03print\x04\x01c\x03 \x04\x01g\x031\x04\x01c' '\x03 \x04\x01Y\x03+\x04\x01c\x03 \x04\x01g\x032\x04'), yellow('print') + cyan(' ') + green('1') + cyan(' ') + bold(yellow('+')) + cyan(' ') + green(u'2'))
def test_enter_text(self): [self.repl.add_normal_character(c) for c in "1 + 1"] screen = fsarray([ cyan(">>> ") + bold( green("1") + cyan(" ") + yellow("+") + cyan(" ") + green("1")), cyan("Welcome to"), ]) self.assert_paint(screen, (0, 9))
def test_parse(self): self.assertEqual(parse.parse("\x01y\x03print\x04"), yellow("print")) self.assertEqual( parse.parse("\x01y\x03print\x04\x01c\x03 \x04\x01g\x031\x04\x01c" "\x03 \x04\x01Y\x03+\x04\x01c\x03 \x04\x01g\x032\x04"), yellow("print") + cyan(" ") + green("1") + cyan(" ") + bold(yellow("+")) + cyan(" ") + green("2"), )
def test_argspec(self): def foo(x, y, z=10): "docstring!" pass argspec = inspection.getargspec('foo', foo) + [1] array = replpainter.formatted_argspec(argspec, 30, setup_config()) screen = [(bold(cyan(u'foo'))+cyan(':')+cyan(' ')+cyan('(')+cyan('x') + yellow(',')+yellow(' ')+bold(cyan('y'))+yellow(',') + yellow(' ')+cyan('z')+yellow('=')+bold(cyan('10'))+yellow(')'))] self.assertFSArraysEqual(fsarray(array), fsarray(screen))
def test_syntaxerror(self): i, a = self.interp_errlog() i.runsource('1.1.1.1') if pypy: expected = (' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') else: expected = (' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEqual(plain('').join(a), expected)
def test_argspec(self): def foo(x, y, z=10): "docstring!" pass argspec = inspection.getfuncprops('foo', foo) array = replpainter.formatted_argspec(argspec, 1, 30, setup_config()) screen = [bold(cyan('foo')) + cyan(':') + cyan(' ') + cyan('(') + cyan('x') + yellow(',') + yellow(' ') + bold(cyan('y')) + yellow(',') + yellow(' ') + cyan('z') + yellow('=') + bold(cyan('10')) + yellow(')')] self.assertFSArraysEqual(fsarray(array), fsarray(screen))
def test_syntaxerror(self): i, a = self.interp_errlog() i.runsource("1.1.1.1") if sys.version_info[:2] >= (3, 8): expected = ( " File " + green('"<input>"') + ", line " + bold(magenta("1")) + "\n 1.1.1.1\n ^\n" + bold(red("SyntaxError")) + ": " + cyan("invalid syntax") + "\n" ) elif pypy: expected = ( " File " + green('"<input>"') + ", line " + bold(magenta("1")) + "\n 1.1.1.1\n ^\n" + bold(red("SyntaxError")) + ": " + cyan("invalid syntax") + "\n" ) else: expected = ( " File " + green('"<input>"') + ", line " + bold(magenta("1")) + "\n 1.1.1.1\n ^\n" + bold(red("SyntaxError")) + ": " + cyan("invalid syntax") + "\n" ) self.assertMultiLineEqual(str(plain("").join(a)), str(expected)) self.assertEqual(plain("").join(a), expected)
def test_syntaxerror(self): i, a = self.interp_errlog() i.runsource('1.1.1.1') if pypy: expected = ( ' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') else: expected = ( ' File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + '\n 1.1.1.1\n ^\n' + bold(red('SyntaxError')) + ': ' + cyan('invalid syntax') + '\n') self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEqual(plain('').join(a), expected)
def write_out_lines_to_fit_pane(self, pane, p_lines, title): assert pane is not None assert isinstance(pane, SessionPane) assert isinstance(title, unicode) pane_width = pane.get_width() pane_height = pane.get_height() assert pane_width > 39 assert pane_height > 19 # We reserve one row at the end as a pane status line available_pane_height = pane.get_height() - 1 lines_in_pane_str_arr = [] p_lines_str = [] for session_pane_line in p_lines: assert isinstance(session_pane_line, SessionPaneLine) p_lines_str.append(session_pane_line.line_str) p_lines = p_lines_str p_lines_str = None # Scrub any ansi escape sequences. ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') if not self.shutit_global.ispy3: lines = [ ansi_escape.sub('', line).strip().decode('utf-8') for line in p_lines ] else: lines = [ansi_escape.sub('', line).strip() for line in p_lines] # If the last line is blank we can just skip it. if len(lines) > 0 and lines[-1] == '': lines = lines[:-1] for line in lines: # Take the next line in the stream. If it's greater than the pane_width, # Then parcel over multiple lines while len(line) > pane_width - 1 and len(line) > 0: lines_in_pane_str_arr.append(line[:pane_width - 1]) line = line[pane_width - 1:] lines_in_pane_str_arr.append(line) # Status line: lines_in_pane_str_arr.append(title) top_y = pane.top_left_y bottom_y = pane.bottom_right_y for i, line in zip(reversed(range(top_y, bottom_y)), reversed(lines_in_pane_str_arr)): # Status on bottom line # If this is on the top, and height + top_y value == i (ie this is the last line of the pane) # OR this is on the bottom (ie top_y is not 1), and height + top_y == i # One or both of these help prevent glitches on the screen. Don't know why. Maybe replace with more standard list TODO if (top_y == 1 and available_pane_height + top_y == i) or ( top_y != 1 and available_pane_height + top_y == i): self.screen_arr[i:i + 1, pane.top_left_x:pane.top_left_x + len(line)] = [cyan(invert(line))] else: self.screen_arr[i:i + 1, pane.top_left_x:pane.top_left_x + len(line)] = [line]
def test_argspec(self): def foo(x, y, z=10): "docstring!" pass argspec = inspection.getfuncprops("foo", foo) array = replpainter.formatted_argspec(argspec, 1, 30, setup_config()) screen = [ bold(cyan("foo")) + cyan(":") + cyan(" ") + cyan("(") + cyan("x") + yellow(",") + yellow(" ") + bold(cyan("y")) + yellow(",") + yellow(" ") + cyan("z") + yellow("=") + bold(cyan("10")) + yellow(")") ] assertFSArraysEqual(fsarray(array), fsarray(screen))
def test_traceback(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a def f(): return 1/0 def g(): return f() i.runsource('g()') expected = 'Traceback (most recent call last):\n File ' + \ green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + \ cyan('<module>') + '\n' + bold(red('NameError')) + ': ' + \ cyan("name 'g' is not defined") + '\n' self.assertEquals(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_traceback(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a def f(): return 1 / 0 def g(): return f() i.runsource('g()') expected = 'Traceback (most recent call last):\n File ' + \ green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + \ cyan('<module>') + '\n' + bold(red('NameError')) + ': ' + \ cyan("name 'g' is not defined") + '\n' self.assertEquals(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_traceback(self): i, a = self.interp_errlog() def f(): return 1 / 0 def gfunc(): return f() i.runsource('gfunc()') if pypy and not py3: global_not_found = "global name 'gfunc' is not defined" else: global_not_found = "name 'gfunc' is not defined" expected = ( 'Traceback (most recent call last):\n File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + cyan('<module>') + '\n gfunc()\n' + bold(red('NameError')) + ': ' + cyan(global_not_found) + '\n') self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEqual(plain('').join(a), expected)
def test_traceback(self): i, a = self.interp_errlog() def f(): return 1 / 0 def gfunc(): return f() i.runsource('gfunc()') if pypy: global_not_found = "global name 'gfunc' is not defined" else: global_not_found = "name 'gfunc' is not defined" expected = ('Traceback (most recent call last):\n File ' + green('"<input>"') + ', line ' + bold(magenta('1')) + ', in ' + cyan('<module>') + '\n gfunc()\n' + bold(red('NameError')) + ': ' + cyan(global_not_found) + '\n') self.assertMultiLineEqual(str(plain('').join(a)), str(expected)) self.assertEqual(plain('').join(a), expected)
def write_out_lines_to_fit_pane(self, pane, p_lines, title): assert pane is not None assert isinstance(pane, SessionPane) assert isinstance(title, unicode) pane_width = pane.get_width() pane_height = pane.get_height() assert pane_width > 39 assert pane_height > 19 # We reserve one row at the end as a pane status line available_pane_height = pane.get_height() - 1 lines_in_pane_str_arr = [] p_lines_str = [] for session_pane_line in p_lines: assert isinstance(session_pane_line, SessionPaneLine) p_lines_str.append(session_pane_line.line_str) p_lines = p_lines_str p_lines_str = None # Scrub any ansi escape sequences. ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') if not self.shutit_global.ispy3: lines = [ ansi_escape.sub('', line).strip().decode('utf-8') for line in p_lines ] else: lines = [ ansi_escape.sub('', line).strip() for line in p_lines ] # If the last line is blank we can just skip it. if len(lines) > 0 and lines[-1] == '': lines = lines[:-1] for line in lines: # Take the next line in the stream. If it's greater than the pane_width, # Then parcel over multiple lines while len(line) > pane_width-1 and len(line) > 0: lines_in_pane_str_arr.append(line[:pane_width-1]) line = line[pane_width-1:] lines_in_pane_str_arr.append(line) # Status line: lines_in_pane_str_arr.append(title) top_y = pane.top_left_y bottom_y = pane.bottom_right_y for i, line in zip(reversed(range(top_y,bottom_y)), reversed(lines_in_pane_str_arr)): # Status on bottom line # If this is on the top, and height + top_y value == i (ie this is the last line of the pane) # OR this is on the bottom (ie top_y is not 1), and height + top_y == i # One or both of these help prevent glitches on the screen. Don't know why. Maybe replace with more standard list TODO if (top_y == 1 and available_pane_height + top_y == i) or (top_y != 1 and available_pane_height + top_y == i): self.screen_arr[i:i+1, pane.top_left_x:pane.top_left_x+len(line)] = [cyan(invert(line))] else: self.screen_arr[i:i+1, pane.top_left_x:pane.top_left_x+len(line)] = [line]
def test_startup(self): screen = fsarray([cyan('>>> '), cyan('Welcome to')]) self.assert_paint(screen, (0, 4))
def test_enter_text(self): [self.repl.add_normal_character(c) for c in '1 + 1'] screen = fsarray([cyan('>>> ') + bold(green('1') + cyan(' ') + yellow('+') + cyan(' ') + green('1')), cyan('Welcome to')]) self.assert_paint(screen, (0, 9))
def test_syntaxerror(self): i = interpreter.Interp() a = [] def append_to_a(message): a.append(message) i.write = append_to_a i.runsource('1.1.1.1') expected = ''+u''+u' File '+green(u'"<input>"')+u', line '+bold(magenta(u'1'))+u'\n'+u' '+u'1.1'+u'.'+u'1.1'+u'\n'+u' '+u' '+u'^'+u'\n'+bold(red(u'SyntaxError'))+u': '+cyan(u'invalid syntax')+u'\n' self.assertEquals(str(plain('').join(a)), str(expected)) self.assertEquals(plain('').join(a), expected)
def test_startup(self): screen = fsarray([cyan(">>> "), cyan("Welcome to")]) self.assert_paint(screen, (0, 4))
def mainloop(window, p2_bot=False): p1_attrs = { "appearance": on_blue(cyan("1")), "x": window.width // 4, "y": window.height // 2, "keys": { "w": 90, "a": 180, "s": 270, "d": 0 }, } p2_attrs = { "appearance": on_red(yellow("2")), "x": 3 * window.width // 4, "y": window.height // 2, "keys": { "<UP>": 90, "<LEFT>": 180, "<DOWN>": 270, "<RIGHT>": 0 }, } FPS = 15 players = [Cycle(p1_attrs), Cycle(p2_attrs)] if p2_bot: # make p2 a bot players[1] = Bot(p2_attrs) world = gameboard(window.width, window.height, players) dt = 1 / FPS world.draw_border() window.render_to_terminal(world.grid) reactor = Input() schedule_next_frame = reactor.scheduled_event_trigger(Frame) schedule_next_frame(when=time.time()) with reactor: for c in reactor: if isinstance(c, Frame): tick = world.tick() window.render_to_terminal(world.grid) if not tick: # if no crashes when = c.when + dt while when < time.time(): when += dt schedule_next_frame(when) else: # if crashed world.grid[0:4, 0:25] = fsarray([ world.winner_msg(tick), "r to restart", "q to quit", "b to make player 2 a bot", ]) window.render_to_terminal(world.grid) elif c.lower() in ["r", "q", "b"]: break else: # common case world.process_event(c) if c.lower() == "r": mainloop(window, p2_bot) elif c.lower() == "b": mainloop(window, True)