From 03affacaa3d5530e96f869a8a6e900ca5e714918 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 5 Oct 2013 03:20:56 +0200 Subject: QCommandLineParser: pluck some low-hanging fruit re: exception safety Make QCommandLineParser::add{Help,Version}Option() QCommandLineOption::setDefaultValue() QCommandLineOptionPrivate::setNames() have transaction semantics: either they succeed, or they change nothing. It's trivial to provide this guarantee, so do it. Add a test for the surprising property that setDefaultValue("") resets defaultValues() to an empty QStringList instead of one that contains the empty string. Change-Id: I61623019de3c7d2e52c24f42cc2e23ec5fddc4da Reviewed-by: David Faure Reviewed-by: Thiago Macieira --- .../tools/qcommandlineparser/tst_qcommandlineparser.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp index f37e192ad3..d111c53551 100644 --- a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp +++ b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp @@ -69,6 +69,7 @@ private slots: void testUnknownOptionErrorHandling(); void testDoubleDash_data(); void testDoubleDash(); + void testDefaultValue(); void testProcessNotCalled(); void testEmptyArgsList(); void testMissingOptionValue(); @@ -322,6 +323,17 @@ void tst_QCommandLineParser::testDoubleDash() QCOMPARE(parser.unknownOptionNames(), QStringList()); } +void tst_QCommandLineParser::testDefaultValue() +{ + QCommandLineOption opt(QStringLiteral("name"), QStringLiteral("desc"), + QStringLiteral("valueName"), QStringLiteral("default")); + QCOMPARE(opt.defaultValues(), QStringList(QStringLiteral("default"))); + opt.setDefaultValue(QStringLiteral("")); + QCOMPARE(opt.defaultValues(), QStringList()); + opt.setDefaultValue(QStringLiteral("default")); + QCOMPARE(opt.defaultValues(), QStringList(QStringLiteral("default"))); +} + void tst_QCommandLineParser::testProcessNotCalled() { QCoreApplication app(empty_argc, empty_argv); -- cgit v1.2.3