blob: 1479face5837b5e72940cb51ed340197e8822ada [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2022 The Chromium Authors
Roland Bockd662a2d2022-07-12 20:55:272// 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
10namespace {
11bool g_this_is_a_test = false;
12}
13
14namespace base::internal {
Ari Chivukulac0ee1d02024-03-26 20:12:2415void check_is_test_impl(base::NotFatalUntil fatal_milestone) {
16 CHECK(g_this_is_a_test, fatal_milestone);
Roland Bockd662a2d2022-07-12 20:55:2717}
Daniel Murphy5fa7a3192024-04-17 23:08:4318
19// static
20bool IsInTest::Get() {
21 return g_this_is_a_test;
22}
Roland Bockd662a2d2022-07-12 20:55:2723} // namespace base::internal
24
25namespace base::test {
Roland Bock3c0853f2022-10-23 01:23:0126// 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.
29BASE_EXPORT void AllowCheckIsTestForTesting() {
30 // This CHECK ensures that `AllowCheckIsTestForTesting` is called
Daniel Murphy5fa7a3192024-04-17 23:08:4331 // just once. Since it is called in `base::TestSuite`, this should effectively
Roland Bock3c0853f2022-10-23 01:23:0132 // prevent calls to `AllowCheckIsTestForTesting` in production code
33 // (assuming that code has unit test coverage).
Roland Bockd662a2d2022-07-12 20:55:2734 //
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 Bock3c0853f2022-10-23 01:23:0138 << "AllowCheckIsTestForTesting must not be called more than once";
Roland Bockd662a2d2022-07-12 20:55:2739
40 g_this_is_a_test = true;
41}
Roland Bockd662a2d2022-07-12 20:55:2742} // namespace base::test