Desktop file entry initial commit

This commit is contained in:
TheAssassin
2018-11-08 23:08:32 +01:00
parent e584088656
commit 9a4916fe84
5 changed files with 208 additions and 1 deletions

View File

@@ -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})

View File

@@ -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<PrivateData>& 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;
}

View File

@@ -0,0 +1,44 @@
#pragma once
// system headers
#include <memory>
#include <string>
class DesktopFileEntry {
private:
// opaque data class pattern
class PrivateData;
std::shared_ptr<PrivateData> 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;
};

View File

@@ -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)

View File

@@ -0,0 +1,92 @@
// library headers
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>
// 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);
}