assertion_tests.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #include "deathTestCommon.h"
  17. #include <gsl/assert> // for Ensures, Expects
  18. #include <gtest/gtest.h>
  19. using namespace gsl;
  20. namespace
  21. {
  22. int f(int i)
  23. {
  24. Expects(i > 0 && i < 10);
  25. return i;
  26. }
  27. int g(int i)
  28. {
  29. i++;
  30. Ensures(i > 0 && i < 10);
  31. return i;
  32. }
  33. } // namespace
  34. TEST(assertion_tests, expects)
  35. {
  36. const auto terminateHandler = std::set_terminate([] {
  37. std::cerr << "Expected Death. expects";
  38. std::abort();
  39. });
  40. EXPECT_TRUE(f(2) == 2);
  41. EXPECT_DEATH(f(10), GetExpectedDeathString(terminateHandler));
  42. }
  43. TEST(assertion_tests, ensures)
  44. {
  45. const auto terminateHandler = std::set_terminate([] {
  46. std::cerr << "Expected Death. ensures";
  47. std::abort();
  48. });
  49. EXPECT_TRUE(g(2) == 3);
  50. EXPECT_DEATH(g(9), GetExpectedDeathString(terminateHandler));
  51. }