check_is_test.cc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/check_is_test.h"
  5. #include "base/check.h"
  6. #include "base/logging.h"
  7. namespace {
  8. bool g_this_is_a_test = false;
  9. }
  10. namespace base::internal {
  11. void check_is_test_impl() {
  12. CHECK(g_this_is_a_test);
  13. }
  14. } // namespace base::internal
  15. namespace base::test {
  16. // base/test/allow_check_is_test_to_be_called.h declares
  17. // `AllowCheckIsTestToBeCalled`, but is only allowed to be included in test
  18. // code.
  19. // We therefore have to also mark the symbol as exported here.
  20. BASE_EXPORT void AllowCheckIsTestToBeCalled() {
  21. LOG(WARNING) << "Allowing special test code paths";
  22. // This CHECK ensures that `AllowCheckIsTestToBeCalled` is called just once.
  23. // Since it is called in `base::TestSuite`, this should effectivly prevent
  24. // calls to AllowCheckIsTestToBeCalled in production code (assuming that code
  25. // has unit test coverage).
  26. //
  27. // This is just in case someone ignores the fact that this function in the
  28. // `base::test` namespace and ends on "ForTesting".
  29. CHECK(!g_this_is_a_test)
  30. << "AllowCheckIsTestToBeCalled must not be called more than once";
  31. g_this_is_a_test = true;
  32. }
  33. } // namespace base::test