Initial import from the monolithic Qt.
[profile/ivi/qtxmlpatterns.git] / tests / auto / qapplicationargumentparser / tst_qapplicationargumentparser.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52
53 #include "qapplicationargumentparser_p.h"
54 #include "qapplicationargument_p.h"
55 #include <QtTest/QtTest>
56
57 Q_DECLARE_METATYPE(QList<QApplicationArgument>);
58 Q_DECLARE_METATYPE(QApplicationArgumentParser::ExitCode);
59
60 /*!
61   \class tst_QApplicationArgumentParser
62   \brief The class tst_QApplicationArgumentParser tests class QApplicationArgumentParser.
63   \internal
64   \since 4.5
65
66 */
67 class tst_QApplicationArgumentParser : public QObject
68 {
69     Q_OBJECT
70 private slots:
71     void negativeTest() const;
72     void negativeTest_data() const;
73     void mandatoryArguments() const;
74     void mandatoryArguments_data() const;
75 };
76
77 /*
78 Comments from notes.txt:
79
80 Different arg types:
81 * -name <mandatory value>
82 * -name <no value, it's a switch>
83
84 Both of these types in addition have a cardinality. For instance:
85 -name -name -name
86 -name value1 -name value2 -name value3
87
88 Possible Tests
89 -------------------
90     ./foo -ab -cd -
91     ./foo -ab -cd - - -
92     ./foo -ab -cd - input1 input
93     ./foo -help -
94     ./foo - -help
95
96
97     // -switch has upper limit of 2
98     ./foo -switch -switch -switch
99
100     // -switch has upper limit of 1
101     ./foo -switch -switch
102
103     // -switch has lower limit of 1
104     ./foo
105
106     ./foo -switch cruft -switch
107     ./foo -option value1 cruft -option value2
108     ./foo -option value1 cruft cruft -option value2
109     ./foo -option value1 cruft cruft cruft -option value2
110     ./foo -option -option -option2 -option2
111     ./foo -option
112     ./foo -option -option -option2
113     ./foo -option -
114     ./foo -option - -
115 */
116
117 void tst_QApplicationArgumentParser::negativeTest() const
118 {
119 }
120
121 void tst_QApplicationArgumentParser::negativeTest_data() const
122 {
123     QTest::addColumn<QStringList>("inputArgs");
124     QTest::addColumn<QApplicationArgumentParser::ExitCode>("expectedExitCode");
125     QTest::addColumn<QString>("expectedStderr");
126     QTest::addColumn<QList<QApplicationArgument> >("declarations");
127 }
128
129 void tst_QApplicationArgumentParser::mandatoryArguments() const
130 {
131    QFETCH(QStringList, inputArgs);
132    QFETCH(QList<QApplicationArgument>, inputDecls);
133
134    QApplicationArgumentParser parser(inputArgs);
135    parser.setDeclaredArguments(inputDecls);
136
137    QVERIFY(!parser.parse());
138    QCOMPARE(parser.exitCode(), QApplicationArgumentParser::ParseError);
139 }
140
141 void tst_QApplicationArgumentParser::mandatoryArguments_data() const
142 {
143     QTest::addColumn<QStringList>("inputArgs");
144     QTest::addColumn<QList<QApplicationArgument> >("inputDecls");
145
146     {
147         QStringList in;
148         in << "./appName";
149
150         QList<QApplicationArgument> decls;
151         QApplicationArgument arg1("name", QString(), QVariant::String);
152         arg1.setMinimumOccurrence(1);
153         decls.append(arg1);
154
155         QTest::newRow("A single, named, argument") << in << decls;
156     }
157 }
158
159 QTEST_APPLESS_MAIN(tst_QApplicationArgumentParser)
160 #include "tst_qapplicationargumentparser.moc"