check_is_test.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #ifndef BASE_CHECK_IS_TEST_H_
  5. #define BASE_CHECK_IS_TEST_H_
  6. #include "base/base_export.h"
  7. // Code paths taken in tests are sometimes different from those taken in
  8. // production. This might be because the respective tests do not initialize some
  9. // objects that would be required for the "normal" code path.
  10. //
  11. // Ideally, such code constructs should be avoided, so that tests really test
  12. // the production code and not something different.
  13. //
  14. // However, there already are hundreds of test-only paths in production code
  15. // Cleaning up all these cases retroactively and completely avoiding such cases
  16. // in the future seems unrealistic.
  17. //
  18. // Thus, it is useful to prevent the test code-only paths to be taken in
  19. // production scenarios.
  20. //
  21. // `CHECK_IS_TEST` can be used to assert that a test-only path is actually taken
  22. // only in tests. For instance:
  23. //
  24. // // This only happens in unit tests:
  25. // if (!url_loader_factory)
  26. // {
  27. // // Assert that this code path is really only taken in tests.
  28. // CHECK_IS_TEST();
  29. // return;
  30. // }
  31. //
  32. // `CHECK_IS_TEST` is thread safe.
  33. #define CHECK_IS_TEST() base::internal::check_is_test_impl()
  34. namespace base::internal {
  35. BASE_EXPORT void check_is_test_impl();
  36. } // namespace base::internal
  37. #endif // BASE_CHECK_IS_TEST_H_