35 #include "gtest/gtest.h"
45 # include "src/gtest-internal-inl.h"
46 # include "test/googletest-param-test-test.h"
56 using ::testing::TestWithParam;
60 using ::testing::internal::ParamGenerator;
61 using ::testing::internal::UnitTestOptions;
77 template <
typename T,
size_t N>
79 const T (&expected_values)[
N]) {
81 for (
size_t i = 0;
i <
N; ++
i) {
83 <<
"At element " <<
i <<
" when accessing via an iterator "
84 <<
"created with the copy constructor.\n";
89 <<
", expected_values[i] is " <<
PrintValue(expected_values[
i])
91 <<
", and 'it' is an iterator created with the copy constructor.\n";
95 <<
"At the presumed end of sequence when accessing via an iterator "
96 <<
"created with the copy constructor.\n";
102 it = generator.begin();
103 for (
size_t i = 0;
i <
N; ++
i) {
105 <<
"At element " <<
i <<
" when accessing via an iterator "
106 <<
"created with the assignment operator.\n";
108 <<
"where i is " <<
i
109 <<
", expected_values[i] is " <<
PrintValue(expected_values[
i])
111 <<
", and 'it' is an iterator created with the copy constructor.\n";
115 <<
"At the presumed end of sequence when accessing via an iterator "
116 <<
"created with the assignment operator.\n";
119 template <
typename T>
124 it = generator.begin();
137 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
138 const ParamGenerator<int>
gen =
Range(0, 10);
143 EXPECT_TRUE(*
it == *it2) <<
"Initialized iterators must point to the "
144 <<
"element same as its source points to";
150 EXPECT_TRUE(*
it == *it2) <<
"Assigned iterators must point to the "
151 <<
"element same as its source points to";
154 EXPECT_EQ(&
it, &(++
it)) <<
"Result of the prefix operator++ must be "
155 <<
"refer to the original object";
159 int original_value = *
it;
172 TEST(RangeTest, IntRangeWithDefaultStep) {
173 const ParamGenerator<int>
gen =
Range(0, 3);
174 const int expected_values[] = {0, 1, 2};
180 TEST(RangeTest, IntRangeSingleValue) {
181 const ParamGenerator<int>
gen =
Range(0, 1);
182 const int expected_values[] = {0};
188 TEST(RangeTest, IntRangeEmpty) {
189 const ParamGenerator<int>
gen =
Range(0, 0);
195 TEST(RangeTest, IntRangeWithCustomStep) {
196 const ParamGenerator<int>
gen =
Range(0, 9, 3);
197 const int expected_values[] = {0, 3, 6};
205 TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
206 const ParamGenerator<int>
gen =
Range(0, 4, 3);
207 const int expected_values[] = {0, 3};
237 TEST(RangeTest, WorksWithACustomType) {
238 const ParamGenerator<DogAdder>
gen =
271 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
286 TEST(ValuesInTest, ValuesInArray) {
287 int array[] = {3, 5, 8};
294 TEST(ValuesInTest, ValuesInConstArray) {
295 const int array[] = {3, 5, 8};
302 TEST(ValuesInTest, ValuesInSingleElementArray) {
310 TEST(ValuesInTest, ValuesInVector) {
311 typedef ::std::vector<int> ContainerType;
318 const int expected_values[] = {3, 5, 8};
323 TEST(ValuesInTest, ValuesInIteratorRange) {
324 typedef ::std::vector<int> ContainerType;
331 const int expected_values[] = {3, 5, 8};
337 TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
338 typedef ::std::vector<int> ContainerType;
343 const int expected_values[] = {42};
349 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
350 typedef ::std::vector<int> ContainerType;
358 TEST(ValuesTest, ValuesWorks) {
359 const ParamGenerator<int>
gen =
Values(3, 5, 8);
361 const int expected_values[] = {3, 5, 8};
367 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
368 const ParamGenerator<double>
gen =
Values(3, 5.0
f, 8.0);
370 const double expected_values[] = {3.0, 5.0, 8.0};
374 TEST(ValuesTest, ValuesWorksForMaxLengthList) {
376 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
377 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
378 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
379 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
380 410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
382 const int expected_values[] = {
383 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
384 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
385 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
386 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
387 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
393 TEST(ValuesTest, ValuesWithSingleParameter) {
394 const ParamGenerator<int>
gen =
Values(42);
396 const int expected_values[] = {42};
402 const ParamGenerator<bool>
gen =
Bool();
404 const bool expected_values[] = {
false,
true};
409 TEST(CombineTest, CombineWithTwoParameters) {
410 const char*
foo =
"foo";
411 const char*
bar =
"bar";
412 const ParamGenerator<std::tuple<const char*, int> >
gen =
415 std::tuple<const char*, int> expected_values[] = {
422 TEST(CombineTest, CombineWithThreeParameters) {
423 const ParamGenerator<std::tuple<int, int, int> >
gen =
425 std::tuple<int, int, int> expected_values[] = {
436 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
437 const ParamGenerator<std::tuple<int, int> >
gen =
448 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
449 const ParamGenerator<std::tuple<int, int> >
gen =
459 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
460 const ParamGenerator<std::tuple<int, int> >
gen =
467 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
468 const ParamGenerator<std::tuple<int, int> >
gen =
475 TEST(CombineTest, CombineWithMaxNumberOfParameters) {
476 const char*
foo =
"foo";
477 const char*
bar =
"bar";
478 const ParamGenerator<
479 std::tuple<const char*, int, int, int, int, int, int, int, int, int> >
484 std::tuple<const char*, int, int, int, int, int, int, int, int, int>
485 expected_values[] = {
std::make_tuple(
foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
506 TEST(CombineTest, NonDefaultConstructAssign) {
507 const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> >
gen =
511 ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> >
::iterator
536 TEST(ParamGeneratorTest, AssignmentWorks) {
538 const ParamGenerator<int> gen2 =
Values(3, 4);
541 const int expected_values[] = {3, 4};
554 template <
int kExpectedCalls>
570 bool perform_check =
false;
572 for (
int i = 0;
i < kExpectedCalls; ++
i) {
574 msg <<
"TestsExpandedAndRun/" <<
i;
575 if (UnitTestOptions::FilterMatchesTest(
576 "TestExpansionModule/MultipleTestGenerationTest",
577 msg.GetString().c_str())) {
578 perform_check =
true;
583 <<
"Fixture constructor of ParamTestGenerationTest test case "
584 <<
"has not been run as expected.";
586 <<
"Fixture SetUp method of ParamTestGenerationTest test case "
587 <<
"has not been run as expected.";
589 <<
"Fixture TearDown method of ParamTestGenerationTest test case "
590 <<
"has not been run as expected.";
592 <<
"Test in ParamTestGenerationTest test case "
593 <<
"has not been run as expected.";
634 bool all_tests_in_test_case_selected =
true;
638 test_name <<
"TestsExpandedAndRun/" <<
i;
639 if ( !UnitTestOptions::FilterMatchesTest(
640 "TestExpansionModule/MultipleTestGenerationTest",
641 test_name.GetString())) {
642 all_tests_in_test_case_selected =
false;
646 <<
"When running the TestGenerationTest test case all of its tests\n"
647 <<
"must be selected by the filter flag for the test case to pass.\n"
648 <<
"If not all of them are enabled, we can't reliably conclude\n"
649 <<
"that the correct number of tests have been generated.";
660 sort(expected_values.begin(), expected_values.end());
676 Environment::Instance()->TestBodyExecuted();
677 EXPECT_EQ(current_parameter_, GetParam());
678 collected_parameters_.push_back(GetParam());
756 <<
"If some (but not all) SeparateInstanceTest tests have been "
757 <<
"filtered out this test will fail. Make sure that all "
758 <<
"GeneratorEvaluationTest are selected or de-selected together "
759 <<
"by the test filter.";
779 class NamingTest :
public TestWithParam<int> {};
782 const ::testing::TestInfo*
const test_info =
785 EXPECT_STREQ(
"ZeroToFiveSequence/NamingTest", test_info->test_suite_name());
788 index_stream <<
"TestsReportCorrectNamesAndParameters/" << GetParam();
789 EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
799 #define PREFIX_WITH_FOO(test_name) Foo##test_name
800 #define PREFIX_WITH_MACRO(test_name) Macro##test_name
803 const ::testing::TestInfo*
const test_info =
806 EXPECT_STREQ(
"FortyTwo/MacroNamingTest", test_info->test_suite_name());
817 const ::testing::TestInfo*
const test_info =
820 EXPECT_STREQ(
"MacroNamingTestNonParametrized", test_info->test_suite_name());
824 TEST(MacroNameing, LookupNames) {
825 std::set<std::string> know_suite_names, know_test_names;
830 know_suite_names.insert(
suite->name());
840 know_suite_names.find(
"FortyTwo/MacroNamingTest"),
841 know_suite_names.end());
843 know_suite_names.find(
"MacroNamingTestNonParametrized"),
844 know_suite_names.end());
847 know_test_names.find(
"FortyTwo/MacroNamingTest.FooSomeTestName/0"),
848 know_test_names.end());
850 know_test_names.find(
"MacroNamingTestNonParametrized.FooSomeTestName"),
851 know_test_names.end());
872 Values(
"abcdefghijklmnopqrstuvwxyz",
873 "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"01234567890_"),
877 const ::testing::TestParamInfo<std::string>&
inf) {
899 [](const ::testing::TestParamInfo<std::string>&
inf) {
903 TEST(CustomNamingTest, CheckNameRegistry) {
905 std::set<std::string> test_names;
909 for (
int test_num = 0; test_num <
test_suite->total_test_count();
911 const ::testing::TestInfo* test_info =
test_suite->GetTestInfo(test_num);
915 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/FunctorName"));
916 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/FunctionName"));
917 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/FunctionNameP"));
918 EXPECT_EQ(1
u, test_names.count(
"CustomTestNames/LambdaName"));
926 const ::testing::TestInfo*
const test_info =
929 test_name_stream <<
"TestsReportCorrectNames/" << GetParam();
930 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
951 const ::testing::TestInfo*
const test_info =
954 test_name_stream <<
"TestsReportCorrectNames/" << GetParam();
955 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
981 const ::testing::TestInfo*
const test_info =
985 test_name_stream <<
"TestsReportCorrectNames/" << sum_;
986 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
1006 class CommentTest :
public TestWithParam<Unstreamable> {};
1009 const ::testing::TestInfo*
const test_info =
1053 ".* value-parameterized test .*");
1078 template <
typename T>
1087 TEST_P(NotInstantiatedTest, Used) { }
1094 template <
typename T>