1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include <stringutils.h>
36 #include <QtTest/QtTest>
38 //TESTED_COMPONENT=src/libs/utils
40 class TestMacroExpander : public Utils::AbstractQtcMacroExpander
43 virtual bool resolveMacro(const QString &name, QString *ret)
45 if (name == QLatin1String("a")) {
46 *ret = QLatin1String("hi");
53 class tst_StringUtils : public QObject
58 void testWithTildeHomePath();
59 void testMacroExpander_data();
60 void testMacroExpander();
66 void tst_StringUtils::testWithTildeHomePath()
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("~"));
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")));
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")));
97 void tst_StringUtils::testMacroExpander_data()
100 QTest::addColumn<QString>("in");
101 QTest::addColumn<QString>("out");
103 static const struct {
104 const char * const in;
105 const char * const out;
109 { "pre%{a}", "prehi" },
110 { "%{a}post", "hipost" },
111 { "pre%{a}post", "prehipost" },
112 { "%{a}%{a}", "hihi" },
113 { "%{a}text%{a}", "hitexthi" },
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);
121 void tst_StringUtils::testMacroExpander()
124 QFETCH(QString, out);
126 Utils::expandMacros(&in, &mx);
130 QTEST_MAIN(tst_StringUtils)
132 #include "tst_stringutils.moc"