def test_stdout_disable(self): """Make sure IOCage doesn't capture stdout without `enable_stdout` set to `True`""" internal = IOCage(enable_stdout=False) external = IOCage() with external: with internal: print("(test)") self.assertEqual(internal.stdout, "") self.assertEqual(external.stdout, "(test)\n")
def test_stderr_disable(self): """Make sure IOCage doesn't capture stderr without `enable_stdout` set to `True`""" internal = IOCage(enable_stderr=False) external = IOCage() with external: with internal: warnings.warn("Test") self.assertEqual(internal.stderr, "") self.assertIn('warnings.warn("Test")', external.stderr)
def test_wrapper_arguments(self): """Make sure wrapper with arguments implementation of IOCage works.""" _original_stdout = sys.stdout captured = IOCage() def foo(my_string, my_kwarg="default"): self.assertIsNot(sys.stdout, _original_stdout) print(my_string + my_kwarg) captured.capture(foo, args=("test ", ), kwargs={"my_kwarg": "string"}) self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "test string\n")
def test_manual_reset(self): """Test IOCage with manual reset calling (during disabled automatic reset behavior).""" captured = IOCage(auto_reset=False) with captured: print("print1") self.assertEqual(captured.stdout, "print1\n") captured.reset() with captured: print("print2") self.assertEqual(captured.stdout, "print2\n")
def test_wrapper(self): """Make sure wrapper implementation of IOCage works.""" _original_stdout = sys.stdout captured = IOCage() def foo(): self.assertIsNot(sys.stdout, _original_stdout) print("test string") captured.capture(foo) self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "test string\n")
def test_context_manager(self): """Make sure context manager implementation of IOCage works.""" _original_stdout = sys.stdout captured = IOCage() with captured: self.assertIsNot(sys.stdout, _original_stdout) print("test string") self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "test string\n")
def test_no_auto_reset(self): """Test IOCage automatic reset behavior (accumulates strings).""" captured = IOCage(auto_reset=False) with captured: print("print1") with captured: print("print2") self.assertEqual(captured.stdout, "print1\nprint2\n")
def test_auto_reset(self): """Test automatic reset capability of IOCage.""" captured = IOCage(auto_reset=True) with captured: print("print1") self.assertEqual(captured.stdout, "print1\n") with captured: print("print2") self.assertEqual(captured.stdout, "print2\n")
def test_stdin(self): """Test IOCage capability of simulating STDIN.""" _original_stdin = sys.stdin captured = IOCage(stdin="hello\nthere\n") collected = [] with captured: self.assertIsNot(sys.stdin, _original_stdin) collected.append(input()) collected.append(input()) self.assertEqual(collected, ["hello", "there"])
def test_decorator_arguments(self): """Make sure decorator with arguments implementation of IOCage works.""" _original_stdout = sys.stdout captured = IOCage() @captured def foo(my_string, my_kwarg="default"): self.assertIsNot(sys.stdout, _original_stdout) print(my_string + my_kwarg) foo("test ", my_kwarg="string") self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "test string\n")
def test_decorator(self): """Make sure decorator implementation of IOCage works.""" _original_stdout = sys.stdout captured = IOCage() @captured def foo(): self.assertIsNot(sys.stdout, _original_stdout) print("test string") foo() self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "test string\n")
def test_manual_capture(self): """Test bare manual way of using IOCage""" _original_stdout = sys.stdout captured = IOCage() captured.override_std() print("hello") self.assertIsNot(sys.stdout, _original_stdout) captured.restore_std() self.assertIs(sys.stdout, _original_stdout) self.assertEqual(captured.stdout, "hello\n")
def test_stdout(self): """Make sure IOCage is able to properly capture given prints.""" test_cases = ( ("foo",), # Single print ("python", "is", "cool"), # Multiple prints (None, ), # No print ) for test_prints in test_cases: with self.subTest(test_prints=test_prints): capture = IOCage() with capture: for test_print in test_prints: if test_print is not None: print(test_print) expected_stdout = "\n".join(s for s in test_prints if s is not None) expected_stdout = expected_stdout + "\n" if not expected_stdout == "" else expected_stdout self.assertEqual(capture.stdout, expected_stdout)