diff options
| -rw-r--r-- | nb/test.py | 13 | ||||
| -rw-r--r-- | nbs/test.ipynb | 61 |
2 files changed, 63 insertions, 11 deletions
@@ -1,7 +1,7 @@ # AUTOGENERATED! DO NOT EDIT! # cell: dd03783f-252c-40f5-b92f-0f3f9278ce09 -import ast,copy,contextlib,inspect,os,operator,textwrap,traceback +import ast,copy,contextlib,inspect,os,operator,reprlib,textwrap,traceback from typing import Any,Callable,Generator from dataclasses import dataclass @@ -62,13 +62,18 @@ def expect( cmp:Callable, negstr:str|None=None, # Used in the error message on failure: 'a {negstr} b'. Defaults to 'not {cmp.__name__}'. frames_up:int|None=1, # The number of frames up to look at to get the source expressions. + truncate:bool=True, # Truncate the repr representations of a and b when shown. ): "Expect that cmp(a, b) is true." if negstr is None: negstr = f'not {cmp.__name__}' args_src, _ = get_call_args(frames_up) - lines = [f"{repr(a)} {negstr} {repr(b)}"] - if len(args_src) > 0 and repr(a) != args_src[0]: lines.append(f" where {repr(a)} = {args_src[0]}") - if len(args_src) > 1 and repr(b) != args_src[1]: lines.append(f" where {repr(b)} = {args_src[1]}") + a_repr = reprlib.repr(a) if truncate else repr(a) + b_repr = reprlib.repr(b) if truncate else repr(b) + a_src = args_src[0] if len(args_src) > 0 else a_repr + b_src = args_src[1] if len(args_src) > 1 else b_repr + lines = [f'{a_src} {negstr} {b_src}'] + if len(args_src) > 0 and a_repr != args_src[0]: lines.append(f' where {args_src[0]} = {a_repr}') + if len(args_src) > 1 and b_repr != args_src[1]: lines.append(f' where {args_src[1]} = {b_repr}') assert cmp(a, b), "\n".join(lines) # cell: d1514687 diff --git a/nbs/test.ipynb b/nbs/test.ipynb index 5645626..5f8aafb 100644 --- a/nbs/test.ipynb +++ b/nbs/test.ipynb @@ -30,7 +30,7 @@ "outputs": [], "source": [ "#| export\n", - "import ast,copy,contextlib,inspect,os,operator,textwrap,traceback\n", + "import ast,copy,contextlib,inspect,os,operator,reprlib,textwrap,traceback\n", "from typing import Any,Callable,Generator\n", "from dataclasses import dataclass" ] @@ -698,14 +698,19 @@ " cmp:Callable,\n", " negstr:str|None=None, # Used in the error message on failure: 'a {negstr} b'. Defaults to 'not {cmp.__name__}'.\n", " frames_up:int|None=1, # The number of frames up to look at to get the source expressions.\n", + " truncate:bool=True, # Truncate the repr representations of a and b when shown.\n", "):\n", " \"Expect that cmp(a, b) is true.\"\n", " if negstr is None: negstr = f'not {cmp.__name__}'\n", " args_src, _ = get_call_args(frames_up)\n", - " lines = [f\"{repr(a)} {negstr} {repr(b)}\"]\n", - " if len(args_src) > 0 and repr(a) != args_src[0]: lines.append(f\" where {repr(a)} = {args_src[0]}\")\n", - " if len(args_src) > 1 and repr(b) != args_src[1]: lines.append(f\" where {repr(b)} = {args_src[1]}\")\n", - " assert cmp(a, b), \"\\n\".join(lines)\n" + " a_repr = reprlib.repr(a) if truncate else repr(a)\n", + " b_repr = reprlib.repr(b) if truncate else repr(b)\n", + " a_src = args_src[0] if len(args_src) > 0 else a_repr\n", + " b_src = args_src[1] if len(args_src) > 1 else b_repr\n", + " lines = [f'{a_src} {negstr} {b_src}']\n", + " if len(args_src) > 0 and a_repr != args_src[0]: lines.append(f' where {args_src[0]} = {a_repr}')\n", + " if len(args_src) > 1 and b_repr != args_src[1]: lines.append(f' where {args_src[1]} = {b_repr}')\n", + " assert cmp(a, b), \"\\n\".join(lines)" ] }, { @@ -730,6 +735,48 @@ }, { "cell_type": "markdown", + "id": "1ea3c94e", + "metadata": {}, + "source": [ + "If the `repr` of either argument is too long when being shown in the output, it will be automatically truncated via `reprlib.repr`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dec5f3d2", + "metadata": {}, + "outputs": [], + "source": [ + "with raises() as e: expect('a' * 1000, 'b' * 1000, operator.eq)\n", + "assert e.value.args[0] == \"\"\"'a' * 1000 not eq 'b' * 1000\n", + " where 'a' * 1000 = 'aaaaaaaaaaaa...aaaaaaaaaaaaa'\n", + " where 'b' * 1000 = 'bbbbbbbbbbbb...bbbbbbbbbbbbb'\"\"\"" + ] + }, + { + "cell_type": "markdown", + "id": "b7724e85", + "metadata": {}, + "source": [ + "If we want to see everything, we can turn the truncation off:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2e03d976", + "metadata": {}, + "outputs": [], + "source": [ + "with raises() as e: expect('a' * 1000, 'b' * 1000, operator.eq, truncate=False)\n", + "assert e.value.args[0] == f\"\"\"'a' * 1000 not eq 'b' * 1000\n", + " where 'a' * 1000 = '{'a' * 1000}'\n", + " where 'b' * 1000 = '{'b' * 1000}'\"\"\"" + ] + }, + { + "cell_type": "markdown", "id": "ef59b47e", "metadata": {}, "source": [ @@ -763,7 +810,7 @@ "outputs": [], "source": [ "with raises() as e: expect(1+1, 2*10, operator.eq, negstr='!=')\n", - "assert e.value.args[0] == '2 != 20\\n where 2 = 1 + 1\\n where 20 = 2 * 10'" + "assert e.value.args[0] == '1 + 1 != 2 * 10\\n where 1 + 1 = 2\\n where 2 * 10 = 20'" ] }, { @@ -801,7 +848,7 @@ "outputs": [], "source": [ "with raises() as e: f(1, 2)\n", - "assert e.value.args[0] == '1 not eq 2\\n where 1 = a\\n where 2 = b'" + "assert e.value.args[0] == 'a not eq b\\n where a = 1\\n where b = 2'" ] }, { |