aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2018-07-23 21:42:00 -0700
committerRichard Walters <rwalters@digitalstirling.com>2018-07-23 21:42:00 -0700
commita09abace8d4ec0d2fc9bb65493dfae7b7ce8e0fe (patch)
treef264e91c74d2e033c4e8474e377c9af36ac5c6f9
parentc2dade7a5a0b4e8317e8eb6f249cef91eb54cc1a (diff)
Add copy constructor and assignment operator
-rw-r--r--include/Uri/Uri.hpp4
-rw-r--r--src/Uri.cpp11
-rw-r--r--test/src/UriTests.cpp23
3 files changed, 36 insertions, 2 deletions
diff --git a/include/Uri/Uri.hpp b/include/Uri/Uri.hpp
index f531d09..41a70f5 100644
--- a/include/Uri/Uri.hpp
+++ b/include/Uri/Uri.hpp
@@ -24,9 +24,9 @@ namespace Uri {
// Lifecycle management
public:
~Uri();
- Uri(const Uri&) = delete;
+ Uri(const Uri& other);
Uri(Uri&&);
- Uri& operator=(const Uri&) = delete;
+ Uri& operator=(const Uri& other);
Uri& operator=(Uri&&);
// Public methods
diff --git a/src/Uri.cpp b/src/Uri.cpp
index 73bdfc3..cb44c1e 100644
--- a/src/Uri.cpp
+++ b/src/Uri.cpp
@@ -1217,8 +1217,19 @@ namespace Uri {
};
Uri::~Uri() = default;
+ Uri::Uri(const Uri& other)
+ : impl_(new Impl)
+ {
+ *this = other;
+ }
Uri::Uri(Uri&&) = default;
Uri& Uri::operator=(Uri&&) = default;
+ Uri& Uri::operator=(const Uri& other) {
+ if (this != &other) {
+ *impl_ = *other.impl_;
+ }
+ return *this;
+ }
Uri::Uri()
: impl_(new Impl)
diff --git a/test/src/UriTests.cpp b/test/src/UriTests.cpp
index d8dca7a..72d9f72 100644
--- a/test/src/UriTests.cpp
+++ b/test/src/UriTests.cpp
@@ -872,3 +872,26 @@ TEST(UriTests, QueryEmptyButPresent) {
ASSERT_EQ("", uri.GetQuery());
ASSERT_EQ("http://example.com/?", uri.GenerateString());
}
+
+TEST(UriTests, MakeACopy) {
+ Uri::Uri uri1;
+ (void)uri1.ParseFromString("http://www.example.com/foo.txt");
+ Uri::Uri uri2(uri1);
+ uri1.SetQuery("bar");
+ uri2.SetFragment("page2");
+ uri2.SetHost("example.com");
+ EXPECT_EQ("http://www.example.com/foo.txt?bar", uri1.GenerateString());
+ EXPECT_EQ("http://example.com/foo.txt#page2", uri2.GenerateString());
+}
+
+TEST(UriTests, AssignACopy) {
+ Uri::Uri uri1;
+ (void)uri1.ParseFromString("http://www.example.com/foo.txt");
+ Uri::Uri uri2;
+ uri2 = uri1;
+ uri1.SetQuery("bar");
+ uri2.SetFragment("page2");
+ uri2.SetHost("example.com");
+ EXPECT_EQ("http://www.example.com/foo.txt?bar", uri1.GenerateString());
+ EXPECT_EQ("http://example.com/foo.txt#page2", uri2.GenerateString());
+}