Skip to content

Commit

Permalink
Add tests for FileListPresenterImpl class
Browse files Browse the repository at this point in the history
  • Loading branch information
bilal614 committed Nov 7, 2024
1 parent 14935a6 commit 29d7c32
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions ImLogDetective/TestImLogDetective.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ if(GTest_FOUND)
add_executable(${TEST_IM_LOG_DETECTIVE}
${IM_LOG_DETECTIVE_DIR}/test/src/models/TestGzipFileParsers.cxx
${IM_LOG_DETECTIVE_DIR}/test/src/presenters/TestCopyLogsPresenterImpl.cxx
${IM_LOG_DETECTIVE_DIR}/test/src/presenters/TestFileListPresenterImpl.cxx
${IM_LOG_DETECTIVE_DIR}/test/src/presenters/TestLogFilePresenter.cxx
${IM_LOG_DETECTIVE_DIR}/test/src/presenters/TestLogFileTabsPresenter.cxx
${IM_LOG_DETECTIVE_DIR}/test/src/presenters/TestMainPresenter.cxx
Expand Down
14 changes: 14 additions & 0 deletions ImLogDetective/test/mocks/FileListViewMock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "views/FileListView.h"
#include "gmock/gmock.h"

namespace TestImLogDetective {

class FileListViewMock : public ImLogDetective::FileListView
{
public:
MOCK_METHOD(void, draw,(const std::vector<std::string>&));
};

}
136 changes: 136 additions & 0 deletions ImLogDetective/test/src/presenters/TestFileListPresenterImpl.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "presenters/FileListPresenterImpl.h"
#include "LogFileTabsPresenterMock.h"
#include "FileListViewMock.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <filesystem>
#include <fstream>
#include <functional>
#include <ranges>

namespace TestImLogDetective
{

using namespace ::testing;
using ::testing::StrictMock;

class TestFileListPresenterImpl : public ::testing::Test {
protected:
::testing::InSequence seq;

StrictMock<LogFileTabsPresenterMock> logFileTabsPresenterMock;
StrictMock<FileListViewMock> fileListViewMock;
std::filesystem::path filePath;
std::filesystem::path otherFilePath;
std::vector<std::string> fileNames;
std::vector<std::string> otherFileNames;
std::vector<std::filesystem::path> dummyTempFilePaths;

ImLogDetective::FileListPresenterImpl fileListPresenterImpl;

void SetUp() override;
void TearDown() override;

TestFileListPresenterImpl();
~TestFileListPresenterImpl() = default;

private:
std::filesystem::path initializeTestFolders(const std::string& folderName, const std::vector<std::string>& fileNames);
};

std::filesystem::path TestFileListPresenterImpl::initializeTestFolders(const std::string& folderName, const std::vector<std::string>& fileNames)
{
auto tempTestFolder = std::filesystem::temp_directory_path() / folderName;
std::filesystem::remove_all(tempTestFolder);
std::filesystem::create_directory(tempTestFolder);
for(const auto& fileName : fileNames)
{
auto filePath = tempTestFolder / fileName;
std::ofstream tmpFile (filePath);
tmpFile.close();
dummyTempFilePaths.push_back(filePath);
}
return tempTestFolder;
}

TestFileListPresenterImpl::TestFileListPresenterImpl() :
logFileTabsPresenterMock{},
fileListViewMock{},
filePath{},
otherFilePath{},
fileNames{"foo", "bar", "bla",},
otherFileNames{"up", "down", "left", "right"},
fileListPresenterImpl{logFileTabsPresenterMock,
fileListViewMock}
{
}

void TestFileListPresenterImpl::SetUp()
{
filePath = initializeTestFolders("TestFileListPresenter", fileNames);
otherFilePath = initializeTestFolders("TestFileListPresenterOther", otherFileNames);
}

void TestFileListPresenterImpl::TearDown()
{
std::filesystem::remove_all(filePath);
std::filesystem::remove_all(otherFilePath);
}

TEST_F(TestFileListPresenterImpl, test_FileListPresenterImpl_update_for_given_folder_path) {

auto expectedFileNames = fileNames;
std::sort(expectedFileNames.begin(), expectedFileNames.end());
EXPECT_CALL(fileListViewMock, draw(Eq(expectedFileNames)));
fileListPresenterImpl.update(filePath, false);

std::vector<std::filesystem::path> expectedFilePaths;
std::ranges::copy(std::views::transform(expectedFileNames, [&](const std::string& f){ return filePath / f;}), std::back_inserter(expectedFilePaths));

std::for_each(expectedFilePaths.begin(), expectedFilePaths.end(), [&](const std::filesystem::path& selectedFile)
{
auto selectedFiles = fileListPresenterImpl.getSelectedFiles();
EXPECT_THAT(selectedFiles, Contains(selectedFile));
});
}

TEST_F(TestFileListPresenterImpl, test_FileListPresenterImpl_update_when_given_folder_path_changed_to_another_folder) {

auto expectedFileNames = fileNames;
std::sort(expectedFileNames.begin(), expectedFileNames.end());

auto expectedOtherFileNames = otherFileNames;
std::sort(expectedOtherFileNames.begin(), expectedOtherFileNames.end());


EXPECT_CALL(fileListViewMock, draw(Eq(expectedFileNames))).Times(1);
EXPECT_CALL(fileListViewMock, draw(Eq(expectedOtherFileNames))).Times(1);


fileListPresenterImpl.update(filePath, false);

std::vector<std::filesystem::path> expectedFilePaths;
std::ranges::copy(std::views::transform(expectedFileNames, [&](const std::string& f){ return filePath / f;}), std::back_inserter(expectedFilePaths));

std::for_each(expectedFilePaths.begin(), expectedFilePaths.end(), [&](const std::filesystem::path& selectedFile)
{
auto selectedFiles = fileListPresenterImpl.getSelectedFiles();
EXPECT_THAT(selectedFiles, Contains(selectedFile));
});


fileListPresenterImpl.update(otherFilePath, false);

std::vector<std::filesystem::path> expectedOtherFilePaths;
std::ranges::copy(std::views::transform(expectedOtherFileNames, [&](const std::string& f){ return otherFilePath / f;}), std::back_inserter(expectedOtherFilePaths));

std::for_each(expectedOtherFilePaths.begin(), expectedOtherFilePaths.end(), [&](const std::filesystem::path& selectedFile)
{
auto selectedFiles = fileListPresenterImpl.getSelectedFiles();
EXPECT_THAT(selectedFiles, Contains(selectedFile));
});

}


}

0 comments on commit 29d7c32

Please sign in to comment.