File indexing completed on 2024-05-05 03:57:37

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::fromUtf8(__FILE__) << false << 4;
0031 }
0032 
0033 void IndentDetectTest::test()
0034 {
0035     QFETCH(QString, docPath);
0036     QFETCH(bool, expected_useTabs);
0037     QFETCH(int, expected_indentWidth);
0038 
0039     DocumentPrivate doc;
0040     doc.config()->setAutoDetectIndent(true);
0041     doc.openUrl(QUrl::fromLocalFile(docPath));
0042     QVERIFY(!doc.isEmpty());
0043 
0044     int actualIndentWidth = doc.config()->indentationWidth();
0045     bool actual_useTabs = !doc.config()->replaceTabsDyn();
0046 
0047     QCOMPARE(actual_useTabs, expected_useTabs);
0048     if (!expected_useTabs) {
0049         QCOMPARE(actualIndentWidth, expected_indentWidth);
0050     }
0051 }
0052 
0053 void IndentDetectTest::bench()
0054 {
0055     // kate document is fairly large, lets use it for benchmarking
0056     const QString file = QString::fromLatin1(TEST_DATA_DIR) + QStringLiteral("../../src/document/katedocument.cpp");
0057     DocumentPrivate doc;
0058     doc.openUrl(QUrl::fromLocalFile(file));
0059     QVERIFY(!doc.isEmpty());
0060     QVERIFY(QUrl::fromLocalFile(file).isValid());
0061 
0062     QBENCHMARK {
0063         KateIndentDetecter d(&doc);
0064         d.detect(doc.config()->indentationWidth(), doc.config()->replaceTabsDyn());
0065     }
0066 }