test_name_helper.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_
  5. #define COMPONENTS_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_
  6. #include <string>
  7. #include "base/strings/string_util.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace chrome_cleaner {
  10. // A functor that formats test parameters for use in a test name. Use this
  11. // instead of PrintToStringParamName, which sometimes returns characters that
  12. // aren't valid in test names.
  13. //
  14. // Known limitations:
  15. //
  16. // * A char* initialized with "string value" is formatted as
  17. // ADDR_pointer_to_string_value. To avoid this, declare the parameter with type
  18. // std::string. Example: instead of "TestWithParam<std::tuple<const char*,
  19. // bool>>" use "TestWithParam<std::tuple<std::string, bool>>".
  20. //
  21. // * A string initialized with the literal L"wide string" is formatted as
  22. // Lwide_string. No known workaround.
  23. //
  24. // Note that long test names can cause problems because they can be used to
  25. // generate file names, which have a max length of 255 on Windows. So use this
  26. // judiciously.
  27. struct GetParamNameForTest {
  28. template <typename ParamType>
  29. std::string operator()(
  30. const ::testing::TestParamInfo<ParamType>& info) const {
  31. std::string param_name = ::testing::PrintToString(info.param);
  32. // Remove or convert invalid characters that are inserted by PrintToString:
  33. //
  34. // * Strings formatted as "string value" (including quotes) -> string_value
  35. // * Tuples formatted as (value1, value2) -> value1_value2
  36. // * Mojo enums formatted as Enum::VALUE -> EnumVALUE
  37. base::RemoveChars(param_name, "\"(),:", &param_name);
  38. base::ReplaceChars(param_name, " ", "_", &param_name);
  39. return param_name;
  40. }
  41. };
  42. } // namespace chrome_cleaner
  43. #endif // COMPONENTS_CHROME_CLEANER_TEST_TEST_NAME_HELPER_H_