Skip to content

Commit

Permalink
make the functional call checker support any function name
Browse files Browse the repository at this point in the history
  • Loading branch information
afeld committed Mar 25, 2022
1 parent 8a9871a commit 762e53a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
12 changes: 7 additions & 5 deletions extras/scripts/hw_0_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from .nb_helper import *


class InputChecker(ast.NodeVisitor):
def __init__(self):
self.num_inputs = 0
# https://sadh.life/post/ast/
class FuncCallChecker(ast.NodeVisitor):
def __init__(self, func):
self.func = func
self.num_calls = 0

def visit_Call(self, node):
if isinstance(node.func, ast.Name) and node.func.id == "input":
self.num_inputs += 1
if isinstance(node.func, ast.Name) and node.func.id == self.func:
self.num_calls += 1


@pytest.fixture()
Expand Down
24 changes: 15 additions & 9 deletions hw_0.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -218,20 +218,26 @@
"============================================= FAILURES =============================================\n",
"\u001b[31m\u001b[1m_________________________________________ test_num_inputs __________________________________________\u001b[0m\n",
"\n",
"tree = <ast.Module object at 0x109a5a860>\n",
"tree = <ast.Module object at 0x10e7da1a0>\n",
"\n",
" \u001b[94mdef\u001b[39;49;00m \u001b[92mtest_num_inputs\u001b[39;49;00m(tree):\n",
" checker = InputChecker()\n",
" checker = FuncCallChecker(\u001b[33m\"\u001b[39;49;00m\u001b[33minput\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m)\n",
" checker.visit(tree)\n",
" \n",
"> \u001b[94massert\u001b[39;49;00m checker.num_inputs >= \u001b[94m8\u001b[39;49;00m, \u001b[33m\"\u001b[39;49;00m\u001b[33myou don\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mt have enough calls to input()\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n",
"> \u001b[94massert\u001b[39;49;00m checker.num_calls >= \u001b[94m8\u001b[39;49;00m, \u001b[33m\"\u001b[39;49;00m\u001b[33myou don\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mt have enough calls to input()\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n",
"\u001b[1m\u001b[31mE AssertionError: you don't have enough calls to input()\u001b[0m\n",
"\u001b[1m\u001b[31mE assert 2 >= 8\u001b[0m\n",
"\u001b[1m\u001b[31mE + where 2 = <extras.scripts.hw_0_helper.InputChecker object at 0x10acc9ba0>.num_inputs\u001b[0m\n",
"\u001b[1m\u001b[31mE + where 2 = <extras.scripts.hw_0_helper.FuncCallChecker object at 0x10e7d8df0>.num_calls\u001b[0m\n",
"\n",
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_68325/4145485001.py\u001b[0m:8: AssertionError\n",
"\u001b[33m========================================= warnings summary =========================================\u001b[0m\n",
"tmpc439hpt9.py::test_num_inputs\n",
" /usr/local/Caskroom/miniconda/base/envs/python-public-policy/lib/python3.10/site-packages/IPython/core/inputsplitter.py:21: DeprecationWarning: IPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`\n",
" warn('IPython.core.inputsplitter is deprecated since IPython 7 in favor of `IPython.core.inputtransformer2`',\n",
"\n",
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_67029/2901258849.py\u001b[0m:8: AssertionError\n",
"-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html\n",
"===================================== short test summary info ======================================\n",
"FAILED tmpyoyk9z3m.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
"FAILED tmpc439hpt9.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
]
}
],
Expand All @@ -242,10 +248,10 @@
"\n",
"\n",
"def test_num_inputs(tree):\n",
" checker = InputChecker()\n",
" checker = FuncCallChecker(\"input\")\n",
" checker.visit(tree)\n",
"\n",
" assert checker.num_inputs >= 8, \"you don't have enough calls to input()\""
" assert checker.num_calls >= 8, \"you don't have enough calls to input()\""
]
},
{
Expand Down

0 comments on commit 762e53a

Please sign in to comment.