sorenisanerd / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Smart parameter check enhancement for mocksignature #81

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I screwed around and implemented smart parameter checking which will allow this 
to pass, for example:

    def f(a, b):
        pass
    f = mocksignature(f)
    f(1, b=2)
    f.mock.assert_called_with(1, 2)
    f.mock.assert_called_with(a=1, b=2)
    f.mock.assert_called_with(1, b=2)
    f.mock.assert_called_with(b=2, a=1)

I would like comments. I put my code in a clone on a named branch:

http://code.google.com/r/airportyh-python-mock/source/browse/?r=smart-param-chec
k

Thanks,
Toby

Original issue reported on code.google.com by airpor...@gmail.com on 23 Mar 2011 at 4:06

GoogleCodeExporter commented 9 years ago
Should also mention, accompanying tests were added at the bottom of 
testmocksignature.py.

Original comment by airpor...@gmail.com on 23 Mar 2011 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by fuzzyman on 24 Mar 2011 at 12:33

GoogleCodeExporter commented 9 years ago
This is an interesting patch and technique.

I solved this problem a different way for the new "autospec" functionality. It 
generates functions with a (*args, **kwargs) signature that delegate to a mock, 
but also call a function with the same signature as the original.

This still does the argument checking - but only calls the mock in the same way 
as the user code.

I can't do this for mocksignature as it is backwards incompatible (the asserts 
are different). I'm considering deprecating mocksignature because autospec (and 
create_autospec) obsoletes it. This is implemented in the mercurial repo.

The only downside is that the generated functions don't have the same signature 
as the original - but this only matters for code doing signature introspection 
which is a pretty rare use case.

Original comment by fuzzyman on 24 May 2011 at 11:24

GoogleCodeExporter commented 9 years ago
Very cool. Does autospec have the "smart parameter checking" feature? If not, 
would you like me to have a crack at it?

Original comment by airpor...@gmail.com on 25 May 2011 at 1:45