File indexing completed on 2024-04-21 04:40:04

0001 /* This file is part of the KDE project
0002    Copyright (C) 2012-2013 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "IdentifierTest.h"
0021 
0022 #include <KDb>
0023 
0024 #include <QTest>
0025 
0026 QTEST_GUILESS_MAIN(IdentifierTest)
0027 
0028 void IdentifierTest::initTestCase()
0029 {
0030 }
0031 
0032 void IdentifierTest::testStringToIdentifier_data()
0033 {
0034     QTest::addColumn<QString>("string1");
0035     QTest::addColumn<QString>("string2");
0036 
0037     QTest::newRow("empty") << "" << "";
0038     QTest::newRow("underscore") << "_" << "_";
0039     QTest::newRow("whitespace") << " \n   \t" << "";
0040     QTest::newRow("special chars") << ": \\-abc" << "_abc";
0041     QTest::newRow("special chars2") << " */$" << "_";
0042     QTest::newRow("Upper case") << "a A b2" << "a_A_b2";
0043     QTest::newRow("non-alpha") << "1" << "_1";
0044     QTest::newRow("non-latin1") << QString::fromUtf8("Ñ©Æ ᑫኂ") << "NcAE_kexi";
0045     QTest::newRow("umlauts")
0046             << QString::fromUtf8("Falsches Üben von Xylophonmusik quält jeden größeren Zwerg")
0047             << "Falsches_Ueben_von_Xylophonmusik_quaelt_jeden_groesseren_Zwerg";
0048 }
0049 
0050 void IdentifierTest::testStringToIdentifier()
0051 {
0052     QFETCH(QString, string1);
0053     QFETCH(QString, string2);
0054     QCOMPARE(KDb::stringToIdentifier(string1), string2);
0055 }
0056 
0057 void IdentifierTest::testIsIdentifier_data()
0058 {
0059     QTest::addColumn<QString>("string");
0060     QTest::addColumn<bool>("result");
0061     QTest::newRow("empty") << "" << false;
0062     QTest::newRow("empty") << QString() << false;
0063     QTest::newRow("zero") << "\0" << false;
0064     QTest::newRow("space") << " " << false;
0065     QTest::newRow("number") << "7" << false;
0066     QTest::newRow("underscore") << "_" << true;
0067     QTest::newRow("alpha") << "abc_2" << true;
0068     QTest::newRow("upper") << "Abc_2" << true;
0069     QTest::newRow("upper2") << "_7" << true;
0070 }
0071 
0072 void IdentifierTest::testIsIdentifier()
0073 {
0074     QFETCH(QString, string);
0075     QFETCH(bool, result);
0076     QCOMPARE(KDb::isIdentifier(string), result);
0077     QCOMPARE(KDb::isIdentifier(string.toLatin1()), result);
0078 }
0079 
0080 void IdentifierTest::escapeIdentifier_data()
0081 {
0082     QTest::addColumn<QString>("string");
0083     QTest::addColumn<QString>("result"); // quotes not always added
0084     QTest::addColumn<QString>("resultWithForcedQuotes"); // quotes always added
0085     QTest::newRow("empty") << "" << QString() << "\"\"";
0086     QTest::newRow("empty") << QString() << QString() << "\"\"";
0087     QTest::newRow("\"") << "\"" << "\"\"\"\"" << "\"\"\"\"";
0088     QTest::newRow("\"-\"") << "\"-\"" << "\"\"\"-\"\"\"" << "\"\"\"-\"\"\"";
0089     QTest::newRow("\t") << "\t" << "\"\t\"" << "\"\t\"";
0090     QTest::newRow("id") << "id" << "id" << "\"id\"";
0091     QTest::newRow("keyword") << "select" << "\"select\"" << "\"select\"";
0092     QTest::newRow("alpha") << "a b" << "\"a b\"" << "\"a b\"";
0093 }
0094 
0095 void IdentifierTest::escapeIdentifier()
0096 {
0097     QFETCH(QString, string);
0098     QFETCH(QString, result);
0099     QFETCH(QString, resultWithForcedQuotes);
0100     QCOMPARE(KDb::escapeIdentifier(string), result);
0101     QCOMPARE(KDb::escapeIdentifier(string.toLatin1()), result.toLatin1());
0102     QCOMPARE(KDb::escapeIdentifierAndAddQuotes(string), resultWithForcedQuotes);
0103     QCOMPARE(KDb::escapeIdentifierAndAddQuotes(string.toLatin1()), resultWithForcedQuotes.toLatin1());
0104 }
0105 
0106 void IdentifierTest::cleanupTestCase()
0107 {
0108 }