From 29d7c329653e559cab6264113ad7cfc26cc1c6cd Mon Sep 17 00:00:00 2001 From: Bilal Tahir Butt Date: Thu, 7 Nov 2024 13:12:24 +0100 Subject: [PATCH] Add tests for FileListPresenterImpl class --- ImLogDetective/TestImLogDetective.cmake | 1 + ImLogDetective/test/mocks/FileListViewMock.h | 14 ++ .../presenters/TestFileListPresenterImpl.cxx | 136 ++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 ImLogDetective/test/mocks/FileListViewMock.h create mode 100644 ImLogDetective/test/src/presenters/TestFileListPresenterImpl.cxx diff --git a/ImLogDetective/TestImLogDetective.cmake b/ImLogDetective/TestImLogDetective.cmake index c68fbf8..dafd1d3 100644 --- a/ImLogDetective/TestImLogDetective.cmake +++ b/ImLogDetective/TestImLogDetective.cmake @@ -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 diff --git a/ImLogDetective/test/mocks/FileListViewMock.h b/ImLogDetective/test/mocks/FileListViewMock.h new file mode 100644 index 0000000..2d27c2a --- /dev/null +++ b/ImLogDetective/test/mocks/FileListViewMock.h @@ -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&)); +}; + +} \ No newline at end of file diff --git a/ImLogDetective/test/src/presenters/TestFileListPresenterImpl.cxx b/ImLogDetective/test/src/presenters/TestFileListPresenterImpl.cxx new file mode 100644 index 0000000..dc16fb1 --- /dev/null +++ b/ImLogDetective/test/src/presenters/TestFileListPresenterImpl.cxx @@ -0,0 +1,136 @@ +#include "presenters/FileListPresenterImpl.h" +#include "LogFileTabsPresenterMock.h" +#include "FileListViewMock.h" +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include +#include +#include +#include + +namespace TestImLogDetective +{ + +using namespace ::testing; +using ::testing::StrictMock; + +class TestFileListPresenterImpl : public ::testing::Test { +protected: + ::testing::InSequence seq; + + StrictMock logFileTabsPresenterMock; + StrictMock fileListViewMock; + std::filesystem::path filePath; + std::filesystem::path otherFilePath; + std::vector fileNames; + std::vector otherFileNames; + std::vector 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& fileNames); +}; + +std::filesystem::path TestFileListPresenterImpl::initializeTestFolders(const std::string& folderName, const std::vector& 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 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 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 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)); + }); + +} + + +} \ No newline at end of file