This repository has been archived by the owner on May 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
ToString
Maxime ROUFFET edited this page Mar 25, 2021
·
11 revisions
UTH::ToString is the method used by macros to output a param as a string.
UTH provides a default implementation that tries to call the method ToString from an element if there is one (pseudo code):
template <typename T>
std::string UTH::ToString(const T& _elem)
{
if <has_member_ToString>
return _elem.ToString();
else
return "";
}
Thus, if the method ToString is already defined within the class, it will be automatically called:
class MyClass
{
int value;
std::string ToString() const { return std::to_string(value); }
};
If such a method is not provided, no output string will be output during testing (which makes test debugging harder).
If the class element doesn't implement such a method and you wish to not implement it inside your class, you just need to implement a template specialization of UTH::ToString above the tests:
// Vec2.hpp
struct Vec2
{
float x = 0.0f;
float y = 0.0f;
static bool Equals(const Vec2& _v1, const Vec2& _v2)
{
return _v1.x == _v2.x && _v1.y == _v2.y;
}
};
// main.cpp
#include "Vec2.hpp"
// Template specialization
template <>
std::string UTH::ToString(const Vec2& _elem)
{
return "X: " + std::to_string(_elem.x) + "\tY: " + std::to_string(_elem.y);
}
int main()
{
SA_UTH_INIT();
Vec2 v1{ 1.0f, 2.0f };
Vec2 v2{ 3.0f, 5.0f };
SA_UTH_SF(Vec2::Equals, v1, v2);
SA_UTH_EXIT()
}
Output in default verbosity:
[SA-UTH] Failure: Vec2::Equals(v1, v2) -- l<line>
v1
X: 1.000000 Y: 2.000000
v2
X: 3.000000 Y: 5.000000
For advanced ToString implementation, partial template specialization is easily possible.
// Vec2.hpp
template <typename T>
struct Vec2
{
T x = 0.0f;
T y = 0.0f;
static bool Equals(const Vec2<T>& _v1, const Vec2<T>& _v2)
{
return _v1.x == _v2.x && _v1.y == _v2.y;
}
};
// main.cpp
#include "Vec2.hpp"
// Template partial specialization
template <typename T>
std::string UTH::ToString(const Vec2<T>& _elem)
{
return "X: " + std::to_string(_elem.x) + "\tY: " + std::to_string(_elem.y);
}
int main()
{
SA_UTH_INIT();
Vec2 v1{ 1.0f, 2.0f };
Vec2 v2{ 3.0f, 5.0f };
SA_UTH_SF(Vec2::Equals, v1, v2);
SA_UTH_EXIT()
}
Output in default verbosity:
[SA-UTH] Failure: Vec2::Equals(v1, v2) -- l<line>
v1
X: 1.000000 Y: 2.000000
v2
X: 3.000000 Y: 5.000000
- main_success.cpp for ToString example.
- main_failure.cpp for Stringizer implementation (not requiered) example.
- See SA-Maths for Stringizer (required) implementation: