From 9a4916fe84ee63a2a7c331d717d4802d62810ac5 Mon Sep 17 00:00:00 2001 From: TheAssassin Date: Thu, 8 Nov 2018 23:08:32 +0100 Subject: [PATCH] Desktop file entry initial commit --- src/core/CMakeLists.txt | 2 +- src/core/desktopfileentry.cpp | 65 ++++++++++++++++++++ src/core/desktopfileentry.h | 44 +++++++++++++ tests/core/CMakeLists.txt | 6 ++ tests/core/test_desktopfileentry.cpp | 92 ++++++++++++++++++++++++++++ 5 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 src/core/desktopfileentry.cpp create mode 100644 src/core/desktopfileentry.h create mode 100644 tests/core/test_desktopfileentry.cpp diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index fe9c5ba..a06e853 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -16,7 +16,7 @@ execute_process( add_library(linuxdeploy_core_copyright STATIC copyright.cpp copyright.h copyright_dpkgquery.cpp copyright_dpkgquery.h) target_link_libraries(linuxdeploy_core_copyright PUBLIC subprocess ${BOOST_LIBS}) -add_library(linuxdeploy_core STATIC elf.cpp log.cpp appdir.cpp desktopfile.cpp ${HEADERS}) +add_library(linuxdeploy_core STATIC elf.cpp log.cpp appdir.cpp desktopfile.cpp desktopfilereader.cpp desktopfileentry.cpp ${HEADERS}) target_link_libraries(linuxdeploy_core PUBLIC linuxdeploy_plugin linuxdeploy_util ${BOOST_LIBS} subprocess cpp-feather-ini-parser CImg ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(linuxdeploy_core PRIVATE linuxdeploy_core_copyright) target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/src/core/desktopfileentry.cpp b/src/core/desktopfileentry.cpp new file mode 100644 index 0000000..bb3824a --- /dev/null +++ b/src/core/desktopfileentry.cpp @@ -0,0 +1,65 @@ +// local headers +#include "../../src/core/desktopfileentry.h" +#include "desktopfileentry.h" + + +class DesktopFileEntry::PrivateData { +public: + std::string key; + std::string value; + +public: + void copyData(const std::shared_ptr& other) { + key = other->key; + value = other->value; + } +}; + +DesktopFileEntry::DesktopFileEntry() : d(new PrivateData) {} + +DesktopFileEntry::DesktopFileEntry(std::string key, std::string value) : DesktopFileEntry() { + d->key = std::move(key); + d->value = std::move(value); +} + +DesktopFileEntry::DesktopFileEntry(const DesktopFileEntry& other) : DesktopFileEntry() { + d->copyData(other.d); +} + +DesktopFileEntry& DesktopFileEntry::operator=(const DesktopFileEntry& other) { + if (this != &other) { + d.reset(new PrivateData); + d->copyData(other.d); + } + + return *this; +} + +DesktopFileEntry& DesktopFileEntry::operator=(DesktopFileEntry&& other) noexcept { + if (this != &other) { + d = other.d; + other.d = nullptr; + } + + return *this; +} + +bool DesktopFileEntry::operator==(const DesktopFileEntry& other) const { + return d->key == other.d->key && d->value == other.d->value; +} + +bool DesktopFileEntry::operator!=(const DesktopFileEntry& other) const { + return !operator==(other); +} + +bool DesktopFileEntry::isEmpty() const { + return d->key.empty(); +} + +const std::string& DesktopFileEntry::key() const { + return d->key; +} + +const std::string& DesktopFileEntry::value() const { + return d->value; +} diff --git a/src/core/desktopfileentry.h b/src/core/desktopfileentry.h new file mode 100644 index 0000000..0c0fe62 --- /dev/null +++ b/src/core/desktopfileentry.h @@ -0,0 +1,44 @@ +#pragma once + +// system headers +#include +#include + +class DesktopFileEntry { +private: + // opaque data class pattern + class PrivateData; + std::shared_ptr d; + +public: + // default constructor + DesktopFileEntry(); + + // construct from key and value + explicit DesktopFileEntry(std::string key, std::string value); + + // copy constructor + DesktopFileEntry(const DesktopFileEntry& other); + + // copy assignment constructor + DesktopFileEntry& operator=(const DesktopFileEntry& other); + + // move assignment operator + DesktopFileEntry& operator=(DesktopFileEntry&& other) noexcept; + + // equality operator + bool operator==(const DesktopFileEntry& other) const; + + // inequality operator + bool operator!=(const DesktopFileEntry& other) const; + +public: + // checks whether a key and value have been set + bool isEmpty() const; + + // return entry's key + const std::string& key() const; + + // return entry's value + const std::string& value() const; +}; diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 7b830c5..e147031 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -42,3 +42,9 @@ add_executable(test_desktopfilereader test_desktopfilereader.cpp) target_link_libraries(test_desktopfilereader PRIVATE linuxdeploy_core gtest gtest_main) add_test(test_desktopfilereader test_desktopfilereader) + + +add_executable(test_desktopfileentry test_desktopfileentry.cpp) +target_link_libraries(test_desktopfileentry PRIVATE linuxdeploy_core gtest gtest_main) + +add_test(test_desktopfileentry test_desktopfileentry) diff --git a/tests/core/test_desktopfileentry.cpp b/tests/core/test_desktopfileentry.cpp new file mode 100644 index 0000000..89b7fc6 --- /dev/null +++ b/tests/core/test_desktopfileentry.cpp @@ -0,0 +1,92 @@ +// library headers +#include +#include + +// local headers +#include "../../src/core/desktopfileentry.h" + +namespace bf = boost::filesystem; + +class DesktopFileEntryFixture : public ::testing::Test { +public: + const std::string key; + const std::string value; + +protected: + DesktopFileEntryFixture() : key("testKey"), value("testValue") {} + +private: + void SetUp() override {} + + void TearDown() override {} +}; + +TEST_F(DesktopFileEntryFixture, testDefaultConstructor) { + DesktopFileEntry entry; + EXPECT_TRUE(entry.isEmpty()); +} + +TEST_F(DesktopFileEntryFixture, testKeyValueConstructor) { + DesktopFileEntry entry(key, value); + EXPECT_FALSE(entry.isEmpty()); + EXPECT_EQ(entry.key(), key); + EXPECT_EQ(entry.value(), value); +} + +TEST_F(DesktopFileEntryFixture, testGetters) { + DesktopFileEntry entry(key, value); + EXPECT_EQ(entry.key(), key); + EXPECT_EQ(entry.value(), value); +} + +TEST_F(DesktopFileEntryFixture, testEqualityAndInequalityOperators) { + DesktopFileEntry emptyEntry; + EXPECT_TRUE(emptyEntry == emptyEntry); + EXPECT_FALSE(emptyEntry != emptyEntry); + + DesktopFileEntry nonEmptyEntry(key, value); + EXPECT_NE(emptyEntry, nonEmptyEntry); + + DesktopFileEntry nonEmptyEntryWithDifferentValue(key, value + "abc"); + EXPECT_NE(nonEmptyEntry, nonEmptyEntryWithDifferentValue); +} + +TEST_F(DesktopFileEntryFixture, testCopyConstructor) { + DesktopFileEntry entry(key, value); + EXPECT_FALSE(entry.isEmpty()); + + DesktopFileEntry copy = entry; + EXPECT_FALSE(copy.isEmpty()); + + EXPECT_EQ(entry, copy); +} + +TEST_F(DesktopFileEntryFixture, testCopyAssignmentConstructor) { + DesktopFileEntry entry; + EXPECT_TRUE(entry.isEmpty()); + + DesktopFileEntry otherEntry(key, value); + EXPECT_FALSE(otherEntry.isEmpty()); + + entry = otherEntry; + EXPECT_EQ(entry.key(), key); + EXPECT_EQ(entry.value(), value); + + // test self-assignment + entry = entry; +} + +TEST_F(DesktopFileEntryFixture, testMoveAssignmentConstructor) { + DesktopFileEntry entry; + EXPECT_TRUE(entry.isEmpty()); + + DesktopFileEntry otherEntry(key, value); + EXPECT_FALSE(otherEntry.isEmpty()); + + entry = std::move(otherEntry); + EXPECT_EQ(entry.key(), key); + EXPECT_EQ(entry.value(), value); + + // test self-assignment + entry = std::move(entry); +}