blob: 3edbafeb65a7a5a6b2737b3dfa17956a5d2e1017 [file] [log] [blame]
Christopher Ferris1a993562018-08-21 12:43:50 -07001/*
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
17#pragma once
18
19#include <stdint.h>
20#include <stdlib.h>
21
22#include <cctype>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27namespace android {
28namespace gtest_extras {
29
30class Options {
31 public:
32 Options() = default;
33 ~Options() = default;
34
35 bool Process(const std::vector<const char*>& args, std::vector<const char*>* child_args);
36
37 size_t job_count() const { return job_count_; }
38 int num_iterations() const { return num_iterations_; }
39
Christopher Ferris3215ef12019-03-12 19:10:35 -070040 uint64_t deadline_threshold_ms() const { return numerics_.at("deadline_threshold_ms"); }
41 uint64_t slow_threshold_ms() const { return numerics_.at("slow_threshold_ms"); }
42
43 uint64_t shard_index() const { return numerics_.at("gtest_shard_index"); }
44 uint64_t total_shards() const { return numerics_.at("gtest_total_shards"); }
Christopher Ferris1a993562018-08-21 12:43:50 -070045
46 bool print_time() const { return bools_.at("gtest_print_time"); }
47 bool gtest_format() const { return bools_.at("gtest_format"); }
48 bool allow_disabled_tests() const { return bools_.at("gtest_also_run_disabled_tests"); }
49 bool list_tests() const { return bools_.at("gtest_list_tests"); }
50
51 const std::string& color() const { return strings_.at("gtest_color"); }
52 const std::string& xml_file() const { return strings_.at("xml_file"); }
53 const std::string& filter() const { return strings_.at("gtest_filter"); }
54
55 private:
56 size_t job_count_;
57 int num_iterations_;
58
59 std::unordered_map<std::string, bool> bools_;
60 std::unordered_map<std::string, std::string> strings_;
Christopher Ferris3215ef12019-03-12 19:10:35 -070061 std::unordered_map<std::string, uint64_t> numerics_;
Christopher Ferris1a993562018-08-21 12:43:50 -070062
63 enum FlagType : uint32_t {
64 FLAG_NONE = 0,
65 FLAG_CHILD = 0x1, // Argument preserved for forked child call.
66 FLAG_INCOMPATIBLE = 0x2, // Not compatible with isolation mode.
67 FLAG_ENVIRONMENT_VARIABLE = 0x4, // Can be an environment variable.
Christopher Ferrisfc501f42018-09-21 12:38:12 -070068 FLAG_REQUIRES_VALUE = 0x8, // Flag requires a non-empty value.
69 FLAG_OPTIONAL_VALUE = 0x10, // Flag takes an optional value.
Christopher Ferris1a993562018-08-21 12:43:50 -070070 };
Christopher Ferrisfc501f42018-09-21 12:38:12 -070071 static constexpr uint32_t FLAG_TAKES_VALUE = FLAG_REQUIRES_VALUE | FLAG_OPTIONAL_VALUE;
Christopher Ferris1a993562018-08-21 12:43:50 -070072
73 struct ArgInfo {
74 uint32_t flags;
75 bool (Options::*func)(const std::string&, const std::string&, bool);
76 };
77
78 bool HandleArg(const std::string& arg, const std::string& value, const ArgInfo& info,
79 bool from_env = false);
80
Christopher Ferris3215ef12019-03-12 19:10:35 -070081 bool SetNumeric(const std::string&, const std::string&, bool);
82 bool SetNumericEnvOnly(const std::string&, const std::string&, bool);
Christopher Ferris1a993562018-08-21 12:43:50 -070083 bool SetBool(const std::string&, const std::string&, bool);
84 bool SetString(const std::string&, const std::string&, bool);
85 bool SetIterations(const std::string&, const std::string&, bool);
86 bool SetXmlFile(const std::string&, const std::string&, bool);
87 bool SetPrintTime(const std::string&, const std::string&, bool);
88
89 const static std::unordered_map<std::string, ArgInfo> kArgs;
90};
91
92} // namespace gtest_extras
93} // namespace android