derwiki-adroll / mock

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

call_args_list equality adds extra list #156

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

Create a mock object
>>> m = mock.Mock()

Do some calls
>>> m(1)
<Mock name='mock()' id='40678160'>
>>> m([1])
<Mock name='mock()' id='40678160'>

Verify the calls
>>> m.call_args_list
[call(1), call([1])]

I would expect this to be equal:
>>> m.call_args_list == [((1,), ), (([1,]), )]
False
But somehow I need to add an extra list for the second call?
>>> m.call_args_list == [((1,), ), [(([1,]), )]]
True

So is there something I have misunderstood?

What version of the product are you using? On what operating system?
Fedora 15, Python 2.7.1, mock 0.8.0

Please provide any additional information below.
Description of call_args_list doesn't show any example with lists
http://www.voidspace.org.uk/python/mock/mock.html?highlight=call_args#mock.Mock.
call_args_list

Example with dictionary:
>>> m = mock.Mock()
>>> m({1:2})
<Mock name='mock()' id='41985872'>
>>> m.call_args_list
[call({1: 2})]
>>> m.call_args_list[0]
call({1: 2})
>>> m.call_args_list[0] == (({1:2,}),)
False
>>> m.call_args_list[0] == [(({1:2,}),)]
True

Original issue reported on code.google.com by grimmym...@gmail.com on 9 May 2012 at 1:13

GoogleCodeExporter commented 9 years ago
You're syntax is slightly incorrect (which is why it's better to use the call 
object instead of manually constructing tuples).

This "([1,])" is not a tuple containing a list - your comma is in the wrong 
place. It should be "([1],)".

See the example below:

>>> import mock
>>> m = mock.Mock()
>>> m(1)
<Mock name='mock()' id='4299865232'>
>>> m([1])
<Mock name='mock()' id='4299865232'>
>>> m.call_args_list
[call(1), call([1])]
>>> m.call_args_list == [((1,), ), (([1,]), )]
False
>>> m.call_args_list == [((1,), ), (([1,],), )]
True
>>> call = mock.call
>>> m.call_args_list == [call(1), call([1])]
True

Original comment by fuzzyman on 11 May 2012 at 2:44

GoogleCodeExporter commented 9 years ago
Thanks for your explanation, I see my mistake!

As you say it's better to use the call objects anyway. Any idea though why this 
evaluates to True:
>>> m.call_args_list == [((1,), ), [(([1,]), )]]
True

Original comment by grimmym...@gmail.com on 11 May 2012 at 10:52

GoogleCodeExporter commented 9 years ago
For individual calls, mock doesn't care whether it is a tuple or a list - it 
just unpacks them and looks at the contents. 

 [(([1,]), )] is the same as  [([1], )] which is roughly-sort-of-basically what you wanted in the first place... 

Original comment by fuzzyman on 11 May 2012 at 10:56

GoogleCodeExporter commented 9 years ago
That is true.

Thank you very much for your help. Keep up the good work!

Original comment by grimmym...@gmail.com on 12 May 2012 at 7:30