From ef91d82daffb584a2cf7eae43ec318875aea9597 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Thu, 14 Sep 2023 10:50:02 -0500 Subject: [PATCH] App/Toponaming: Add a few tests for Document --- tests/src/App/CMakeLists.txt | 1 + tests/src/App/Document.cpp | 104 ++++++++++++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 3 deletions(-) diff --git a/tests/src/App/CMakeLists.txt b/tests/src/App/CMakeLists.txt index 2e3019782a..14f763c1a8 100644 --- a/tests/src/App/CMakeLists.txt +++ b/tests/src/App/CMakeLists.txt @@ -4,6 +4,7 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/Application.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Branding.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ComplexGeoData.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Document.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Expression.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ElementMap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/IndexedName.cpp diff --git a/tests/src/App/Document.cpp b/tests/src/App/Document.cpp index b6d9e89d67..027c60c583 100644 --- a/tests/src/App/Document.cpp +++ b/tests/src/App/Document.cpp @@ -1,3 +1,101 @@ -// -// Created by Chris Hennes on 9/13/23. -// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "gtest/gtest.h" +#include + +#include "App/Application.h" +#include "App/Document.h" +#include "App/StringHasher.h" +#include "Base/Writer.h" + +using ::testing::Eq; +using ::testing::Ne; + +// NOLINTBEGIN(readability-magic-numbers) + +class FakeWriter: public Base::Writer +{ + void writeFiles() override + {} + std::ostream& Stream() override + { + return std::cout; + } +}; + +class DocumentTest: public ::testing::Test +{ +protected: + static void SetUpTestSuite() + { + if (App::Application::GetARGC() == 0) { + constexpr int argc = 1; + std::array argv {"FreeCAD"}; + App::Application::Config()["ExeName"] = "FreeCAD"; + App::Application::init(argc, const_cast(argv.data())); // NOLINT + } + } + + void SetUp() override + { + _docName = App::GetApplication().getUniqueDocumentName("test"); + _doc = App::GetApplication().newDocument(_docName.c_str(), "testUser"); + } + + void TearDown() override + { + App::GetApplication().closeDocument(_docName.c_str()); + } + + App::Document* doc() + { + return _doc; + } + +private: + std::string _docName; + App::Document* _doc {}; +}; + + +TEST_F(DocumentTest, addStringHasherIndicatesUnwrittenWhenNew) +{ + // Arrange + App::StringHasherRef hasher(new App::StringHasher); + + // Act + auto addResult = doc()->addStringHasher(hasher); + + // Assert + EXPECT_TRUE(addResult.first); + EXPECT_THAT(addResult.second, Ne(-1)); +} + +TEST_F(DocumentTest, addStringHasherIndicatesAlreadyWritten) +{ + // Arrange + App::StringHasherRef hasher(new App::StringHasher); + doc()->addStringHasher(hasher); + + // Act + auto addResult = doc()->addStringHasher(hasher); + + // Assert + EXPECT_FALSE(addResult.first); +} + +TEST_F(DocumentTest, getStringHasherGivesExpectedHasher) +{ + // Arrange + App::StringHasherRef hasher(new App::StringHasher); + auto pair = doc()->addStringHasher(hasher); + int index = pair.second; + + // Act + auto foundHasher = doc()->getStringHasher(index); + + // Assert + EXPECT_EQ(hasher, foundHasher); +} + +// NOLINTEND(readability-magic-numbers)