check_test.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // TODO: figure out why we can't use \\d+ instead of .+ in these patterns.
  11. ASSERT_DEATH({ CARBON_CHECK(false); },
  12. "Stack trace:\n"
  13. ".+\n"
  14. "CHECK failure at common/check_test.cpp:.+: false\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. "CHECK 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. "FATAL failure at common/check_test.cpp:.+: msg\n");
  38. }
  39. auto FatalNoReturnRequired() -> int { CARBON_FATAL() << "msg"; }
  40. TEST(ErrorTest, FatalNoReturnRequired) {
  41. ASSERT_DEATH({ FatalNoReturnRequired(); },
  42. "FATAL failure at common/check_test.cpp:.+: msg\n");
  43. }
  44. } // namespace
  45. } // namespace Carbon::Testing