-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_unit_test: Support running tests in static libraries
The PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST(suite, test) macro refers to a test in a static library so the linker will include it. Fixed: b/233938994 Change-Id: Id7ae1c48047f2e5a45b68c312e6b49bf9921ca72 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/96040 Reviewed-by: Alexei Frolov <frolv@google.com> Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com> Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
- Loading branch information
Showing
10 changed files
with
305 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
#pragma once | ||
|
||
#include "gtest/gtest.h" | ||
|
||
// Ensures tests in a static library are linked and executed. Provide the test | ||
// suite name and test name for one test in the file linked into a static | ||
// library. Any test in the file may be used, but it is recommended to use the | ||
// first for consistency. The test must be in a static library that is a | ||
// dependency of this target. Referring to a test that does not exist causes a | ||
// linker error. | ||
// | ||
// The linker usually ignores tests linked through a static library. This is | ||
// because test registration relies on the test instance's static constructor | ||
// adding itself to a global list of tests. When linking against a static | ||
// library, static constructors in an object file will be ignored unless at | ||
// least one entity in that object file is linked. | ||
// | ||
// This macro works by passing the internal TestInfo instance to a constructor | ||
// defined in a source file. This guarantees that the TestInfo instance is | ||
// referenced, so the linker will link it and the other tests in that file. | ||
#define PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST(suite, name) \ | ||
_PW_UNIT_TEST_LINK_TESTS(_pw_unit_test_Info_##suite##_##name) | ||
|
||
#define _PW_UNIT_TEST_LINK_TESTS(info) \ | ||
extern "C" { \ | ||
\ | ||
extern ::pw::unit_test::internal::TestInfo info; \ | ||
\ | ||
[[maybe_unused]] const ::pw::unit_test::internal::ReferToTestInfo \ | ||
_pw_unit_test_reference_to_ensure_link_##info(info); \ | ||
\ | ||
} /* extern "C" */ \ | ||
\ | ||
static_assert(true, "Macros must end with a semicolon") | ||
|
||
namespace pw::unit_test::internal { | ||
|
||
// Refers to the TestInfo to ensure it is linked in. | ||
class ReferToTestInfo { | ||
public: | ||
explicit ReferToTestInfo(const TestInfo& info); | ||
}; | ||
|
||
} // namespace pw::unit_test::internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace pw::unit_test { | ||
|
||
extern int test_1_executions; | ||
extern int test_2_executions; | ||
|
||
namespace { | ||
|
||
TEST(StaticLibraryArchivedTest, Test1) { test_1_executions += 1; } | ||
|
||
TEST(StaticLibraryArchivedTest, Test2) { test_2_executions += 1; } | ||
|
||
} // namespace | ||
} // namespace pw::unit_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace pw::unit_test { | ||
|
||
extern int test_3_executions_not_expected; | ||
extern int test_4_executions_not_expected; | ||
|
||
namespace { | ||
|
||
TEST(StaticLibraryArchivedTest, ShouldNotRunTest3) { | ||
test_3_executions_not_expected += 1; | ||
} | ||
|
||
TEST(StaticLibraryArchivedTest, ShouldNotRunTest4) { | ||
test_4_executions_not_expected += 1; | ||
} | ||
|
||
TEST(StaticLibraryArchivedTest, Fails) { FAIL(); } | ||
|
||
} // namespace | ||
} // namespace pw::unit_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2022 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include "pw_unit_test/static_library_support.h" | ||
|
||
namespace pw::unit_test::internal { | ||
|
||
ReferToTestInfo::ReferToTestInfo(const TestInfo& info) { | ||
[[maybe_unused]] const TestInfo* volatile force_reference = &info; | ||
} | ||
|
||
} // namespace pw::unit_test::internal |
Oops, something went wrong.