?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "UnitTest++/UnitTestPP.h"
  2. #include "UnitTest++/TestMacros.h"
  3. #include "UnitTest++/TestList.h"
  4. #include "UnitTest++/TestResults.h"
  5. #include "UnitTest++/TestReporter.h"
  6. #include "UnitTest++/ReportAssert.h"
  7. #include "RecordingReporter.h"
  8. #include "ScopedCurrentTest.h"
  9.  
  10. using namespace UnitTest;
  11. using namespace std;
  12.  
  13. /* test for c++11 support */
  14. #ifndef _MSC_VER
  15.  
  16.    /* Test for clang >= 3.3 */
  17.    #ifdef __clang__
  18.       #if (__clang__ == 1) && (__clang_major__ > 3 || (__clang_major__ == 3 && (__clang_minor__ > 2 )))
  19.          #define _NOEXCEPT_OP(x) noexcept(x)
  20.       #else
  21.          #define _NOEXCEPT_OP(x)
  22.       #endif
  23.    #endif
  24.  
  25.    #ifndef __clang__
  26.       /* Test for GCC >= 4.8.0 */
  27.       #ifdef __GNUC__
  28.          #if (__GNUC__ > 4) || (__GNUC__ == 4 && (__GNUC_MINOR__ > 7 ))
  29.             #define _NOEXCEPT_OP(x) noexcept(x)
  30.          #else
  31.             #define _NOEXCEPT_OP(x)
  32.          #endif
  33.       #endif
  34.    #endif
  35.  
  36. #elif _MSC_VER
  37.  
  38.    #if (_MSC_VER > 1800)
  39.       #define _NOEXCEPT_OP(x) noexcept(x)
  40.    #else
  41.       #define _NOEXCEPT_OP(x)
  42.    #endif
  43.  
  44. #endif
  45.  
  46. namespace {
  47.  
  48.    TestList list1;
  49.    UNITTEST_IMPL_TEST(DummyTest, list1)
  50.    {}
  51.  
  52.    TEST (TestsAreAddedToTheListThroughMacro)
  53.    {
  54.       CHECK(list1.GetHead() != 0);
  55.       CHECK(list1.GetHead()->m_nextTest == 0);
  56.    }
  57.  
  58. #ifndef UNITTEST_NO_EXCEPTIONS
  59.  
  60.    struct ThrowingThingie
  61.    {
  62.       ThrowingThingie() : dummy(false)
  63.       {
  64.          if (!dummy)
  65.             throw "Oops";
  66.       }
  67.  
  68.       bool dummy;
  69.    };
  70.  
  71.    TestList list2;
  72.    UNITTEST_IMPL_TEST_FIXTURE(ThrowingThingie, DummyTestName, list2)
  73.    {}
  74.  
  75.    TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture)
  76.    {
  77.       RecordingReporter reporter;
  78.       TestResults result(&reporter);
  79.       {
  80.          ScopedCurrentTest scopedResults(result);
  81.          list2.GetHead()->Run();
  82.       }
  83.  
  84.       CHECK(strstr(reporter.lastFailedMessage, "xception"));
  85.       CHECK(strstr(reporter.lastFailedMessage, "fixture"));
  86.       CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie"));
  87.    }
  88.  
  89. #endif
  90.  
  91.    struct DummyFixture
  92.    {
  93.       int x;
  94.    };
  95.  
  96. // We're really testing the macros so we just want them to compile and link
  97.    SUITE(TestSuite1)
  98.    {
  99.       TEST(SimilarlyNamedTestsInDifferentSuitesWork)
  100.       {}
  101.  
  102.       TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork)
  103.       {}
  104.    }
  105.  
  106.    SUITE(TestSuite2)
  107.    {
  108.       TEST(SimilarlyNamedTestsInDifferentSuitesWork)
  109.       {}
  110.  
  111.       TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork)
  112.       {}
  113.    }
  114.  
  115.    TestList macroTestList1;
  116.    UNITTEST_IMPL_TEST(MacroTestHelper1, macroTestList1)
  117.    {}
  118.  
  119.    TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite)
  120.    {
  121.       CHECK(macroTestList1.GetHead() != NULL);
  122.       CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName);
  123.       CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName);
  124.    }
  125.  
  126.    TestList macroTestList2;
  127.    UNITTEST_IMPL_TEST_FIXTURE(DummyFixture, MacroTestHelper2, macroTestList2)
  128.    {}
  129.  
  130.    TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite)
  131.    {
  132.       CHECK(macroTestList2.GetHead() != NULL);
  133.       CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName);
  134.       CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName);
  135.    }
  136.  
  137. #ifndef UNITTEST_NO_EXCEPTIONS
  138.  
  139.    struct FixtureCtorThrows
  140.    {
  141.       FixtureCtorThrows() {
  142.          throw "exception";
  143.       }
  144.    };
  145.  
  146.    TestList throwingFixtureTestList1;
  147.    UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1)
  148.    {}
  149.  
  150.    TEST(FixturesWithThrowingCtorsAreFailures)
  151.    {
  152.       CHECK(throwingFixtureTestList1.GetHead() != NULL);
  153.       RecordingReporter reporter;
  154.       TestResults result(&reporter);
  155.       {
  156.          ScopedCurrentTest scopedResult(result);
  157.          throwingFixtureTestList1.GetHead()->Run();
  158.       }
  159.  
  160.       int const failureCount = result.GetFailedTestCount();
  161.       CHECK_EQUAL(1, failureCount);
  162.       CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture"));
  163.    }
  164.  
  165.    struct FixtureDtorThrows
  166.    {
  167.       ~FixtureDtorThrows() _NOEXCEPT_OP(false) {
  168.          throw "exception";
  169.       }
  170.    };
  171.  
  172.    TestList throwingFixtureTestList2;
  173.    UNITTEST_IMPL_TEST_FIXTURE(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2)
  174.    {}
  175.  
  176.    TEST(FixturesWithThrowingDtorsAreFailures)
  177.    {
  178.       CHECK(throwingFixtureTestList2.GetHead() != NULL);
  179.  
  180.       RecordingReporter reporter;
  181.       TestResults result(&reporter);
  182.       {
  183.          ScopedCurrentTest scopedResult(result);
  184.          throwingFixtureTestList2.GetHead()->Run();
  185.       }
  186.  
  187.       int const failureCount = result.GetFailedTestCount();
  188.       CHECK_EQUAL(1, failureCount);
  189.       CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture"));
  190.    }
  191.  
  192.    const int FailingLine = 123;
  193.  
  194.    struct FixtureCtorAsserts
  195.    {
  196.       FixtureCtorAsserts()
  197.       {
  198.          UnitTest::ReportAssert("assert failure", "file", FailingLine);
  199.       }
  200.    };
  201.  
  202.    TestList ctorAssertFixtureTestList;
  203.    UNITTEST_IMPL_TEST_FIXTURE(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList)
  204.    {}
  205.  
  206.    TEST(CorrectlyReportsFixturesWithCtorsThatAssert)
  207.    {
  208.       RecordingReporter reporter;
  209.       TestResults result(&reporter);
  210.       {
  211.          ScopedCurrentTest scopedResults(result);
  212.          ctorAssertFixtureTestList.GetHead()->Run();
  213.       }
  214.  
  215.       const int failureCount = result.GetFailedTestCount();
  216.       CHECK_EQUAL(1, failureCount);
  217.       CHECK_EQUAL(FailingLine, reporter.lastFailedLine);
  218.       CHECK(strstr(reporter.lastFailedMessage, "assert failure"));
  219.    }
  220.  
  221. #endif
  222.  
  223. }
  224.  
  225. // We're really testing if it's possible to use the same suite in two files
  226. // to compile and link successfuly (TestTestSuite.cpp has suite with the same name)
  227. // Note: we are outside of the anonymous namespace
  228. SUITE(SameTestSuite)
  229. {
  230.    TEST(DummyTest1)
  231.    {}
  232. }
  233.  
  234. #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo
  235. #define INNER_STRINGIFY(X) #X
  236. #define STRINGIFY(X) INNER_STRINGIFY(X)
  237.  
  238. TEST(CUR_TEST_NAME)
  239. {
  240.    const UnitTest::TestDetails* details = CurrentTest::Details();
  241.    CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName);
  242. }
  243.  
  244. #undef CUR_TEST_NAME
  245. #undef INNER_STRINGIFY
  246. #undef STRINGIFY
  247.