Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Options.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Options.hpp"
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std;
22using namespace toolbox;
23
25
27{
28 int var{-1};
29 bool flag{false};
30 std::string command;
31 int factor{-1};
32
33 Options opts{"Unit Test options [OPTIONS] [COMMAND]"};
34 // clang-format off
35 opts('o', "option", Value{var}.default_value(3).required(), "Option Description")
36 ('s', NoOp{}, "ShortOption Description")
37 ("long_opt", NoOp{}, "LongOption Description")
38 ('x', Switch{flag}, "Switch Description")
39 (Value{command}.default_value("init").required(), "Positional Command")
40 (Value{factor}.required(), "Positional Command");
41 // clang-format on
42
45
46 stringstream ss;
47 ss << opts;
48 const string expected{"Usage: Unit Test options [OPTIONS] [COMMAND]\n"
49 "Options:\n"
50 " -o, --option Option Description\n"
51 " -s ShortOption Description\n"
52 " --long_opt LongOption Description\n"
53 " -x Switch Description\n"};
55
56 {
57 const char* argv[] = {"executable_name", "-o", "123", "print", "456"};
58 int argc = 5;
59
60 opts.parse(argc, argv);
62 BOOST_CHECK_EQUAL(flag, false);
63 BOOST_CHECK_EQUAL(command, "print");
65 }
66 {
67 const char* argv[] = {"executable_name", "-x"};
68 int argc = 2;
69
70 opts.parse(argc, argv);
72 }
73}
74
76{
77 int var{-1};
78
79 Options opts{"Exception Test options"};
80 opts('l', "long_option", Value{var}, "LongOption Description");
81
82 // Trying to add a duplicate option fails
83 BOOST_CHECK_THROW(opts('x', "long_option", Value{var}, "Description"), std::runtime_error);
84 BOOST_CHECK_THROW(opts('l', "l_exists", Value{var}, "Description"), std::runtime_error);
85 BOOST_CHECK_THROW(opts('l', "long_option", Value{var}, "Description"), std::runtime_error);
86}
87
89{
90 {
91 int var{-1};
92 Options opts{"Options"};
93 opts('l', "long_option", Value{var}, "LongOption Description");
94
95 const char* argv[] = {"executable_name", "--bad", "123"};
96 int argc = 3;
97
98 BOOST_CHECK_THROW(opts.parse(argc, argv), std::runtime_error);
99 }
100
101 {
102 int var{-1};
103 Options opts{"Options"};
104 opts('l', "long_option", Value{var}, "LongOption Description");
105
106 const char* argv[] = {"executable_name", "-l123", "456"};
107 int argc = 3;
108
109 BOOST_CHECK_THROW(opts.parse(argc, argv), std::runtime_error);
110 }
111}
112
114{
115 int var{-1};
116 Options opts{"Options"};
117 opts('l', "long_option", Value{var}, "LongOption Description");
118
119 const char* argv[] = {"executable_name", "-l"};
120 int argc = 2;
121
122 BOOST_CHECK_THROW(opts.parse(argc, argv), std::runtime_error);
123}
124
126{
127 int var{-1};
128 std::vector<int> single;
129 std::vector<int> multiple;
130
131 {
132 Options opts{"Multitoken Test options"};
133 opts('v', Value{var}, "Raw int")('s', Value{single}, "Single int")(
134 'm', Value{multiple}.multitoken(), "Multiple ints");
135
136 const char* argv[] = {"executable_name", "-v", "1", "-s", "2", "-m", "3", "-m", "4"};
137 int argc = 9;
138
139 opts.parse(argc, argv);
144 }
145
146 {
147 Options opts{"Multitoken Test options"};
148 opts('v', Value{var}, "Raw int")('s', Value{single}, "Single int")(
149 'm', Value{multiple}.multitoken(), "Multiple ints");
150
151 const char* argv[] = {"executable_name", "-s", "1", "-s", "2"};
152 int argc = 5;
153
154 BOOST_CHECK_THROW(opts.parse(argc, argv), std::runtime_error);
155 }
156}
157
BOOST_CHECK_EQUAL(v.size(), 10U)
Command-line options parser.
BOOST_AUTO_TEST_CASE(OptionsNormalCase)
DerivedT & required()
Definition Options.hpp:45
Value & multitoken()
Definition Options.hpp:82
Value & default_value(const ValueT &value)
Definition Options.hpp:73
STL namespace.
double var(const VarAccum &v) noexcept
Definition Math.hpp:106
constexpr auto bind() noexcept
Definition Slot.hpp:92