1. Matt Oswald
  2. xUnit++

Commits

Matt Oswald  committed ee72cea

went a little overboard with move-optimizations, lost theory names and filenames when containing more than one item in the data set

  • Participants
  • Parent commits 9bd0742
  • Branches default

Comments (0)

Files changed (2)

File Tests/UnitTests/Theory.cpp

View file
 {
 public:
     TheoryFixture()
+        : fakeFileName("FakeFile.cpp")
     {
     }
 
             std::move(params),
             attributes,
             -1,
-            __FILE__, __LINE__,
+            GetFakeFileName(), __LINE__,
             localEventRecorders);
         (void)reg;
     }
         Run();
     }
 
+    std::string GetFakeFileName() const
+    {
+        return fakeFileName;
+    }
+
+    std::string fakeFileName;
+
     xUnitpp::Tests::OutputRecord record;
     xUnitpp::AttributeCollection attributes;
     xUnitpp::TestCollection collection;
 
     auto doTheory = [&](int x) { std::lock_guard<std::mutex> guard(lock); dataProvided.push_back(x); };
     xUnitpp::TestCollection::Register reg(collection, doTheory, RawFunctionProvider,
-        "TheoriesGetAllDataPassedToThem", "Theory", "(int x)", attributes, -1, __FILE__, __LINE__, localEventRecorders);
+        "TheoriesGetAllDataPassedToThem", "Theory", "(int x)", attributes, -1, GetFakeFileName(), __LINE__, localEventRecorders);
     (void)reg;
 
     Run();
     auto doTheory = [](int) { Assert.Fail() << "Should not be run."; };
 
     xUnitpp::TestCollection::Register reg(collection, doTheory, RawFunctionProvider,
-        "TheoriesGetAllDataPassedToThem", "Theory", "(int x)", attributes, -1, __FILE__, __LINE__, localEventRecorders);
+        "TheoriesGetAllDataPassedToThem", "Theory", "(int x)", attributes, -1, GetFakeFileName(), __LINE__, localEventRecorders);
     (void)reg;
 
     Run();
 }
 
-ATTRIBUTES(("Cats", "Meow"))
-{
-DATA_THEORY("TheoriesCanHaveAttributes", (int), RawFunctionProvider)
-{
-    for (const auto &test : xUnitpp::TestCollection::Instance().Tests())
-    {
-        if (test->TestDetails().ShortName == "TheoriesCanHaveAttributes")
-        {
-            auto it = std::find_if(test->TestDetails().Attributes.begin(), test->TestDetails().Attributes.end(), [](const std::pair<std::string, std::string> &item) { return item.first == "Cats"; });
-            Assert.True(it != test->TestDetails().Attributes.end());
-            Assert.True(it->second == "Meow");
-            return;
-        }
-    }
-
-    Assert.Fail() << "Could not find self in test list.";
-}
-}
-
 std::vector<std::tuple<std::string, std::vector<std::tuple<int, std::string>>>> ComplexProvider()
 {
     std::vector<std::tuple<std::string, std::vector<std::tuple<int, std::string>>>> result;
     Run();
 }
 
+FACT_FIXTURE("Every theory instance should have a name", TheoryFixture)
+{
+    auto dataProvider =
+        []()
+        {
+            std::vector<std::tuple<int>> dataProvider;
+            dataProvider.emplace_back(10);
+            dataProvider.emplace_back(20);
+            return dataProvider;
+        };
+    
+    RegisterAndRun("TheoryName", "(int x)", std::move(dataProvider));
+
+    Assert.Equal(2U, record.events.size());
+
+    for (auto &&t: record.events)
+    {
+        auto test = std::get<0>(t);
+
+        Assert.True(test.Name.substr(0, 10) == "TheoryName");
+    }
 }
+
+FACT_FIXTURE("Every theory instance should have a short name", TheoryFixture)
+{
+    auto dataProvider =
+        []()
+        {
+            std::vector<std::tuple<int>> dataProvider;
+            dataProvider.emplace_back(10);
+            dataProvider.emplace_back(20);
+            return dataProvider;
+        };
+    
+    RegisterAndRun("TheoryName", "(int x)", std::move(dataProvider));
+
+    Assert.Equal(2U, record.events.size());
+
+    for (auto &&t: record.events)
+    {
+        auto test = std::get<0>(t);
+
+        Assert.Equal("TheoryName", test.ShortName);
+    }
+}
+
+FACT_FIXTURE("Every theory instance should know the file it is in", TheoryFixture)
+{
+    auto dataProvider =
+        []()
+        {
+            std::vector<std::tuple<int>> dataProvider;
+            dataProvider.emplace_back(10);
+            dataProvider.emplace_back(20);
+            return dataProvider;
+        };
+    
+    RegisterAndRun("TheoryName", "(int x)", std::move(dataProvider));
+
+    Assert.Equal(2U, record.events.size());
+
+    for (auto &&t: record.events)
+    {
+        auto test = std::get<0>(t);
+
+        Assert.Equal(fakeFileName, test.LineInfo.file);
+    }
+}
+
+ATTRIBUTES(("Cats", "Meow"))
+{
+DATA_THEORY("TheoriesCanHaveAttributes", (int), RawFunctionProvider)
+{
+    for (const auto &test : xUnitpp::TestCollection::Instance().Tests())
+    {
+        if (test->TestDetails().ShortName == "TheoriesCanHaveAttributes")
+        {
+            auto it = std::find_if(test->TestDetails().Attributes.begin(), test->TestDetails().Attributes.end(), [](const std::pair<std::string, std::string> &item) { return item.first == "Cats"; });
+            Assert.True(it != test->TestDetails().Attributes.end());
+            Assert.True(it->second == "Meow");
+            return;
+        }
+    }
+
+    Assert.Fail() << "Could not find self in test list.";
+}
+}
+
+}

File xUnit++/xUnit++/TestCollection.h

View file
             AttributeCollection &&attributes, int milliseconds, std::string &&filename, int line, std::vector<std::shared_ptr<TestEventRecorder>> &&testEventRecorders);
 
         template<typename TTheory, typename TTheoryData>
-        Register(TestCollection &collection, TTheory &&theory_, TTheoryData &&theoryData_, std::string &&name, const std::string &suite, std::string &&params,
+        Register(TestCollection &collection, TTheory &&theory, TTheoryData &&theoryData, std::string &&name, const std::string &suite, std::string &&params,
             const AttributeCollection &attributes, int milliseconds, std::string &&filename, int line, const std::vector<std::shared_ptr<TestEventRecorder>> &testEventRecorders)
         {
-            auto theory = theory_;
-            auto theoryData = theoryData_;
-
             int id = 0;
             for (auto t : theoryData())
             {
                 auto theoryName = GetLongTheoryName(name + " [" + ToString(id++) + "] (", SplitParams(std::move(params)), std::forward<decltype(t)>(t));
 
-                collection.mTests.push_back(std::make_shared<xUnitTest>(TheoryHelper(std::forward<TTheory>(theory), std::move(t)), std::move(theoryName), std::move(name), suite,
-                    AttributeCollection(attributes), Time::ToDuration(Time::ToMilliseconds(milliseconds)), std::move(filename), line, testEventRecorders));
+                collection.mTests.push_back(std::make_shared<xUnitTest>(TheoryHelper(std::forward<TTheory>(theory), std::move(t)), std::string(theoryName), std::string(name), suite,
+                    AttributeCollection(attributes), Time::ToDuration(Time::ToMilliseconds(milliseconds)), std::string(filename), line, testEventRecorders));
             }
         }
     };