Skip to content

Commit

Permalink
Add UnitLabel inequality operators plus tests
Browse files Browse the repository at this point in the history
Refs #9252
  • Loading branch information
martyngigg committed Apr 9, 2014
1 parent 708707d commit b09f38e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Code/Mantid/Framework/Kernel/inc/MantidKernel/UnitLabel.h
Expand Up @@ -48,14 +48,27 @@ namespace Mantid
UnitLabel(const AsciiString & ascii, const Utf8String & unicode);
/// Constructor giving both labels as ascii
UnitLabel(const AsciiString & ascii);
/// Constructor giving both labels as ascii using a C-style string
UnitLabel(const char * ascii);

/// Equality operator with other label
bool operator==(const UnitLabel & rhs) const;
/// Equality operator with std::string
bool operator==(const std::string & rhs) const;
/// Equality operator with std::wtring
/// Equality operator with c-style string
bool operator==(const char * rhs) const;
/// Equality operator with std::wstring
bool operator==(const std::wstring & rhs) const;

/// Inqquality operator with other label
bool operator!=(const UnitLabel & rhs) const;
/// Inequality operator with std::string
bool operator!=(const std::string & rhs) const;
/// Inequality operator with c-style string
bool operator!=(const char * rhs) const;
/// Inequality operator with std::wstring
bool operator!=(const std::wstring & rhs) const;

/// Return an ascii label for unit
const AsciiString & ascii() const;
/// Return a utf-8 encoded label for unit
Expand Down
65 changes: 65 additions & 0 deletions Code/Mantid/Framework/Kernel/src/UnitLabel.cpp
@@ -1,4 +1,5 @@
#include "MantidKernel/UnitLabel.h"
#include <cstring>

namespace Mantid
{
Expand All @@ -23,6 +24,16 @@ namespace Mantid
{
}

/**
* Use an ASCII string for the unicode variant too, given
* as a C-style string
* @param ascii A plain-text label
*/
UnitLabel::UnitLabel(const char *ascii)
: m_ascii(ascii), m_utf8(m_ascii.begin(), m_ascii.end())
{
}

/**
* Test if two objects are considered equal
* @param rhs A second label object
Expand All @@ -45,6 +56,17 @@ namespace Mantid
return (this->ascii() == rhs);
}

/**
* Test if this object is considered equal to another c-style string.
* It compares to result of ascii()
* @param rhs A string to compare
* @return True if they are conisdered equal, false otherwise
*/
bool UnitLabel::operator==(const char *rhs) const
{
return (strcmp(ascii().c_str(), rhs) == 0);
}

/**
* Test if this object is considered equal to another std::wstring.
* It compares to result of utf8()
Expand All @@ -56,6 +78,49 @@ namespace Mantid
return (this->utf8() == rhs);
}

/**
* Test if two objects are not considered equal
* @param rhs A second label object
* @return True if they are conisdered equal, false otherwise
*/
bool UnitLabel::operator!=(const UnitLabel &rhs) const
{
return !(*this == rhs);
}

/**
* Test if this object is not considered equal to another std::string.
* It compares to result of ascii()
* @param rhs A string to compare
* @return True if they are conisdered equal, false otherwise
*/
bool UnitLabel::operator!=(const std::string &rhs) const
{
return !(*this == rhs);
}

/**
* Test if this object is not considered equal to another c-style string
* It compares to result of ascii()
* @param rhs A string to compare
* @return True if they are conisdered equal, false otherwise
*/
bool UnitLabel::operator!=(const char * rhs) const
{
return !(*this == rhs);
}

/**
* Test if this object is not considered equal to another std::wstring.
* It compares to result of utf8()
* @param rhs A string to compare
* @return True if they are conisdered equal, false otherwise
*/
bool UnitLabel::operator!=(const std::wstring &rhs) const
{
return !(*this == rhs);
}

/**
* @return A std::string containing the plain-text label
*/
Expand Down
37 changes: 37 additions & 0 deletions Code/Mantid/Framework/Kernel/test/UnitLabelTest.h
Expand Up @@ -30,6 +30,19 @@ class UnitLabelTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(L"\u212b", label.utf8());
}

void test_construction_from_single_string_sets_both_label_types_to_equal()
{
UnitLabel label("LabelText");

TS_ASSERT_EQUALS("LabelText", label.ascii());
TS_ASSERT_EQUALS(L"LabelText", label.utf8());
}

void test_implicit_construction_from_std_string_sets_both_label_types_to_equal()
{
doImplicitConversionTest("LabelText", "LabelText");
}

void test_implicit_string_converter_returns_ascii_method()
{
UnitLabel label("TextLabel", L"\u212b");
Expand All @@ -38,6 +51,30 @@ class UnitLabelTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS("TextLabel", asciiText);
}

void test_comparision_operators()
{
UnitLabel label("TextLabel", L"\u212b");
UnitLabel labelDiffAscii("TextLabe", L"\u212b");
UnitLabel labelDiffUtf8("TextLabel", L"\u207b");

TS_ASSERT(label == label);
TS_ASSERT(label == "TextLabel");
TS_ASSERT(label == L"\u212b");

TS_ASSERT(label != labelDiffAscii);
TS_ASSERT(label != labelDiffUtf8);
TS_ASSERT(label != "TextLabe");
TS_ASSERT(label != L"\u207b");
}

private:
void doImplicitConversionTest(UnitLabel lbl, std::string expected)
{
TS_ASSERT_EQUALS(expected, lbl.ascii());
const auto & utf8 = lbl.utf8();
TS_ASSERT_EQUALS(expected, std::string(utf8.begin(), utf8.end()));
}

};


Expand Down

0 comments on commit b09f38e

Please sign in to comment.