Commits

Matt Oswald committed 2379b8f

xUnitpp::ToString will print pointer values, not just pointer type

Comments (0)

Files changed (2)

Tests/UnitTests/ToString.cpp

+#include <sstream>
 #include "xUnit++/xUnit++.h"
 
 SUITE("ToString")
     Assert.Equal("ABCD", s);
 }
 
+FACT("ToString will print pointer addresses")
+{
+    int x;
+    int y;
+
+    auto result = Assert.Throws<xUnitpp::xUnitAssert>([&]() { Assert.Equal(&x, &y); });
+
+    std::stringstream str;
+    str << "int *: " << std::showbase << std::hex << reinterpret_cast<int>(&x);
+
+    Assert.Equal(str.str(), result.Expected());
 }
+
+}

xUnit++/xUnit++/xUnitToString.h

     struct to_string_impl<false>
     {
         template<typename T>
-        static std::string to_string(T &&)
+        static std::string to_string(T)
         {
             // going to truncate the type if it is longer than 20 characters
             std::string type = typeid(T).name();
 
-            return type.size() > 20 ?
+            return (type.size() > 20 ?
                 type.substr(0, 20) + "..." :
-                type;
+                type);
+        }
+
+        template<typename T>
+        static std::string to_string(T *val)
+        {
+            auto addr = reinterpret_cast<int>(val);
+
+            std::stringstream str;
+            str << to_string(*val) << " *: " << std::showbase << std::hex << addr;
+            return str.str();
         }
     };
 }