def test_lazy_load_hooks_from_variable(self): class ndarray: pass nd_array = ndarray() var = Variable("nd_array", 2, nd_array) with patch("frosch.type_hooks.HookLoader._lazy_load_hooks") as lazy_hook_mock: hook_loader = HookLoader() hook_loader.lazy_load_hooks_from_variable(var) lazy_hook_mock.assert_called_once_with("hook_numpy")
def test_lazy_load_torch_hooks_from_variable(self): class Tensor: pass _tensor = Tensor() var = Variable("tens", 2, _tensor) with patch("frosch.type_hooks.HookLoader._lazy_load_hooks" ) as lazy_hook_mock: hook_loader = HookLoader() hook_loader.lazy_load_hooks_from_variable(var) lazy_hook_mock.assert_called_once_with("hook_torch")
def test_output_traceback_no_formatting_applied(capsys): cw = writer.ConsoleWriter("emacs", sys.stderr, HookLoader()) with patch.object(writer.traceback, "format_exception", return_value=["Hello", "\n"]) as format_exception: cw.write_traceback("A", "B", "C") format_exception.assert_called_once_with("A", "B", "C") captured = capsys.readouterr() assert captured.err == "Hello\n"
def test_out_traceback_with_format(capsys): cw = writer.ConsoleWriter("monokai", sys.stderr, HookLoader()) tb = "Traceback Value" error_message = "Some Error Message" error_type = "IndexError" with patch.object(writer.traceback, "format_exception", return_value=["Some", "traceback"]) as format_mock: cw.write_traceback(error_type, error_message, tb) format_mock.assert_called_once_with(error_type, error_message, tb) captured = capsys.readouterr() escaped_tb = escape_ansi(captured.err) assert escaped_tb.strip() == "Sometraceback"
def test_write_debug_tree_offset_2(capsys): Variable = writer.Variable var1 = Variable("y", 0) var1.value = "Something" var2 = Variable("x", 2) var2.value = "Other" sorted_values = [var1, var2] console_writer = writer.ConsoleWriter("monokai", sys.stderr, HookLoader()) console_writer.left_offset = 2 console_writer.write_debug_tree(sorted_values) capture = capsys.readouterr() result = escape_ansi(capture.err) expected_result = """ || │ │ || │ └── x: str = 'Other' || │ || └── y: str = 'Something' || \n""" assert result == expected_result
def test_lazy_load_hooks(self): with mock_numpy_module() as numpy_mock: hook_loader = HookLoader() hook_loader._lazy_load_hooks("hook_numpy") self.assertEqual(len(hook_loader._hooks), 2)
def setUp(self) -> None: self.cw = writer.ConsoleWriter("monokai", sys.stderr, HookLoader())
def test_render_last_line(capsys): cw = writer.ConsoleWriter("vim", sys.stderr, HookLoader()) cw.write_last_line(42, "x = hello * 'String'") captured = capsys.readouterr() output = escape_ansi(captured.err).strip() assert output == "42 || x = hello * 'String'"
def test_write_out(capsys): cw = writer.ConsoleWriter("monokai", sys.stderr, HookLoader()) cw._write_out("Hello World") captured = capsys.readouterr() assert captured.err == "Hello World"
def test_lazy_load_hooks_pytorch(self): with mock_module("torch"): hook_loader = HookLoader() hook_loader._lazy_load_hooks("hook_torch") self.assertEqual(len(hook_loader._hooks), 1)