datacamp / pythonwhat

Verify Python code submissions and auto-generate meaningful feedback messages.
http://pythonwhat.readthedocs.io/
GNU Affero General Public License v3.0
70 stars 31 forks source link

test_lambda_functions that are arguments #79

Closed hugobowne closed 8 years ago

hugobowne commented 8 years ago

test_lambda_function() is very cool!

You can see that I'm using it nicely here.

As you know, the power of lambda functions comes into its own when passing them anonymously to other functions, e.g.

# Create a list of strings: spells
spells = ['protego', 'accio', 'expecto patronum', 'legilimens']

# Use map() to apply a lambda function over spells: shout_spells
shout_spells = map(lambda item: item + '!!!', ['spells'])

The above is an example of something I need to test. Ideally, if the student writes the wrong lambda function, I can catch it and catch the actual error they make.

Currently, I can only catch the error and NOT use any of the great functionality of test_lambda_function() in this example, which is what most examples in my course are like.

Exercise is here: https://campus.datacamp.com/courses/1692/4444?ex=4 SCT is here (yes, I also intend to try with test_function_v2 ;-) ): https://github.com/datacamp/courses-pythonista-data-science-toolbox-I/blame/SCT_edits/chapter3.md#L226

Check out the two exercises that follow also. The real issue is that the error message I throw should really be dependent on how they mess up the lambda function.

Thoughts?

filipsch commented 8 years ago

With the current setup of pythonwhat, there is no way in which you can have 'sub-SCTs' for function arguments. It would also be extremely messy and hard to work with.

As a workaround, can't you do a test_correct() to check the value of shout_spells, and if that's not correct, check if there was a lambda function with the appropriate definition (it's not possible to demand that this is inside the first argument of the map() function, just 'somewhere') and check whether map() is called correctly?

test_object('shout_spells')
def diagnose():
    test_lambda_function(1, results = ["lam('hello')"])
    test_function("map", args = [1])
test_correct(lambda: test_object('shout_spells'), diagnose)

This is not 100% ideal, but I think it's OK.

By the way: For some reason, python3 can't easily tell the equality of map and filter objects. I've added something to pythonwhat so that they are converted into list before comparison, so test_object() should work on map and filter objects. Should be live together with all the other stuff soon (today or tomorrow, depending on availability product team).