Skip to content

Commit ae2b0e4

Browse files
committed
Parsing failures tests.
1 parent d3c7446 commit ae2b0e4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

test/cppcommandlinetest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ void CppCommandLineTest::boundValue()
124124
QCOMPARE(option.boundValue<std::string>(), &boundValue);
125125
}
126126

127+
{
128+
SCENARIO("Option bound value with default value")
129+
std::string boundValue;
130+
cppcommandline::Option option = std::move(cppcommandline::Option().withDefaultValue(std::string("filename")));
131+
option.bindTo(boundValue);
132+
QCOMPARE(option.boundValue<std::string>(), &boundValue);
133+
QCOMPARE(boundValue, std::string("filename"));
134+
}
135+
127136
{
128137
SCENARIO("Option bound value with differently typed default value is not possible")
129138
std::string boundValue;
@@ -351,7 +360,38 @@ void CppCommandLineTest::parse()
351360

352361
void CppCommandLineTest::parseFailed()
353362
{
363+
{
364+
SCENARIO("Unmatched argument")
365+
std::vector<const char*> args{"-v"};
366+
QVERIFY_EXCEPTION_THROWN(cppcommandline::Parser().parse(static_cast<int>(args.size()), const_cast<char**>(args.data())), std::logic_error);
367+
}
368+
369+
{
370+
SCENARIO("Unmatched required option")
371+
std::vector<const char*> args{"-v"};
372+
cppcommandline::Parser parser;
373+
parser.option("longName").asShortName("v");
374+
parser.option().required();
375+
QVERIFY_EXCEPTION_THROWN(parser.parse(static_cast<int>(args.size()), const_cast<char**>(args.data())), std::logic_error);
376+
}
377+
378+
{
379+
SCENARIO("Type mismatch")
380+
std::vector<const char*> args{"-v=hello"};
381+
cppcommandline::Parser parser;
382+
int value = 0;
383+
parser.option("value").asShortName("v").bindTo(value);
384+
QVERIFY_EXCEPTION_THROWN(parser.parse(static_cast<int>(args.size()), const_cast<char**>(args.data())), std::logic_error);
385+
}
354386

387+
{
388+
SCENARIO("Missing value")
389+
std::vector<const char*> args{"-v"};
390+
cppcommandline::Parser parser;
391+
int value = 0;
392+
parser.option("value").asShortName("v").bindTo(value);
393+
QVERIFY_EXCEPTION_THROWN(parser.parse(static_cast<int>(args.size()), const_cast<char**>(args.data())), std::logic_error);
394+
}
355395
}
356396

357397
QTEST_APPLESS_MAIN(CppCommandLineTest)

0 commit comments

Comments
 (0)