From 537c326510ae6c110dd5f371c5398db8ab6c7edf Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 9 Jun 2018 22:24:03 +0200 Subject: [PATCH] Test Field --- CMakeLists.txt | 2 ++ tests/entrytests.cpp | 17 ++--------- tests/fieldtests.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ tests/utils.h | 26 ++++++++++++++++ 4 files changed, 102 insertions(+), 15 deletions(-) create mode 100644 tests/fieldtests.cpp create mode 100644 tests/utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dc9e3f3..93884bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,8 +24,10 @@ set(TEST_HEADER_FILES ) set(TEST_SRC_FILES tests/cppunit.cpp + tests/utils.h tests/passwordfiletests.cpp tests/entrytests.cpp + tests/fieldtests.cpp tests/opensslrandomdevice.cpp ) diff --git a/tests/entrytests.cpp b/tests/entrytests.cpp index 6e9d430..610d414 100644 --- a/tests/entrytests.cpp +++ b/tests/entrytests.cpp @@ -1,22 +1,9 @@ -#include "../io/cryptoexception.h" #include "../io/entry.h" -#include "../io/passwordfile.h" -#include -#include - -namespace TestUtilities { - -inline std::ostream &operator<<(std::ostream &out, const Io::Entry *entry) -{ - return out << ConversionUtilities::joinStrings(entry->path(), "/"); -} - -} // namespace TestUtilities - -using namespace TestUtilities; +#include "./utils.h" #include +using namespace TestUtilities; #include #include diff --git a/tests/fieldtests.cpp b/tests/fieldtests.cpp new file mode 100644 index 0000000..4136a10 --- /dev/null +++ b/tests/fieldtests.cpp @@ -0,0 +1,72 @@ +#include "../io/entry.h" +#include "../io/field.h" + +#include "./utils.h" + +#include +using namespace TestUtilities; + +#include +#include + +using namespace std; +using namespace Io; +using namespace TestUtilities::Literals; + +using namespace CPPUNIT_NS; + +/*! + * \brief The FieldTests class tests the Io::Field class. + */ +class FieldTests : public TestFixture { + CPPUNIT_TEST_SUITE(FieldTests); + CPPUNIT_TEST(testNewFieldCorrectlyInitialized); + CPPUNIT_TEST(testMutation); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testNewFieldCorrectlyInitialized(); + void testMutation(); +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(FieldTests); + +void FieldTests::setUp() +{ +} + +void FieldTests::tearDown() +{ +} + +/*! + * \brief Tests whether a new field is correctly initialized (default values set). + */ +void FieldTests::testNewFieldCorrectlyInitialized() +{ + AccountEntry account("account"); + const Field emptyField(&account); + CPPUNIT_ASSERT(emptyField.isEmpty()); + + const Field field(&account, "foo", "bar"); + CPPUNIT_ASSERT(!field.isEmpty()); + CPPUNIT_ASSERT_EQUAL(&account, field.tiedAccount()); + CPPUNIT_ASSERT_EQUAL("foo"s, field.name()); + CPPUNIT_ASSERT_EQUAL("bar"s, field.value()); + CPPUNIT_ASSERT_EQUAL(FieldType::Normal, field.type()); +} + +void FieldTests::testMutation() +{ + AccountEntry account("account"); + Field field(&account, "foo", "bar"); + field.setName("bar"); + field.setValue("foo"); + field.setType(FieldType::Password); + CPPUNIT_ASSERT_EQUAL("bar"s, field.name()); + CPPUNIT_ASSERT_EQUAL("foo"s, field.value()); + CPPUNIT_ASSERT_EQUAL(FieldType::Password, field.type()); +} diff --git a/tests/utils.h b/tests/utils.h new file mode 100644 index 0000000..67675a7 --- /dev/null +++ b/tests/utils.h @@ -0,0 +1,26 @@ +#ifndef PASSWORDFILE_TESTS_UTILS_H +#define PASSWORDFILE_TESTS_UTILS_H + +#include "../io/entry.h" +#include "../io/field.h" + +#include +#include + +#include + +namespace TestUtilities { + +inline std::ostream &operator<<(std::ostream &out, const Io::Entry *entry) +{ + return out << ConversionUtilities::joinStrings(entry->path(), "/"); +} + +inline std::ostream &operator<<(std::ostream &out, const Io::Field *field) +{ + return out << field->name() << '=' << field->value(); +} + +} // namespace TestUtilities + +#endif // PASSWORDFILE_TESTS_UTILS_H