using hamcrest with pyDoubles and kwargs

31 views
Skip to first unread message

Tyler Rush

unread,
Jan 17, 2012, 2:04:27 PM1/17/12
to pydo...@googlegroups.com
pyDoubles version: 1.6
python version: 2.7
hamcrest version: 1.6

Hi,

Does pyDoubles support the use of hamcrest matchers for keyword arguments when writing expectaions for a Mock object? Hamcrest works great for plain args, but I can't figure out how/if it works with kwargs. If I had to guess, after looking at pyDoubles.core for a little while, it seems there is not matching for kwargs like there is for normal args: _MethodPool_._do_kwargs_match() doesn't seem to make use of self.match_finder like _MathodPool_._do_args_match() does. Could you confirm/deny this?

Below is the code I used to tryout hamcrest matchers in kwargs... but with no success. Thanks for the help.

from hamcrest import equal_to, is_
import unittest
from pyDoubles.framework import *

class ClassToMock():
    def __init__(self):
        pass
    def func(self, anumber, akw1="nothing", akw2="something"):
        print anumber, akw1, akw2

class SimpleTestCase(unittest.TestCase):
    def setUp(self):
        self.classmock = mock(ClassToMock())

    def tearDown(self):
        self.classmock.assert_that_is_satisfied()

    def test_a_simple_test(self):
        """ works fine """
        expect_call(self.classmock.func).with_args(4, akw2="four")
        expect_call(self.classmock.func).with_args(5, akw2="five")
        self.classmock.func(4, akw2="four")
        self.classmock.func(5, akw2="five")

    def test_an_args_matcher(self):
        """ works fine """
        expect_call(self.classmock.func).with_args(equal_to(4), akw2="four")
        expected_args = [equal_to(5)]
        expect_call(self.classmock.func).with_args(*expected_args, akw2="five")
        self.classmock.func(4, akw2="four")
        self.classmock.func(5, akw2="five")

    def test_a_kwarg_test1(self):
        """ DOESN'T WORK """
        expect_call(self.classmock.func).with_args(4, akw2=equal_to("four"))
        self.classmock.func(4, akw2="four")

    def test_a_kwarg_test2(self):
        """ DOESN'T WORK """
        kwargs = {'akw2' : equal_to("four")}
        expect_call(self.classmock.func).with_args(4, **kwargs)
        self.classmock.func(4, akw2="four")

    def test_a_kwarg_test3(self):
        """ DOESN'T WORK """
        kwargs = equal_to({'akw2' : "four"})
        expect_call(self.classmock.func).with_args(4, **kwargs)
        self.classmock.func(4, akw2="four")

if __name__ == "__main__":
    unittest.main()

~ Tyler

Miguel Angel

unread,
Jan 18, 2012, 3:40:28 PM1/18/12
to pydo...@googlegroups.com
Hi there.

I'm a little busy nowadays. I promise to try what you say as soon as possible if nobody does.

Perhaps a little refactoring I made in last commits is the responsible of the fail. But it only implies that tests were not enough :D

I hope to send more feedback in a couple of days.

See you!
--
Miguel Ángel García Martínez

Carlos Ble

unread,
Jan 18, 2012, 4:06:33 PM1/18/12
to pydo...@googlegroups.com
Hi Tyler!
Thanks for using pyDoubles :-)

You are right, that feature is not implemented. I didn't implement it because I didn't need it. I try to not user kwargs too much. However,
it is a great feature for the next version :-) I can't tell you when will I implement it, although you can try it yourself. Your changes will be accepted to the main repository if you develop them with TDD.

If you can blog about pyDoubles or publish anything about the framework that would be appreciated :-)

Cheers!

2012/1/17 Tyler Rush <m...@tylerlogic.com>



--
Carlos Ble

Tyler Rush

unread,
Jan 18, 2012, 4:09:44 PM1/18/12
to pydo...@googlegroups.com
Sounds good. Thank you.

~ Tyler

David Villa Alises

unread,
Sep 5, 2012, 3:04:59 PM9/5/12
to pydo...@googlegroups.com, m...@tylerlogic.com
Hi Tyler:

doublex has support for your requested feature too. See a example:

 spy.Spy()
 spy.m6(name="john doe")
 assert_that(spy.m6, called_with(name=contains_string("doe")))

More examples in:
Reply all
Reply to author
Forward
0 new messages