Diff and merge tool with syntax highlighting: Meld

For some time I was using KDiff as a diff/merge tool. What I missed in Kdiff was syntax highlighting. So I searched around and found Meld which seems to support everything I liked in Kdiff and more! It does require some initial configuration to get the desired workflow – changing font, tab width, enabling syntax highlighting, current line highlighting:

Meld preferencesand ignoring white space diffs:

Meld text filtersThe commands I use to bring up a diff or merge:

C:\Program Files (x86)\Meld\Meld.exe "A" "B"

C:\Program Files (x86)\Meld\Meld.exe "A" "B" "C" --output "D" --auto-merge

The way the merge command works is it has the A file on the left column, C file on the right. The file in the middle is the output D, with B being the base.

GoogleMock: Testing a method and mocking another in the same class

How can we test that a class method was called from within another method of that class when unit testing using GoogleMock? The goal was to call method Foo::runMe() on an object and check whether method Foo::mockMe() of the same object was called with a given set of parameters. In other words, I needed to run method Foo::runMe(), and check expectations regarding the call to Foo::mockMe(). This is how the original and mock class would look like:

class Foo
{
public:
    virtual void runMe() = 0;
    virtual void mockMe(bool) = 0;
};

class MockFoo : public Foo {
public:
    MOCK_METHOD0(runMe, void());
    MOCK_METHOD1(mockMe, void(bool));
};

If we create a MockFoo object and call runMe(), the method will not run, because it is mocked. If we set an expectation that mockMe() is called, it will fail.

// this will fail
TEST_F(FooTest, runMeCallsMockMeWithTrue)
{
    MockFoo foo;
    EXPECT_CALL(foo, mockMe(true)).Times(1);
    foo.runMe();
}

This can be solved by adding an additional method to MockFoo that will call the parent runMe() method.

class MockFoo : public Foo {
public:
    MOCK_METHOD0(runMe, void());
    MOCK_METHOD1(mockMe, int(bool));

    void realRunMe()
    {
        Foo::runMe();
    }
};

Now in the test code the method realRunMe() should be called, which will call Foo:runMe(). The call to mockMe() will be called on the MockFoo object, because it is virtual, allowing our expectations to be checked.

TEST_F(FooTest, runMeCallsMockMeWithTrue)
{
    MockFoo foo;
    EXPECT_CALL(foo, mockMe(true)).Times(1);
    foo.realRunMe();
}

The same can probably be achieved by creating a method that makes ON_CALL(… Invoke …) calls, but that would still have to happen from within the MockFoo class in order to have access to the parent Foo object. The overhead seems to be the same, but the given solution seems more straightforward.