Google Test requires all tests in the same test suite to use the same test fixture class. This function checks if the current test has the same fixture class as the first test in the current test suite. If yes, it returns true; otherwise it generates a Google Test failure and returns false.
| 3792 | // yes, it returns true; otherwise it generates a Google Test failure and |
| 3793 | // returns false. |
| 3794 | bool Test::HasSameFixtureClass() { |
| 3795 | internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); |
| 3796 | const TestSuite* const test_suite = impl->current_test_suite(); |
| 3797 | |
| 3798 | // Info about the first test in the current test suite. |
| 3799 | const TestInfo* const first_test_info = test_suite->test_info_list()[0]; |
| 3800 | const internal::TypeId first_fixture_id = first_test_info->fixture_class_id_; |
| 3801 | const char* const first_test_name = first_test_info->name(); |
| 3802 | |
| 3803 | // Info about the current test. |
| 3804 | const TestInfo* const this_test_info = impl->current_test_info(); |
| 3805 | const internal::TypeId this_fixture_id = this_test_info->fixture_class_id_; |
| 3806 | const char* const this_test_name = this_test_info->name(); |
| 3807 | |
| 3808 | if (this_fixture_id != first_fixture_id) { |
| 3809 | // Is the first test defined using TEST? |
| 3810 | const bool first_is_TEST = first_fixture_id == internal::GetTestTypeId(); |
| 3811 | // Is this test defined using TEST? |
| 3812 | const bool this_is_TEST = this_fixture_id == internal::GetTestTypeId(); |
| 3813 | |
| 3814 | if (first_is_TEST || this_is_TEST) { |
| 3815 | // Both TEST and TEST_F appear in same test suite, which is incorrect. |
| 3816 | // Tell the user how to fix this. |
| 3817 | |
| 3818 | // Gets the name of the TEST and the name of the TEST_F. Note |
| 3819 | // that first_is_TEST and this_is_TEST cannot both be true, as |
| 3820 | // the fixture IDs are different for the two tests. |
| 3821 | const char* const TEST_name = |
| 3822 | first_is_TEST ? first_test_name : this_test_name; |
| 3823 | const char* const TEST_F_name = |
| 3824 | first_is_TEST ? this_test_name : first_test_name; |
| 3825 | |
| 3826 | ADD_FAILURE() |
| 3827 | << "All tests in the same test suite must use the same test fixture\n" |
| 3828 | << "class, so mixing TEST_F and TEST in the same test suite is\n" |
| 3829 | << "illegal. In test suite " << this_test_info->test_suite_name() |
| 3830 | << ",\n" |
| 3831 | << "test " << TEST_F_name << " is defined using TEST_F but\n" |
| 3832 | << "test " << TEST_name << " is defined using TEST. You probably\n" |
| 3833 | << "want to change the TEST to TEST_F or move it to another test\n" |
| 3834 | << "case."; |
| 3835 | } else { |
| 3836 | // Two fixture classes with the same name appear in two different |
| 3837 | // namespaces, which is not allowed. Tell the user how to fix this. |
| 3838 | ADD_FAILURE() |
| 3839 | << "All tests in the same test suite must use the same test fixture\n" |
| 3840 | << "class. However, in test suite " |
| 3841 | << this_test_info->test_suite_name() << ",\n" |
| 3842 | << "you defined test " << first_test_name << " and test " |
| 3843 | << this_test_name << "\n" |
| 3844 | << "using two different test fixture classes. This can happen if\n" |
| 3845 | << "the two classes are from different namespaces or translation\n" |
| 3846 | << "units and have the same name. You should probably rename one\n" |
| 3847 | << "of the classes to put the tests into different test suites."; |
| 3848 | } |
| 3849 | return false; |
| 3850 | } |
| 3851 |
nothing calls this directly
no test coverage detected