1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* This file is part of KDevelop
*
* Copyright 2016 Carlos Nihelton <carlosnsoliveira@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "test_replacementparser.h"
// SUT
#include "parsers/replacementparser.h"
// KDevPlatform
#include <tests/autotestshell.h>
#include <tests/testcore.h>
// Qt
#include <QtTest>
using namespace KDevelop;
using namespace ClangTidy;
void ReplacementsParserTester::initTestCase()
{
// Called before the first testfunction is executed
AutoTestShell::init({ "kdevclangtidy" });
TestCore::initialize(Core::NoUi);
}
void ReplacementsParserTester::cleanupTestCase()
{
// Called after the last testfunction was executed
TestCore::shutdown();
}
void ReplacementsParserTester::doTest()
{
ReplacementParser parser("data/plugin.cpp.yaml", "data/plugin.cpp");
parser.parse();
auto v = parser.allReplacements();
QVERIFY(!v.isEmpty());
QCOMPARE(v.length(), static_cast<int>(parser.count()));
QCOMPARE(v[0].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[0].offset, size_t(6263));
QCOMPARE(v[0].length, size_t(1));
QCOMPARE(v[0].replacementText, QString());
QCOMPARE(v[0].range.start().line() + 1, 155); // as would appear in editor.
QCOMPARE(v[0].range.start().column() + 1, 9); // as would appear in editor.
QCOMPARE(v[1].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[1].offset, size_t(6267));
QCOMPARE(v[1].length, size_t(0));
QCOMPARE(v[1].replacementText, QString(" == nullptr"));
QCOMPARE(v[1].range.start().line() + 1, 155);
QCOMPARE(v[1].range.start().column() + 1, 13);
QCOMPARE(v[2].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[2].offset, size_t(6561));
QCOMPARE(v[2].length, size_t(1));
QCOMPARE(v[2].replacementText, QString());
QCOMPARE(v[2].range.start().line() + 1, 162);
QCOMPARE(v[2].range.start().column() + 1, 9);
QCOMPARE(v[3].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[3].offset, size_t(6569));
QCOMPARE(v[3].length, size_t(0));
QCOMPARE(v[3].replacementText, QStringLiteral(" == nullptr"));
QCOMPARE(v[3].range.start().line() + 1, 162);
QCOMPARE(v[3].range.start().column() + 1, 17);
QCOMPARE(v[4].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[4].offset, size_t(7233));
QCOMPARE(v[4].length, size_t(69));
QCOMPARE(v[4].replacementText, QStringLiteral("// TODO(cnihelton): auto detect clang-tidy executable"
" instead of hard-coding it."));
QCOMPARE(v[4].range.start().line() + 1, 181);
QCOMPARE(v[4].range.start().column() + 1, 5);
QCOMPARE(v[5].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[5].offset, size_t(8800));
QCOMPARE(v[5].length, size_t(4));
QCOMPARE(v[5].replacementText, QStringLiteral("auto "));
QCOMPARE(v[5].range.start().line() + 1, 210);
QCOMPARE(v[5].range.start().column() + 1, 9);
QCOMPARE(v[6].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[6].offset, size_t(8945));
QCOMPARE(v[6].length, size_t(4));
QCOMPARE(v[6].replacementText, QStringLiteral("auto "));
QCOMPARE(v[6].range.start().line() + 1, 214);
QCOMPARE(v[6].range.start().column() + 1, 5);
QCOMPARE(v[7].range.document.str(), QStringLiteral("data/plugin.cpp"));
QCOMPARE(v[7].offset, size_t(9406));
QCOMPARE(v[7].length, size_t(1));
QCOMPARE(v[7].replacementText, QString());
QCOMPARE(v[7].range.start().line() + 1, 238);
QCOMPARE(v[7].range.start().column() + 1, 9);
// Invalid stuff.
// Modified the plugin.cpp.yaml file to try to cause and exception during parsing.
// Added three lines from other file, which goes beyond the end of the plugin.cpp file in the last offsets.
// The parser didn't throw exception event though it might have generated empty ranges for the last replacements.
// And also invalidates the generated replacement.
QVERIFY(!v[15].range.isValid());
QCOMPARE(v[15].range.document.str(), QStringLiteral(""));
QCOMPARE(v[15].offset, size_t(0));
QCOMPARE(v[15].length, size_t(0));
QCOMPARE(v[15].replacementText, QString());
QCOMPARE(v[15].range.start().line() + 1, 0);
QCOMPARE(v[15].range.start().column() + 1, 0);
// testing multibyte chars in source code.
ReplacementParser nihonParser("data/nihon_plugin.cpp.yaml", "data/nihon_plugin.cpp");
nihonParser.parse();
auto nv = nihonParser.allReplacements();
QVERIFY(!nv.isEmpty());
QCOMPARE(nv.length(), static_cast<int>(nihonParser.count()));
QCOMPARE(nv[0].range.document.str(), QStringLiteral("data/nihon_plugin.cpp"));
QCOMPARE(nv[0].offset, size_t(10165));
QCOMPARE(nv[0].length, size_t(1));
QCOMPARE(nv[0].replacementText, QString());
QCOMPARE(nv[0].range.start().line() + 1, 288);
QCOMPARE(nv[0].range.start().column() + 1, 9);
QCOMPARE(nv[1].range.document.str(), QStringLiteral("data/nihon_plugin.cpp"));
QCOMPARE(nv[1].offset, size_t(10169));
QCOMPARE(nv[1].length, size_t(0));
QCOMPARE(nv[1].replacementText, QStringLiteral(" == nullptr"));
QCOMPARE(nv[1].range.start().line() + 1, 288);
QCOMPARE(nv[1].range.start().column() + 1, 13);
QCOMPARE(nv[2].range.document.str(), QStringLiteral("data/nihon_plugin.cpp"));
QCOMPARE(nv[2].offset, size_t(10463));
QCOMPARE(nv[2].length, size_t(1));
QCOMPARE(nv[2].replacementText, QString());
QCOMPARE(nv[2].range.start().line() + 1, 295);
QCOMPARE(nv[2].range.start().column() + 1, 9);
QCOMPARE(nv[3].range.document.str(), QStringLiteral("data/nihon_plugin.cpp"));
QCOMPARE(nv[3].offset, size_t(10471));
QCOMPARE(nv[3].length, size_t(0));
QCOMPARE(nv[3].replacementText, QStringLiteral(" == nullptr"));
QCOMPARE(nv[3].range.start().line() + 1, 295);
QCOMPARE(nv[3].range.start().column() + 1, 17);
QCOMPARE(nv[4].range.document.str(), QStringLiteral("data/nihon_plugin.cpp"));
QCOMPARE(nv[4].offset, size_t(11135));
QCOMPARE(nv[4].length, size_t(69));
QCOMPARE(nv[4].replacementText, QStringLiteral("// TODO(cnihelton): auto detect clang-tidy executable "
"instead of hard-coding it."));
QCOMPARE(nv[4].range.start().line() + 1, 314);
QCOMPARE(nv[4].range.start().column() + 1, 5);
}
QTEST_GUILESS_MAIN(ReplacementsParserTester);
|