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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* This file is part of KDevelop
* Copyright 2012 Miha Čančula <miha@noughmad.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "test_templateclassgenerator.h"
#include "codegen_tests_config.h"
#include "language/codegen/templateclassgenerator.h"
#include "language/codegen/documentchangeset.h"
#include "language/codegen/sourcefiletemplate.h"
#include "language/codegen/templaterenderer.h"
#include "language/codegen/templatesmodel.h"
#include "tests/autotestshell.h"
#include "tests/testcore.h"
/*
* CHECK_TEMPLATE_VARIABLE expects second parameter as type with 'to' instead of 'Q' in begin
* For example, QString -> toString
*/
#define CHECK_TEMPLATE_VARIABLE(name, type, val) \
QVERIFY(variables.contains(QStringLiteral(#name))); \
QCOMPARE(variables.value(QStringLiteral(#name)).type(), val)
#define COMPARE_FILES(one, two) \
QCOMPARE(QString(one.readAll()), QString(two.readAll())) \
using namespace KDevelop;
void TestTemplateClassGenerator::initTestCase()
{
AutoTestShell::init();
TestCore::initialize(Core::NoUi);
// Use a temporary directory for the template work
tempDir.setAutoRemove(true);
baseUrl = QUrl::fromLocalFile(tempDir.path() + '/');
// Needed for extracting description out of template archives
TemplatesModel model(QStringLiteral("kdevcodegentest"));
model.refresh();
description.members << VariableDescription(QStringLiteral("QString"), QStringLiteral("name"))
<< VariableDescription(QStringLiteral("int"), QStringLiteral("number"))
<< VariableDescription(QStringLiteral("SomeCustomType"), QStringLiteral("data"));
FunctionDescription function;
function.name = QStringLiteral("doSomething");
function.isVirtual = true;
function.arguments << VariableDescription(QStringLiteral("double"), QStringLiteral("howMuch"))
<< VariableDescription(QStringLiteral("bool"), QStringLiteral("doSomethingElse"));
VariableDescriptionList args;
VariableDescriptionList returnArgs;
returnArgs << VariableDescription(QStringLiteral("int"), QStringLiteral("someOtherNumber"));
FunctionDescription otherFunction(QStringLiteral("getSomeOtherNumber"), args, returnArgs);
description.methods << function << otherFunction;
}
void TestTemplateClassGenerator::cleanupTestCase()
{
TestCore::shutdown();
}
void TestTemplateClassGenerator::fileLabelsCpp()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_cpp"));
QHash<QString,QString> labels = generator->fileLabels();
QCOMPARE(labels.size(), 2);
/*
* File labels can be translated, so we don't check their equality here.
* But they have to be present and non-empty
*/
QVERIFY(labels.contains(QStringLiteral("Header")));
QVERIFY(!labels[QStringLiteral("Header")].isEmpty());
QVERIFY(labels.contains(QStringLiteral("Implementation")));
QVERIFY(!labels[QStringLiteral("Implementation")].isEmpty());
}
void TestTemplateClassGenerator::fileLabelsYaml()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
QHash<QString,QString> labels = generator->fileLabels();
QCOMPARE(labels.size(), 1);
QVERIFY(labels.contains(QStringLiteral("Description")));
QVERIFY(!labels[QStringLiteral("Description")].isEmpty());
}
void TestTemplateClassGenerator::defaultFileUrlsCpp()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_cpp"));
QHash<QString,QUrl> files = generator->fileUrls();
QCOMPARE(files.size(), 2);
QVERIFY(files.contains(QStringLiteral("Header")));
QCOMPARE(files[QStringLiteral("Header")], baseUrl.resolved(QUrl(QStringLiteral("ClassName.h"))));
QVERIFY(files.contains(QStringLiteral("Implementation")));
QCOMPARE(files[QStringLiteral("Implementation")], baseUrl.resolved(QUrl(QStringLiteral("ClassName.cpp"))));
}
void TestTemplateClassGenerator::defaultFileUrlsYaml()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
QHash<QString,QUrl> files = generator->fileUrls();
QCOMPARE(files.size(), 1);
QVERIFY(files.contains(QStringLiteral("Description")));
QCOMPARE(files[QStringLiteral("Description")], baseUrl.resolved(QUrl(QStringLiteral("ClassName.yaml"))));
}
void TestTemplateClassGenerator::customOptions()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
QCOMPARE(generator->sourceFileTemplate().hasCustomOptions(), false);
generator = loadTemplate(QStringLiteral("test_options"));
QCOMPARE(generator->sourceFileTemplate().hasCustomOptions(), true);
// test if option data loaded with all values in same order as in kcfg file
struct ExpectedOption
{
const char* name; const char* label; const char* type; const char* value; QStringList values;
};
const struct {
const char* name;
QVector<ExpectedOption> expectedOptions;
} expectedGroupDataList[] = {
{ "A Group", {
{"bool_option", "A Bool", "Bool", "true", {}},
{"string_option", "Zzz String", "String", "Test", {}},
{"enum_option", "Bb Enum", "Enum", "Second", {
{QLatin1String("First")}, {QLatin1String("Second")}, {QLatin1String("Last")}
}}
}},
{ "Zzz Group", {
{"z_option", "Z Bool", "Bool", "false", {}}
}},
{ "Bb Group", {
{"b_option", "B Bool", "Bool", "true", {}}
}}
};
const int expectedGroupDataCount = sizeof(expectedGroupDataList)/sizeof(expectedGroupDataList[0]);
const auto customOptionGroups = generator->sourceFileTemplate().customOptions(generator->renderer());
QCOMPARE(customOptionGroups.count(), expectedGroupDataCount);
for (int i = 0; i < expectedGroupDataCount; ++i) {
const auto& customOptionGroup = customOptionGroups[i];
const auto& expectedGroupData = expectedGroupDataList[i];
QCOMPARE(customOptionGroup.name, QString::fromLatin1(expectedGroupData.name));
QCOMPARE(customOptionGroup.options.count(), expectedGroupData.expectedOptions.count());
for (int j = 0; j < expectedGroupData.expectedOptions.count(); ++j) {
const auto& customOption = customOptionGroup.options[j];
const auto& expectedOptionData = expectedGroupData.expectedOptions[j];
QCOMPARE(customOption.name, QString::fromLatin1(expectedOptionData.name));
QCOMPARE(customOption.label, QString::fromLatin1(expectedOptionData.label));
QCOMPARE(customOption.type, QString::fromLatin1(expectedOptionData.type));
QCOMPARE(customOption.value.toString(), QString::fromLatin1(expectedOptionData.value));
QCOMPARE(customOption.values, expectedOptionData.values);
}
}
}
void TestTemplateClassGenerator::templateVariablesCpp()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_cpp"));
setLowercaseFileNames(generator);
QVariantHash variables = generator->renderer()->variables();
CHECK_TEMPLATE_VARIABLE(name, toString, QStringLiteral("ClassName"));
CHECK_TEMPLATE_VARIABLE(output_file_header, toString, QStringLiteral("classname.h"));
CHECK_TEMPLATE_VARIABLE(output_file_header_absolute, toString, baseUrl.resolved(QUrl(QStringLiteral("classname.h"))).toLocalFile());
}
void TestTemplateClassGenerator::templateVariablesYaml()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
setLowercaseFileNames(generator);
QVariantHash variables = generator->renderer()->variables();
CHECK_TEMPLATE_VARIABLE(name, toString, QStringLiteral("ClassName"));
CHECK_TEMPLATE_VARIABLE(output_file_description, toString, QStringLiteral("classname.yaml"));
CHECK_TEMPLATE_VARIABLE(output_file_description_absolute, toString, baseUrl.resolved(QUrl(QStringLiteral("classname.yaml"))).toLocalFile());
}
void TestTemplateClassGenerator::codeDescription()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
QVariantHash variables = generator->renderer()->variables();
qDebug() << variables;
QVERIFY(variables.contains(QStringLiteral("base_classes")));
QVariantList inheritance = variables[QStringLiteral("base_classes")].toList();
QCOMPARE(inheritance.size(), 1);
QCOMPARE(inheritance.first().value<InheritanceDescription>().baseType, QStringLiteral("QObject"));
QCOMPARE(inheritance.first().value<InheritanceDescription>().inheritanceMode, QStringLiteral("public"));
QVERIFY(variables.contains(QStringLiteral("members")));
QVariantList members = variables[QStringLiteral("members")].toList();
QCOMPARE(members.size(), 3);
QCOMPARE(members.first().value<VariableDescription>().type, QStringLiteral("QString"));
QCOMPARE(members.first().value<VariableDescription>().name, QStringLiteral("name"));
QVERIFY(variables.contains(QStringLiteral("functions")));
QVariantList methods = variables[QStringLiteral("functions")].toList();
QCOMPARE(methods.size(), 2);
QCOMPARE(methods.first().value<FunctionDescription>().name, QStringLiteral("doSomething"));
QCOMPARE(methods.first().value<FunctionDescription>().arguments.size(), 2);
QCOMPARE(methods.first().value<FunctionDescription>().returnArguments.size(), 0);
QCOMPARE(methods.last().value<FunctionDescription>().name, QStringLiteral("getSomeOtherNumber"));
QCOMPARE(methods.last().value<FunctionDescription>().arguments.size(), 0);
QCOMPARE(methods.last().value<FunctionDescription>().returnArguments.size(), 1);
}
void TestTemplateClassGenerator::generate()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_cpp"));
DocumentChangeSet changes = generator->generate();
DocumentChangeSet::ChangeResult result = changes.applyAllChanges();
QVERIFY(result.m_success);
QDir dir(baseUrl.toLocalFile());
QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
QCOMPARE(entries.size(), 2);
}
void TestTemplateClassGenerator::cppOutput()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_cpp"));
setLowercaseFileNames(generator);
DocumentChangeSet changes = generator->generate();
changes.setFormatPolicy(DocumentChangeSet::NoAutoFormat);
changes.applyAllChanges();
QFile header(baseUrl.resolved(QUrl(QStringLiteral("classname.h"))).toLocalFile());
QVERIFY(header.open(QIODevice::ReadOnly));
QFile testHeader(QStringLiteral(CODEGEN_TESTS_EXPECTED_DIR "/classname.h"));
testHeader.open(QIODevice::ReadOnly);
COMPARE_FILES(header, testHeader);
QFile implementation(baseUrl.resolved(QUrl(QStringLiteral("classname.cpp"))).toLocalFile());
QVERIFY(implementation.open(QIODevice::ReadOnly));
QFile testImplementation(QStringLiteral(CODEGEN_TESTS_EXPECTED_DIR "/classname.cpp"));
testImplementation.open(QIODevice::ReadOnly);
COMPARE_FILES(implementation, testImplementation);
}
void TestTemplateClassGenerator::yamlOutput()
{
TemplateClassGenerator* generator = loadTemplate(QStringLiteral("test_yaml"));
setLowercaseFileNames(generator);
generator->generate().applyAllChanges();
QFile yaml(baseUrl.resolved(QUrl(QStringLiteral("classname.yaml"))).toLocalFile());
QVERIFY(yaml.open(QIODevice::ReadOnly));
QFile testYaml(QStringLiteral(CODEGEN_TESTS_EXPECTED_DIR "/classname.yaml"));
testYaml.open(QIODevice::ReadOnly);
COMPARE_FILES(yaml, testYaml);
}
TemplateClassGenerator* TestTemplateClassGenerator::loadTemplate (const QString& name)
{
QDir dir(baseUrl.toLocalFile());
const auto files = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
for (const QString& fileName : files) {
dir.remove(fileName);
}
auto* generator = new TemplateClassGenerator(baseUrl);
QString tplDescription = QStringLiteral(CODEGEN_DATA_DIR) + "/kdevcodegentest/templates/" + name + "/" + name + ".desktop";
Q_ASSERT(!tplDescription.isEmpty());
SourceFileTemplate tpl;
tpl.addAdditionalSearchLocation(QStringLiteral(CODEGEN_TESTS_DATA_DIR) + "/kdevcodegentest/templates/");
tpl.setTemplateDescription(tplDescription);
Q_ASSERT(tpl.isValid());
generator->setTemplateDescription(tpl);
generator->setDescription(description);
generator->setIdentifier(QStringLiteral("ClassName"));
generator->addBaseClass(QStringLiteral("public QObject"));
generator->setLicense(QStringLiteral("This is just a test.\nYou may do with it as you please."));
return generator;
}
void TestTemplateClassGenerator::setLowercaseFileNames(TemplateClassGenerator* generator)
{
QHash<QString, QUrl> urls = generator->fileUrls();
QHash<QString, QUrl>::const_iterator it = urls.constBegin();
for (; it != urls.constEnd(); ++it)
{
QString fileName = it.value().fileName().toLower();
QUrl base = it.value().resolved(QUrl(QStringLiteral("./%1").arg(fileName)));
generator->setFileUrl(it.key(), base);
}
}
QTEST_GUILESS_MAIN(TestTemplateClassGenerator)
|