| 1 | /* |
| 2 | * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the <organization> nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "CppUTest/TestHarness.h" |
| 29 | #include "CppUTest/TestRegistry.h" |
| 30 | #include "CppUTest/TestOutput.h" |
| 31 | #include "CppUTest/PlatformSpecificFunctions.h" |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | const int testLineNumber = 1; |
| 36 | } |
| 37 | |
| 38 | class MockTest: public UtestShell |
| 39 | { |
| 40 | public: |
| 41 | MockTest(const char* group = "Group") : |
| 42 | UtestShell(group, "Name", "File", testLineNumber), hasRun_(false) |
| 43 | { |
| 44 | } |
| 45 | virtual void runOneTest(TestPlugin*, TestResult&) |
| 46 | { |
| 47 | hasRun_ = true; |
| 48 | } |
| 49 | |
| 50 | bool hasRun_; |
| 51 | }; |
| 52 | |
| 53 | class MockTestResult: public TestResult |
| 54 | { |
| 55 | public: |
| 56 | |
| 57 | int countTestsStarted; |
| 58 | int countTestsEnded; |
| 59 | int countCurrentTestStarted; |
| 60 | int countCurrentTestEnded; |
| 61 | int countCurrentGroupStarted; |
| 62 | int countCurrentGroupEnded; |
| 63 | |
| 64 | MockTestResult(TestOutput& p) : |
| 65 | TestResult(p) |
| 66 | { |
| 67 | resetCount(); |
| 68 | } |
| 69 | |
| 70 | virtual ~MockTestResult() _destructor_override |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void resetCount() |
| 75 | { |
| 76 | countTestsStarted = 0; |
| 77 | countTestsEnded = 0; |
| 78 | countCurrentTestStarted = 0; |
| 79 | countCurrentTestEnded = 0; |
| 80 | countCurrentGroupStarted = 0; |
| 81 | countCurrentGroupEnded = 0; |
| 82 | } |
| 83 | |
| 84 | virtual void testsStarted() _override |
| 85 | { |
| 86 | countTestsStarted++; |
| 87 | } |
| 88 | virtual void testsEnded() _override |
| 89 | { |
| 90 | countTestsEnded++; |
| 91 | } |
| 92 | virtual void currentTestStarted(UtestShell* /*test*/) _override |
| 93 | { |
| 94 | countCurrentTestStarted++; |
| 95 | } |
| 96 | virtual void currentTestEnded(UtestShell* /*test*/) _override |
| 97 | { |
| 98 | countCurrentTestEnded++; |
| 99 | } |
| 100 | virtual void currentGroupStarted(UtestShell* /*test*/) _override |
| 101 | { |
| 102 | countCurrentGroupStarted++; |
| 103 | } |
| 104 | virtual void currentGroupEnded(UtestShell* /*test*/) _override |
| 105 | { |
| 106 | countCurrentGroupEnded++; |
| 107 | } |
| 108 | |
| 109 | }; |
| 110 | |
| 111 | TEST_GROUP(TestRegistry) |
| 112 | { |
| 113 | TestRegistry* myRegistry; |
| 114 | StringBufferTestOutput* output; |
| 115 | MockTest* test1; |
| 116 | MockTest* test2; |
| 117 | MockTest* test3; |
| 118 | MockTest* test4; |
| 119 | TestResult *result; |
| 120 | MockTestResult *mockResult; |
| 121 | void setup() |
| 122 | { |
| 123 | output = new StringBufferTestOutput(); |
| 124 | mockResult = new MockTestResult(*output); |
| 125 | result = mockResult; |
| 126 | test1 = new MockTest(); |
| 127 | test2 = new MockTest(); |
| 128 | test3 = new MockTest("group2"); |
| 129 | test4 = new MockTest(); |
| 130 | myRegistry = new TestRegistry(); |
| 131 | myRegistry->setCurrentRegistry(myRegistry); |
| 132 | } |
| 133 | |
| 134 | void teardown() |
| 135 | { |
| 136 | myRegistry->setCurrentRegistry(NULLPTR); |
| 137 | delete myRegistry; |
| 138 | delete test1; |
| 139 | delete test2; |
| 140 | delete test3; |
| 141 | delete test4; |
| 142 | delete result; |
| 143 | delete output; |
| 144 | } |
| 145 | |
| 146 | void addAndRunAllTests() |
| 147 | { |
| 148 | myRegistry->addTest(test1); |
| 149 | myRegistry->addTest(test2); |
| 150 | myRegistry->addTest(test3); |
| 151 | myRegistry->runAllTests(*result); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | TEST(TestRegistry, registryMyRegistryAndReset) |
| 156 | { |
| 157 | CHECK(myRegistry->getCurrentRegistry() == myRegistry); |
| 158 | } |
| 159 | |
| 160 | TEST(TestRegistry, emptyRegistryIsEmpty) |
| 161 | { |
| 162 | CHECK(myRegistry->countTests() == 0); |
| 163 | } |
| 164 | |
| 165 | TEST(TestRegistry, addOneTestIsNotEmpty) |
| 166 | { |
| 167 | myRegistry->addTest(test1); |
| 168 | CHECK(myRegistry->countTests() == 1); |
| 169 | } |
| 170 | |
| 171 | TEST(TestRegistry, addOneTwoTests) |
| 172 | { |
| 173 | myRegistry->addTest(test1); |
| 174 | myRegistry->addTest(test2); |
| 175 | CHECK(myRegistry->countTests() == 2); |
| 176 | } |
| 177 | |
| 178 | TEST(TestRegistry, runTwoTests) |
| 179 | { |
| 180 | myRegistry->addTest(test1); |
| 181 | myRegistry->addTest(test2); |
| 182 | CHECK(!test1->hasRun_); |
| 183 | CHECK(!test2->hasRun_); |
| 184 | myRegistry->runAllTests(*result); |
| 185 | CHECK(test1->hasRun_); |
| 186 | CHECK(test2->hasRun_); |
| 187 | } |
| 188 | |
| 189 | TEST(TestRegistry, runTwoTestsCheckResultFunctionsCalled) |
| 190 | { |
| 191 | myRegistry->addTest(test1); |
| 192 | myRegistry->addTest(test2); |
| 193 | myRegistry->runAllTests(*result); |
| 194 | LONGS_EQUAL(1, mockResult->countTestsStarted); |
| 195 | LONGS_EQUAL(1, mockResult->countTestsEnded); |
| 196 | LONGS_EQUAL(1, mockResult->countCurrentGroupStarted); |
| 197 | LONGS_EQUAL(1, mockResult->countCurrentGroupEnded); |
| 198 | LONGS_EQUAL(2, mockResult->countCurrentTestStarted); |
| 199 | LONGS_EQUAL(2, mockResult->countCurrentTestEnded); |
| 200 | } |
| 201 | |
| 202 | TEST(TestRegistry, runThreeTestsandTwoGroupsCheckResultFunctionsCalled) |
| 203 | { |
| 204 | addAndRunAllTests(); |
| 205 | LONGS_EQUAL(2, mockResult->countCurrentGroupStarted); |
| 206 | LONGS_EQUAL(2, mockResult->countCurrentGroupEnded); |
| 207 | LONGS_EQUAL(3, mockResult->countCurrentTestStarted); |
| 208 | LONGS_EQUAL(3, mockResult->countCurrentTestEnded); |
| 209 | } |
| 210 | |
| 211 | TEST(TestRegistry, unDoTest) |
| 212 | { |
| 213 | myRegistry->addTest(test1); |
| 214 | CHECK(myRegistry->countTests() == 1); |
| 215 | myRegistry->unDoLastAddTest(); |
| 216 | CHECK(myRegistry->countTests() == 0); |
| 217 | } |
| 218 | |
| 219 | TEST(TestRegistry, unDoButNoTest) |
| 220 | { |
| 221 | CHECK(myRegistry->countTests() == 0); |
| 222 | myRegistry->unDoLastAddTest(); |
| 223 | CHECK(myRegistry->countTests() == 0); |
| 224 | } |
| 225 | |
| 226 | TEST(TestRegistry, reallyUndoLastTest) |
| 227 | { |
| 228 | myRegistry->addTest(test1); |
| 229 | myRegistry->addTest(test2); |
| 230 | CHECK(myRegistry->countTests() == 2); |
| 231 | myRegistry->unDoLastAddTest(); |
| 232 | CHECK(myRegistry->countTests() == 1); |
| 233 | myRegistry->runAllTests(*result); |
| 234 | CHECK(test1->hasRun_); |
| 235 | CHECK(!test2->hasRun_); |
| 236 | } |
| 237 | |
| 238 | TEST(TestRegistry, findTestWithNameDoesntExist) |
| 239 | { |
| 240 | CHECK(myRegistry->findTestWithName("ThisTestDoesntExists") == NULLPTR); |
| 241 | } |
| 242 | |
| 243 | TEST(TestRegistry, findTestWithName) |
| 244 | { |
| 245 | test1->setTestName("NameOfATestThatDoesExist"); |
| 246 | test2->setTestName("SomeOtherTest"); |
| 247 | myRegistry->addTest(test1); |
| 248 | myRegistry->addTest(test2); |
| 249 | CHECK(myRegistry->findTestWithName("NameOfATestThatDoesExist") != NULLPTR); |
| 250 | } |
| 251 | |
| 252 | TEST(TestRegistry, findTestWithGroupDoesntExist) |
| 253 | { |
| 254 | CHECK(myRegistry->findTestWithGroup("ThisTestGroupDoesntExists") == NULLPTR); |
| 255 | } |
| 256 | |
| 257 | TEST(TestRegistry, findTestWithGroup) |
| 258 | { |
| 259 | test1->setGroupName("GroupOfATestThatDoesExist"); |
| 260 | test2->setGroupName("SomeOtherGroup"); |
| 261 | myRegistry->addTest(test1); |
| 262 | myRegistry->addTest(test2); |
| 263 | CHECK(myRegistry->findTestWithGroup("GroupOfATestThatDoesExist") != NULLPTR); |
| 264 | } |
| 265 | |
| 266 | TEST(TestRegistry, nameFilterWorks) |
| 267 | { |
| 268 | test1->setTestName("testname"); |
| 269 | test2->setTestName("noname"); |
| 270 | TestFilter nameFilter("testname"); |
| 271 | myRegistry->setNameFilters(&nameFilter); |
| 272 | addAndRunAllTests(); |
| 273 | CHECK(test1->hasRun_); |
| 274 | CHECK(!test2->hasRun_); |
| 275 | } |
| 276 | |
| 277 | TEST(TestRegistry, groupFilterWorks) |
| 278 | { |
| 279 | test1->setGroupName("groupname"); |
| 280 | test2->setGroupName("noname"); |
| 281 | TestFilter groupFilter("groupname"); |
| 282 | myRegistry->setGroupFilters(&groupFilter); |
| 283 | addAndRunAllTests(); |
| 284 | CHECK(test1->hasRun_); |
| 285 | CHECK(!test2->hasRun_); |
| 286 | } |
| 287 | |
| 288 | TEST(TestRegistry, runTestInSeperateProcess) |
| 289 | { |
| 290 | myRegistry->setRunTestsInSeperateProcess(); |
| 291 | myRegistry->addTest(test1); |
| 292 | myRegistry->runAllTests(*result); |
| 293 | CHECK(test1->isRunInSeperateProcess()); |
| 294 | } |
| 295 | |
| 296 | TEST(TestRegistry, CurrentRepetitionIsCorrectNone) |
| 297 | { |
| 298 | CHECK(0 == myRegistry->getCurrentRepetition()); |
| 299 | myRegistry->runAllTests(*result); |
| 300 | LONGS_EQUAL(1, myRegistry->getCurrentRepetition()); |
| 301 | } |
| 302 | |
| 303 | TEST(TestRegistry, CurrentRepetitionIsCorrectTwo) |
| 304 | { |
| 305 | CHECK(0 == myRegistry->getCurrentRepetition()); |
| 306 | myRegistry->runAllTests(*result); |
| 307 | myRegistry->runAllTests(*result); |
| 308 | LONGS_EQUAL(2, myRegistry->getCurrentRepetition()); |
| 309 | } |
| 310 | |
| 311 | class MyTestPluginDummy: public TestPlugin |
| 312 | { |
| 313 | public: |
| 314 | MyTestPluginDummy(const SimpleString& name) : TestPlugin(name) {} |
| 315 | virtual ~MyTestPluginDummy() _destructor_override {} |
| 316 | virtual void runAllPreTestAction(UtestShell&, TestResult&) _override {} |
| 317 | virtual void runAllPostTestAction(UtestShell&, TestResult&) _override {} |
| 318 | }; |
| 319 | |
| 320 | TEST(TestRegistry, ResetPluginsWorks) |
| 321 | { |
| 322 | MyTestPluginDummy plugin1("Plugin-1"); |
| 323 | MyTestPluginDummy plugin2("Plugin-2"); |
| 324 | myRegistry->installPlugin(&plugin1); |
| 325 | myRegistry->installPlugin(&plugin2); |
| 326 | LONGS_EQUAL(2, myRegistry->countPlugins()); |
| 327 | myRegistry->resetPlugins(); |
| 328 | LONGS_EQUAL(0, myRegistry->countPlugins()); |
| 329 | } |
| 330 | |
| 331 | TEST(TestRegistry, listTestGroupNames_shouldListBackwardsGroup1AfterGroup11AndGroup2OnlyOnce) |
| 332 | { |
| 333 | test1->setGroupName("GROUP_1"); |
| 334 | myRegistry->addTest(test1); |
| 335 | test2->setGroupName("GROUP_2"); |
| 336 | myRegistry->addTest(test2); |
| 337 | test3->setGroupName("GROUP_11"); |
| 338 | myRegistry->addTest(test3); |
| 339 | test4->setGroupName("GROUP_2"); |
| 340 | myRegistry->addTest(test4); |
| 341 | |
| 342 | myRegistry->listTestGroupNames(*result); |
| 343 | SimpleString s = output->getOutput(); |
| 344 | STRCMP_EQUAL("GROUP_2 GROUP_11 GROUP_1", s.asCharString()); |
| 345 | } |
| 346 | |
| 347 | TEST(TestRegistry, listTestGroupAndCaseNames_shouldListBackwardsGroupATestaAfterGroupAtestaa) |
| 348 | { |
| 349 | test1->setGroupName("GROUP_A"); |
| 350 | test1->setTestName("test_a"); |
| 351 | myRegistry->addTest(test1); |
| 352 | test2->setGroupName("GROUP_B"); |
| 353 | test2->setTestName("test_b"); |
| 354 | myRegistry->addTest(test2); |
| 355 | test3->setGroupName("GROUP_A"); |
| 356 | test3->setTestName("test_aa"); |
| 357 | myRegistry->addTest(test3); |
| 358 | |
| 359 | myRegistry->listTestGroupAndCaseNames(*result); |
| 360 | SimpleString s = output->getOutput(); |
| 361 | STRCMP_EQUAL("GROUP_A.test_aa GROUP_B.test_b GROUP_A.test_a", s.asCharString()); |
| 362 | } |
| 363 | |
| 364 | TEST(TestRegistry, shuffleEmptyListIsNoOp) |
| 365 | { |
| 366 | CHECK_TRUE(myRegistry->getFirstTest() == NULLPTR); |
| 367 | myRegistry->shuffleTests(0); |
| 368 | CHECK_TRUE(myRegistry->getFirstTest() == NULLPTR); |
| 369 | } |
| 370 | |
| 371 | TEST(TestRegistry, shuffleSingleTestIsNoOp) |
| 372 | { |
| 373 | myRegistry->addTest(test1); |
| 374 | myRegistry->shuffleTests(0); |
| 375 | CHECK_TRUE(myRegistry->getFirstTest() == test1); |
| 376 | } |
| 377 | |
| 378 | static int getZero() |
| 379 | { |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | IGNORE_TEST(TestRegistry, shuffleTestList) |
| 384 | { |
| 385 | UT_PTR_SET(PlatformSpecificRand, getZero); |
| 386 | myRegistry->addTest(test3); |
| 387 | myRegistry->addTest(test2); |
| 388 | myRegistry->addTest(test1); |
| 389 | |
| 390 | UtestShell* first_before = myRegistry->getFirstTest(); |
| 391 | UtestShell* second_before = first_before->getNext(); |
| 392 | UtestShell* third_before = second_before->getNext(); |
| 393 | |
| 394 | CHECK_TRUE(first_before == test1); |
| 395 | CHECK_TRUE(second_before == test2); |
| 396 | CHECK_TRUE(third_before == test3); |
| 397 | CHECK_TRUE(third_before->getNext() == NULLPTR); |
| 398 | |
| 399 | // shuffle always with element at index 0: [1] 2 [3] --> [3] [2] 1 --> 2 3 1 |
| 400 | myRegistry->shuffleTests(0); |
| 401 | |
| 402 | UtestShell* first_after = myRegistry->getFirstTest(); |
| 403 | UtestShell* second_after = first_after->getNext(); |
| 404 | UtestShell* third_after = second_after->getNext(); |
| 405 | |
| 406 | CHECK_TRUE(first_after == test2); |
| 407 | CHECK_TRUE(second_after == test3); |
| 408 | CHECK_TRUE(third_after == test1); |
| 409 | CHECK_TRUE(third_after->getNext() == NULLPTR); |
| 410 | } |
| 411 | |
| 412 | TEST(TestRegistry, reverseTests) |
| 413 | { |
| 414 | myRegistry->addTest(test1); |
| 415 | myRegistry->addTest(test2); |
| 416 | |
| 417 | myRegistry->reverseTests(); |
| 418 | |
| 419 | CHECK(test1 == myRegistry->getFirstTest()); |
| 420 | } |
| 421 | |
| 422 | TEST(TestRegistry, reverseZeroTests) |
| 423 | { |
| 424 | myRegistry->reverseTests(); |
| 425 | |
| 426 | CHECK(NULLPTR == myRegistry->getFirstTest()); |
| 427 | } |