• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15 
16 #include "gtest/gtest.h"
17 
18 // Ensures tests in a static library are linked and executed. Provide the test
19 // suite name and test name for one test in the file linked into a static
20 // library. Any test in the file may be used, but it is recommended to use the
21 // first for consistency. The test must be in a static library that is a
22 // dependency of this target. Referring to a test that does not exist causes a
23 // linker error.
24 //
25 // The linker usually ignores tests linked through a static library. This is
26 // because test registration relies on the test instance's static constructor
27 // adding itself to a global list of tests. When linking against a static
28 // library, static constructors in an object file will be ignored unless at
29 // least one entity in that object file is linked.
30 //
31 // This macro works by passing the internal TestInfo instance to a constructor
32 // defined in a source file. This guarantees that the TestInfo instance is
33 // referenced, so the linker will link it and the other tests in that file.
34 #define PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST(suite, name) \
35   _PW_UNIT_TEST_LINK_TESTS(_pw_unit_test_Info_##suite##_##name)
36 
37 #define _PW_UNIT_TEST_LINK_TESTS(info)                              \
38   extern "C" {                                                      \
39                                                                     \
40   extern ::pw::unit_test::internal::TestInfo info;                  \
41                                                                     \
42   [[maybe_unused]] const ::pw::unit_test::internal::ReferToTestInfo \
43       _pw_unit_test_reference_to_ensure_link_##info(info);          \
44                                                                     \
45   } /* extern "C" */                                                \
46                                                                     \
47   static_assert(true, "Macros must end with a semicolon")
48 
49 namespace pw::unit_test::internal {
50 
51 // Refers to the TestInfo to ensure it is linked in.
52 class ReferToTestInfo {
53  public:
54   explicit ReferToTestInfo(const TestInfo& info);
55 };
56 
57 }  // namespace pw::unit_test::internal
58