Skip to content

Commit

Permalink
Fix higher order function call parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hermansje authored and TimSangster committed May 24, 2020
1 parent 92c07dc commit 78e780f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pythonwhat/parsing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import ast
import re

from pythonwhat.utils_ast import wrap_in_module
from collections.abc import Sequence, Mapping
from collections import OrderedDict
Expand Down Expand Up @@ -326,13 +328,15 @@ def visit_Dict(self, node):
def visit_Call(self, node):
if self.call_lookup_active:
self.visit(node.func)
self.gen_name += "()"
else:
self.call_lookup_active = True
self.visit(
node.func
) # Need to visit func to start recording the current function name.

if self.gen_name:
self.gen_name = re.sub(r"(?:\(\))+(.)", "\\1", self.gen_name)
if self.gen_name not in self.out:
self.out[self.gen_name] = []

Expand Down
20 changes: 14 additions & 6 deletions tests/test_check_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,18 +522,26 @@ def test_function_call_in_comparison(code):
assert res["correct"]


def test_ho_function():
# TODO: FunctionParser.visit_Call should append something to name to discern HOF calls
# e.g. () if node.func is Func (this should only affect limited exercises)
sct = "Ex().check_function('hof').check_args(0).has_equal_value(override=2)"
@pytest.mark.parametrize(
"sct",
[
"Ex().check_function('numpy.array')",
"Ex().check_function('hof').check_args(0).has_equal_value(override=1)",
"Ex().check_function('hof()').check_args(0).has_equal_value(override=2)",
],
)
def test_ho_function(sct):

code = """
import numpy as np
np.array([])
def hof(arg1):
def inner(arg2):
return arg1, arg2
return inner
hof(1)(2)
"""

Expand Down

0 comments on commit 78e780f

Please sign in to comment.