Skip to content

Commit 3e8b33e

Browse files
committed
Added tests for cppcommandline::Option
1 parent 71b2318 commit 3e8b33e

File tree

4 files changed

+157
-79
lines changed

4 files changed

+157
-79
lines changed

include/cppcommandline.h

Lines changed: 22 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ namespace cppcommandline
1212
class Option
1313
{
1414
public:
15-
enum class Match
16-
{
17-
None,
18-
Argument,
19-
ArgumentAndValue
20-
};
21-
2215
Option() :
2316
d(std::unique_ptr<OptionPrivate>(new OptionPrivate))
2417
{
@@ -34,6 +27,19 @@ class Option
3427
d->longName = longName;
3528
}
3629

30+
Option(Option &&option) :
31+
d(option.d.release())
32+
{
33+
34+
}
35+
36+
Option(const Option &option) = delete;
37+
38+
Option &operator=(Option &&option)
39+
{
40+
d.reset(option.d.release()); return *this;
41+
}
42+
3743
bool isPositional() const
3844
{
3945
return d->longName.empty();
@@ -94,20 +100,6 @@ class Option
94100
return val;
95101
}
96102

97-
Option(Option &&option) :
98-
d(option.d.release())
99-
{
100-
101-
}
102-
103-
Option(const Option &option) = delete;
104-
105-
Option &operator=(Option &&option)
106-
{
107-
d.reset(option.d.release());
108-
return *this;
109-
}
110-
111103
Option &asShortName(std::string shortName)
112104
{
113105

@@ -163,49 +155,17 @@ class Option
163155
setValueBinding(&value);
164156
}
165157

166-
Match match(std::string argument, std::string next = std::string())
158+
private:
159+
enum class Type
167160
{
168-
Match result = Match::None;
169-
KeyValue keyValue = getKeyValue(argument);
170-
171-
if(keyValue.key.empty()) //either positional or next is a value
172-
{
173-
if(isLongNameArgument(argument))
174-
{
175-
176-
}
177-
else if(isShortNameArgument(argument))
178-
{
179-
180-
}
181-
else //positional, next is not part of this resolution
182-
{
183-
if(d->type == Type::Bool) //next is not part of this resolution
184-
{
185-
186-
}
187-
else
188-
{
189-
190-
}
191-
}
192-
}
193-
else //next is not part of this resolution
194-
{
195-
if(isLongNameArgument(keyValue.key))
196-
{
197-
198-
}
199-
else if(isShortNameArgument(keyValue.key))
200-
{
201-
202-
}
203-
}
204-
205-
return result;
206-
}
161+
Undefined,
162+
String,
163+
Integer,
164+
LongLong,
165+
Double,
166+
Bool
167+
};
207168

208-
private:
209169
union DefaultValue
210170
{
211171
int i;
@@ -229,16 +189,6 @@ class Option
229189
std::string value;
230190
};
231191

232-
enum class Type
233-
{
234-
Undefined,
235-
String,
236-
Integer,
237-
LongLong,
238-
Double,
239-
Bool
240-
};
241-
242192
struct OptionPrivate
243193
{
244194
std::string longName;

test/cppcommandlinetest.cpp

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
void CppCommandLineTest::OptionDefaultCtor()
77
{
88
SCENARIO("Option default constructor")
9-
109
cppcommandline::Option option;
1110
QCOMPARE(true, option.isPositional());
1211
QCOMPARE(std::string(), option.longName());
@@ -17,4 +16,124 @@ void CppCommandLineTest::OptionDefaultCtor()
1716
QCOMPARE(nullptr, option.boundValue<std::string>());
1817
}
1918

19+
void CppCommandLineTest::OptionLongNameCtor()
20+
{
21+
{
22+
SCENARIO("Option long name constructor")
23+
cppcommandline::Option option("longName1");
24+
QCOMPARE(std::string("longName1"), option.longName());
25+
}
26+
27+
{
28+
SCENARIO("Option invalid long name")
29+
QVERIFY_EXCEPTION_THROWN(cppcommandline::Option option("long.name"), std::logic_error);
30+
}
31+
}
32+
33+
void CppCommandLineTest::OptionMoveCtor()
34+
{
35+
SCENARIO("Option move ctor")
36+
cppcommandline::Option option(cppcommandline::Option("longName"));
37+
QCOMPARE(std::string("longName"), option.longName());
38+
}
39+
40+
void CppCommandLineTest::OptionMoveAssignment()
41+
{
42+
SCENARIO("Option move assignment")
43+
cppcommandline::Option option = cppcommandline::Option("longName");
44+
QCOMPARE(std::string("longName"), option.longName());
45+
}
46+
47+
void CppCommandLineTest::positional()
48+
{
49+
{
50+
SCENARIO("Option is positional without any names")
51+
cppcommandline::Option option;
52+
QCOMPARE(true, option.isPositional());
53+
}
54+
55+
{
56+
SCENARIO("Option is not positional if it has a name")
57+
cppcommandline::Option option("longName");
58+
QCOMPARE(false, option.isPositional());
59+
}
60+
}
61+
62+
void CppCommandLineTest::shortName()
63+
{
64+
{
65+
SCENARIO("Option short name")
66+
cppcommandline::Option option = std::move(cppcommandline::Option("longName1").asShortName("o"));
67+
QCOMPARE(std::string("o"), option.shortName());
68+
}
69+
70+
{
71+
SCENARIO("Option invalid short name")
72+
QVERIFY_EXCEPTION_THROWN(cppcommandline::Option("longName").asShortName("o1"), std::logic_error);
73+
}
74+
}
75+
76+
void CppCommandLineTest::description()
77+
{
78+
SCENARIO("Option description")
79+
cppcommandline::Option option = std::move(cppcommandline::Option("longName").withDescription("My fancty description"));
80+
QCOMPARE(std::string("My fancty description"), option.description());
81+
}
82+
83+
void CppCommandLineTest::required()
84+
{
85+
{
86+
SCENARIO("Option required")
87+
cppcommandline::Option option = std::move(cppcommandline::Option("longName").required());
88+
QCOMPARE(true, option.isRequired());
89+
}
90+
91+
{
92+
SCENARIO("Option with default value cannot be required")
93+
QVERIFY_EXCEPTION_THROWN(cppcommandline::Option("longName").withDefaultValue(10).required(), std::logic_error);
94+
}
95+
}
96+
97+
void CppCommandLineTest::defaultValue()
98+
{
99+
{
100+
SCENARIO("Option default value")
101+
cppcommandline::Option option = std::move(cppcommandline::Option("longName").withDefaultValue(std::string("option")));
102+
QCOMPARE(std::string("option"), option.defaultValue<std::string>());
103+
}
104+
105+
{
106+
SCENARIO("Option default value cannot be specified for required options")
107+
QVERIFY_EXCEPTION_THROWN(cppcommandline::Option("longName").required().withDefaultValue(std::string("option")), std::logic_error);
108+
}
109+
110+
{
111+
SCENARIO("Option with default value cannot return different type")
112+
cppcommandline::Option option = std::move(cppcommandline::Option("longName").withDefaultValue(std::string("option")));
113+
QVERIFY_EXCEPTION_THROWN(option.defaultValue<int>(), std::logic_error);
114+
}
115+
}
116+
117+
void CppCommandLineTest::boundValue()
118+
{
119+
{
120+
SCENARIO("Option bound value")
121+
std::string boundValue;
122+
cppcommandline::Option option;
123+
option.bindTo(boundValue);
124+
QCOMPARE(&boundValue, option.boundValue<std::string>());
125+
}
126+
127+
{
128+
SCENARIO("Option bound value with differently typed default value is not possible")
129+
std::string boundValue;
130+
cppcommandline::Option option = std::move(cppcommandline::Option().withDefaultValue(10));
131+
QVERIFY_EXCEPTION_THROWN(option.bindTo(boundValue), std::logic_error);
132+
}
133+
}
134+
135+
136+
137+
138+
20139
QTEST_APPLESS_MAIN(CppCommandLineTest)

test/cppcommandlinetest.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ class CppCommandLineTest : public QObject
77
Q_OBJECT
88
private slots:
99
void OptionDefaultCtor();
10+
void OptionLongNameCtor();
11+
void OptionMoveCtor();
12+
void OptionMoveAssignment();
13+
void positional();
14+
void shortName();
15+
void description();
16+
void required();
17+
void defaultValue();
18+
void boundValue();
1019
};

test/qtestbdd.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,19 @@ namespace qtestbdd
120120

121121
//gtestbdd
122122
#define FEATURE(description)\
123-
qInfo() << "FEATURE " << description;
123+
qInfo() << "FEATURE " << description;
124124

125125
#define SCENARIO(description)\
126-
qtestbdd::Scenario s(description);
126+
qtestbdd::Scenario s(description);
127127

128128
#define GIVEN(description)\
129-
s.given(description);
129+
s.given(description);
130130

131131
#define WHEN(description)\
132-
s.when(description);
132+
s.when(description);
133133

134134
#define THEN(description)\
135-
s.then(description);
135+
s.then(description);
136136

137137
#define AND(description)\
138-
s.et(description);
138+
s.et(description);

0 commit comments

Comments
 (0)