?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. #include "UnitTest++/Config.h"
  2. #ifndef UNITTEST_NO_EXCEPTIONS
  3.  
  4. #include "UnitTest++/UnitTestPP.h"
  5. #include "UnitTest++/CurrentTest.h"
  6. #include "RecordingReporter.h"
  7. #include "ScopedCurrentTest.h"
  8.  
  9. #include <stdexcept>
  10.  
  11. using namespace std;
  12.  
  13. namespace {
  14.  
  15.    int ThrowingFunction()
  16.    {
  17.       throw "Doh";
  18.    }
  19.  
  20.    int ThrowingStdExceptionFunction()
  21.    {
  22.       throw std::logic_error("Doh");
  23.    }
  24.  
  25.    SUITE(CheckExceptionTests)
  26.    {
  27.       struct CheckFixture
  28.       {
  29.          CheckFixture()
  30.             : reporter()
  31.             , testResults(&reporter)
  32.          {}
  33.  
  34.          void PerformCheckWithNonStdThrow()
  35.          {
  36.             ScopedCurrentTest scopedResults(testResults);
  37.             CHECK(ThrowingFunction() == 1);
  38.          }
  39.  
  40.          void PerformCheckWithStdThrow()
  41.          {
  42.             ScopedCurrentTest scopedResults(testResults);
  43.             CHECK(ThrowingStdExceptionFunction() == 1);
  44.          }
  45.  
  46.          RecordingReporter reporter;
  47.          UnitTest::TestResults testResults;
  48.       };
  49.  
  50.       TEST_FIXTURE(CheckFixture, CheckFailsOnException)
  51.       {
  52.          PerformCheckWithNonStdThrow();
  53.          CHECK(testResults.GetFailureCount() > 0);
  54.       }
  55.  
  56.       TEST_FIXTURE(CheckFixture, CheckFailsOnStdException)
  57.       {
  58.          PerformCheckWithStdThrow();
  59.          CHECK(testResults.GetFailureCount() > 0);
  60.       }
  61.  
  62.       TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfExceptionIncludesCheckContents)
  63.       {
  64.          PerformCheckWithNonStdThrow();
  65.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction() == 1"));
  66.       }
  67.  
  68.       TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStdExceptionIncludesCheckContents)
  69.       {
  70.          PerformCheckWithStdThrow();
  71.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction() == 1"));
  72.       }
  73.  
  74.       TEST_FIXTURE(CheckFixture, CheckFailureBecauseOfStandardExceptionIncludesWhat)
  75.       {
  76.          PerformCheckWithStdThrow();
  77.          CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
  78.       }
  79.    }
  80.  
  81.    SUITE(CheckEqualExceptionTests)
  82.    {
  83.       struct CheckEqualFixture
  84.       {
  85.          CheckEqualFixture()
  86.             : reporter()
  87.             , testResults(&reporter)
  88.             , line(-1)
  89.          {}
  90.  
  91.          void PerformCheckWithNonStdThrow()
  92.          {
  93.             UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1);
  94.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  95.             CHECK_EQUAL(ThrowingFunction(), 123); line = __LINE__;
  96.          }
  97.  
  98.          void PerformCheckWithStdThrow()
  99.          {
  100.             UnitTest::TestDetails const testDetails("testName", "suiteName", "filename", -1);
  101.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  102.             CHECK_EQUAL(ThrowingStdExceptionFunction(), 123); line = __LINE__;
  103.          }
  104.  
  105.          RecordingReporter reporter;
  106.          UnitTest::TestResults testResults;
  107.          int line;
  108.       };
  109.  
  110.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnException)
  111.       {
  112.          PerformCheckWithNonStdThrow();
  113.          CHECK(testResults.GetFailureCount() > 0);
  114.       }
  115.  
  116.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailsOnStdException)
  117.       {
  118.          PerformCheckWithStdThrow();
  119.          CHECK(testResults.GetFailureCount() > 0);
  120.       }
  121.  
  122.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionContainsCorrectDetails)
  123.       {
  124.          PerformCheckWithNonStdThrow();
  125.  
  126.          CHECK_EQUAL("testName", reporter.lastFailedTest);
  127.          CHECK_EQUAL("suiteName", reporter.lastFailedSuite);
  128.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  129.          CHECK_EQUAL(line, reporter.lastFailedLine);
  130.       }
  131.  
  132.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionContainsCorrectDetails)
  133.       {
  134.          PerformCheckWithStdThrow();
  135.  
  136.          CHECK_EQUAL("testName", reporter.lastFailedTest);
  137.          CHECK_EQUAL("suiteName", reporter.lastFailedSuite);
  138.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  139.          CHECK_EQUAL(line, reporter.lastFailedLine);
  140.       }
  141.  
  142.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfExceptionIncludesCheckContents)
  143.       {
  144.          PerformCheckWithNonStdThrow();
  145.  
  146.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingFunction()"));
  147.          CHECK(strstr(reporter.lastFailedMessage, "123"));
  148.       }
  149.  
  150.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStdExceptionIncludesCheckContents)
  151.       {
  152.          PerformCheckWithStdThrow();
  153.  
  154.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingStdExceptionFunction()"));
  155.          CHECK(strstr(reporter.lastFailedMessage, "123"));
  156.       }
  157.  
  158.       TEST_FIXTURE(CheckEqualFixture, CheckEqualFailureBecauseOfStandardExceptionIncludesWhat)
  159.       {
  160.          PerformCheckWithStdThrow();
  161.  
  162.          CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
  163.       }
  164.    }
  165.  
  166.    SUITE(CheckCloseExceptionTests)
  167.    {
  168.       struct CheckCloseFixture
  169.       {
  170.          CheckCloseFixture()
  171.             : reporter()
  172.             , testResults(&reporter)
  173.             , line(-1)
  174.          {}
  175.  
  176.          void PerformCheckWithNonStdThrow()
  177.          {
  178.             UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1);
  179.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  180.             CHECK_CLOSE(static_cast<float>(ThrowingFunction()), 1.0001f, 0.1f); line = __LINE__;
  181.          }
  182.  
  183.          void PerformCheckWithStdThrow()
  184.          {
  185.             UnitTest::TestDetails const testDetails("closeTest", "closeSuite", "filename", -1);
  186.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  187.             CHECK_CLOSE(static_cast<float>(ThrowingStdExceptionFunction()), 1.0001f, 0.1f); line = __LINE__;
  188.          }
  189.  
  190.          RecordingReporter reporter;
  191.          UnitTest::TestResults testResults;
  192.          int line;
  193.       };
  194.  
  195.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnException)
  196.       {
  197.          PerformCheckWithNonStdThrow();
  198.  
  199.          CHECK(testResults.GetFailureCount() > 0);
  200.       }
  201.  
  202.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailsOnStdException)
  203.       {
  204.          PerformCheckWithStdThrow();
  205.  
  206.          CHECK(testResults.GetFailureCount() > 0);
  207.       }
  208.  
  209.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionContainsCorrectDetails)
  210.       {
  211.          PerformCheckWithNonStdThrow();
  212.  
  213.          CHECK_EQUAL("closeTest", reporter.lastFailedTest);
  214.          CHECK_EQUAL("closeSuite", reporter.lastFailedSuite);
  215.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  216.          CHECK_EQUAL(line, reporter.lastFailedLine);
  217.       }
  218.  
  219.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionContainsCorrectDetails)
  220.       {
  221.          PerformCheckWithStdThrow();
  222.  
  223.          CHECK_EQUAL("closeTest", reporter.lastFailedTest);
  224.          CHECK_EQUAL("closeSuite", reporter.lastFailedSuite);
  225.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  226.          CHECK_EQUAL(line, reporter.lastFailedLine);
  227.       }
  228.  
  229.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfExceptionIncludesCheckContents)
  230.       {
  231.          PerformCheckWithNonStdThrow();
  232.  
  233.          CHECK(strstr(reporter.lastFailedMessage, "static_cast<float>(ThrowingFunction())"));
  234.          CHECK(strstr(reporter.lastFailedMessage, "1.0001f"));
  235.       }
  236.  
  237.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStdExceptionIncludesCheckContents)
  238.       {
  239.          PerformCheckWithStdThrow();
  240.  
  241.          CHECK(strstr(reporter.lastFailedMessage, "static_cast<float>(ThrowingStdExceptionFunction())"));
  242.          CHECK(strstr(reporter.lastFailedMessage, "1.0001f"));
  243.       }
  244.  
  245.       TEST_FIXTURE(CheckCloseFixture, CheckCloseFailureBecauseOfStandardExceptionIncludesWhat)
  246.       {
  247.          PerformCheckWithStdThrow();
  248.  
  249.          CHECK(strstr(reporter.lastFailedMessage, "exception (Doh)"));
  250.       }
  251.    }
  252.  
  253.    class ThrowingObject
  254.    {
  255.    public:
  256.       float operator[](size_t) const
  257.       {
  258.          throw "Test throw";
  259.       }
  260.    };
  261.  
  262.    class StdThrowingObject
  263.    {
  264.    public:
  265.       float operator[](size_t) const
  266.       {
  267.          throw std::runtime_error("Test throw");
  268.       }
  269.    };
  270.  
  271.    SUITE(CheckArrayCloseExceptionTests)
  272.    {
  273.       struct CheckArrayCloseFixture
  274.       {
  275.          CheckArrayCloseFixture()
  276.             : reporter()
  277.             , testResults(&reporter)
  278.             , line(-1)
  279.          {}
  280.  
  281.          void PerformCheckWithNonStdThrow()
  282.          {
  283.             UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1);
  284.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  285.             int const data[4] = { 0, 1, 2, 3 };
  286.             CHECK_ARRAY_CLOSE(data, ThrowingObject(), 4, 0.01f); line = __LINE__;
  287.          }
  288.  
  289.          void PerformCheckWithStdThrow()
  290.          {
  291.             UnitTest::TestDetails const testDetails("arrayCloseTest", "arrayCloseSuite", "filename", -1);
  292.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  293.             int const data[4] = { 0, 1, 2, 3 };
  294.             CHECK_ARRAY_CLOSE(data, StdThrowingObject(), 4, 0.01f); line = __LINE__;
  295.          }
  296.  
  297.          RecordingReporter reporter;
  298.          UnitTest::TestResults testResults;
  299.          int line;
  300.       };
  301.  
  302.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
  303.       {
  304.          PerformCheckWithNonStdThrow();
  305.  
  306.          CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
  307.          CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
  308.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  309.          CHECK_EQUAL(line, reporter.lastFailedLine);
  310.       }
  311.  
  312.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
  313.       {
  314.          PerformCheckWithStdThrow();
  315.  
  316.          CHECK_EQUAL("arrayCloseTest", reporter.lastFailedTest);
  317.          CHECK_EQUAL("arrayCloseSuite", reporter.lastFailedSuite);
  318.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  319.          CHECK_EQUAL(line, reporter.lastFailedLine);
  320.       }
  321.  
  322.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnException)
  323.       {
  324.          PerformCheckWithNonStdThrow();
  325.  
  326.          CHECK(testResults.GetFailureCount() > 0);
  327.       }
  328.  
  329.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailsOnStdException)
  330.       {
  331.          PerformCheckWithStdThrow();
  332.  
  333.          CHECK(testResults.GetFailureCount() > 0);
  334.       }
  335.  
  336.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnExceptionIncludesCheckContents)
  337.       {
  338.          PerformCheckWithNonStdThrow();
  339.  
  340.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  341.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()"));
  342.       }
  343.  
  344.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents)
  345.       {
  346.          PerformCheckWithStdThrow();
  347.  
  348.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  349.          CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()"));
  350.       }
  351.  
  352.       TEST_FIXTURE(CheckArrayCloseFixture, CheckFailureOnStdExceptionIncludesWhat)
  353.       {
  354.          PerformCheckWithStdThrow();
  355.  
  356.          CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
  357.       }
  358.    }
  359.  
  360.    SUITE(CheckArrayEqualExceptionTests)
  361.    {
  362.       struct CheckArrayEqualFixture
  363.       {
  364.          CheckArrayEqualFixture()
  365.             : reporter()
  366.             , testResults(&reporter)
  367.             , line(-1)
  368.          {}
  369.  
  370.          void PerformCheckWithNonStdThrow()
  371.          {
  372.             UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1);
  373.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  374.             int const data[4] = { 0, 1, 2, 3 };
  375.             CHECK_ARRAY_EQUAL(data, ThrowingObject(), 4); line = __LINE__;
  376.          }
  377.  
  378.          void PerformCheckWithStdThrow()
  379.          {
  380.             UnitTest::TestDetails const testDetails("arrayEqualTest", "arrayEqualSuite", "filename", -1);
  381.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  382.             int const data[4] = { 0, 1, 2, 3 };
  383.             CHECK_ARRAY_EQUAL(data, StdThrowingObject(), 4); line = __LINE__;
  384.          }
  385.  
  386.          RecordingReporter reporter;
  387.          UnitTest::TestResults testResults;
  388.          int line;
  389.       };
  390.  
  391.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
  392.       {
  393.          PerformCheckWithNonStdThrow();
  394.  
  395.          CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest);
  396.          CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite);
  397.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  398.          CHECK_EQUAL(line, reporter.lastFailedLine);
  399.       }
  400.  
  401.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
  402.       {
  403.          PerformCheckWithStdThrow();
  404.  
  405.          CHECK_EQUAL("arrayEqualTest", reporter.lastFailedTest);
  406.          CHECK_EQUAL("arrayEqualSuite", reporter.lastFailedSuite);
  407.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  408.          CHECK_EQUAL(line, reporter.lastFailedLine);
  409.       }
  410.  
  411.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnException)
  412.       {
  413.          PerformCheckWithNonStdThrow();
  414.  
  415.          CHECK(testResults.GetFailureCount() > 0);
  416.       }
  417.  
  418.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailsOnStdException)
  419.       {
  420.          PerformCheckWithStdThrow();
  421.  
  422.          CHECK(testResults.GetFailureCount() > 0);
  423.       }
  424.  
  425.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnExceptionIncludesCheckContents)
  426.       {
  427.          PerformCheckWithNonStdThrow();
  428.  
  429.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  430.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject()"));
  431.       }
  432.  
  433.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesCheckContents)
  434.       {
  435.          PerformCheckWithStdThrow();
  436.  
  437.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  438.          CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject()"));
  439.       }
  440.  
  441.       TEST_FIXTURE(CheckArrayEqualFixture, CheckFailureOnStdExceptionIncludesWhat)
  442.       {
  443.          PerformCheckWithStdThrow();
  444.  
  445.          CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
  446.       }
  447.    }
  448.  
  449.    SUITE(CheckArray2DExceptionTests)
  450.    {
  451.       class ThrowingObject2D
  452.       {
  453.       public:
  454.          float* operator[](size_t) const
  455.          {
  456.             throw "Test throw";
  457.          }
  458.       };
  459.  
  460.       class StdThrowingObject2D
  461.       {
  462.       public:
  463.          float* operator[](size_t) const
  464.          {
  465.             throw std::runtime_error("Test throw");
  466.          }
  467.       };
  468.  
  469.       struct CheckArray2DCloseFixture
  470.       {
  471.          CheckArray2DCloseFixture()
  472.             : reporter()
  473.             , testResults(&reporter)
  474.             , line(-1)
  475.          {}
  476.  
  477.          void PerformCheckWithNonStdThrow()
  478.          {
  479.             UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1);
  480.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  481.             const float data[2][2] = { {0, 1}, {2, 3} };
  482.             CHECK_ARRAY2D_CLOSE(data, ThrowingObject2D(), 2, 2, 0.01f); line = __LINE__;
  483.          }
  484.  
  485.          void PerformCheckWithStdThrow()
  486.          {
  487.             UnitTest::TestDetails const testDetails("array2DCloseTest", "array2DCloseSuite", "filename", -1);
  488.             ScopedCurrentTest scopedResults(testResults, &testDetails);
  489.             const float data[2][2] = { {0, 1}, {2, 3} };
  490.             CHECK_ARRAY2D_CLOSE(data, StdThrowingObject2D(), 2, 2, 0.01f); line = __LINE__;
  491.          }
  492.  
  493.          RecordingReporter reporter;
  494.          UnitTest::TestResults testResults;
  495.          int line;
  496.       };
  497.  
  498.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfExceptionContainsCorrectDetails)
  499.       {
  500.          PerformCheckWithNonStdThrow();
  501.  
  502.          CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
  503.          CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
  504.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  505.          CHECK_EQUAL(line, reporter.lastFailedLine);
  506.       }
  507.  
  508.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureBecauseOfStdExceptionContainsCorrectDetails)
  509.       {
  510.          PerformCheckWithStdThrow();
  511.  
  512.          CHECK_EQUAL("array2DCloseTest", reporter.lastFailedTest);
  513.          CHECK_EQUAL("array2DCloseSuite", reporter.lastFailedSuite);
  514.          CHECK_EQUAL("filename", reporter.lastFailedFile);
  515.          CHECK_EQUAL(line, reporter.lastFailedLine);
  516.       }
  517.  
  518.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnException)
  519.       {
  520.          PerformCheckWithNonStdThrow();
  521.  
  522.          CHECK(testResults.GetFailureCount() > 0);
  523.       }
  524.  
  525.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailsOnStdException)
  526.       {
  527.          PerformCheckWithStdThrow();
  528.  
  529.          CHECK(testResults.GetFailureCount() > 0);
  530.       }
  531.  
  532.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnExceptionIncludesCheckContents)
  533.       {
  534.          PerformCheckWithNonStdThrow();
  535.  
  536.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  537.          CHECK(strstr(reporter.lastFailedMessage, "ThrowingObject2D()"));
  538.       }
  539.  
  540.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesCheckContents)
  541.       {
  542.          PerformCheckWithStdThrow();
  543.  
  544.          CHECK(strstr(reporter.lastFailedMessage, "data"));
  545.          CHECK(strstr(reporter.lastFailedMessage, "StdThrowingObject2D()"));
  546.       }
  547.  
  548.       TEST_FIXTURE(CheckArray2DCloseFixture, CheckFailureOnStdExceptionIncludesWhat)
  549.       {
  550.          PerformCheckWithStdThrow();
  551.  
  552.          CHECK(strstr(reporter.lastFailedMessage, "exception (Test throw)"));
  553.       }
  554.    }
  555. }
  556.  
  557. #endif
  558.