check_test.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 {
  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, "{0}", 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, CheckFalseFormattedMessage) {
  30. const char msg[] = "msg";
  31. std::string str = "str";
  32. int i = 1;
  33. ASSERT_DEATH(
  34. { CARBON_CHECK(false, "{0} {1} {2} {3}", msg, str, i, 0); },
  35. "\nCHECK failure at common/check_test.cpp:.+: false: msg str 1 0\n");
  36. }
  37. TEST(CheckTest, CheckOutputForms) {
  38. const char msg[] = "msg";
  39. std::string str = "str";
  40. int i = 1;
  41. CARBON_CHECK(true, "{0} {1} {2} {3}", msg, str, i, 0);
  42. }
  43. TEST(CheckTest, Fatal) {
  44. ASSERT_DEATH({ CARBON_FATAL("msg"); },
  45. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  46. }
  47. TEST(CheckTest, FatalHasStackDump) {
  48. ASSERT_DEATH({ CARBON_FATAL("msg"); }, "\nStack dump:\n");
  49. }
  50. auto FatalNoReturnRequired() -> int { CARBON_FATAL("msg"); }
  51. TEST(ErrorTest, FatalNoReturnRequired) {
  52. ASSERT_DEATH({ FatalNoReturnRequired(); },
  53. "\nFATAL failure at common/check_test.cpp:.+: msg\n");
  54. }
  55. } // namespace
  56. } // namespace Carbon