xref: /aosp_15_r20/external/cronet/base/check_is_test.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_CHECK_IS_TEST_H_
6 #define BASE_CHECK_IS_TEST_H_
7 
8 #include "base/base_export.h"
9 #include "base/not_fatal_until.h"
10 
11 // Code paths taken in tests are sometimes different from those taken in
12 // production. This might be because the respective tests do not initialize some
13 // objects that would be required for the "normal" code path.
14 //
15 // Ideally, such code constructs should be avoided, so that tests really test
16 // the production code and not something different.
17 //
18 // However, there already are hundreds of test-only paths in production code
19 // Cleaning up all these cases retroactively and completely avoiding such cases
20 // in the future seems unrealistic.
21 //
22 // Thus, it is useful to prevent the test code-only paths to be taken in
23 // production scenarios.
24 //
25 // `CHECK_IS_TEST` can be used to assert that a test-only path is actually taken
26 // only in tests. For instance:
27 //
28 //   // This only happens in unit tests:
29 //   if (!url_loader_factory)
30 //   {
31 //     // Assert that this code path is really only taken in tests.
32 //     CHECK_IS_TEST();
33 //     return;
34 //   }
35 //
36 // `CHECK_IS_TEST` is thread safe.
37 //
38 // An optional base::NotFatalUntil argument can be provided to make the
39 // instance non-fatal (dumps without crashing) before a provided milestone.
40 // See base/check.h for details.
41 
42 #define CHECK_IS_TEST(...) base::internal::check_is_test_impl(__VA_ARGS__)
43 
44 namespace base::internal {
45 BASE_EXPORT void check_is_test_impl(
46     base::NotFatalUntil fatal_milestone =
47         base::NotFatalUntil::NoSpecifiedMilestoneInternal);
48 }  // namespace base::internal
49 
50 #endif  // BASE_CHECK_IS_TEST_H_
51