Make Location, Message, MessageContent and Tag comparisons case-sensitive

It makes more sense for their data.
This commit is contained in:
Oliver Hamlet
2018-10-20 12:48:21 +01:00
parent fa37dc6754
commit b142dd1a8f
14 changed files with 68 additions and 55 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ The scalar form is simply the value of the map form's ``link`` key. Using the sc
Equality
--------
Two location data structures are equal if the lowercased values of their ``link`` keys are identical.
Two location data structures are equal if the values of their ``link`` keys are identical.
Examples
--------
+2 -6
View File
@@ -57,12 +57,8 @@ If a message's ``content`` value is a string, the message will use the string as
Equality
--------
The equality of two message data structures is determined by comparing the values of their ``content`` keys. As the values of the keys can be different types, a comparison value is selected for each message using the following logic:
* If a value's type is a localised content list, then the English content string in that list is selected as the comparison value.
* If a value's type is a string, then that string is selected as the comparison value.
The two message data structures are then equal if their lowercased comparison values are identical.
The equality of two message data structures is determined by comparing the values of their ``content`` keys. If a content key is a string, it is treated as a localised content list
containing a single English-language string. The two message data structures are then equal if their localised content lists are identical.
Examples
--------
+1 -1
View File
@@ -22,7 +22,7 @@ The scalar form is simply the value of the map form's ``name`` key. Using the sc
Equality
--------
Two tag data structures are equal if the lowercased values of their ``name`` keys are identical.
Two tag data structures are equal if the values of their ``name`` keys are identical.
Examples
--------
+3 -4
View File
@@ -54,15 +54,14 @@ public:
/**
* A less-than operator implemented with no semantics so that Location objects
* can be stored in sets.
* @returns True if this Location's URL is case-insensitively
* lexicographically less than the given Location's URL, false
* otherwise.
* @returns True if this Location's URL string is lexicographically less than
* the given Location's URL string, false otherwise.
*/
LOOT_API bool operator<(const Location& rhs) const;
/**
* Check if two Location objects are equal by comparing their URLs.
* @returns True if the URLs are case-insensitively equal, false otherwise.
* @returns True if the URL strings are equal, false otherwise.
*/
LOOT_API bool operator==(const Location& rhs) const;
+2 -5
View File
@@ -79,11 +79,8 @@ public:
/**
* A less-than operator implemented with no semantics so that Message objects
* can be stored in sets.
* @returns If both messages have content, returns true if this Message's
* English text is case-insensitively lexicographically less than the
* given Message's English text, and false otherwise.
* Otherwise returns true if this Message has no content, and false
* otherwise.
* @returns Returns true if this Message's content is lexicographically less
* than the given Message's content, and false otherwise.
*/
LOOT_API bool operator<(const Message& rhs) const;
+3 -4
View File
@@ -73,15 +73,14 @@ public:
/**
* A less-than operator implemented with no semantics so that MessageContent
* objects can be stored in sets.
* @returns True if this MessageContent's text is case-insensitively
* lexicographically less than the given MessageContent's text, false
* otherwise.
* @returns True if this MessageContent's text is lexicographically less than
* the given MessageContent's text, false otherwise.
*/
LOOT_API bool operator<(const MessageContent& rhs) const;
/**
* Check if two MessageContent objects are equal by comparing their texts.
* @returns True if the texts are case-insensitively equal, false otherwise.
* @returns True if the texts are equal, false otherwise.
*/
LOOT_API bool operator==(const MessageContent& rhs) const;
+3 -5
View File
@@ -62,17 +62,15 @@ public:
* can be stored in sets.
* @returns True if this Tag is suggested for addition and the other is not.
* If both Tags are suggested for addition or both are suggested for
* removal, returns true if this Tag's name is case-insensitively
* lexicographically less than the given Tag's name, false
* otherwise.
* removal, returns true if this Tag's name is lexicographically less
* than the given Tag's name, false otherwise.
*/
LOOT_API bool operator<(const Tag& rhs) const;
/**
* Check if two Tag objects are equal.
* @returns True if both Tags are suggested for addition or both are suggested
* for removal, and the Tag names are case-insensitively equal, false
* otherwise.
* for removal, and the Tag names are equal, false otherwise.
*/
LOOT_API bool operator==(const Tag& rhs) const;
+2 -4
View File
@@ -24,8 +24,6 @@
#include "loot/metadata/location.h"
#include <boost/algorithm/string.hpp>
namespace loot {
Location::Location() {}
@@ -34,11 +32,11 @@ Location::Location(const std::string& url, const std::string& name) :
name_(name) {}
bool Location::operator<(const Location& rhs) const {
return boost::ilexicographical_compare(url_, rhs.GetURL());
return url_ < rhs.url_;
}
bool Location::operator==(const Location& rhs) const {
return boost::iequals(url_, rhs.GetURL());
return url_ == rhs.url_;
}
std::string Location::GetURL() const { return url_; }
+2 -9
View File
@@ -59,18 +59,11 @@ Message::Message(const MessageType type,
}
bool Message::operator<(const Message& rhs) const {
if (!content_.empty() && !rhs.GetContent().empty())
return boost::ilexicographical_compare(
GetContent(MessageContent::defaultLanguage).GetText(),
rhs.GetContent(MessageContent::defaultLanguage).GetText());
else if (content_.empty() && !rhs.GetContent().empty())
return true;
else
return false;
return content_ < rhs.GetContent();
}
bool Message::operator==(const Message& rhs) const {
return (content_ == rhs.GetContent());
return content_ == rhs.GetContent();
}
MessageType Message::GetType() const { return type_; }
+2 -2
View File
@@ -41,11 +41,11 @@ std::string MessageContent::GetText() const { return text_; }
std::string MessageContent::GetLanguage() const { return language_; }
bool MessageContent::operator<(const MessageContent& rhs) const {
return boost::ilexicographical_compare(text_, rhs.GetText());
return text_ < rhs.text_;
}
bool MessageContent::operator==(const MessageContent& rhs) const {
return (boost::iequals(text_, rhs.GetText()));
return text_ == rhs.text_;
}
MessageContent MessageContent::Choose(const std::vector<MessageContent> content,
const std::string& language) {
+4 -4
View File
@@ -38,14 +38,14 @@ Tag::Tag(const std::string& tag,
bool Tag::operator<(const Tag& rhs) const {
if (addTag_ != rhs.IsAddition())
return (addTag_ && !rhs.IsAddition());
return addTag_ && !rhs.IsAddition();
else
return boost::ilexicographical_compare(GetName(), rhs.GetName());
return GetName() < rhs.GetName();
}
bool Tag::operator==(const Tag& rhs) const {
return (addTag_ == rhs.IsAddition() &&
boost::iequals(GetName(), rhs.GetName()));
return addTag_ == rhs.IsAddition() &&
GetName() == rhs.GetName();
}
bool Tag::IsAddition() const { return addTag_; }
@@ -47,11 +47,16 @@ TEST(Location, stringsConstructorShouldStoreGivenStrings) {
EXPECT_EQ("example", location.GetName());
}
TEST(Location, locationsWithCaseInsensitiveEqualUrlsShouldBeEqual) {
TEST(Location, locationsWithCaseSensitiveEqualUrlsShouldBeEqual) {
Location location1("http://www.example.com", "example1");
Location location2("HTTP://WWW.EXAMPLE.COM", "example2");
Location location2("http://www.example.com", "example2");
EXPECT_TRUE(location1 == location2);
location1 = Location("http://www.example.com");
location2 = Location("HTTP://WWW.EXAMPLE.COM");
EXPECT_FALSE(location1 == location2);
}
TEST(Location, locationsWithDifferentUrlsShouldBeUnequal) {
@@ -62,13 +67,19 @@ TEST(Location, locationsWithDifferentUrlsShouldBeUnequal) {
}
TEST(Location,
lessThanOperatorShouldUseCaseInsensitiveLexicographicalUrlComparison) {
lessThanOperatorShouldUseCaseSensitiveLexicographicalUrlComparison) {
Location location1("http://www.example.com", "example1");
Location location2("HTTP://WWW.EXAMPLE.COM", "example2");
Location location2("http://www.example.com", "example2");
EXPECT_FALSE(location1 < location2);
EXPECT_FALSE(location2 < location1);
location1 = Location("http://www.example.com");
location2 = Location("HTTP://WWW.EXAMPLE.COM");
EXPECT_FALSE(location1 < location2);
EXPECT_TRUE(location2 < location1);
location1 = Location("http://www.example1.com");
location2 = Location("http://www.example2.com");
@@ -51,9 +51,14 @@ TEST(MessageContent, contentConstructorShouldStoreGivenStringAndLanguage) {
TEST(MessageContent, contentShouldBeEqualIfStringsAreCaseInsensitivelyEqual) {
MessageContent content1("content");
MessageContent content2("Content", french);
MessageContent content2("content", french);
EXPECT_TRUE(content1 == content2);
content1 = MessageContent("content");
content2 = MessageContent("Content", french);
EXPECT_FALSE(content1 == content2);
}
TEST(MessageContent,
@@ -65,13 +70,19 @@ TEST(MessageContent,
}
TEST(MessageContent,
LessThanOperatorShouldUseCaseInsensitiveLexicographicalComparison) {
lessThanOperatorShouldUseCaseSensitiveLexicographicalComparison) {
MessageContent content1("content");
MessageContent content2("Content", french);
MessageContent content2("content", french);
EXPECT_FALSE(content1 < content2);
EXPECT_FALSE(content2 < content1);
content1 = MessageContent("content", french);
content2 = MessageContent("Content");
EXPECT_FALSE(content1 < content2);
EXPECT_TRUE(content2 < content1);
content1 = MessageContent("content1", french);
content2 = MessageContent("content2");
+14 -3
View File
@@ -52,10 +52,15 @@ TEST(Tag, dataConstructorShouldSetFieldsToGivenValues) {
TEST(Tag,
tagsWithCaseInsensitiveEqualNamesAndEqualAdditionStatesShouldBeEqual) {
Tag tag1("Name", true, "condition1");
Tag tag1("name", true, "condition1");
Tag tag2("name", true, "condition2");
EXPECT_TRUE(tag1 == tag2);
tag1 = Tag("name");
tag2 = Tag("Name");
EXPECT_FALSE(tag1 == tag2);
}
TEST(Tags, tagsWithUnequalNamesShouldNotBeEqual) {
@@ -74,13 +79,19 @@ TEST(Tag, tagsWithUnequalAdditionStatesShouldNotBeEqual) {
TEST(
Tag,
lessThanOperatorShouldCaseInsensitivelyLexicographicallyCompareNameStrings) {
Tag tag1("Name");
lessThanOperatorShouldCaseSensitivelyLexicographicallyCompareNameStrings) {
Tag tag1("name");
Tag tag2("name");
EXPECT_FALSE(tag1 < tag2);
EXPECT_FALSE(tag2 < tag1);
tag1 = Tag("name");
tag2 = Tag("Name");
EXPECT_FALSE(tag1 < tag2);
EXPECT_TRUE(tag2 < tag1);
tag1 = Tag("name1");
tag2 = Tag("name2");