Commits

Matt Oswald committed c9b8287

Assert.NotInRange tests added

Comments (0)

Files changed (4)

xUnit++/Tests/Assert.NotInRange.cpp

+#include <tuple>
+#include <vector>
+#include "../xUnit++.h"
+
+using xUnitpp::xUnitAssert;
+using xUnitpp::Assert;
+
+SUITE(AssertNotInRange)
+{
+
+std::vector<std::tuple<int, int, int>> NotInRangeSuccessData()
+{
+    std::vector<std::tuple<int, int, int>> data;
+
+    data.push_back(std::make_tuple(-1, 0, 1));
+    data.push_back(std::make_tuple(1, 0, 1));
+    data.push_back(std::make_tuple(2, 0, 1));
+    data.push_back(std::make_tuple(0, -2, -1));
+    data.push_back(std::make_tuple(-1, -2, -1));
+    data.push_back(std::make_tuple(-3, -2, -1));
+
+    return data;
+}
+
+THEORY(NotInRangeSuccess, (int actual, int min, int max), NotInRangeSuccessData)
+{
+    Assert.NotInRange(actual, min, max);
+}
+
+std::vector<std::tuple<int, int, int>> NotInRangeAssertsOnFailData()
+{
+    std::vector<std::tuple<int, int, int>> data;
+
+    data.push_back(std::make_tuple(0, 0, 1));
+    data.push_back(std::make_tuple(2, 1, 3));
+    data.push_back(std::make_tuple(-1, -1, 0));
+    data.push_back(std::make_tuple(-2, -3, -1));
+
+    return data;
+}
+
+THEORY(NotInRangeAssertsOnFailure, (int actual, int min, int max), NotInRangeAssertsOnFailData)
+{
+    Assert.Throws<xUnitAssert>([=]() { Assert.NotInRange(actual, min, max); });
+}
+
+FACT(NotInRangeAppendsUserMessage)
+{
+    static const std::string msg = "xUnit++";
+
+    auto assert = Assert.Throws<xUnitAssert>([=]() { Assert.NotInRange(0, 0, 1, msg); });
+
+    Assert.Contains(assert.what(), msg.c_str());
+}
+
+FACT(NotInRangeNeedsValidRange)
+{
+    Assert.Throws<std::invalid_argument>([]() { Assert.NotInRange(0, 0, 0); });
+    Assert.Throws<std::invalid_argument>([]() { Assert.NotInRange(0, 1, 0); });
+}
+
+}

xUnit++/xUnit++.vcxproj

     <ClCompile Include="Tests\Assert.Fail.cpp" />
     <ClCompile Include="Tests\Assert.False.cpp" />
     <ClCompile Include="Tests\Assert.NotEqual.cpp" />
+    <ClCompile Include="Tests\Assert.NotInRange.cpp" />
     <ClCompile Include="Tests\Assert.Throws.cpp" />
     <ClCompile Include="Tests\Assert.True.cpp" />
     <ClCompile Include="Theory.cpp" />

xUnit++/xUnit++.vcxproj.filters

     <ClCompile Include="Tests\Assert.Contains.cpp">
       <Filter>Tests</Filter>
     </ClCompile>
+    <ClCompile Include="Tests\Assert.NotInRange.cpp">
+      <Filter>Tests</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="Fact.h" />

xUnit++/xUnitAssert.h

     template<typename TActual, typename TRange>
     void InRange(TActual actual, TRange min, TRange max, const std::string &msg = "") const
     {
+        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) + ").");
+        }
+
         if (actual < min || actual >= max)
         {
             throw xUnitAssert("InRange", msg, "", "[" + std::to_string(min) + " - " + std::to_string(max) + ")", std::to_string(actual));
     template<typename TActual, typename TRange>
     void NotInRange(TActual actual, TRange min, TRange max, const std::string &msg = "") const
     {
+        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) + ").");
+        }
+
         if (actual >= min && actual < max)
         {
             throw xUnitAssert("NotInRange", msg, "", "[" + std::to_string(min) + " - " + std::to_string(max) + ")", std::to_string(actual));
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.