Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/check_is_test.h" |
| 6 | |
| 7 | #include "base/check.h" |
| 8 | #include "base/logging.h" |
| 9 | |
| 10 | namespace { |
| 11 | bool g_this_is_a_test = false; |
| 12 | } |
| 13 | |
| 14 | namespace base::internal { |
Ari Chivukula | c0ee1d0 | 2024-03-26 20:12:24 | [diff] [blame] | 15 | void check_is_test_impl(base::NotFatalUntil fatal_milestone) { |
| 16 | CHECK(g_this_is_a_test, fatal_milestone); |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 17 | } |
Daniel Murphy | 5fa7a319 | 2024-04-17 23:08:43 | [diff] [blame^] | 18 | |
| 19 | // static |
| 20 | bool IsInTest::Get() { |
| 21 | return g_this_is_a_test; |
| 22 | } |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 23 | } // namespace base::internal |
| 24 | |
| 25 | namespace base::test { |
Roland Bock | 3c0853f | 2022-10-23 01:23:01 | [diff] [blame] | 26 | // base/test/allow_check_is_test_for_testing.h declares |
| 27 | // `AllowCheckIsTestForTesting`, but is only allowed to be included in test |
| 28 | // code. We therefore have to also mark the symbol as exported here. |
| 29 | BASE_EXPORT void AllowCheckIsTestForTesting() { |
| 30 | // This CHECK ensures that `AllowCheckIsTestForTesting` is called |
Daniel Murphy | 5fa7a319 | 2024-04-17 23:08:43 | [diff] [blame^] | 31 | // just once. Since it is called in `base::TestSuite`, this should effectively |
Roland Bock | 3c0853f | 2022-10-23 01:23:01 | [diff] [blame] | 32 | // prevent calls to `AllowCheckIsTestForTesting` in production code |
| 33 | // (assuming that code has unit test coverage). |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 34 | // |
| 35 | // This is just in case someone ignores the fact that this function in the |
| 36 | // `base::test` namespace and ends on "ForTesting". |
| 37 | CHECK(!g_this_is_a_test) |
Roland Bock | 3c0853f | 2022-10-23 01:23:01 | [diff] [blame] | 38 | << "AllowCheckIsTestForTesting must not be called more than once"; |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 39 | |
| 40 | g_this_is_a_test = true; |
| 41 | } |
Roland Bock | d662a2d | 2022-07-12 20:55:27 | [diff] [blame] | 42 | } // namespace base::test |