Options.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <cctype>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <vector>
  23. namespace android {
  24. namespace gtest_extras {
  25. class Options {
  26. public:
  27. Options() = default;
  28. ~Options() = default;
  29. bool Process(const std::vector<const char*>& args, std::vector<const char*>* child_args);
  30. size_t job_count() const { return job_count_; }
  31. int num_iterations() const { return num_iterations_; }
  32. uint64_t deadline_threshold_ms() const { return numerics_.at("deadline_threshold_ms"); }
  33. uint64_t slow_threshold_ms() const { return numerics_.at("slow_threshold_ms"); }
  34. uint64_t shard_index() const { return numerics_.at("gtest_shard_index"); }
  35. uint64_t total_shards() const { return numerics_.at("gtest_total_shards"); }
  36. bool print_time() const { return bools_.at("gtest_print_time"); }
  37. bool gtest_format() const { return bools_.at("gtest_format"); }
  38. bool allow_disabled_tests() const { return bools_.at("gtest_also_run_disabled_tests"); }
  39. bool list_tests() const { return bools_.at("gtest_list_tests"); }
  40. const std::string& color() const { return strings_.at("gtest_color"); }
  41. const std::string& xml_file() const { return strings_.at("xml_file"); }
  42. const std::string& filter() const { return strings_.at("gtest_filter"); }
  43. private:
  44. size_t job_count_;
  45. int num_iterations_;
  46. std::unordered_map<std::string, bool> bools_;
  47. std::unordered_map<std::string, std::string> strings_;
  48. std::unordered_map<std::string, uint64_t> numerics_;
  49. enum FlagType : uint32_t {
  50. FLAG_NONE = 0,
  51. FLAG_CHILD = 0x1, // Argument preserved for forked child call.
  52. FLAG_INCOMPATIBLE = 0x2, // Not compatible with isolation mode.
  53. FLAG_ENVIRONMENT_VARIABLE = 0x4, // Can be an environment variable.
  54. FLAG_REQUIRES_VALUE = 0x8, // Flag requires a non-empty value.
  55. FLAG_OPTIONAL_VALUE = 0x10, // Flag takes an optional value.
  56. };
  57. static constexpr uint32_t FLAG_TAKES_VALUE = FLAG_REQUIRES_VALUE | FLAG_OPTIONAL_VALUE;
  58. struct ArgInfo {
  59. uint32_t flags;
  60. bool (Options::*func)(const std::string&, const std::string&, bool);
  61. };
  62. bool HandleArg(const std::string& arg, const std::string& value, const ArgInfo& info,
  63. bool from_env = false);
  64. bool SetNumeric(const std::string&, const std::string&, bool);
  65. bool SetNumericEnvOnly(const std::string&, const std::string&, bool);
  66. bool SetBool(const std::string&, const std::string&, bool);
  67. bool SetString(const std::string&, const std::string&, bool);
  68. bool SetIterations(const std::string&, const std::string&, bool);
  69. bool SetXmlFile(const std::string&, const std::string&, bool);
  70. bool SetPrintTime(const std::string&, const std::string&, bool);
  71. const static std::unordered_map<std::string, ArgInfo> kArgs;
  72. };
  73. } // namespace gtest_extras
  74. } // namespace android