Saturday, May 5, 2018

Unit test code strategy on private method

During the initial development, the code was driven by the TDD concept. Thus, I have the following code published to the world temporarily. The 2 methods are the primary methods of this program; neither one of them missing will not work.
public:
    FileNode* constructParentPath(boost::filesystem::path inputPath);
    void constructChildPath(FileNode *currentRootNode);

    ...
    ...
This design was to make sure that I have enough test coverage for each method. After unit testing have been done, I have a plan to change the visibility of that 2 primary methods to private, and replace them with a more meaningful method. The new design would look like below:
public:
    FileNode* constructAbstractPath(boost::filesystem::path inputPath);


protected:
    FileNode* constructParentPath(boost::filesystem::path inputPath);
    void constructChildPath(FileNode *currentRootNode);


FileNode* FileBot::constructAbstractPath(boost::filesystem::path inputPath)
{
    constructParentPath(inputPath);
    constructChildPath(root);

    return root;
}
Ahhaa~ Feeling so nice with this method now. But the flip site of this change will spoil my existing unit test code. Thus, I think of a new solution that wouldn't make such a huge change while adapting the new design. I made a new class that mimics the existing method call as shown in below:
class FileBotUnderTest : public FileBot
{
public:
    FileBotUnderTest();

    FileNode* constructParentPath(boost::filesystem::path inputPath) {
        return FileBot::constructParentPath(inputPath);
    }

    void constructChildPath(FileNode *currentRootNode) {
        return FileBot::constructChildPath(currentRootNode);
    }
};
Changes in unit test code are very minimal, instead of interacting with FileBot, I'll just interact with FileBotUnderTest. Below are the work done for these changes:
BOOST_AUTO_TEST_CASE(TL_6, *boost::unit_test::precondition(skipTest(true)))
{
    BOOST_TEST_MESSAGE("TC6 : load 3 level parent path");
    string path = "";
    FileBotUnderTest fb;

#if defined(WIN32)
    BOOST_TEST_MESSAGE("Test path: D:\\workspaceqt\\ui1");
    path = "D:/workspaceqt/ui1";
#else
    BOOST_TEST_MESSAGE("Test path: /home/kokhoe/workspaceqt");
    path = "/home/kokhoe";
#endif

    if( fb.constructParentPath(path) != nullptr )
       path = fb.getParentPathAddress();

#if defined(WIN32)
    BOOST_TEST(path == "D:\\workspaceqt\\ui1\\");
    fb.clearMemory();
#else
    BOOST_TEST(path == "/home/kokhoe/");
#endif
}

No comments: