Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test (non)equality for alt_string implementation #1130

Merged
merged 4 commits into from
Jun 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions test/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ SOFTWARE.
#include <string>
#include <utility>


/* forward declarations */
class alt_string;
bool operator<(const char* op1, const alt_string& op2);


/*
* This is virtually a string class.
* It covers std::string under the hood.
Expand Down Expand Up @@ -60,17 +66,26 @@ class alt_string
}

template <typename op_type>
bool operator==(op_type&& op) const
bool operator==(const op_type& op) const
{
return str_impl == op;
}

bool operator==(const alt_string& op) const
{
return str_impl == op.str_impl;
}

template <typename op_type>
bool operator!=(op_type&& op) const
bool operator!=(const op_type& op) const
{
return str_impl != op;
}

bool operator!=(const alt_string& op) const {
return str_impl != op.str_impl;
}

std::size_t size() const noexcept
{
return str_impl.size();
Expand All @@ -87,7 +102,7 @@ class alt_string
}

template <typename op_type>
bool operator<(op_type&& op) const
bool operator<(const op_type& op) const
{
return str_impl < op;
}
Expand Down Expand Up @@ -134,6 +149,8 @@ class alt_string

private:
std::string str_impl;

friend bool ::operator<(const char*, const alt_string&);
};


Expand All @@ -149,6 +166,11 @@ using alt_json = nlohmann::basic_json <
nlohmann::adl_serializer >;


bool operator<(const char* op1, const alt_string& op2) {
return op1 < op2.str_impl;
}



TEST_CASE("alternative string type")
{
Expand Down Expand Up @@ -210,4 +232,34 @@ TEST_CASE("alternative string type")
alt_string dump = doc.dump();
CHECK(dump == R"({"foo":"bar"})");
}

SECTION("equality")
{
alt_json doc;
doc["Who are you?"] = "I'm Batman";

CHECK("I'm Batman" == doc["Who are you?"]);
CHECK(doc["Who are you?"] == "I'm Batman");
CHECK_FALSE("I'm Batman" != doc["Who are you?"]);
CHECK_FALSE(doc["Who are you?"] != "I'm Batman");

CHECK("I'm Bruce Wayne" != doc["Who are you?"]);
CHECK(doc["Who are you?"] != "I'm Bruce Wayne");
CHECK_FALSE("I'm Bruce Wayne" == doc["Who are you?"]);
CHECK_FALSE(doc["Who are you?"] == "I'm Bruce Wayne");

{
const alt_json& const_doc = doc;

CHECK("I'm Batman" == const_doc["Who are you?"]);
CHECK(const_doc["Who are you?"] == "I'm Batman");
CHECK_FALSE("I'm Batman" != const_doc["Who are you?"]);
CHECK_FALSE(const_doc["Who are you?"] != "I'm Batman");

CHECK("I'm Bruce Wayne" != const_doc["Who are you?"]);
CHECK(const_doc["Who are you?"] != "I'm Bruce Wayne");
CHECK_FALSE("I'm Bruce Wayne" == const_doc["Who are you?"]);
CHECK_FALSE(const_doc["Who are you?"] == "I'm Bruce Wayne");
}
}
}