From 8989d41bc16add5d66f1d111ea1f2199e83f0d8a Mon Sep 17 00:00:00 2001 From: David Schuldenfrei Date: Wed, 11 Jul 2018 17:03:06 +0300 Subject: [PATCH] New command line parameter: list_tests_with_location Allows to see the file and line where the test is defined --- googletest/include/gtest/gtest.h | 5 +++++ googletest/src/gtest-internal-inl.h | 6 +++++- googletest/src/gtest.cc | 29 ++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/googletest/include/gtest/gtest.h b/googletest/include/gtest/gtest.h index 686750e0..debb26cf 100644 --- a/googletest/include/gtest/gtest.h +++ b/googletest/include/gtest/gtest.h @@ -116,6 +116,11 @@ GTEST_DECLARE_string_(filter); // are actually run if the flag is provided. GTEST_DECLARE_bool_(list_tests); +// This flag causes the Google Test to list tests with their location +// (file and line number). None of the tests listed +// are actually run if the flag is provided. +GTEST_DECLARE_bool_(list_tests_with_location); + // This flag controls whether Google Test emits a detailed XML report to a file // in addition to its normal textual output. GTEST_DECLARE_string_(output); diff --git a/googletest/src/gtest-internal-inl.h b/googletest/src/gtest-internal-inl.h index e77c8b6c..8a014351 100644 --- a/googletest/src/gtest-internal-inl.h +++ b/googletest/src/gtest-internal-inl.h @@ -84,6 +84,7 @@ const char kCatchExceptionsFlag[] = "catch_exceptions"; const char kColorFlag[] = "color"; const char kFilterFlag[] = "filter"; const char kListTestsFlag[] = "list_tests"; +const char kListTestsWithLocationFlag[] = "list_tests_with_location"; const char kOutputFlag[] = "output"; const char kPrintTimeFlag[] = "print_time"; const char kPrintUTF8Flag[] = "print_utf8"; @@ -165,6 +166,7 @@ class GTestFlagSaver { filter_ = GTEST_FLAG(filter); internal_run_death_test_ = GTEST_FLAG(internal_run_death_test); list_tests_ = GTEST_FLAG(list_tests); + list_tests_with_location_ = GTEST_FLAG(list_tests_with_location); output_ = GTEST_FLAG(output); print_time_ = GTEST_FLAG(print_time); print_utf8_ = GTEST_FLAG(print_utf8); @@ -187,6 +189,7 @@ class GTestFlagSaver { GTEST_FLAG(filter) = filter_; GTEST_FLAG(internal_run_death_test) = internal_run_death_test_; GTEST_FLAG(list_tests) = list_tests_; + GTEST_FLAG(list_tests_with_location) = list_tests_with_location_; GTEST_FLAG(output) = output_; GTEST_FLAG(print_time) = print_time_; GTEST_FLAG(print_utf8) = print_utf8_; @@ -209,6 +212,7 @@ class GTestFlagSaver { std::string filter_; std::string internal_run_death_test_; bool list_tests_; + bool list_tests_with_location_; std::string output_; bool print_time_; bool print_utf8_; @@ -722,7 +726,7 @@ class GTEST_API_ UnitTestImpl { int FilterTests(ReactionToSharding shard_tests); // Prints the names of the tests matching the user-specified filter flag. - void ListTestsMatchingFilter(); + void ListTestsMatchingFilter(bool list_location); const TestCase* current_test_case() const { return current_test_case_; } TestInfo* current_test_info() { return current_test_info_; } diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index ce6c07fa..53c7398f 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -231,6 +231,9 @@ GTEST_DEFINE_string_( GTEST_DEFINE_bool_(list_tests, false, "List all tests without running them."); +GTEST_DEFINE_bool_(list_tests_with_location, false, + "List all tests, and their location, without running them."); + // The net priority order after flag processing is thus: // --gtest_output command line flag // GTEST_OUTPUT environment variable @@ -4994,9 +4997,15 @@ bool UnitTestImpl::RunAllTests() { // Lists the tests and exits if the --gtest_list_tests flag was specified. if (GTEST_FLAG(list_tests)) { // This must be called *after* FilterTests() has been called. - ListTestsMatchingFilter(); + ListTestsMatchingFilter(false); return true; } + if (GTEST_FLAG(list_tests_with_location)) { + // This must be called *after* FilterTests() has been called. + ListTestsMatchingFilter(true); + return true; + } + random_seed_ = GTEST_FLAG(shuffle) ? GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0; @@ -5276,7 +5285,7 @@ static void PrintOnOneLine(const char* str, int max_length) { } // Prints the names of the tests matching the user-specified filter flag. -void UnitTestImpl::ListTestsMatchingFilter() { +void UnitTestImpl::ListTestsMatchingFilter(bool list_location) { // Print at most this many characters for each type/value parameter. const int kMaxParamLength = 250; @@ -5299,7 +5308,11 @@ void UnitTestImpl::ListTestsMatchingFilter() { } printf("\n"); } - printf(" %s", test_info->name()); + printf(" "); + if (list_location) { + printf("%d;%s;", test_info->line(), test_info->file()); + } + printf("%s", test_info->name()); if (test_info->value_param() != NULL) { printf(" # %s = ", kValueParamLabel); // We print the value parameter on a single line to make the @@ -5582,6 +5595,9 @@ static const char kColorEncodedHelpMessage[] = " @G--" GTEST_FLAG_PREFIX_ "list_tests@D\n" " List the names of all tests instead of running them. The name of\n" " TEST(Foo, Bar) is \"Foo.Bar\".\n" +" @G--" GTEST_FLAG_PREFIX_ "list_tests_with_location@D\n" +" List the names of all tests, and their location instead of running them. The name of\n" +" TEST(Foo, Bar) is \"Foo.Bar\", and the lcation is lineNumber;fileName.\n" " @G--" GTEST_FLAG_PREFIX_ "filter=@YPOSTIVE_PATTERNS" "[@G-@YNEGATIVE_PATTERNS]@D\n" " Run only the tests whose name matches one of the positive patterns but\n" @@ -5627,8 +5643,9 @@ static const char kColorEncodedHelpMessage[] = " Do not report exceptions as test failures. Instead, allow them\n" " to crash the program or throw a pop-up (on Windows).\n" "\n" -"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, you can alternatively set " - "the corresponding\n" +"Except for @G--" GTEST_FLAG_PREFIX_ "list_tests@D, and " + "@G--" GTEST_FLAG_PREFIX_ "list_tests_with_location@D " + "you can alternatively set the corresponding\n" "environment variable of a flag (all letters in upper-case). For example, to\n" "disable colored text output, you can either specify @G--" GTEST_FLAG_PREFIX_ "color=no@D or set\n" @@ -5655,6 +5672,8 @@ static bool ParseGoogleTestFlag(const char* const arg) { ParseStringFlag(arg, kInternalRunDeathTestFlag, >EST_FLAG(internal_run_death_test)) || ParseBoolFlag(arg, kListTestsFlag, >EST_FLAG(list_tests)) || + ParseBoolFlag(arg, kListTestsWithLocationFlag, + >EST_FLAG(list_tests_with_location)) || ParseStringFlag(arg, kOutputFlag, >EST_FLAG(output)) || ParseBoolFlag(arg, kPrintTimeFlag, >EST_FLAG(print_time)) || ParseBoolFlag(arg, kPrintUTF8Flag, >EST_FLAG(print_utf8)) ||