1. Matt Oswald
  2. xUnit++

Commits

Matt Oswald  committed 6e58331

made sure it's possible to print any type with a provided to_string function

  • Participants
  • Parent commits 1fd6d26
  • Branches default

Comments (0)

Files changed (5)

File Tests/UnitTests/TestsCanOutputAnythingWithToString.cpp

View file
+#include <string>
+#include "xUnit++/xUnit++.h"
+
+SUITE(KNRToString)
+{
+
+namespace HasToString
+{
+    struct Point
+    {
+        Point()
+            : x(0)
+            , y(0)
+        {
+        }
+
+        Point(int x, int y)
+            : x(x)
+            , y(y)
+        {
+        }
+
+        friend bool operator <(const Point &p0, const Point &p1)
+        {
+            return p0.x < p1.x;
+        }
+
+        friend bool operator >=(const Point &p0, const Point &p1)
+        {
+            return p0.x >= p1.x;
+        }
+
+        friend bool operator ==(const Point &p0, const Point &p1)
+        {
+            return p0.x == p1.x && p0.y == p1.y;
+        }
+
+        friend std::string to_string(const Point &pt)
+        {
+            return "(" + std::to_string(pt.x) + ", " + std::to_string(pt.y) + ")";
+        }
+
+        int x;
+        int y;
+    };
+}
+
+using HasToString::Point;
+
+FACT(EqualKNR)
+{
+    Point p0(0, 0);
+    Point p1(1, 1);
+
+    auto result = Assert.Throws<xUnitpp::xUnitAssert>([&]() { Assert.Equal(p0, p1); });
+
+    Check.Contains(result.what(), "Expected: (0, 0)");
+    Check.Contains(result.what(), "Actual: (1, 1)");
+}
+
+FACT(RangeEqualKNR)
+{
+    Point p0[] = { Point(0, 0), Point(1, 0), Point(2, 0) };
+    Point p1[] = { Point(0, 0), Point(1, 1), Point(2, 0) };
+
+    auto result = Assert.Throws<xUnitpp::xUnitAssert>([&]() { Assert.Equal(&p0[0], &p0[3], &p1[0], &p1[3]); });
+
+    Check.Contains(result.what(), "Sequence unequal at location 1.");
+    Check.Contains(result.what(), "Expected: [ (0, 0), (1, 0), (2, 0) ]");
+    Check.Contains(result.what(), "Actual: [ (0, 0), (1, 1), (2, 0) ]");
+}
+
+FACT(InRangeKNR)
+{
+    Point min(0, 0);
+    Point max(10, 0);
+    Point test(20, 0);
+
+    auto result = Assert.Throws<xUnitpp::xUnitAssert>([&]() { Assert.InRange(test, min, max); });
+
+    Check.Contains(result.what(), "Expected: [(0, 0) - (10, 0))");
+    Check.Contains(result.what(), "Actual: (20, 0)");
+}
+
+FACT(NotInRangeKNR)
+{
+    Point min(0, 0);
+    Point max(10, 0);
+    Point test(5, 0);
+
+    auto result = Assert.Throws<xUnitpp::xUnitAssert>([&]() { Assert.NotInRange(test, min, max); });
+
+    Check.Contains(result.what(), "Expected: [(0, 0) - (10, 0))");
+    Check.Contains(result.what(), "Actual: (5, 0)");
+}
+
+}

File Tests/UnitTests/UnitTests.vcxproj

View file
     <ClCompile Include="Helpers\OutputRecord.cpp" />
     <ClCompile Include="LineInfo.cpp" />
     <ClCompile Include="TestRunner.cpp" />
+    <ClCompile Include="TestsCanOutputAnythingWithToString.cpp" />
     <ClCompile Include="Theory.cpp" />
   </ItemGroup>
   <ItemGroup>

File Tests/UnitTests/UnitTests.vcxproj.filters

View file
     <ClCompile Include="Helpers\OutputRecord.cpp">
       <Filter>Test Helpers</Filter>
     </ClCompile>
+    <ClCompile Include="TestsCanOutputAnythingWithToString.cpp" />
   </ItemGroup>
   <ItemGroup>
     <Filter Include="Test Helpers">

File notes.txt

View file
 finish unit testing the lib
     - ensure output formats
     - several tests (look at Theory.cpp) do not actually assert anything happened
-redo include structure
 vsix project needs embedded eula and license
 need license for whole project
 

File xUnit++/xUnit++/xUnitAssert.h

View file
     template<typename T>
     static std::string RangeToString(const T &begin, const T &end)
     {
+        using std::to_string;
         std::string result = "[ ";
 
-        std::for_each(begin, end, [&result](decltype(*begin) val) { result += std::to_string(val) + ", "; });
+        std::for_each(begin, end, [&result](decltype(*begin) val) { result += to_string(val) + ", "; });
 
         result[result.size() - 2] = ' ';
         result[result.size() - 1] = ']';
     template<typename TSequence, typename TPredicate>
     xUnitFailure DoesNotContainPred(const TSequence &sequence, TPredicate &&predicate, const LineInfo &lineInfo = LineInfo::empty()) const
     {
-        using namespace std;
+        using std::to_string;
 
-        auto found = find_if(begin(sequence), end(sequence), predicate);
-        if (found != end(sequence))
+        auto found = std::find_if(std::begin(sequence), std::end(sequence), predicate);
+        if (found != std::end(sequence))
         {
             return OnFailure(xUnitAssert(callPrefix + "DoesNotContain", lineInfo)
-                .CustomMessage("Found: matching value at position " + to_string(distance(begin(sequence), found)) + "."));
+                .CustomMessage("Found: matching value at position " + to_string(std::distance(std::begin(sequence), found)) + "."));
         }
 
         return OnSuccess();
     template<typename TActual, typename TRange>
     xUnitFailure InRange(TActual actual, TRange min, TRange max, const LineInfo &lineInfo = LineInfo::empty()) const
     {
+        using std::to_string;
+
         if (min >= max)
         {
-            throw std::invalid_argument("Assert.InRange argument error: min (" + std::to_string(min) + ") must be strictly less than max (" + std::to_string(max) + ").");
+            throw std::invalid_argument("Assert.InRange argument error: min (" + to_string(min) + ") must be strictly less than max (" + to_string(max) + ").");
         }
 
         if (actual < min || actual >= max)
         {
             return OnFailure(xUnitAssert(callPrefix + "InRange", lineInfo)
-                .Expected("[" + std::to_string(min) + " - " + std::to_string(max) + ")")
-                .Actual(std::to_string(actual)));
+                .Expected("[" + to_string(min) + " - " + to_string(max) + ")")
+                .Actual(to_string(actual)));
         }
 
         return OnSuccess();
     template<typename TActual, typename TRange>
     xUnitFailure NotInRange(TActual actual, TRange min, TRange max, const LineInfo &lineInfo = LineInfo::empty()) const
     {
+        using std::to_string;
+
         if (min >= max)
         {
-            throw std::invalid_argument("Assert.NotInRange argument error: min (" + std::to_string(min) + ") must be strictly less than max (" + std::to_string(max) + ").");
+            throw std::invalid_argument("Assert.NotInRange argument error: min (" + to_string(min) + ") must be strictly less than max (" + to_string(max) + ").");
         }
 
         if (actual >= min && actual < max)
         {
             return OnFailure(xUnitAssert(callPrefix + "NotInRange", lineInfo)
-                .Expected("[" + std::to_string(min) + " - " + std::to_string(max) + ")")
-                .Actual(std::to_string(actual)));
+                .Expected("[" + to_string(min) + " - " + to_string(max) + ")")
+                .Actual(to_string(actual)));
         }
 
         return OnSuccess();