def test_reversic_get_board_size(self): app = Reversic() # normal pattern for i in range(MIN_BOARD_SIZE, MAX_BOARD_SIZE + 1, 2): with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write(str(i)) stdin.seek(0) ret = app._get_board_size() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'press board size') self.assertEqual(lines[1], '>> ') with self.assertRaises(IndexError): print(lines[2]) self.assertEqual(ret, i) # illegal pattern for i in range(MIN_BOARD_SIZE - 1, MAX_BOARD_SIZE + 2, 2): with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write(str(i) + '\n' + str(MIN_BOARD_SIZE)) stdin.seek(0) ret = app._get_board_size() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'press board size') self.assertEqual(lines[1], '>> >> ') with self.assertRaises(IndexError): print(lines[2]) self.assertEqual(ret, MIN_BOARD_SIZE) with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('a\n08\n1\nあ\n' + str(MAX_BOARD_SIZE)) stdin.seek(0) ret = app._get_board_size() lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'press board size') self.assertEqual(lines[1], '>> >> >> >> >> ') with self.assertRaises(IndexError): print(lines[2]) self.assertEqual(ret, MAX_BOARD_SIZE)
def test_cgi_xmlrpc_response(self): data = """<?xml version='1.0'?> <methodCall> <methodName>test_method</methodName> <params> <param> <value><string>foo</string></value> </param> <param> <value><string>bar</string></value> </param> </params> </methodCall> """ with support.EnvironmentVarGuard() as env, captured_stdout( encoding=self.cgi.encoding ) as data_out, support.captured_stdin() as data_in: data_in.write(data) data_in.seek(0) env['CONTENT_LENGTH'] = str(len(data)) self.cgi.handle_request() data_out.seek(0) handle = data_out.read() self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) content = handle[handle.find('<?xml'):] self.assertEqual( int(re.search('Content-Length: (\\d+)', handle).group(1)), len(content))
def test_captured_stdin(self): with support.captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) # call test code that consumes from sys.stdin captured = input() self.assertEqual(captured, "hello")
def test_one_human(self): with captured_stdout() as stdout, captured_stdin() as stdin: stdin.write("Kenneth") stdin.seek(0) game = yatzy.Yatzy(humans=1) self.assertEqual("Name for Player 1: ", stdout.getvalue()) self.assertEqual(len(game.humans), 1)
def test_two_humans(self): with captured_stdout() as stdout, captured_stdin() as stdin: stdin.write("Kenneth\n") stdin.write("Elaine\n") stdin.seek(0) game = yatzy.Yatzy(humans=2) self.assertIn("Name for Player 2:", stdout.getvalue()) self.assertEqual(len(game.humans), 2) self.assertEqual(game.humans[1].name, "Elaine")
def test_player_display(self): with captured_stdout() as stdout, captured_stdin() as stdin: stdin.write("Kenneth\n") stdin.seek(0) game = yatzy.Yatzy(humans=1) stdout.seek(0) game.show_player_info(game.humans[0]) output = stdout.getvalue() self.assertEqual(len(output.split('\n')[0]), 90) self.assertIn(game.humans[0].name, output)
def test_consoleuserinput_next_move(self): board = Board() console_user_input = ConsoleUserInput() move = None with captured_stdin() as stdin: stdin.write('1') stdin.seek(0) move = console_user_input.next_move('black', board) self.assertEqual(move, (3, 2)) move = None with captured_stdin() as stdin: stdin.write('2') stdin.seek(0) move = console_user_input.next_move('black', board) self.assertEqual(move, (2, 3))
def test_cgi_xmlrpc_response(self): data = """<?xml version='1.0'?> <methodCall> <methodName>test_method</methodName> <params> <param> <value><string>foo</string></value> </param> <param> <value><string>bar</string></value> </param> </params> </methodCall> """ with support.EnvironmentVarGuard() as env, \ captured_stdout(encoding=self.cgi.encoding) as data_out, \ support.captured_stdin() as data_in: data_in.write(data) data_in.seek(0) env['CONTENT_LENGTH'] = str(len(data)) self.cgi.handle_request() data_out.seek(0) # will respond exception, if so, our goal is achieved ;) handle = data_out.read() # start with 44th char so as not to get http header, we just # need only xml self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) # Also test the content-length returned by handle_request # Using the same test method inorder to avoid all the datapassing # boilerplate code. # Test for bug: http://bugs.python.org/issue5040 content = handle[handle.find("<?xml"):] self.assertEqual( int(re.search('Content-Length: (\d+)', handle).group(1)), len(content))
def test_search_time_return(self): with support.captured_stdin() as stdin: stdin.write('c\n') stdin.seek(0) next_step = worklogdb.search_menu() self.assertIs(next_step, time_search)
def test_reversic__menu(self): app = Reversic() app.players_info = {'black': 'BLACK', 'white': 'WHITE'} app._get_board_size = lambda: 26 app._get_player = lambda x: x # play app.state = None with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('\n') stdin.seek(0) app._Reversic__menu() lines = stdout.getvalue().splitlines() def check_header(self, lines): self.assertEqual(lines[0], 'press any key') self.assertEqual(lines[1], '-----------------------------') self.assertEqual(lines[2], ' enter : start game') self.assertEqual(lines[3], ' s : change board size') self.assertEqual(lines[4], ' b : change black player') self.assertEqual(lines[5], ' w : change white player') self.assertEqual(lines[6], ' q : quit') self.assertEqual(lines[7], '-----------------------------') check_header(self, lines) self.assertEqual(lines[8], '>> ') with self.assertRaises(IndexError): print(lines[9]) self.assertEqual(app.state, Reversic.PLAY) # size app.state = None with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('s') stdin.seek(0) app._Reversic__menu() lines = stdout.getvalue().splitlines() check_header(self, lines) self.assertEqual(lines[8], '>> ') with self.assertRaises(IndexError): print(lines[9]) self.assertEqual(app.board_size, 26) self.assertEqual(app.state, Reversic.START) # black app.state = None with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('b') stdin.seek(0) app._Reversic__menu() lines = stdout.getvalue().splitlines() check_header(self, lines) self.assertEqual(lines[8], '>> ') with self.assertRaises(IndexError): print(lines[9]) self.assertEqual(app.player_names['black'], 'BLACK') self.assertEqual(app.state, Reversic.START) # white app.state = None with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('w') stdin.seek(0) app._Reversic__menu() lines = stdout.getvalue().splitlines() check_header(self, lines) self.assertEqual(lines[8], '>> ') with self.assertRaises(IndexError): print(lines[9]) self.assertEqual(app.player_names['white'], 'WHITE') self.assertEqual(app.state, Reversic.START) # invalid keyword & quit app.state = None ret = False with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('invalid keyword\nq') stdin.seek(0) ret = app._Reversic__menu() lines = stdout.getvalue().splitlines() check_header(self, lines) self.assertEqual(lines[8], '>> >> See you!') with self.assertRaises(IndexError): print(lines[9]) self.assertTrue(ret) self.assertIsNone(app.state)
def test_captured_stdin(self): with support.captured_stdin() as s: print("hello", file=sys.stdin) self.assertEqual(s.getvalue(), "hello\n")
def test_reversic_get_player(self): app = Reversic() test_players = {'Test1': None, 'Test2': None} # normal pattern for i in range(1, 3): with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write(str(i)) stdin.seek(0) ret = app._get_player(test_players) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'select number for player') self.assertEqual(lines[1], '-----------------------------') self.assertEqual(lines[2], ' 1 : Test1') self.assertEqual(lines[3], ' 2 : Test2') self.assertEqual(lines[4], '-----------------------------') self.assertEqual(lines[5], '>> ') with self.assertRaises(IndexError): print(lines[6]) self.assertEqual(ret, list(test_players.keys())[i - 1]) # illegal pattern for i in range(0, 4, 3): with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write(str(i) + '\n' + str(1)) stdin.seek(0) ret = app._get_player(test_players) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'select number for player') self.assertEqual(lines[1], '-----------------------------') self.assertEqual(lines[2], ' 1 : Test1') self.assertEqual(lines[3], ' 2 : Test2') self.assertEqual(lines[4], '-----------------------------') self.assertEqual(lines[5], '>> >> ') with self.assertRaises(IndexError): print(lines[6]) self.assertEqual(ret, 'Test1') with captured_stdout() as stdout: with captured_stdin() as stdin: stdin.write('a\n08\n1\nあ\n' + str(2)) stdin.seek(0) ret = app._get_player(test_players) lines = stdout.getvalue().splitlines() self.assertEqual(lines[0], 'select number for player') self.assertEqual(lines[1], '-----------------------------') self.assertEqual(lines[2], ' 1 : Test1') self.assertEqual(lines[3], ' 2 : Test2') self.assertEqual(lines[4], '-----------------------------') self.assertEqual(lines[5], '>> >> >> >> >> ') with self.assertRaises(IndexError): print(lines[6]) self.assertEqual(ret, 'Test2')
def test_captured_stdin(self): with support.captured_stdin() as stdin: stdin.write('hello\n') stdin.seek(0) captured = input() self.assertEqual(captured, 'hello')
def get_input(self): '''this method captures input from the console''' with support.captured_stdin() as self.stdin: return self.stdin.readline()