Skip to content

Commit

Permalink
Simplify UnitLabel class by just storing attributes.
Browse files Browse the repository at this point in the history
It makes it easier to use.
Refs #9252
  • Loading branch information
martyngigg committed Apr 7, 2014
1 parent 0cbe00a commit 3f2d4da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
25 changes: 19 additions & 6 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/UnitLabel.h
Expand Up @@ -23,6 +23,7 @@
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/ClassMacros.h"
#include "MantidKernel/DllConfig.h"
#include <string>

Expand All @@ -37,10 +38,14 @@ namespace Mantid
class MANTID_KERNEL_DLL UnitLabel
{
public:
/// Virtual destructor
virtual ~UnitLabel();
/// "Virtual" copy constructor
virtual UnitLabel * clone() const = 0;
/// Type that contains a plain-text string
typedef std::string AsciiString;
/// Type that can hold a unicode string. This may vary per-platform depending on the
/// width of the the built-in std::wstring
typedef std::wstring Utf8String;

/// Constructor giving both labels
UnitLabel(const std::string & ascii, const std::wstring & unicode);

/// Equality operator with other label
bool operator==(const UnitLabel & rhs) const;
Expand All @@ -50,12 +55,20 @@ namespace Mantid
bool operator==(const std::wstring & rhs) const;

/// Return an ascii label for unit
virtual const std::string ascii() const = 0;
const AsciiString & ascii() const;
/// Return a utf-8 encoded label for unit
virtual const std::wstring utf8() const = 0;
const Utf8String &utf8() const;

/// Implicit conversion to std::string
operator std::string() const;

private:
DISABLE_DEFAULT_CONSTRUCT(UnitLabel);

/// Value of plain-text label
std::string m_ascii;
/// Value of utf-8 encoded string
std::wstring m_utf8;
};

} // namespace Kernel
Expand Down
21 changes: 20 additions & 1 deletion Code/Mantid/Framework/Kernel/src/UnitLabel.cpp
Expand Up @@ -6,8 +6,11 @@ namespace Mantid
{

/**
* @param ascii A plain-text label containing only ascii characters
* @param unicode A label that can contain unicode characters
*/
UnitLabel::~UnitLabel()
UnitLabel::UnitLabel(const std::string &ascii, const std::wstring &unicode)
: m_ascii(ascii), m_utf8(unicode)
{
}

Expand Down Expand Up @@ -44,6 +47,22 @@ namespace Mantid
return (this->utf8() == rhs);
}

/**
* @return A std::string containing the plain-text label
*/
const UnitLabel::AsciiString & UnitLabel::ascii() const
{
return m_ascii;
}

/**
* @return A UnitLabel::utf8string containing the unicode label
*/
const UnitLabel::Utf8String & UnitLabel::utf8() const
{
return m_utf8;
}

/**
* Returns the results of the ascii() method
*/
Expand Down
45 changes: 8 additions & 37 deletions Code/Mantid/Framework/Kernel/test/UnitLabelTest.h
Expand Up @@ -4,28 +4,11 @@
#include "MantidKernel/UnitLabel.h"

#include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

using Mantid::Kernel::UnitLabel;

class UnitLabelTest : public CxxTest::TestSuite
{
class MockLabel: public UnitLabel
{
public:
MockLabel()
{
using namespace ::testing;
ON_CALL(*this, ascii()).WillByDefault(Return(std::string("TextLabel")));
ON_CALL(*this, utf8()).WillByDefault(Return(std::wstring(L"Utf8TextLabel")));
}

MockLabel * clone() const { return new MockLabel(); }

MOCK_CONST_METHOD0(ascii, const std::string());
MOCK_CONST_METHOD0(utf8, const std::wstring());
};

public:
// This pair of boilerplate methods prevent the suite being created statically
Expand All @@ -35,36 +18,24 @@ class UnitLabelTest : public CxxTest::TestSuite

void test_simple_string()
{
using namespace ::testing;
MockLabel label;
EXPECT_CALL(label, ascii()).Times(::testing::Exactly(1));
UnitLabel label("TextLabel", L"TextLabel");

label.ascii();

TS_ASSERT(Mock::VerifyAndClearExpectations(&label));
TS_ASSERT_EQUALS("TextLabel", label.ascii());
}

void test_utf8_string()
void test_utf8_string_can_hold_unicode_data()
{
using namespace ::testing;
MockLabel label;
EXPECT_CALL(label, utf8()).WillOnce(Return(std::wstring(L"")));

label.utf8();
UnitLabel label("TextLabel", L"\u212b");

TS_ASSERT(Mock::VerifyAndClearExpectations(&label));
TS_ASSERT_EQUALS(L"\u212b", label.utf8());
}

void test_implicit_string_converter_returns_ascii_method()
{
using namespace ::testing;
MockLabel label;
EXPECT_CALL(label, ascii()).Times(Exactly(1));

std::string text = label;
UnitLabel label("TextLabel", L"\u212b");
std::string asciiText = label;

TS_ASSERT(Mock::VerifyAndClearExpectations(&label));
TS_ASSERT_EQUALS("TextLabel", text);
TS_ASSERT_EQUALS("TextLabel", asciiText);
}

};
Expand Down

0 comments on commit 3f2d4da

Please sign in to comment.