check_test.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "Stack trace:\n"
  12. "(.|\n)+\n"
  13. "CHECK failure at common/check_test.cpp:\\d+: false\n");
  14. }
  15. TEST(CheckTest, CheckTrueCallbackNotUsed) {
  16. bool called = false;
  17. auto callback = [&]() {
  18. called = true;
  19. return "called";
  20. };
  21. CARBON_CHECK(true) << callback();
  22. EXPECT_FALSE(called);
  23. }
  24. TEST(CheckTest, CheckFalseMessage) {
  25. ASSERT_DEATH({ CARBON_CHECK(false) << "msg"; },
  26. "CHECK failure at common/check_test.cpp:.+: false: msg\n");
  27. }
  28. TEST(CheckTest, CheckOutputForms) {
  29. const char msg[] = "msg";
  30. std::string str = "str";
  31. int i = 1;
  32. CARBON_CHECK(true) << msg << str << i << 0;
  33. }
  34. TEST(CheckTest, Fatal) {
  35. ASSERT_DEATH({ CARBON_FATAL() << "msg"; },
  36. "FATAL failure at common/check_test.cpp:.+: msg\n");
  37. }
  38. auto FatalNoReturnRequired() -> int { CARBON_FATAL() << "msg"; }
  39. TEST(ErrorTest, FatalNoReturnRequired) {
  40. ASSERT_DEATH({ FatalNoReturnRequired(); },
  41. "FATAL failure at common/check_test.cpp:.+: msg\n");
  42. }
  43. } // namespace
  44. } // namespace Carbon::Testing