File indexing completed on 2024-12-01 12:38:39

0001 /*
0002     SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 #include "indentdetect_test.h"
0006 #include "kateconfig.h"
0007 #include "katedocument.h"
0008 #include "kateindentdetecter.cpp" // HACK
0009 #include "kateindentdetecter.h"
0010 
0011 #include <QDebug>
0012 #include <QTest>
0013 
0014 using namespace KTextEditor;
0015 
0016 QTEST_MAIN(IndentDetectTest)
0017 
0018 void IndentDetectTest::test_data()
0019 {
0020     QString dir = QLatin1String(TEST_DATA_DIR) + QStringLiteral("/indent_detect/");
0021 
0022     // The document
0023     QTest::addColumn<QString>("docPath");
0024     QTest::addColumn<bool>("expected_useTabs");
0025     QTest::addColumn<int>("expected_indentWidth");
0026 
0027     QTest::addRow("2space") << QString(dir + QStringLiteral("2space.js")) << false << 2;
0028     QTest::addRow("4space") << QString(dir + QStringLiteral("4space.cpp")) << false << 4;
0029     QTest::addRow("tabs") << QString(dir + QStringLiteral("tab.c")) << true << 4;
0030     QTest::addRow("this_file") << QString(dir + QStringLiteral("indentdetect_test.cpp")) << false << 4;
0031     QTest::addRow("xml_1_space") << QString(dir + QStringLiteral("a.xml")) << false << 1;
0032     QTest::addRow("main_bad_1_space") << QString(dir + QStringLiteral("main_bad_1_space.cpp")) << false << 4;
0033 }
0034 
0035 void IndentDetectTest::test()
0036 {
0037     QFETCH(QString, docPath);
0038     QFETCH(bool, expected_useTabs);
0039     QFETCH(int, expected_indentWidth);
0040 
0041     DocumentPrivate doc;
0042     doc.config()->setAutoDetectIndent(true);
0043     doc.openUrl(QUrl::fromLocalFile(docPath));
0044     QVERIFY(!doc.isEmpty());
0045 
0046     int actualIndentWidth = doc.config()->indentationWidth();
0047     bool actual_useTabs = !doc.config()->replaceTabsDyn();
0048 
0049     QCOMPARE(actual_useTabs, expected_useTabs);
0050     if (!expected_useTabs) {
0051         QCOMPARE(actualIndentWidth, expected_indentWidth);
0052     }
0053 }
0054 
0055 void IndentDetectTest::bench()
0056 {
0057     // kate document is fairly large, lets use it for benchmarking
0058     const QString file = QString::fromLatin1(TEST_DATA_DIR) + QStringLiteral("../../src/document/katedocument.cpp");
0059     DocumentPrivate doc;
0060     doc.openUrl(QUrl::fromLocalFile(file));
0061     QVERIFY(!doc.isEmpty());
0062     QVERIFY(QUrl::fromLocalFile(file).isValid());
0063 
0064     QBENCHMARK {
0065         KateIndentDetecter d(&doc);
0066         d.detect(doc.config()->indentationWidth(), doc.config()->replaceTabsDyn());
0067     }
0068 }
0069 
0070 #include "moc_indentdetect_test.cpp"