check_test.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "common/check.h"
  5. #include <gtest/gtest.h>
  6. namespace Carbon::Testing {
  7. namespace {
  8. TEST(CheckTest, CheckTrue) { CARBON_CHECK(true); }
  9. TEST(CheckTest, CheckFalse) {
  10. ASSERT_DEATH({ CARBON_CHECK(false); },
  11. "\nCHECK failure at common/check_test.cpp:\\d+: false\n");
  12. }
  13. TEST(CheckTest, CheckFalseHasStackDump) {
  14. ASSERT_DEATH({ CARBON_CHECK(false); }, "\nStack dump:\n");
  15. }
  16. TEST(CheckTest, CheckTrueCallbackNotUsed) {
  17. bool called = false;
  18. auto callback = [&]() {
  19. called = true;
  20. return "called";
  21. };
  22. CARBON_CHECK(true) << callback();
  23. EXPECT_FALSE(called);
  24. }
  25. TEST(CheckTest, CheckFalseMessage) {
  26. ASSERT_DEATH({ CARBON_CHECK(false) << "msg"; },
  27. "\nCHECK failure at common/check_test.cpp:.+: false: msg\n");
  28. }
  29. TEST(CheckTest, CheckOutputForms) {
  30. const char msg[] = "msg";
  31. std::string str = "str";
  32. int i = 1;
  33. CARBON_CHECK(true) << msg << str << i << 0;
  34. }
  35. TEST(CheckTest, Fatal) {
  36. ASSERT_DEATH({ CARBON_FATAL() << "msg"; },
  37. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  38. }
  39. TEST(CheckTest, FatalHasStackDump) {
  40. ASSERT_DEATH({ CARBON_FATAL() << "msg"; }, "\nStack dump:\n");
  41. }
  42. auto FatalNoReturnRequired() -> int { CARBON_FATAL() << "msg"; }
  43. TEST(ErrorTest, FatalNoReturnRequired) {
  44. ASSERT_DEATH({ FatalNoReturnRequired(); },
  45. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  46. }
  47. } // namespace
  48. } // namespace Carbon::Testing