OSDN Git Service

b24e3d1df5396535b30a200a74ba803539393faa
[qt-creator-jp/qt-creator-jp.git] / tests / auto / utils_stringutils / tst_stringutils.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include <stringutils.h>
35
36 #include <QtTest/QtTest>
37
38 //TESTED_COMPONENT=src/libs/utils
39
40 class TestMacroExpander : public Utils::AbstractQtcMacroExpander
41 {
42 public:
43     virtual bool resolveMacro(const QString &name, QString *ret)
44     {
45         if (name == QLatin1String("a")) {
46             *ret = QLatin1String("hi");
47             return true;
48         }
49         return false;
50     }
51 };
52
53 class tst_StringUtils : public QObject
54 {
55     Q_OBJECT
56
57 private slots:
58     void testWithTildeHomePath();
59     void testMacroExpander_data();
60     void testMacroExpander();
61
62 private:
63     TestMacroExpander mx;
64 };
65
66 void tst_StringUtils::testWithTildeHomePath()
67 {
68 #ifndef Q_OS_WIN
69     // home path itself
70     QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QString::fromLatin1("~"));
71     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1Char('/')),
72              QString::fromLatin1("~"));
73     QCOMPARE(Utils::withTildeHomePath(QString::fromLatin1("/unclean/..") + QDir::homePath()),
74              QString::fromLatin1("~"));
75     // sub of home path
76     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo")),
77              QString::fromLatin1("~/foo"));
78     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo/")),
79              QString::fromLatin1("~/foo"));
80     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/some/path/file.txt")),
81              QString::fromLatin1("~/some/path/file.txt"));
82     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/some/unclean/../path/file.txt")),
83              QString::fromLatin1("~/some/path/file.txt"));
84     // not sub of home path
85     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")),
86              QString(QDir::homePath() + QString::fromLatin1("/../foo")));
87 #else
88     // windows: should return same as input
89     QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QDir::homePath());
90     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/foo")),
91              QDir::homePath() + QString::fromLatin1("/foo"));
92     QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")),
93              Utils::withTildeHomePath(QDir::homePath() + QString::fromLatin1("/../foo")));
94 #endif
95 }
96
97 void tst_StringUtils::testMacroExpander_data()
98
99 {
100     QTest::addColumn<QString>("in");
101     QTest::addColumn<QString>("out");
102
103     static const struct {
104         const char * const in;
105         const char * const out;
106     } vals[] = {
107         { "text", "text" },
108         { "%{a}", "hi" },
109         { "pre%{a}", "prehi" },
110         { "%{a}post", "hipost" },
111         { "pre%{a}post", "prehipost" },
112         { "%{a}%{a}", "hihi" },
113         { "%{a}text%{a}", "hitexthi" },
114     };
115
116     for (unsigned i = 0; i < sizeof(vals)/sizeof(vals[0]); i++)
117         QTest::newRow(vals[i].in) << QString::fromLatin1(vals[i].in)
118                                   << QString::fromLatin1(vals[i].out);
119 }
120
121 void tst_StringUtils::testMacroExpander()
122 {
123     QFETCH(QString, in);
124     QFETCH(QString, out);
125
126     Utils::expandMacros(&in, &mx);
127     QCOMPARE(in, out);
128 }
129
130 QTEST_MAIN(tst_StringUtils)
131
132 #include "tst_stringutils.moc"