def test_multiple_debugs(): debug.format([i * 2 for i in range(2)]) debug.format([i * 2 for i in range(2)]) v = debug.format([i * 2 for i in range(2)]) s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert s == ('tests/test_main.py:<line no> test_multiple_debugs\n' ' [i * 2 for i in range(2)]: [0, 2] (list) len=2')
def test_multiple_debugs(): debug.format([i * 2 for i in range(2)]) debug.format([i * 2 for i in range(2)]) v = debug.format([i * 2 for i in range(2)]) s = normalise_output(str(v)) assert s == ('tests/test_main.py:<line no> test_multiple_debugs\n' ' [i * 2 for i in range(2)]: [0, 2] (list) len=2')
def test_multiple_debugs_38(): debug.format([i * 2 for i in range(2)]) debug.format([i * 2 for i in range(2)]) v = debug.format([i * 2 for i in range(2)]) s = re.sub(r':\d{2,}', ':<line no>', str(v)) # FIXME there's an extraneous bracket here, due to some error building code from the ast assert s == ('tests/test_main.py:<line no> test_multiple_debugs_38\n' ' ([i * 2 for i in range(2)]: [0, 2] (list) len=2')
def test_wrong_ast_type(mocker): mocked_ast_parse = mocker.patch('ast.parse') code = 'async def wrapper():\n x = "foobar"' mocked_ast_parse.return_value = ast.parse(code, filename='testing.py').body[0].body[0].value v = debug.format('x') assert "(error parsing code, found <class 'unittest.mock.MagicMock'> not Call)" in v.str()
def test_kwargs_multiline(): v = debug.format(foobar(1, 2, 3), a=6, b=7) s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert ('tests/test_expr_render.py:<line no> test_kwargs_multiline\n' ' foobar(1, 2, 3): 6 (int)\n' ' a: 6 (int)\n' ' b: 7 (int)') == s
def test_inspect_error(mocker): mocked_getouterframes = mocker.patch('inspect.getouterframes') mocked_getouterframes.side_effect = IndexError() v = debug.format('x') assert str( v ) == "<unknown>:0 (error parsing code, IndexError)\n 'x' (str) len=1"
def test_simple(): a = [1, 2, 3] v = debug.format(len(a)) s = normalise_output(str(v)) # print(s) assert ('tests/test_expr_render.py:<line no> test_simple\n' ' len(a): 3 (int)') == s
def test_multiline_trailing_bracket(): v = debug.format(foobar(1, 2, 3)) s = normalise_output(str(v)) # print(s) assert ( 'tests/test_expr_render.py:<line no> test_multiline_trailing_bracket\n' ' foobar(1, 2, 3 ): 6 (int)') == s
def test_kwargs_multiline(): v = debug.format(foobar(1, 2, 3), a=6, b=7) s = normalise_output(str(v)) assert ('tests/test_expr_render.py:<line no> test_kwargs_multiline\n' ' foobar(1, 2, 3): 6 (int)\n' ' a: 6 (int)\n' ' b: 7 (int)') == s
def test_simple(): a = [1, 2, 3] v = debug.format(len(a)) s = re.sub(r':\d{2,}', ':<line no>', str(v)) # print(s) assert ('tests/test_expr_render.py:<line no> test_simple\n' ' len(a): 3 (int)') == s
def test_multiline_trailing_bracket(): v = debug.format(foobar(1, 2, 3)) s = re.sub(r':\d{2,}', ':<line no>', str(v)) # print(s) assert ( 'tests/test_expr_render.py:<line no> test_multiline_trailing_bracket\n' ' foobar(1, 2, 3 ): 6 (int)') == s
def test_colours_warnings(mocker): mocked_getframe = mocker.patch('sys._getframe') mocked_getframe.side_effect = ValueError() v = debug.format('x') s = normalise_output(v.str(True)) assert s.startswith('\x1b[35m<unknown>'), repr(s) s2 = strip_ansi(s) assert s2 == v.str(), repr(s2)
def test_other_debug_arg(): debug.timer() v = debug.format([1, 2]) # check only the original code is included in the warning s = normalise_output(str(v)) assert s == ('tests/test_expr_render.py:<line no> test_other_debug_arg\n' ' [1, 2] (list) len=2')
def test_kwargs(): a = 'variable' v = debug.format(first=a, second='literal') s = re.sub(r':\d{2,}', ':<line no>', str(v)) print(s) assert s == ("tests/test_main.py:<line no> test_kwargs\n" " first: 'variable' (str) len=8 variable=a\n" " second: 'literal' (str) len=7")
def test_inspect_error(mocker): mocked_getframe = mocker.patch('sys._getframe') mocked_getframe.side_effect = ValueError() v = debug.format('x') print(repr(str(v))) assert str( v ) == "<unknown>:0 (error parsing code, call stack too shallow)\n 'x' (str) len=1"
def test_colours_warnings(mocker): mocked_getframe = mocker.patch('sys._getframe') mocked_getframe.side_effect = ValueError() v = debug.format('x') s = re.sub(r':\d{2,}', ':<line no>', v.str(True)) assert s.startswith('\x1b[35m<unknown>'), repr(s) s2 = strip_ansi(s) assert s2 == v.str(), repr(s2)
def test_syntax_warning(): # exceed the 4 extra lines which are normally checked v = debug.format(abs(abs(abs(abs(-1))))) # check only the original code is included in the warning s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert s.startswith( 'tests/test_expr_render.py:<line no> test_syntax_warning (error passing code, ' 'SyntaxError: unexpected EOF')
def test_odd_path(mocker): # all valid calls mocked_relative_to = mocker.patch('pathlib.Path.relative_to') mocked_relative_to.side_effect = ValueError() v = debug.format('test') assert re.search( r"/.*?/test_main.py:\d{2,} test_odd_path\n 'test' \(str\) len=4", str(v)), v
def test_subscription(): a = {1: 2} v = debug.format(a[1]) s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert ( 'tests/test_expr_render.py:<line no> test_subscription\n' ' a[1]: 2 (int)' ) == s
def test_kwargs(): a = 'variable' v = debug.format(first=a, second='literal') s = normalise_output(str(v)) print(s) assert s == ("tests/test_main.py:<line no> test_kwargs\n" " first: 'variable' (str) len=8 variable=a\n" " second: 'literal' (str) len=7")
def test_starred_kwargs(): v = {'foo': 1, 'bar': 2} v = debug.format(**v) s = re.sub(r':\d{2,}', ':<line no>', v.str()) assert set(s.split('\n')) == { 'tests/test_main.py:<line no> test_starred_kwargs', ' foo: 1 (int)', ' bar: 2 (int)', }
def test_format(): a = b'i might bite' b = "hello this is a test" v = debug.format(a, b) s = re.sub(r':\d{2,}', ':<line no>', str(v)) print(s) assert s == ("tests/test_main.py:<line no> test_format\n" " a: b'i might bite' (bytes) len=12\n" " b: 'hello this is a test' (str) len=20")
def test_format(): a = b'i might bite' b = "hello this is a test" v = debug.format(a, b) s = normalise_output(str(v)) print(s) assert s == ("tests/test_main.py:<line no> test_format\n" " a: b'i might bite' (bytes) len=12\n" " b: 'hello this is a test' (str) len=20")
def test_starred_kwargs(): v = {'foo': 1, 'bar': 2} v = debug.format(**v) s = normalise_output(v.str()) assert set(s.split('\n')) == { 'tests/test_main.py:<line no> test_starred_kwargs', ' foo: 1 (int)', ' bar: 2 (int)', }
def test_newline(): v = debug.format( foobar(1, 2, 3)) s = re.sub(r':\d{2,}', ':<line no>', str(v)) # print(s) assert ( 'tests/test_expr_render.py:<line no> test_newline\n' ' foobar(1, 2, 3): 6 (int)' ) == s
def test_other_debug_arg_not_literal(): debug.timer() x = 1 y = 2 v = debug.format([x, y]) s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert s == ( 'tests/test_expr_render.py:<line no> test_other_debug_arg_not_literal\n' ' [x, y]: [1, 2] (list) len=2')
def test_multiple_trailing_lines(): v = debug.format( foobar( 1, 2, 3 ), ) s = re.sub(r':\d{2,}', ':<line no>', str(v)) assert ( 'tests/test_expr_render.py:<line no> test_multiple_trailing_lines\n foobar( 1, 2, 3 ): 6 (int)' ) == s
def test_other_debug_arg_not_literal(): debug.timer() x = 1 y = 2 v = debug.format([x, y]) s = normalise_output(str(v)) assert s == ( 'tests/test_expr_render.py:<line no> test_other_debug_arg_not_literal\n' ' [x, y]: [1, 2] (list) len=2')
def test_attributes(): class Foo: x = 1 class Bar: y = Foo() b = Bar() v = debug.format(b.y.x) assert 'test_attributes\n b.y.x: 1 (int)' in str(v)
def test_kwargs_orderless(): # for python3.5 a = 'variable' v = debug.format(first=a, second='literal') s = normalise_output(str(v)) assert set(s.split('\n')) == { "tests/test_main.py:<line no> test_kwargs_orderless", " first: 'variable' (str) len=8 variable=a", " second: 'literal' (str) len=7", }