def test_citation(): from sys import stdout t = mh.citation(False) assert len(stdout.getvalue()) == 0 t2 = mh.citation(True) assert len(stdout.getvalue()) != 0 assert t == t2
def test_output(self): from sys import stdout if not hasattr(stdout, 'getvalue'): self.skipTest("cannot grab stdout") par, func = parallel_loop(f, n_jobs=None, verbose=10) self.assertEqual(stdout.getvalue().strip().split(' ')[-1], 'serially') par, func = parallel_loop(f, n_jobs=2, verbose=10) self.assertEqual(stdout.getvalue().strip().split(' ')[-1], 'parallel')
def gather_info_for_failure(self, err, test): """Gather traceback, stdout, stderr, dosemu and expect logs""" TITLE_NAME_FMT = '{:^16}' TITLE_BANNER_FMT = '{:-^70}\n' STDOUT_LINE = '\nStdout:\n%s' STDERR_LINE = '\nStderr:\n%s' # Traceback exctype, value, tb = err while tb and self._is_relevant_tb_level(tb): # Skip test runner traceback levels tb = tb.tb_next if exctype is test.failureException: # Skip assert*() traceback levels length = self._count_relevant_tb_levels(tb) else: length = None tb_e = traceback.TracebackException(exctype, value, tb, limit=length, capture_locals=self.tb_locals) msgLines = list(tb_e.format()) # Stdout, Stderr if self.buffer: output = stdout.getvalue() error = stderr.getvalue() if output: name = TITLE_NAME_FMT.format('stdout') msgLines.append(TITLE_BANNER_FMT.format(name)) if not output.endswith('\n'): output += '\n' msgLines.append(STDOUT_LINE % output) if error: name = TITLE_NAME_FMT.format('stderr') msgLines.append(TITLE_BANNER_FMT.format(name)) if not error.endswith('\n'): error += '\n' msgLines.append(STDERR_LINE % error) # Our logs for _, l in test.logfiles.items(): if not environ.get("CI"): msgLines.append("Further info in file '%s'\n" % l[0]) continue name = TITLE_NAME_FMT.format(l[1]) msgLines.append(TITLE_BANNER_FMT.format(name)) try: with open(l[0]) as f: cnt = f.read() if not cnt.endswith('\n'): cnt += '\n' msgLines.append(cnt) except FileNotFoundError: msgLines.append("File not present\n") return ''.join(msgLines)
def test_good_end(self): hero: Hero = self.game_state.hero hero.inventory.append("#item_thank_you_letter") stdout = io.StringIO() with contextlib.redirect_stdout(stdout), self.assertRaises( SystemExit) as e: self.ih.handle_user_input("read letter") result_output = stdout.getvalue() expected_output = "If you are reading this, it means you have solved my puzzles and collected the artifact.\nThank you for your time, it means a lot to me.\n" self.assertEqual(expected_output, result_output) self.assertEqual('0', str(e.exception))
def brainfuck(info, src): """Executes a brainfuck program""" stdin = io.BytesIO( "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod") stdout = io.BytesIO() # Keep running under very strict conditions steps = 0 interpreter = Brainfuck(src, stdin, stdout) while interpreter.is_running() and stdout.tell() < 100 and steps < 500: interpreter.step() steps += 1 return stdout.getvalue()
def test_bad_end(self): hero = self.game_state.hero hero.location = "#room_ancient_temple" stdout = io.StringIO() with contextlib.redirect_stdout(stdout), self.assertRaises( SystemExit) as e: self.ih.handle_user_input("attack demon") result_output = stdout.getvalue() expected_output = "You hit the guardian of the artifact for 5 damage! Guardian of the artifact has 95 HP left.\n" \ "Guardian of the artifact hit you for 100 damage! You have 0 HP left.\n" \ "GAME OVER. You were killed by guardian of the artifact. Better luck next time.\n" self.assertEqual(expected_output, result_output) self.assertEqual('0', str(e.exception)) demon: Creature = self.game_state.creatures["#creature_temple_demon"] self.assertEqual(95, demon.health) self.assertEqual(0, hero.health)