def test_display_04(self): """ test display_guess_matrix guess 9 length 9 """ with RedirectStdIO(stdinout=True) as stdio: self.a1.display_guess_matrix(9, 9, (26, 14, 0, 12, 14, 26, 0, 0)) actual = stdio.stdinout expected = self.load_test_data('display_04.out') self.assertEqual(actual, expected)
def test_get_event_details(self): """ test get event details """ ui = self.event_decision.UserInteraction() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin("My Event\nY\nY\n18\n") event = ui.get_event_details() self.assertEqual(str(event), 'Event(My Event @ 18, True, True)')
def test_upper_no_overflow(self): encrypt = getattr(self.a1, self.guess_function_name(self.a1, ENCRYPT)) with RedirectStdIO(stdin=True, stdout=True): try: self.assertEqual(encrypt('MNO', 1), 'NOP') except EOFError: self.fail( msg=f"{ENCRYPT} function should not prompt for input")
def test_display_03(self): """ test display_guess_matrix guess 3 length 6 """ with RedirectStdIO(stdinout=True) as stdio: self.a1.display_guess_matrix(3, 6, (26, 14)) actual = stdio.stdinout expected = self.load_test_data('display_03.out') self.assertEqual(actual, expected)
def test_draw(self): """ test GameApp.draw """ app = self.a2.GameApp() with RedirectStdIO(stdout=True) as stdio: app.draw() expected = self.load_test_data('game_draw.out') self.assertEqual(stdio.stdout, expected)
def test_lower_no_overflow(self): decrypt = getattr(self.a1, self.guess_function_name(self.a1, DECRYPT)) with RedirectStdIO(stdin=True, stdout=True): try: self.assertEqual(decrypt('mno', 1), 'lmn') except EOFError: self.fail( msg=f"{DECRYPT} function should not prompt for input")
def test_simple_game(self): """ test display game """ with RedirectStdIO(stdout=True) as stdio: result = self.a1.display_game(self.game, self.grid_size) expected = self.load_test_data("display_game_simple.out") self.assertMultiLineEqual(stdio.stdout, expected) self.assertIsNone(result, msg="display_game should not return a non None value")
def test_not_recursive(self): _main = getattr(self.a1, self.guess_function_name(self.a1, MAIN)) with open('data_files/recursive.in') as fin: inputs = fin.read() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(inputs) self.assertIsNotRecursive(_main)
def test_full_game(self): """ test display large game """ game = self.load_test_data("display_game_full.in") with RedirectStdIO(stdout=True) as stdio: result = self.a1.display_game(game, 26) expected = self.load_test_data("display_game_full.out") self.assertMultiLineEqual(stdio.stdout, expected) self.assertIsNone(result, msg="display_game should not return a non None value")
def test_with_symbols(self): """ test display with symbols """ game = "☺♥1♥31110" with RedirectStdIO(stdout=True) as stdio: result = self.a1.display_game(game, 3) expected = self.load_test_data("display_game_symbols.out") self.assertMultiLineEqual(stdio.stdout, expected) self.assertIsNone(result, msg="display_game should not return a non None value")
def test_display_02(self): """ test display_guess_matrix guess 2 length 6 """ with RedirectStdIO(stdinout=True) as stdio: result = self.a1.display_guess_matrix(2, 6, (0,)) actual = stdio.stdinout expected = self.load_test_data('display_02.out') self.assertEqual(actual, expected) self.assertIsNone(result)
def test_door_on_hit_without_key(self): """ test GameLogic with Door.on_hit without Key """ game = self.a2.GameLogic('game1.txt') player = game.get_player() player.set_position((1, 3)) door = game.get_entity((3, 2)) with RedirectStdIO(stdout=True) as stdio: door.on_hit(game) self.assertIs(game.won(), False) self.assertEqual(stdio.stdout, "You don't have the key!\n")
def test_get_prediction_model(self): """ test get prediction model """ ui = self.event_decision.UserInteraction() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin("1\n") model = ui.get_prediction_model(self.data) # would rather compare against instance and type # but the way testrunner imports makes them different types self.assertEqual(model.__class__.__name__, self.prediction.YesterdaysWeather.__name__)
def test_door_on_hit_without_key(self): """ test GameLogic with Door.on_hit without Key """ game = self.a2.GameLogic('game1.txt') player = game.get_player() player.set_position((3, 2)) door = game.get_entity((3, 2)) with RedirectStdIO(stdout=True) as stdio: ret = door.on_hit(game) self.assertIs(game.won(), False) self.assertEqual(stdio.stdout, "You don't have the key!\n") self.assertIsNone(ret, msg="This function should not return a value")
def _run_main(self, in_path, out_path): with open(TEST_DATA / in_path) as fin, \ open(TEST_DATA / out_path) as fout: in_data = fin.read() expected_output = fout.read() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(in_data) self.a1.main() self.assertMultiLineEqual(stdio.stdinout, expected_output)
def test_extension_autodecrypt(self): with open('data_files/example_extension_inputs_2.txt') as fin, \ open('data_files/example_extension_output_2.txt') as fout: inputs = fin.read() outputs = fout.read() with RedirectStdIO(stdin=True, stdout=True) as stdio: stdio.set_stdin(inputs) a1.main() self.assertMultiLineEqual(stdio.stdout, outputs)
def test_extension_autodecrypt_sample(self): _main = getattr(self.a1, self.guess_function_name(self.a1, MAIN)) with open('data_files/sample_ext_a.in') as fin, \ open('data_files/sample_ext_a.out') as fout: inputs = fin.read() output = fout.read() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(inputs) _main() self.assertMultiLineEqual(stdio.stdinout, output, strip=True)
def test_invalid(self): _main = getattr(self.a1, self.guess_function_name(self.a1, MAIN)) with open('data_files/invalid.in') as fin, \ open('data_files/invalid.out') as fout: inputs = fin.read() outputs = fout.read() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(inputs) _main() self.assertMultiLineEqual(stdio.stdinout, outputs, strip=True)
def test_zero_offset(self): encrypt = getattr(self.a1, self.guess_function_name(self.a1, ENCRYPT)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: self.assertEqual( encrypt("you will always remember this as the day", 0), 'you will always remember this as the day') except EOFError: self.fail(msg="encrypt function should not prompt for input") self.assertEqual(stdio.stdout, '', msg="encrypt function should not be printing")
def test_zero_offset(self): decrypt = getattr(self.a1, self.guess_function_name(self.a1, DECRYPT)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: self.assertEqual( decrypt("a bmkl kso lzw sv sfv lzgmyzl al dggcwv xmf", 0), 'a bmkl kso lzw sv sfv lzgmyzl al dggcwv xmf') except EOFError: self.fail(msg="encrypt function should not prompt for input") self.assertEqual(stdio.stdout, '', msg="encrypt function should not be printing")
def test_sample(self): feo = getattr(self.a1, self.guess_function_name(self.a1, FEO)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: self.assertEqual( feo("iynjo fuhsudj ev jxu jycu yj mehai qbb jxu jycu"), (16, )) self.assertEqual( feo("vftg amnl aqkkxmn mjmcqlm emkveoxtmn lvbmlomz"), ()) self.assertEqual(feo("nmd"), (4, 12, 21, 25)) except EOFError: self.fail(msg=f"{FEO} function should not prompt for input") self.assertEqual(stdio.stdout, '', msg=f"{FEO} function should not be printing")
def test_decrypt_calls_another(self): """ decorates all methods of a1 except decrpyt counting number of function calls for each. pass if at least one function is called once. """ decrypt = getattr(self.a1, self.guess_function_name(self.a1, DECRYPT)) attrs = { k: v for k, v in inspect.getmembers(self.a1, predicate=inspect.isfunction) if v != decrypt } calls = {k: 0 for k in attrs} def decorator(func): @wraps(func) def wrapper(*args, **kwargs): nonlocal calls calls[func.__name__] += 1 return func(*args, **kwargs) return wrapper for key, value in attrs.items(): setattr(self.a1, key, decorator(value)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: decrypt("xyz", 1) except EOFError: self.fail( msg=f"{ENCRYPT} function should not prompt for input") finally: for key, value in attrs.items(): setattr(self.a1, key, value) self.assertEqual(stdio.stdout, '', msg=f"{ENCRYPT} function should not be printing") self.assertIn(1, calls.values(), msg=f"{DECRYPT} does not call another function once")
def _run_main(self, in_data, raw_data=False): if not raw_data: with open(self.INPUTS_PATH / in_data) as fin: in_data = fin.read() error = None with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(in_data) try: self.a1.main() except EOFError as e: error = e if error: raise EOFError( f'Below is the captured output until EOF\n{stdio.stdinout}' ).with_traceback(error.__traceback__) return stdio
def test_sample(self): decrypt = getattr(self.a1, self.guess_function_name(self.a1, DECRYPT)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: self.assertEqual( decrypt("a bmkl kso lzw sv sfv lzgmyzl al dggcwv xmf", 17), 'j kvtu tbx uif be boe uipvhiu ju mpplfe gvo') self.assertEqual( decrypt("a bmkl kso lzw sv sfv lzgmyzl al dggcwv xmf", 18), 'i just saw the ad and thought it looked fun') self.assertEqual( decrypt("asdf ghjkl qwerty uiop z xcvbnm", 17), 'jbmo pqstu zfnach drxy i glekwv') except EOFError: self.fail( msg=f"{DECRYPT} function should not prompt for input") self.assertEqual(stdio.stdout, '', msg=f"{DECRYPT} function should not be printing")
def test_extension_decrypt_zero(self): _main = getattr(self.a1, self.guess_function_name(self.a1, MAIN)) with open('data_files/ext_d_zero.in') as fin, \ open('data_files/ext_d_zero.out') as fout, \ open('data_files/ext_d_zero.back.out') as fout_back: inputs = fin.read() output = fout.read() output_back = fout_back.read() with RedirectStdIO(stdinout=True) as stdio: stdio.set_stdin(inputs) _main() backup_failed = False try: self.assertMultiLineEqual(stdio.stdinout, output_back, strip=True) except AssertionError: backup_failed = True if backup_failed: self.assertMultiLineEqual(stdio.stdinout, output, strip=True)
def test_sample(self): encrypt = getattr(self.a1, self.guess_function_name(self.a1, ENCRYPT)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: self.assertEqual( encrypt("you will always remember this as the day", 7), 'fvb dpss hsdhfz yltltily aopz hz aol khf') self.assertEqual( encrypt("music is the shorthand of emotion", 2), 'owuke ku vjg ujqtvjcpf qh goqvkqp') self.assertEqual( encrypt("qgnrag hgorkey gtj cnovvkj ixkgs", 9), 'zpwajp qpxatnh pcs lwxeets rgtpb') except EOFError: self.fail( msg=f"{ENCRYPT} function should not prompt for input") self.assertEqual(stdio.stdout, '', msg=f"{ENCRYPT} function should not be printing")
def _run_main(self, file_in: str, file_out: str, stop_early: bool): """ runs the main function and captures output """ data_in = self.load_test_data(file_in) error = None result = None with RedirectStdIO(stdinout=True) as stdio: stdio.stdin = data_in try: result = self.a1.main() except EOFError as err: error = err # self.write_test_data(file_out, stdio.stdinout) expected = self.load_test_data(file_out) if error is not None and not stop_early: last_output = "\n\n".join(stdio.stdinout.rsplit("\n\n")[-2:]) raise AssertionError( f'Your program is asking for too much input\nEOFError: {error}\n\n{last_output}' ).with_traceback(error.__traceback__) return expected, result, stdio
def _run_play(self, file_in: str, file_out: str, stop_early: bool): """ runs the play function and captures output """ data_in = self.load_test_data(file_in) error = None result = None app = self.a2.GameApp() with RedirectStdIO(stdinout=True) as stdio: stdio.stdin = data_in try: result = app.play() except EOFError as err: error = err # self.write_test_data(file_out, stdio.stdinout) expected = self.load_test_data(file_out) if error is not None and not stop_early: last_output = "\n\n".join(stdio.stdinout.rsplit("\n\n")[-4:]) raise AssertionError( f'Your program is asking for more input when it should have ended\nEOFError: {error}\n\n{last_output}' ).with_traceback(error.__traceback__) return expected, result, stdio
def test_feo_calls_encrypt_or_decrypt(self): encrypt_name = self.guess_function_name(self.a1, ENCRYPT) decrypt_name = self.guess_function_name(self.a1, DECRYPT) encrypt = getattr(self.a1, encrypt_name) decrypt = getattr(self.a1, decrypt_name) feo = getattr(self.a1, self.guess_function_name(self.a1, FEO)) called = False def decorator(func): @wraps(func) def wrapper(*args, **kwargs): nonlocal called called = True return func(*args, **kwargs) return wrapper setattr(self.a1, encrypt_name, decorator(encrypt)) setattr(self.a1, decrypt_name, decorator(decrypt)) with RedirectStdIO(stdin=True, stdout=True) as stdio: try: feo("xyz") except EOFError: self.fail(msg=f"{FEO} function should not prompt for input") finally: setattr(self.a1, encrypt_name, encrypt) setattr(self.a1, decrypt_name, decrypt) self.assertEqual(stdio.stdout, '', msg=f"{FEO} function should not be printing") self.assertIs(called, True, msg=f"{FEO} does not call {ENCRYPT}/{DECRYPT}")