1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | // |
31 | // W A R N I N G |
32 | // ------------- |
33 | // |
34 | // This file is not part of the Qt API. It exists purely as an |
35 | // implementation detail. This header file may change from version to |
36 | // version without notice, or even be removed. |
37 | // |
38 | // We mean it. |
39 | |
40 | #include "qapplicationargumentparser_p.h" |
41 | #include "qapplicationargument_p.h" |
42 | #include <QtTest/QtTest> |
43 | |
44 | Q_DECLARE_METATYPE(QList<QApplicationArgument>); |
45 | Q_DECLARE_METATYPE(QApplicationArgumentParser::ExitCode); |
46 | |
47 | /*! |
48 | \class tst_QApplicationArgumentParser |
49 | \brief The class tst_QApplicationArgumentParser tests class QApplicationArgumentParser. |
50 | \internal |
51 | \since 4.5 |
52 | |
53 | */ |
54 | class tst_QApplicationArgumentParser : public QObject |
55 | { |
56 | Q_OBJECT |
57 | private slots: |
58 | void negativeTest() const; |
59 | void negativeTest_data() const; |
60 | void mandatoryArguments() const; |
61 | void mandatoryArguments_data() const; |
62 | }; |
63 | |
64 | /* |
65 | Comments from notes.txt: |
66 | |
67 | Different arg types: |
68 | * -name <mandatory value> |
69 | * -name <no value, it's a switch> |
70 | |
71 | Both of these types in addition have a cardinality. For instance: |
72 | -name -name -name |
73 | -name value1 -name value2 -name value3 |
74 | |
75 | Possible Tests |
76 | ------------------- |
77 | ./foo -ab -cd - |
78 | ./foo -ab -cd - - - |
79 | ./foo -ab -cd - input1 input |
80 | ./foo -help - |
81 | ./foo - -help |
82 | |
83 | |
84 | // -switch has upper limit of 2 |
85 | ./foo -switch -switch -switch |
86 | |
87 | // -switch has upper limit of 1 |
88 | ./foo -switch -switch |
89 | |
90 | // -switch has lower limit of 1 |
91 | ./foo |
92 | |
93 | ./foo -switch cruft -switch |
94 | ./foo -option value1 cruft -option value2 |
95 | ./foo -option value1 cruft cruft -option value2 |
96 | ./foo -option value1 cruft cruft cruft -option value2 |
97 | ./foo -option -option -option2 -option2 |
98 | ./foo -option |
99 | ./foo -option -option -option2 |
100 | ./foo -option - |
101 | ./foo -option - - |
102 | */ |
103 | |
104 | void tst_QApplicationArgumentParser::negativeTest() const |
105 | { |
106 | } |
107 | |
108 | void tst_QApplicationArgumentParser::negativeTest_data() const |
109 | { |
110 | QTest::addColumn<QStringList>(name: "inputArgs" ); |
111 | QTest::addColumn<QApplicationArgumentParser::ExitCode>(name: "expectedExitCode" ); |
112 | QTest::addColumn<QString>(name: "expectedStderr" ); |
113 | QTest::addColumn<QList<QApplicationArgument> >(name: "declarations" ); |
114 | } |
115 | |
116 | void tst_QApplicationArgumentParser::mandatoryArguments() const |
117 | { |
118 | QFETCH(QStringList, inputArgs); |
119 | QFETCH(QList<QApplicationArgument>, inputDecls); |
120 | |
121 | QApplicationArgumentParser parser(inputArgs); |
122 | parser.setDeclaredArguments(inputDecls); |
123 | |
124 | QVERIFY(!parser.parse()); |
125 | QCOMPARE(parser.exitCode(), QApplicationArgumentParser::ParseError); |
126 | } |
127 | |
128 | void tst_QApplicationArgumentParser::mandatoryArguments_data() const |
129 | { |
130 | QTest::addColumn<QStringList>(name: "inputArgs" ); |
131 | QTest::addColumn<QList<QApplicationArgument> >(name: "inputDecls" ); |
132 | |
133 | { |
134 | QStringList in; |
135 | in << "./appName" ; |
136 | |
137 | QList<QApplicationArgument> decls; |
138 | QApplicationArgument arg1("name" , QString(), QVariant::String); |
139 | arg1.setMinimumOccurrence(1); |
140 | decls.append(t: arg1); |
141 | |
142 | QTest::newRow(dataTag: "A single, named, argument" ) << in << decls; |
143 | } |
144 | } |
145 | |
146 | QTEST_APPLESS_MAIN(tst_QApplicationArgumentParser) |
147 | #include "tst_qapplicationargumentparser.moc" |
148 | |