Menu

#50 How to set expectations for multiple arguments

closed
None
2015-04-26
2014-07-10
Louis Pan
No

Great work on turtle, I like you made it easy to also mock template functions.

How do I set an expectation that uses more than one argument from the call?
Eg.
MOCK_CONST_METHOD(Foo, 2, void (std::list<int>::const_iterator begin, std::list<int>::const_iterator end), Foo)

And if I setup:
const std::list<int> expected_foos = boost::assign::list_of(0)(1);

How do I setup an expectation that the contents of (begin, end) equals expected_foos?

Cheers,

Louis

Discussion

  • Mathieu Champlon

    • status: open --> accepted
    • assigned_to: Mathieu Champlon
     
  • Mathieu Champlon

    Hi Louis,

    Thanks for your feedback!

    As for the issue you're raising I must admit it has never come up before and there is no built-in solution for this.
    I will add the feature as soon as possible but in the meantime I'm afraid you're stuck with a workaround along the following:

    namespace
    {
        MOCK_CLASS( my_class )
        {
            MOCK_CONST_METHOD(Foo, 2, void (std::list<int>::const_iterator begin, std::list<int>::const_iterator end))
        };
    
        void workaround( const std::list<int>& expected,
            std::list<int>::const_iterator actualBegin, std::list<int>::const_iterator actualEnd )
        {
            BOOST_CHECK_EQUAL_COLLECTIONS( expected.begin(), expected.end(), actualBegin, actualEnd );
        }
    }
    
    BOOST_AUTO_TEST_CASE( super_with )
    {
        my_class c;
        const std::list<int> expected_foos = boost::assign::list_of(0)(1);
        MOCK_EXPECT( c.Foo ).calls( boost::bind( &workaround, expected_foos, _1, _2 ) );
        const std::list<int> actual_foos = boost::assign::list_of(0)(2);
        c.Foo( actual_foos.begin(), actual_foos.end() );
    }
    

    This is not very satisfactory especially because upon error it won't point to the correct location, but I'm afraid it's the only way for now...

    I'm starting the work on integrating this feature into turtle right away.

    Thanks for pointing that out!

    Regards,
    MAT.

     
  • Mathieu Champlon

    • status: accepted --> pending
     
  • Mathieu Champlon

    • status: pending --> closed
     

Log in to post a comment.