?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/UnitTestPP.h"
  2. #include "RecordingReporter.h"
  3. #include "UnitTest++/ReportAssert.h"
  4. #include "UnitTest++/TestList.h"
  5. #include "UnitTest++/TimeHelpers.h"
  6. #include "UnitTest++/TimeConstraint.h"
  7. #include "UnitTest++/ReportAssertImpl.h"
  8.  
  9. using namespace UnitTest;
  10.  
  11. namespace
  12. {
  13.  
  14.    struct MockTest : public Test
  15.    {
  16.       MockTest(char const* testName, bool const success_, bool const assert_, int const count_ = 1)
  17.          : Test(testName)
  18.          , success(success_)
  19.          , asserted(assert_)
  20.          , count(count_)
  21.       {}
  22.  
  23.       virtual void RunImpl() const
  24.       {
  25.          TestResults& testResults_ = *CurrentTest::Results();
  26.  
  27.          for (int i=0; i < count; ++i)
  28.          {
  29.             if (asserted)
  30.             {
  31.                ReportAssert("desc", "file", 0);
  32.             }
  33.             else if (!success)
  34.             {
  35.                testResults_.OnTestFailure(m_details, "message");
  36.             }
  37.          }
  38.       }
  39.  
  40.       bool const success;
  41.       bool const asserted;
  42.       int const count;
  43.    };
  44.  
  45.    struct FixtureBase
  46.    {
  47.       FixtureBase()
  48.          : runner(reporter)
  49.       {}
  50.  
  51.       template <class Predicate>
  52.       int RunTestsIf(TestList const& list, char const* suiteName,
  53.                      const Predicate& predicate, int maxTestTimeInMs)
  54.       {
  55.          TestResults* oldResults = CurrentTest::Results();
  56.          const TestDetails* oldDetails = CurrentTest::Details();
  57.          int result = runner.RunTestsIf(list, suiteName, predicate, maxTestTimeInMs);
  58.          CurrentTest::Results() = oldResults;
  59.          CurrentTest::Details() = oldDetails;
  60.          return result;
  61.       }
  62.  
  63.       TestRunner runner;
  64.       RecordingReporter reporter;
  65.    };
  66.  
  67.    struct TestRunnerFixture : public FixtureBase
  68.    {
  69.       TestList list;
  70.    };
  71.  
  72.    TEST_FIXTURE(TestRunnerFixture, TestStartIsReportedCorrectly)
  73.    {
  74.       MockTest test("goodtest", true, false);
  75.       list.Add(&test);
  76.  
  77.       RunTestsIf(list, NULL, True(), 0);
  78.       CHECK_EQUAL(1, reporter.testRunCount);
  79.       CHECK_EQUAL("goodtest", reporter.lastStartedTest);
  80.    }
  81.  
  82.    TEST_FIXTURE(TestRunnerFixture, TestFinishIsReportedCorrectly)
  83.    {
  84.       MockTest test("goodtest", true, false);
  85.       list.Add(&test);
  86.  
  87.       RunTestsIf(list, NULL, True(), 0);
  88.       CHECK_EQUAL(1, reporter.testFinishedCount);
  89.       CHECK_EQUAL("goodtest", reporter.lastFinishedTest);
  90.    }
  91.  
  92.    class SlowTest : public Test
  93.    {
  94.    public:
  95.       SlowTest()
  96.          : Test("slow", "somesuite", "filename", 123)
  97.       {}
  98.  
  99.       virtual void RunImpl() const
  100.       {
  101.          TimeHelpers::SleepMs(20);
  102.       }
  103.    };
  104.  
  105.    TEST_FIXTURE(TestRunnerFixture, TestFinishIsCalledWithCorrectTime)
  106.    {
  107.       SlowTest test;
  108.       list.Add(&test);
  109.  
  110.       // Using UnitTest::Timer here is arguably a bit hokey and self-referential, but
  111.       // it should guarantee that the test time recorded is less than that plus the
  112.       // overhead of RunTestsIf -- the only thing we can reliably assert without
  113.       // reworking the test to not use sleeps at all
  114.       Timer actual;
  115.       actual.Start();
  116.       RunTestsIf(list, NULL, True(), 0);
  117.  
  118.       CHECK(reporter.lastFinishedTestTime >= 0.005f && reporter.lastFinishedTestTime <= actual.GetTimeInMs());
  119.    }
  120.  
  121.    TEST_FIXTURE(TestRunnerFixture, FailureCountIsZeroWhenNoTestsAreRun)
  122.    {
  123.       CHECK_EQUAL(0, RunTestsIf(list, NULL, True(), 0));
  124.       CHECK_EQUAL(0, reporter.testRunCount);
  125.       CHECK_EQUAL(0, reporter.testFailedCount);
  126.    }
  127.  
  128.    TEST_FIXTURE(TestRunnerFixture, CallsReportFailureOncePerFailingTest)
  129.    {
  130.       MockTest test1("test", false, false);
  131.       list.Add(&test1);
  132.       MockTest test2("test", true, false);
  133.       list.Add(&test2);
  134.       MockTest test3("test", false, false);
  135.       list.Add(&test3);
  136.  
  137.       CHECK_EQUAL(2, RunTestsIf(list, NULL, True(), 0));
  138.       CHECK_EQUAL(2, reporter.testFailedCount);
  139.    }
  140.  
  141.    TEST_FIXTURE(TestRunnerFixture, TestsThatAssertAreReportedAsFailing)
  142.    {
  143.       MockTest test("test", true, true);
  144.       list.Add(&test);
  145.  
  146.       RunTestsIf(list, NULL, True(), 0);
  147.       CHECK_EQUAL(1, reporter.testFailedCount);
  148.    }
  149.  
  150.  
  151.    TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfTestCount)
  152.    {
  153.       MockTest test1("test", true, false);
  154.       MockTest test2("test", true, false);
  155.       MockTest test3("test", true, false);
  156.       list.Add(&test1);
  157.       list.Add(&test2);
  158.       list.Add(&test3);
  159.  
  160.       RunTestsIf(list, NULL, True(), 0);
  161.       CHECK_EQUAL(3, reporter.summaryTotalTestCount);
  162.    }
  163.  
  164.    TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailedTests)
  165.    {
  166.       MockTest test1("test", false, false, 2);
  167.       MockTest test2("test", true, false);
  168.       MockTest test3("test", false, false, 3);
  169.       list.Add(&test1);
  170.       list.Add(&test2);
  171.       list.Add(&test3);
  172.  
  173.       RunTestsIf(list, NULL, True(), 0);
  174.       CHECK_EQUAL(2, reporter.summaryFailedTestCount);
  175.    }
  176.  
  177.    TEST_FIXTURE(TestRunnerFixture, ReporterNotifiedOfFailures)
  178.    {
  179.       MockTest test1("test", false, false, 2);
  180.       MockTest test2("test", true, false);
  181.       MockTest test3("test", false, false, 3);
  182.       list.Add(&test1);
  183.       list.Add(&test2);
  184.       list.Add(&test3);
  185.  
  186.       RunTestsIf(list, NULL, True(), 0);
  187.       CHECK_EQUAL(5, reporter.summaryFailureCount);
  188.    }
  189.  
  190.    TEST_FIXTURE(TestRunnerFixture, SlowTestPassesForHighTimeThreshold)
  191.    {
  192.       SlowTest test;
  193.       list.Add(&test);
  194.  
  195.       RunTestsIf(list, NULL, True(), 0);
  196.       CHECK_EQUAL(0, reporter.testFailedCount);
  197.    }
  198.  
  199.    TEST_FIXTURE(TestRunnerFixture, SlowTestFailsForLowTimeThreshold)
  200.    {
  201.       SlowTest test;
  202.       list.Add(&test);
  203.  
  204.       RunTestsIf(list, NULL, True(), 3);
  205.       CHECK_EQUAL(1, reporter.testFailedCount);
  206.    }
  207.  
  208.    TEST_FIXTURE(TestRunnerFixture, SlowTestHasCorrectFailureInformation)
  209.    {
  210.       SlowTest test;
  211.       list.Add(&test);
  212.  
  213.       RunTestsIf(list, NULL, True(), 3);
  214.  
  215.       using namespace std;
  216.  
  217.       CHECK_EQUAL(test.m_details.testName, reporter.lastFailedTest);
  218.       CHECK(strstr(test.m_details.filename, reporter.lastFailedFile));
  219.       CHECK_EQUAL(test.m_details.lineNumber, reporter.lastFailedLine);
  220.  
  221.       CHECK(strstr(reporter.lastFailedMessage, "Global time constraint failed"));
  222.       CHECK(strstr(reporter.lastFailedMessage, "3ms"));
  223.    }
  224.  
  225.    TEST_FIXTURE(TestRunnerFixture, SlowTestWithTimeExemptionPasses)
  226.    {
  227.       class SlowExemptedTest : public Test
  228.       {
  229.       public:
  230.          SlowExemptedTest() : Test("slowexempted", "", 0) {}
  231.          virtual void RunImpl() const
  232.          {
  233.             UNITTEST_TIME_CONSTRAINT_EXEMPT();
  234.             TimeHelpers::SleepMs(20);
  235.          }
  236.       };
  237.  
  238.       SlowExemptedTest test;
  239.       list.Add(&test);
  240.  
  241.       RunTestsIf(list, NULL, True(), 3);
  242.       CHECK_EQUAL(0, reporter.testFailedCount);
  243.    }
  244.  
  245.    struct TestSuiteFixture : FixtureBase
  246.    {
  247.       TestSuiteFixture()
  248.          : test1("TestInDefaultSuite")
  249.          , test2("TestInOtherSuite", "OtherSuite")
  250.          , test3("SecondTestInDefaultSuite")
  251.       {
  252.          list.Add(&test1);
  253.          list.Add(&test2);
  254.       }
  255.  
  256.       Test test1;
  257.       Test test2;
  258.       Test test3;
  259.       TestList list;
  260.    };
  261.  
  262.    TEST_FIXTURE(TestSuiteFixture, TestRunnerRunsAllSuitesIfNullSuiteIsPassed)
  263.    {
  264.       RunTestsIf(list, NULL, True(), 0);
  265.       CHECK_EQUAL(2, reporter.summaryTotalTestCount);
  266.    }
  267.  
  268.    TEST_FIXTURE(TestSuiteFixture,TestRunnerRunsOnlySpecifiedSuite)
  269.    {
  270.       RunTestsIf(list, "OtherSuite", True(), 0);
  271.       CHECK_EQUAL(1, reporter.summaryTotalTestCount);
  272.       CHECK_EQUAL("TestInOtherSuite", reporter.lastFinishedTest);
  273.    }
  274.  
  275.    struct RunTestIfNameIs
  276.    {
  277.       RunTestIfNameIs(char const* name_)
  278.          : name(name_)
  279.       {}
  280.  
  281.       bool operator()(const Test* const test) const
  282.       {
  283.          using namespace std;
  284.          return (0 == strcmp(test->m_details.testName, name));
  285.       }
  286.  
  287.       char const* name;
  288.    };
  289.  
  290.    TEST(TestMockPredicateBehavesCorrectly)
  291.    {
  292.       RunTestIfNameIs predicate("pass");
  293.  
  294.       Test pass("pass");
  295.       Test fail("fail");
  296.  
  297.       CHECK(predicate(&pass));
  298.       CHECK(!predicate(&fail));
  299.    }
  300.  
  301.    TEST_FIXTURE(TestRunnerFixture, TestRunnerRunsTestsThatPassPredicate)
  302.    {
  303.       Test should_run("goodtest");
  304.       list.Add(&should_run);
  305.  
  306.       Test should_not_run("badtest");
  307.       list.Add(&should_not_run);
  308.  
  309.       RunTestsIf(list, NULL, RunTestIfNameIs("goodtest"), 0);
  310.       CHECK_EQUAL(1, reporter.testRunCount);
  311.       CHECK_EQUAL("goodtest", reporter.lastStartedTest);
  312.    }
  313.  
  314.    TEST_FIXTURE(TestRunnerFixture, TestRunnerOnlyRunsTestsInSpecifiedSuiteAndThatPassPredicate)
  315.    {
  316.       Test runningTest1("goodtest", "suite");
  317.       Test skippedTest2("goodtest");
  318.       Test skippedTest3("badtest", "suite");
  319.       Test skippedTest4("badtest");
  320.  
  321.       list.Add(&runningTest1);
  322.       list.Add(&skippedTest2);
  323.       list.Add(&skippedTest3);
  324.       list.Add(&skippedTest4);
  325.  
  326.       RunTestsIf(list, "suite", RunTestIfNameIs("goodtest"), 0);
  327.  
  328.       CHECK_EQUAL(1, reporter.testRunCount);
  329.       CHECK_EQUAL("goodtest", reporter.lastStartedTest);
  330.       CHECK_EQUAL("suite", reporter.lastStartedSuite);
  331.    }
  332.  
  333. }
  334.