aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-05-22 02:24:58 +0000
committerEugene Triguba <hello@eugenetriguba.com>2026-05-22 02:27:11 +0000
commit8eebbe0a7f5fc85b50731d63d4cd5f05cf0b0fe9 (patch)
tree9e71359c9cdfc7b5d3c58048e621b545871c7e2c
parentAdd py.typed (diff)
downloadnb-8eebbe0a7f5fc85b50731d63d4cd5f05cf0b0fe9.zip
Add expect, expect_fail, and nequals to test
-rw-r--r--nb/test.py51
-rw-r--r--nbs/test.ipynb647
2 files changed, 652 insertions, 46 deletions
diff --git a/nb/test.py b/nb/test.py
index 8bc6df4..378352a 100644
--- a/nb/test.py
+++ b/nb/test.py
@@ -1,8 +1,8 @@
# AUTOGENERATED! DO NOT EDIT!
# cell: dd03783f-252c-40f5-b92f-0f3f9278ce09
-import ast,copy,contextlib,inspect,textwrap,traceback
-from typing import Any,Generator
+import ast,copy,contextlib,inspect,operator,textwrap,traceback
+from typing import Any,Callable,Generator
from dataclasses import dataclass
# cell: a6c67919
@@ -32,15 +32,15 @@ def get_call_args(frames_up: int = 1) -> tuple[list[str], dict[str,Any]]:
# cell: 623a480c
@dataclass
class ExceptionInfo:
- type_: type[Exception] | None = None
- value: Exception | None = None
- traceback: str | None = None
+ type_: type[Exception]|None=None
+ value: Exception|None=None
+ traceback: str|None=None
# cell: 0ad5fcef-f04d-469d-8ad1-94d3a98694e4
@contextlib.contextmanager
def raises(
- exc: type[Exception] = Exception, # The exception type to check is raised.
- contains: str | None = None, # Optionally, check whether the raised exception contains this message.
+ exc: type[Exception]=Exception, # The exception type to check is raised.
+ contains: str|None=None, # Check whether the raised exception contains this message.
) -> Generator[ExceptionInfo, None, None]:
"Asserts that the body of the context manager raises `exc`."
raised = True
@@ -54,17 +54,34 @@ def raises(
info.traceback = traceback.format_tb(e.__traceback__)
if contains and contains not in str(e):
raise AssertionError(f"Raised {e} but message does not contain {contains}") from e
- if not raised:
- raise AssertionError(f"Did not raise {e}")
+ assert raised, "Did not raise"
+
+# cell: 10ba9dcd
+def expect(
+ a, b,
+ 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.
+):
+ "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 repr(a) != args_src[0]: lines.append(f" where {repr(a)} = {args_src[0]}")
+ if repr(b) != args_src[1]: lines.append(f" where {repr(b)} = {args_src[1]}")
+ assert cmp(a, b), "\n".join(lines)
+
+# cell: d1514687
+def expect_fail(a, b, cmp:Callable, exc:type[Exception]=Exception, contains:str|None=None):
+ "Expect that cmp(a, b) raises `exc` and optionally that `exc` also contains `contains`."
+ with raises(exc, contains): cmp(a, b)
# cell: 2b503871-3f39-4013-a7ae-f5143af3138e
def equal(a, b):
"Assert that a == b."
- args_src, _ = get_call_args()
- if a != b:
- msg = (
- f"FAIL: {args_src[0]} != {args_src[1]}\n" +
- ' ' * (len(AssertionError.__name__)+2) +
- f" {a!r} != {b!r}"
- )
- raise AssertionError(msg)
+ expect(a, b, operator.eq, negstr='!=', frames_up=2)
+
+# cell: 9611b552
+def nequal(a, b):
+ "Assert that a != b"
+ expect(a, b, operator.ne, negstr='==', frames_up=2)
diff --git a/nbs/test.ipynb b/nbs/test.ipynb
index 2302a27..102162c 100644
--- a/nbs/test.ipynb
+++ b/nbs/test.ipynb
@@ -29,8 +29,8 @@
"outputs": [],
"source": [
"#| export\n",
- "import ast,copy,contextlib,inspect,textwrap,traceback\n",
- "from typing import Any,Generator\n",
+ "import ast,copy,contextlib,inspect,operator,textwrap,traceback\n",
+ "from typing import Any,Callable,Generator\n",
"from dataclasses import dataclass"
]
},
@@ -74,6 +74,334 @@
]
},
{
+ "cell_type": "markdown",
+ "id": "86b4b04c",
+ "metadata": {},
+ "source": [
+ "Let's take a look at `inspect.currentframe` to start, which returns the frame object for the caller's stack frame.\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "058c5afe",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['clear',\n",
+ " 'f_back',\n",
+ " 'f_builtins',\n",
+ " 'f_code',\n",
+ " 'f_globals',\n",
+ " 'f_lasti',\n",
+ " 'f_lineno',\n",
+ " 'f_locals',\n",
+ " 'f_trace',\n",
+ " 'f_trace_lines',\n",
+ " 'f_trace_opcodes']"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "pubattr(inspect.currentframe())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "5cd6117c",
+ "metadata": {},
+ "source": [
+ "Okay, so we can get the *current* frame, but we want the caller's frame. For any given frame, we can get the *outer frames* of it via `inspect.getouterframes`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "28ec6212",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def f(x,y,z,a=1):\n",
+ " return inspect.getouterframes(inspect.currentframe())[1]\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a7fa576a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with open(\"/tmp/tmp.txt\", mode=\"a\"):\n",
+ " caller_fm = f(\n",
+ " 1+1,\n",
+ " 2,\n",
+ " max(5, 2),\n",
+ " a=2,\n",
+ " )\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f2088db2",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' caller_fm = f(\\n']"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "caller_fm.code_context"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c463da2f",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "Positions(lineno=2, end_lineno=7, col_offset=16, end_col_offset=5)"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "caller_fm.positions"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "cf730643",
+ "metadata": {},
+ "source": [
+ "From this, we can see `code_context` has the first line of context of where we're executing `f`. Furthermore, note `positions` has all the information around we're extract out only our call to `f` from this block of code."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "95cd0a65",
+ "metadata": {},
+ "source": [
+ "Okay, so what do we do from here? We _could_ use `code_context` and pass in `context=X` to `inspect.getouterframes`. However, there isn't a great static value to pass in for `context` since a function could span an arbitrary number of lines.\n",
+ "\n",
+ "So, what could we do instead? The `inspect` module has a function called `inspect.getsourcelines`. Given a frame, we can get the source code with `inspect.getsourcelines` and we can extract out our exact function from the source code via the `positions` on the frame object."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cde6030a",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['with open(\"/tmp/tmp.txt\", mode=\"a\"):\\n',\n",
+ " ' caller_fm = f(\\n',\n",
+ " ' 1+1,\\n',\n",
+ " ' 2,\\n',\n",
+ " ' max(5, 2),\\n',\n",
+ " ' a=2,\\n',\n",
+ " ' )\\n']"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "src, _ = inspect.getsourcelines(caller_fm.frame)\n",
+ "src"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "022f2436",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[' caller_fm = f(\\n',\n",
+ " ' 1+1,\\n',\n",
+ " ' 2,\\n',\n",
+ " ' max(5, 2),\\n',\n",
+ " ' a=2,\\n',\n",
+ " ' )\\n']"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "srcl = src[caller_fm.positions.lineno-1:caller_fm.positions.end_lineno]\n",
+ "srcl"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0b306110",
+ "metadata": {},
+ "source": [
+ "So, now we have the *lines* of source code we're interested in. However, we also need to look at the *column offsets* of the first and last line to get only our call to `f`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a2c3f459",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'f'"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "srcl[0][caller_fm.positions.col_offset]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5a7d9955",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'\\n'"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "srcl[-1][caller_fm.positions.end_col_offset]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c30f907f",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'f(\\n 1+1,\\n 2,\\n max(5, 2),\\n a=2,\\n )'"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "fn_src = \"\".join(\n",
+ " [srcl[0][caller_fm.positions.col_offset:]] +\n",
+ " srcl[1:-1] +\n",
+ " [srcl[-1][:caller_fm.positions.end_col_offset]]\n",
+ ")\n",
+ "fn_src"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "70b80328",
+ "metadata": {},
+ "source": [
+ "Beautiful! Anywhere we are, we can get the current frame, get the caller frame, get the source code for that frame, and extract out the function call that got us to where we are.\n",
+ "\n",
+ "Now, how can we extract out the arguments to the function call? One approach we can use here is to use the `ast` module to parse the function call into a tree that is knowledable about what each piece of syntax means. That then allows us to easily find the function arguments without having to parse raw text."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "54df1747",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "['1 + 1', '2', 'max(5, 2)']"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "fn_src_args = [ast.unparse(arg) for arg in ast.parse(fn_src).body[0].value.args]\n",
+ "fn_src_args"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ee5cb9e8",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "{'a': '2'}"
+ ]
+ },
+ "execution_count": null,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "fn_src_kwargs = {kw.arg: ast.unparse(kw.value) for kw in ast.parse(fn_src).body[0].value.keywords}\n",
+ "fn_src_kwargs"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0dde3bf7",
+ "metadata": {},
+ "source": [
+ "Okay, we should now have everything we need. Let's put it together."
+ ]
+ },
+ {
"cell_type": "code",
"execution_count": null,
"id": "a6c67919",
@@ -283,6 +611,14 @@
]
},
{
+ "cell_type": "markdown",
+ "id": "a25dcdf0",
+ "metadata": {},
+ "source": [
+ "### Raises"
+ ]
+ },
+ {
"cell_type": "code",
"execution_count": null,
"id": "623a480c",
@@ -292,9 +628,9 @@
"#| export\n",
"@dataclass\n",
"class ExceptionInfo:\n",
- " type_: type[Exception] | None = None\n",
- " value: Exception | None = None\n",
- " traceback: str | None = None"
+ " type_: type[Exception]|None=None\n",
+ " value: Exception|None=None\n",
+ " traceback: str|None=None"
]
},
{
@@ -307,8 +643,8 @@
"#| export\n",
"@contextlib.contextmanager\n",
"def raises(\n",
- " exc: type[Exception] = Exception, # The exception type to check is raised.\n",
- " contains: str | None = None, # Optionally, check whether the raised exception contains this message.\n",
+ " exc: type[Exception]=Exception, # The exception type to check is raised.\n",
+ " contains: str|None=None, # Check whether the raised exception contains this message.\n",
") -> Generator[ExceptionInfo, None, None]:\n",
" \"Asserts that the body of the context manager raises `exc`.\"\n",
" raised = True\n",
@@ -322,8 +658,7 @@
" info.traceback = traceback.format_tb(e.__traceback__)\n",
" if contains and contains not in str(e):\n",
" raise AssertionError(f\"Raised {e} but message does not contain {contains}\") from e\n",
- " if not raised:\n",
- " raise AssertionError(f\"Did not raise {e}\")"
+ " assert raised, \"Did not raise\""
]
},
{
@@ -331,19 +666,236 @@
"execution_count": null,
"id": "7c478098-7a09-403c-a62b-3c34cf0f7bab",
"metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "ExceptionInfo(type_=<class 'Exception'>, value=Exception('abc'), traceback=[' File \"/tmp/ipykernel_572/3259713399.py\", line 10, in raises\\n yield info\\n', ' File \"/tmp/ipykernel_572/3922684010.py\", line 2, in <module>\\n raise Exception(\"abc\")\\n'])\n"
- ]
- }
- ],
+ "outputs": [],
"source": [
"with raises() as e:\n",
" raise Exception(\"abc\")\n",
- "print(e)"
+ "assert type(e) == ExceptionInfo\n",
+ "assert e.type_ == Exception\n",
+ "assert e.value.args == ('abc',)\n",
+ "assert len(e.traceback) > 0"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "765265ca",
+ "metadata": {},
+ "source": [
+ "### Primitives (expect, expect_fail)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "10ba9dcd",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def expect(\n",
+ " a, b,\n",
+ " 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",
+ "):\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 repr(a) != args_src[0]: lines.append(f\" where {repr(a)} = {args_src[0]}\")\n",
+ " if repr(b) != args_src[1]: lines.append(f\" where {repr(b)} = {args_src[1]}\")\n",
+ " assert cmp(a, b), \"\\n\".join(lines)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "b583f813",
+ "metadata": {},
+ "source": [
+ "`expect` is a basic primitive for creating other assertion helpers. It takes two arguments and a callable and checks whether calling the callable with the two arguments yields `True`. If it doesn't, it prints a friendly error message."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "19d62642",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises() as e: expect(1, 2, operator.eq)\n",
+ "assert e.type_ == AssertionError\n",
+ "assert e.value.args[0] == '1 not eq 2'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ef59b47e",
+ "metadata": {},
+ "source": [
+ "The operator in the error message can be customized with `negstr`:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b9eedc92",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises() as e: expect(1, 2, operator.eq, negstr='!=')\n",
+ "assert e.value.args[0] == '1 != 2'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a2df82d4",
+ "metadata": {},
+ "source": [
+ "Furthermore, if you pass in expressions rather than constants, you'll see a print out of what the expressions were:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "84055a16",
+ "metadata": {},
+ "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'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "91ea0818",
+ "metadata": {},
+ "source": [
+ "If you end up creating a test helper with `expect`, you'll want to use `frames_up=2` to get the correct caller frame now, so `expect` grabs the right function invocation source code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ec18eb15",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def f(a,b,f=1):\n",
+ " expect(a,b,cmp=operator.eq,frames_up=f)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "8819d27d",
+ "metadata": {},
+ "source": [
+ "If we don't change `frames_up` (f) to 2, we'll get an error message for the call stack _inside f_ (because we're the caller of `expect`)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5f8837b1",
+ "metadata": {},
+ "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'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "af756a6c",
+ "metadata": {},
+ "source": [
+ "Instead, we want to use `frames_up=2` to tell `expect` to look at the source code expressions for the *caller of f*."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7b4bd85a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises() as e: f(1, 2, f=2)\n",
+ "assert e.value.args[0] == '1 not eq 2'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d1514687",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def expect_fail(a, b, cmp:Callable, exc:type[Exception]=Exception, contains:str|None=None):\n",
+ " \"Expect that cmp(a, b) raises `exc` and optionally that `exc` also contains `contains`.\"\n",
+ " with raises(exc, contains): cmp(a, b)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3ae99417",
+ "metadata": {},
+ "source": [
+ "`expect_fail` is another primitive that allows us to check whether some comparison should result in an exception being raised."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "69096bc9",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "expect_fail(1, 0, operator.itruediv)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2d182509",
+ "metadata": {},
+ "source": [
+ "By default, it will check that any `Exception` is raised. However, we can be more specific and also check that the exception message contains some string."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "823ada13",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "expect_fail(1, 0, operator.itruediv, exc=ZeroDivisionError, contains='division by zero')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a0b25b81",
+ "metadata": {},
+ "source": [
+ "If our expectation fails, `expect_fail` raises an `AssertionError`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0e6df664",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises(exc=AssertionError) as e: expect_fail(1, 1, operator.itruediv)\n",
+ "assert e.value.args[0] == 'Did not raise'"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "4ca8f337",
+ "metadata": {},
+ "source": [
+ "### Higher Level Assertions"
]
},
{
@@ -356,14 +908,7 @@
"#| export\n",
"def equal(a, b):\n",
" \"Assert that a == b.\"\n",
- " args_src, _ = get_call_args()\n",
- " if a != b:\n",
- " msg = (\n",
- " f\"FAIL: {args_src[0]} != {args_src[1]}\\n\" +\n",
- " ' ' * (len(AssertionError.__name__)+2) +\n",
- " f\" {a!r} != {b!r}\"\n",
- " )\n",
- " raise AssertionError(msg)"
+ " expect(a, b, operator.eq, negstr='!=', frames_up=2)"
]
},
{
@@ -373,8 +918,52 @@
"metadata": {},
"outputs": [],
"source": [
- "with raises(exc=AssertionError) as e:\n",
- " equal(1, 2)"
+ "with raises() as e: equal(1, 2)\n",
+ "assert e.value.args[0] == '1 != 2'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ec0a5998",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "equal(1, 1)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9611b552",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#| export\n",
+ "def nequal(a, b):\n",
+ " \"Assert that a != b\"\n",
+ " expect(a, b, operator.ne, negstr='==', frames_up=2)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4e8a42da",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "with raises() as e: nequal(1, 1)\n",
+ "assert e.value.args[0] == '1 == 1'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b26a3d35",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "nequal(1, 2)"
]
},
{