File: config_option_set.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (218 lines) | stat: -rw-r--r-- 8,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE config_option_set

#include "caf/config_option_set.hpp"

#include "core-test.hpp"
#include "inspector-tests.hpp"

#include <map>
#include <string>
#include <vector>

#include "caf/detail/move_if_not_ptr.hpp"
#include "caf/settings.hpp"

using std::string;
using std::vector;

using namespace caf;

namespace {

struct fixture {
  config_option_set opts;

  template <class T>
  error read(settings& cfg, std::vector<std::string> args) {
    auto res = opts.parse(cfg, std::move(args));
    if (res.first != pec::success)
      return res.first;
    return none;
  }

  template <class T>
  expected<T> read(std::vector<std::string> args) {
    settings cfg;
    auto res = opts.parse(cfg, std::move(args));
    if (res.first != pec::success)
      return {res.first};
    else if (auto x = get_as<T>(cfg, key))
      return {std::move(*x)};
    else
      return {sec::invalid_argument};
  }

  std::string key = "value";
};

} // namespace

BEGIN_FIXTURE_SCOPE(fixture)

CAF_TEST(lookup) {
  opts.add<int>("opt1,1", "test option 1")
    .add<float>("test", "opt2,2", "test option 2")
    .add<bool>("test", "flag,fl3", "test flag");
  CHECK_EQ(opts.size(), 3u);
  MESSAGE("lookup by long name");
  CHECK_NE(opts.cli_long_name_lookup("opt1"), nullptr);
  CHECK_NE(opts.cli_long_name_lookup("test.opt2"), nullptr);
  CHECK_NE(opts.cli_long_name_lookup("test.flag"), nullptr);
  MESSAGE("lookup by short name");
  CHECK_NE(opts.cli_short_name_lookup('1'), nullptr);
  CHECK_NE(opts.cli_short_name_lookup('2'), nullptr);
  CHECK_NE(opts.cli_short_name_lookup('f'), nullptr);
  CHECK_NE(opts.cli_short_name_lookup('l'), nullptr);
  CHECK_NE(opts.cli_short_name_lookup('3'), nullptr);
}

CAF_TEST(parse with ref syncing) {
  using ls = vector<string>;     // list of strings
  using ds = dictionary<string>; // dictionary of strings
  auto foo_i = 0;
  auto foo_f = 0.f;
  auto foo_b = false;
  auto bar_s = string{};
  auto bar_l = ls{};
  auto bar_d = ds{};
  opts.add<int>(foo_i, "foo", "i,i", "")
    .add<float>(foo_f, "foo", "f,f", "")
    .add<bool>(foo_b, "foo", "b,b", "")
    .add<string>(bar_s, "bar", "s,s", "")
    .add<vector<string>>(bar_l, "bar", "l,l", "")
    .add<dictionary<string>>(bar_d, "bar", "d,d", "");
  settings cfg;
  vector<string> args{"-i42",
                      "-f",
                      "1e2",
                      "-shello",
                      "--bar.l=[\"hello\", \"world\"]",
                      "-d",
                      "{a=\"a\",b=\"b\"}",
                      "-b"};
  MESSAGE("parse arguments");
  auto res = opts.parse(cfg, args);
  CHECK_EQ(res.first, pec::success);
  if (res.second != args.end())
    CAF_FAIL("parser stopped at: " << *res.second);
  MESSAGE("verify referenced values");
  CHECK_EQ(foo_i, 42);
  CHECK_EQ(foo_f, 1e2);
  CHECK_EQ(foo_b, true);
  CHECK_EQ(bar_s, "hello");
  CHECK_EQ(bar_l, ls({"hello", "world"}));
  CHECK_EQ(bar_d, ds({{"a", "a"}, {"b", "b"}}));
  MESSAGE("verify dictionary content");
  CHECK_EQ(get_as<int>(cfg, "foo.i"), 42);
}

CAF_TEST(string parameters) {
  opts.add<std::string>("value,v", "some value");
  CHECK_EQ(read<std::string>({"--value=foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"-v", "foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"-vfoobar"}), "foobar");
}

CAF_TEST(flat CLI options) {
  key = "foo.bar";
  opts.add<std::string>("?foo", "bar,b", "some value");
  CHECK(opts.begin()->has_flat_cli_name());
  CHECK_EQ(read<std::string>({"-b", "foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"--bar=foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"--foo.bar=foobar"}), "foobar");
}

CAF_TEST(flat CLI parsing with nested categories) {
  key = "foo.goo.bar";
  opts.add<std::string>("?foo.goo", "bar,b", "some value");
  CHECK(opts.begin()->has_flat_cli_name());
  CHECK_EQ(read<std::string>({"-b", "foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"--bar=foobar"}), "foobar");
  CHECK_EQ(read<std::string>({"--foo.goo.bar=foobar"}), "foobar");
}

CAF_TEST(square brackets are optional on the command line) {
  using int_list = std::vector<int>;
  opts.add<int_list>("global", "value,v", "some list");
  CHECK_EQ(read<int_list>({"--value=[1]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"--value=[1,]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"--value=[ 1 , ]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"--value=[1,2]"}), int_list({1, 2}));
  CHECK_EQ(read<int_list>({"--value=[1, 2, 3]"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"--value=[1, 2, 3, ]"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"--value=1"}), int_list({1}));
  CHECK_EQ(read<int_list>({"--value=1,2,3"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"--value=1, 2 , 3 , "}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"-v", "[1]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"-v", "[1,]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"-v", "[ 1 , ]"}), int_list({1}));
  CHECK_EQ(read<int_list>({"-v", "[1,2]"}), int_list({1, 2}));
  CHECK_EQ(read<int_list>({"-v", "[1, 2, 3]"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"-v", "[1, 2, 3, ]"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"-v", "1"}), int_list({1}));
  CHECK_EQ(read<int_list>({"-v", "1,2,3"}), int_list({1, 2, 3}));
  CHECK_EQ(read<int_list>({"-v", "1, 2 , 3 , "}), int_list({1, 2, 3}));
}

#define SUBTEST(msg)                                                           \
  opts.clear();                                                                \
  if (true)

CAF_TEST(CLI arguments override defaults) {
  using int_list = std::vector<int>;
  using string_list = std::vector<std::string>;
  SUBTEST("with ref syncing") {
    settings cfg;
    int_list ints;
    string_list strings;
    MESSAGE("add --foo and --bar options");
    opts.add(strings, "global", "foo,f", "some list");
    opts.add(ints, "global", "bar,b", "some list");
    MESSAGE("test integer lists");
    ints = int_list{1, 2, 3};
    cfg["bar"] = config_value{ints};
    CHECK_EQ(get_as<int_list>(cfg, "bar"), int_list({1, 2, 3}));
    CHECK_EQ(read<int_list>(cfg, {"--bar=[10, 20, 30]"}), none);
    CHECK_EQ(ints, int_list({10, 20, 30}));
    CHECK_EQ(get_as<int_list>(cfg, "bar"), int_list({10, 20, 30}));
    MESSAGE("test string lists");
    strings = string_list{"one", "two", "three"};
    cfg["foo"] = config_value{strings};
    CHECK_EQ(get_as<string_list>(cfg, "foo"),
             string_list({"one", "two", "three"}));
    CHECK_EQ(read<string_list>(cfg, {R"_(--foo=["hello", "world"])_"}), none);
    CHECK_EQ(strings, string_list({"hello", "world"}));
    CHECK_EQ(get_as<string_list>(cfg, "foo"), string_list({"hello", "world"}));
  }
  SUBTEST("without ref syncing") {
    settings cfg;
    MESSAGE("add --foo and --bar options");
    opts.add<string_list>("global", "foo,f", "some list");
    opts.add<int_list>("global", "bar,b", "some list");
    MESSAGE("test integer lists");
    cfg["bar"] = config_value{int_list{1, 2, 3}};
    CHECK_EQ(get_as<int_list>(cfg, "bar"), int_list({1, 2, 3}));
    CHECK_EQ(read<int_list>(cfg, {"--bar=[10, 20, 30]"}), none);
    CHECK_EQ(get_as<int_list>(cfg, "bar"), int_list({10, 20, 30}));
    MESSAGE("test string lists");
    cfg["foo"] = config_value{string_list{"one", "two", "three"}};
    CHECK_EQ(get_as<string_list>(cfg, "foo"),
             string_list({"one", "two", "three"}));
    CHECK_EQ(read<string_list>(cfg, {R"_(--foo=["hello", "world"])_"}), none);
    CHECK_EQ(get_as<string_list>(cfg, "foo"), string_list({"hello", "world"}));
  }
}

CAF_TEST(CLI arguments may use custom types) {
  settings cfg;
  opts.add<foobar>("global", "foobar,f", "test option");
  CHECK_EQ(read<foobar>(cfg, {"-f{foo=\"hello\",bar=\"world\"}"}), none);
  if (auto fb = get_as<foobar>(cfg, "foobar"); CHECK(fb))
    CHECK_EQ(*fb, foobar("hello", "world"));
}

END_FIXTURE_SCOPE()