Sunday, June 24, 2018

Verifying the "files" are being loaded correctly

I'd made a function that its sole responsibility is to verify the files have successfully loaded into memory. Following code snippet shows my hard work:
std::list<string> FileBot::getLoadedFiles(FileNode *fileNode)
{
    std::list<string> mlist;

    BOOST_LOG_TRIVIAL(info) << "current root address : " << fileNode;

    if( fileNode == nullptr || fileNode->sibling.size() == 0 )
        return mlist;

    for( FileNodeListType::iterator it = fileNode->sibling.begin(); it != fileNode->sibling.end(); ++it )
    {
        FileNode *n = (&*it);

        if( n->getType() == 'd' ) {
            std::list<string> subList;
            subList = getLoadedFiles(n);

            mlist.insert(mlist.begin(), subList.begin(), subList.end());
        }
        else {
            string filePath = constructPathAddress(n);
            mlist.push_back(filePath);

            BOOST_LOG_TRIVIAL(info) << "filePath : " << filePath;
        }
    }

    return mlist;
}
I need this function in my unit test code to ensure constructParentPath() and constructChildPath() are doing their job correctly. Take the following test case for example:
BOOST_AUTO_TEST_CASE(TL_5, *boost::unit_test::precondition(skipTest(true)))
{
    BOOST_TEST_MESSAGE("TC8 : one file is captured without sub folder");

    string testPathA = current_path().string() + string(1, path::preferred_separator) + "FolderA";
    createTestFile(testPathA, 1);

#if defined(WIN32)
    BOOST_TEST(testPathA == "D:\\workspaceqt\\backupUtil\\backupUtilUnitTest\\FolderA");
#else
    BOOST_TEST(testPathA == "/home/kokhoe/workspaceqt/debug/FolderA");
#endif

    FileBotUnderTest fb;
    FileNode *root = nullptr;
    if( (root = fb.constructParentPath(testPathA)) == nullptr ) {
        BOOST_TEST_MESSAGE("Initialization failed.");
        return;
    }

    string path = fb.getParentPathAddress();

#if defined(WIN32)
    BOOST_TEST(path == "D:\\workspaceqt\\backupUtil\\backupUtilUnitTest\\FolderA\\");
#else
    BOOST_TEST(path == "/home/kokhoe/workspaceqt/debug/FolderA/");
#endif

    fb.constructChildPath(root);

    // verify the file name was captured
    std::list<string> files = fb.getLoadedFiles(root);

    BOOST_TEST(files.size() == 1);

#if defined(WIN32)
    if( std::find(files.begin(), files.end(), "D:/workspaceqt/backupUtil/backupUtilUnitTest/FolderA/file_1.txt") == files.end() )
        BOOST_TEST(false);
#else
    if( std::find(files.begin(), files.end(), "/home/kokhoe/workspaceqt/debug/FolderA/file_1.txt") == files.end() )
        BOOST_TEST(false);
#endif

    destroyTestFile(testPathA);
}
This function is gets invoked right after the "files" are loaded into memory. There are not the physical file, it's just a shadow. To effectively process the files stored in the sub folder, it will invoke itself to dive into the sub folder bringing together a reference of the current path, until it has iterated through the end of the path. That is the reason why this function has a parameter of FileNode type. But there was a question come into my mind, why do I still need to pass the root (a reference value came from constructParentPath() in the unit test mention above) into this function since the FileBot class already got a reference to this value?
class FileBot
{
   ...
   ...

private:
    // *root should never be NULL
    FileNode *root = nullptr;       // store the a reference to root path

}
I know this was stupid. The initial objective is to create a function that could verify the "files" that has been loaded into memory are done correctly. I think this function is only used for the unit test purpose. I'll just leave it as it is.

No comments: