Utils: Move template file processing from projectexplorer to utils

I want to use it e.g. for snippets and the TextEditor plugin may
not depend on the ProjectExplorer, so the code has to move.

This adds a dependency on QtQml to Utils, but that does not really
matter since that is loaded into QtCreator anyway.

Change-Id: Iada9f40b2966a1fc41631ab33da09812ad67d967
Reviewed-by: Daniel Teske <daniel.teske@theqtcompany.com>
This commit is contained in:
Tobias Hunger
2015-10-01 15:03:18 +02:00
parent b899684a89
commit 95b0dc9120
19 changed files with 272 additions and 226 deletions

1
.gitignore vendored
View File

@@ -196,6 +196,7 @@ tmp/
/tests/auto/treeviewfind/tst_treeviewfind
/tests/auto/utils/ansiescapecodehandler/tst_ansiescapecodehandler
/tests/auto/utils/fileutils/tst_fileutils
/tests/auto/utils/templateengine/tst_templateengine
/tests/auto/utils_stringutils/tst_utils_stringutils
/tests/auto/valgrind/callgrind/tst_callgrindparsertests
/tests/auto/valgrind/memcheck/modeldemo

View File

@@ -28,25 +28,17 @@
**
****************************************************************************/
#include "customwizardpreprocessor.h"
#ifdef WITH_TESTS
# include "../projectexplorer.h"
# include <QTest>
#endif
#include "templateengine.h"
#include <utils/qtcassert.h>
#include "qtcassert.h"
#include <QStringList>
#include <QStack>
#include <QRegExp>
#include <QDebug>
#include <QJSEngine>
#include <QStack>
namespace Utils {
namespace ProjectExplorer {
namespace Internal {
enum { debug = 0 };
// Preprocessor: Conditional section type.
enum PreprocessorSection {
IfSection,
@@ -58,7 +50,8 @@ enum PreprocessorSection {
// Preprocessor: Section stack entry containing nested '@if' section
// state.
struct PreprocessStackEntry {
class PreprocessStackEntry {
public:
PreprocessStackEntry(PreprocessorSection section = OtherSection,
bool parentEnabled = true,
bool condition = false,
@@ -70,11 +63,9 @@ struct PreprocessStackEntry {
bool anyIfClauseMatched; // Determines if 'else' triggers
};
PreprocessStackEntry::PreprocessStackEntry(PreprocessorSection s,
bool p, bool c, bool a) :
PreprocessStackEntry::PreprocessStackEntry(PreprocessorSection s, bool p, bool c, bool a) :
section(s), parentEnabled(p), condition(c), anyIfClauseMatched(a)
{
}
{ }
// Context for preprocessing.
class PreprocessContext {
@@ -102,13 +93,14 @@ PreprocessContext::PreprocessContext() :
m_elsePattern(QLatin1String("^[\\s]*@[\\s]*else.*$")),
m_endifPattern(QLatin1String("^[\\s]*@[\\s]*endif.*$"))
{
QTC_ASSERT(m_ifPattern.isValid() && m_elsifPattern.isValid()
&& m_elsePattern.isValid() &&m_endifPattern.isValid(), return);
QTC_CHECK(m_ifPattern.isValid() && m_elsifPattern.isValid()
&& m_elsePattern.isValid() && m_endifPattern.isValid());
}
void PreprocessContext::reset()
{
m_sectionStack.clear(); // Add a default, enabled section.
m_sectionStack.clear();
// Add a default, enabled section.
m_sectionStack.push(PreprocessStackEntry(OtherSection, true, true));
}
@@ -135,35 +127,6 @@ PreprocessorSection PreprocessContext::preprocessorLine(const QString &in,
return OtherSection;
}
// Evaluate an expression within an 'if'/'elsif' to a bool via QJSEngine
bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage)
{
errorMessage->clear();
*result = false;
const QJSValue value = engine.evaluate(expression);
if (value.isError()) {
*errorMessage = QString::fromLatin1("Error in \"%1\": %2").
arg(expression, value.toString());
return false;
}
// Try to convert to bool, be that an int or whatever.
if (value.isBool()) {
*result = value.toBool();
return true;
}
if (value.isNumber()) {
*result = !qFuzzyCompare(value.toNumber(), 0);
return true;
}
if (value.isString()) {
*result = !value.toString().isEmpty();
return true;
}
*errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.").
arg(expression, value.toString());
return false;
}
static inline QString msgEmptyStack(int line)
{
return QString::fromLatin1("Unmatched '@endif' at line %1.").arg(line);
@@ -184,7 +147,8 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
for (int l = 0; l < lineCount; l++) {
// Check for element of the stack (be it dummy, else something is wrong).
if (m_sectionStack.isEmpty()) {
*errorMessage = msgEmptyStack(l);
if (errorMessage)
*errorMessage = msgEmptyStack(l);
return false;
}
QString expression;
@@ -195,32 +159,33 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
case IfSection:
// '@If': Push new section
if (top.condition) {
if (!evaluateBooleanJavaScriptExpression(m_scriptEngine, expression, &expressionValue, errorMessage)) {
*errorMessage = QString::fromLatin1("Error in @if at %1: %2").
arg(l + 1).arg(*errorMessage);
if (!TemplateEngine::evaluateBooleanJavaScriptExpression(m_scriptEngine, expression,
&expressionValue, errorMessage)) {
if (errorMessage)
*errorMessage = QString::fromLatin1("Error in @if at %1: %2")
.arg(l + 1).arg(*errorMessage);
return false;
}
}
if (debug)
qDebug("'%s' : expr='%s' -> %d", qPrintable(lines.at(l)), qPrintable(expression), expressionValue);
m_sectionStack.push(PreprocessStackEntry(IfSection,
top.condition, expressionValue, expressionValue));
break;
case ElsifSection: // '@elsif': Check condition.
if (top.section != IfSection && top.section != ElsifSection) {
*errorMessage = QString::fromLatin1("No preceding @if found for @elsif at %1").
arg(l + 1);
if (errorMessage)
*errorMessage = QString::fromLatin1("No preceding @if found for @elsif at %1")
.arg(l + 1);
return false;
}
if (top.parentEnabled) {
if (!evaluateBooleanJavaScriptExpression(m_scriptEngine, expression, &expressionValue, errorMessage)) {
*errorMessage = QString::fromLatin1("Error in @elsif at %1: %2").
arg(l + 1).arg(*errorMessage);
if (!TemplateEngine::evaluateBooleanJavaScriptExpression(m_scriptEngine, expression,
&expressionValue, errorMessage)) {
if (errorMessage)
*errorMessage = QString::fromLatin1("Error in @elsif at %1: %2")
.arg(l + 1).arg(*errorMessage);
return false;
}
}
if (debug)
qDebug("'%s' : expr='%s' -> %d", qPrintable(lines.at(l)), qPrintable(expression), expressionValue);
top.section = ElsifSection;
// ignore consecutive '@elsifs' once something matched
if (top.anyIfClauseMatched) {
@@ -232,13 +197,12 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
break;
case ElseSection: // '@else': Check condition.
if (top.section != IfSection && top.section != ElsifSection) {
*errorMessage = QString::fromLatin1("No preceding @if/@elsif found for @else at %1").
arg(l + 1);
if (errorMessage)
*errorMessage = QString::fromLatin1("No preceding @if/@elsif found for @else at %1")
.arg(l + 1);
return false;
}
expressionValue = top.parentEnabled && !top.anyIfClauseMatched;
if (debug)
qDebug("%s -> %d", qPrintable(lines.at(l)), expressionValue);
top.section = ElseSection;
top.condition = expressionValue;
break;
@@ -257,97 +221,96 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
return true;
}
/*!
Implements a custom wizard preprocessor based on JavaScript expressions.
} // namespace Internal
Preprocesses a string using a simple syntax:
\code
Text
@if <JavaScript-expression>
Bla...
@elsif <JavaScript-expression2>
Blup
@endif
\endcode
The JavaScript-expressions must evaluate to integers or boolean, like
\c '2 == 1 + 1', \c '"a" == "a"'. The variables of the custom wizard will be
expanded before, so \c "%VAR%" should be used for strings and \c %VAR% for integers.
\sa ProjectExplorer::CustomWizard
*/
bool customWizardPreprocess(const QString &in, QString *out, QString *errorMessage)
bool TemplateEngine::preprocessText(const QString &in, QString *out, QString *errorMessage)
{
PreprocessContext context;
Internal::PreprocessContext context;
return context.process(in, out, errorMessage);
}
} // namespace Internal
#ifdef WITH_TESTS // Run qtcreator -test ProjectExplorer
void ProjectExplorerPlugin::testCustomWizardPreprocessor_data()
QString TemplateEngine::processText(MacroExpander *expander, const QString &input,
QString *errorMessage)
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expectedOutput");
QTest::addColumn<bool>("expectedSuccess");
QTest::addColumn<QString>("expectedErrorMessage");
QTest::newRow("if")
<< QString::fromLatin1("@if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 1")
<< true << QString();
QTest::newRow("elsif")
<< QString::fromLatin1("@if 0\nline 1\n@elsif 1\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 2")
<< true << QString();
QTest::newRow("else")
<< QString::fromLatin1("@if 0\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 3")
<< true << QString();
QTest::newRow("nested-if")
<< QString::fromLatin1("@if 1\n"
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
"@else\n"
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
"@endif\n")
<< QString::fromLatin1("line 1")
<< true << QString();
QTest::newRow("nested-else")
<< QString::fromLatin1("@if 0\n"
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
"@else\n"
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
"@endif\n")
<< QString::fromLatin1("line 4")
<< true << QString();
QTest::newRow("twice-nested-if")
<< QString::fromLatin1("@if 0\n"
" @if 1\n"
" @if 1\nline 1\n@else\nline 2\n@endif\n"
" @endif\n"
"@else\n"
" @if 1\n"
" @if 1\nline 3\n@else\nline 4\n@endif\n"
" @endif\n"
"@endif\n")
<< QString::fromLatin1("line 3")
<< true << QString();
if (errorMessage)
errorMessage->clear();
if (input.isEmpty())
return input;
// Recursively expand macros:
QString in = input;
QString oldIn;
for (int i = 0; i < 5 && in != oldIn; ++i) {
oldIn = in;
in = expander->expand(oldIn);
}
QString out;
if (!preprocessText(in, &out, errorMessage))
return QString();
// Expand \n, \t and handle line continuation:
QString result;
result.reserve(out.count());
bool isEscaped = false;
for (int i = 0; i < out.count(); ++i) {
const QChar c = out.at(i);
if (isEscaped) {
if (c == QLatin1Char('n'))
result.append(QLatin1Char('\n'));
else if (c == QLatin1Char('t'))
result.append(QLatin1Char('\t'));
else if (c != QLatin1Char('\n'))
result.append(c);
isEscaped = false;
} else {
if (c == QLatin1Char('\\'))
isEscaped = true;
else
result.append(c);
}
}
return result;
}
void ProjectExplorerPlugin::testCustomWizardPreprocessor()
bool TemplateEngine::evaluateBooleanJavaScriptExpression(QJSEngine &engine,
const QString &expression, bool *result,
QString *errorMessage)
{
QFETCH(QString, input);
QFETCH(QString, expectedOutput);
QFETCH(bool, expectedSuccess);
QFETCH(QString, expectedErrorMessage);
if (errorMessage)
errorMessage->clear();
if (result)
*result = false;
const QJSValue value = engine.evaluate(expression);
if (value.isError()) {
if (errorMessage)
*errorMessage = QString::fromLatin1("Error in \"%1\": %2")
.arg(expression, value.toString());
return false;
}
// Try to convert to bool, be that an int or whatever.
if (value.isBool()) {
if (result)
*result = value.toBool();
return true;
}
if (value.isNumber()) {
if (result)
*result = !qFuzzyCompare(value.toNumber(), 0);
return true;
}
if (value.isString()) {
if (result)
*result = !value.toString().isEmpty();
return true;
}
if (errorMessage)
*errorMessage = QString::fromLatin1("Cannot convert result of \"%1\" (\"%2\"to bool.")
.arg(expression, value.toString());
QString errorMessage;
QString output;
const bool success = Internal::customWizardPreprocess(input, &output, &errorMessage);
QCOMPARE(success, expectedSuccess);
QCOMPARE(output.trimmed(), expectedOutput.trimmed());
QCOMPARE(errorMessage, expectedErrorMessage);
return false;
}
#endif // WITH_TESTS
} // namespace ProjectExplorer
} // namespace Utils

View File

@@ -28,20 +28,30 @@
**
****************************************************************************/
#ifndef CUSTOMWIZARDPREPROCESSOR_H
#define CUSTOMWIZARDPREPROCESSOR_H
#ifndef TEMPLATEENGINE_H
#define TEMPLATEENGINE_H
#include "utils_global.h"
#include "macroexpander.h"
#include <QString>
QT_FORWARD_DECLARE_CLASS(QJSEngine)
QT_FORWARD_DECLARE_CLASS(QJSEngine);
namespace ProjectExplorer {
namespace Internal {
namespace Utils {
bool customWizardPreprocess(const QString &in, QString *out, QString *errorMessage);
/* Helper to evaluate an expression. */
bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression, bool *result, QString *errorMessage);
} // namespace Internal
} // namespace ProjectExplorer
class QTCREATOR_UTILS_EXPORT TemplateEngine {
public:
static bool preprocessText(const QString &input, QString *output, QString *errorMessage);
#endif // CUSTOMWIZARDPREPROCESSOR_H
static QString processText(MacroExpander *expander, const QString &input,
QString *errorMessage);
static bool evaluateBooleanJavaScriptExpression(QJSEngine &engine, const QString &expression,
bool *result, QString *errorMessage);
};
} // namespace Utils
#endif // TEMPLATEENGINE_H

View File

@@ -16,6 +16,7 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/shellcommandpage.cpp \
$$PWD/settingsselector.cpp \
$$PWD/stringutils.cpp \
$$PWD/templateengine.cpp \
$$PWD/textfieldcheckbox.cpp \
$$PWD/textfieldcombobox.cpp \
$$PWD/filesearch.cpp \
@@ -107,6 +108,7 @@ HEADERS += \
$$PWD/shellcommand.h \
$$PWD/shellcommandpage.h \
$$PWD/stringutils.h \
$$PWD/templateengine.h \
$$PWD/textfieldcheckbox.h \
$$PWD/textfieldcombobox.h \
$$PWD/filesearch.h \

View File

@@ -1,4 +1,4 @@
QT += gui network
QT += gui network qml
include(../../qtcreatorlibrary.pri)
include(utils-lib.pri)

View File

@@ -28,7 +28,7 @@ QtcLibrary {
cpp.frameworks: ["Foundation"]
}
Depends { name: "Qt"; submodules: ["widgets", "network", "script", "concurrent"] }
Depends { name: "Qt"; submodules: ["gui", "network", "qml"] }
Depends { name: "app_version_header" }
files: [

View File

@@ -1,10 +1,8 @@
HEADERS += $$PWD/customwizard.h \
$$PWD/customwizardparameters.h \
$$PWD/customwizardpage.h \
customwizard/customwizardpreprocessor.h \
customwizard/customwizardscriptgenerator.h
$$PWD/customwizardscriptgenerator.h
SOURCES += $$PWD/customwizard.cpp \
$$PWD/customwizardparameters.cpp \
$$PWD/customwizardpage.cpp \
customwizard/customwizardpreprocessor.cpp \
customwizard/customwizardscriptgenerator.cpp
$$PWD/customwizardscriptgenerator.cpp

View File

@@ -29,13 +29,14 @@
****************************************************************************/
#include "customwizardparameters.h"
#include "customwizardpreprocessor.h"
#include "customwizardscriptgenerator.h"
#include <coreplugin/icore.h>
#include <cpptools/cpptoolsconstants.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/macroexpander.h>
#include <utils/templateengine.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
@@ -194,7 +195,7 @@ bool CustomWizardValidationRule::validate(QJSEngine &engine, const QMap<QString,
CustomWizardContext::replaceFields(replacementMap, &cond);
bool valid = false;
QString errorMessage;
if (!evaluateBooleanJavaScriptExpression(engine, cond, &valid, &errorMessage)) {
if (!Utils::TemplateEngine::evaluateBooleanJavaScriptExpression(engine, cond, &valid, &errorMessage)) {
qWarning("Error in custom wizard validation expression '%s': %s",
qPrintable(cond), qPrintable(errorMessage));
return false;
@@ -954,7 +955,7 @@ QString CustomWizardContext::processFile(const FieldReplacementMap &fm, QString
QString out;
QString errorMessage;
if (!customWizardPreprocess(in, &out, &errorMessage)) {
if (!Utils::TemplateEngine::preprocessText(in, &out, &errorMessage)) {
qWarning("Error preprocessing custom widget file: %s\nFile:\n%s",
qPrintable(errorMessage), qPrintable(in));
return QString();

View File

@@ -34,7 +34,6 @@
#include "../project.h"
#include "../projectexplorer.h"
#include "../customwizard/customwizardpreprocessor.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/messagemanager.h>
@@ -202,51 +201,6 @@ QHash<QString, QVariant> JsonWizard::variables() const
return result;
}
QString JsonWizard::processText(Utils::MacroExpander *expander, const QString &input,
QString *errorMessage)
{
errorMessage->clear();
if (input.isEmpty())
return input;
// Recursively expand macros:
QString in = input;
QString oldIn;
for (int i = 0; i < 5 && in != oldIn; ++i) {
oldIn = in;
in = expander->expand(oldIn);
}
QString out;
if (!Internal::customWizardPreprocess(in, &out, errorMessage))
return QString();
// Expand \n, \t and handle line continuation:
QString result;
result.reserve(out.count());
bool isEscaped = false;
for (int i = 0; i < out.count(); ++i) {
const QChar c = out.at(i);
if (isEscaped) {
if (c == QLatin1Char('n'))
result.append(QLatin1Char('\n'));
else if (c == QLatin1Char('t'))
result.append(QLatin1Char('\t'));
else if (c != QLatin1Char('\n'))
result.append(c);
isEscaped = false;
} else {
if (c == QLatin1Char('\\'))
isEscaped = true;
else
result.append(c);
}
}
return result;
}
void JsonWizard::accept()
{
auto page = qobject_cast<Utils::WizardPage *>(currentPage());

View File

@@ -86,9 +86,6 @@ public:
QHash<QString, QVariant> variables() const override;
static QString processText(Utils::MacroExpander *expander, const QString &input,
QString *errorMessage);
signals:
void preGenerateFiles(); // emitted before files are generated (can happen several times!)
void postGenerateFiles(const JsonWizard::GeneratorFiles &files); // emitted after commitToFileList was called.

View File

@@ -39,6 +39,7 @@
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
#include <utils/macroexpander.h>
#include <utils/templateengine.h>
#include <QCoreApplication>
#include <QDir>
@@ -164,8 +165,8 @@ Core::GeneratedFiles JsonWizardFileGenerator::fileList(Utils::MacroExpander *exp
});
nested.registerExtraResolver([expander](QString n, QString *ret) { return expander->resolveMacro(n, ret); });
gf.setContents(JsonWizard::processText(&nested, QString::fromUtf8(reader.data()),
errorMessage));
gf.setContents(Utils::TemplateEngine::processText(&nested, QString::fromUtf8(reader.data()),
errorMessage));
if (!errorMessage->isEmpty()) {
*errorMessage = QCoreApplication::translate("ProjectExplorer::JsonWizard", "When processing \"%1\":<br>%2")
.arg(sourcePath, *errorMessage);

View File

@@ -30,7 +30,6 @@
#include "jsonwizardscannergenerator.h"
#include "../customwizard/customwizardpreprocessor.h"
#include "../projectexplorer.h"
#include "../iprojectmanager.h"
#include "jsonwizard.h"

View File

@@ -219,9 +219,6 @@ private slots:
void testAbiFromTargetTriplet();
void testDeviceManager();
void testCustomWizardPreprocessor_data();
void testCustomWizardPreprocessor();
#endif
};

View File

@@ -192,7 +192,6 @@ QtcPlugin {
"customwizard.cpp", "customwizard.h",
"customwizardpage.cpp", "customwizardpage.h",
"customwizardparameters.cpp", "customwizardparameters.h",
"customwizardpreprocessor.cpp", "customwizardpreprocessor.h",
"customwizardscriptgenerator.cpp", "customwizardscriptgenerator.h"
]
}

View File

@@ -0,0 +1,6 @@
QTC_LIB_DEPENDS += utils
include(../../qttest.pri)
DEFINES -= QT_USE_FAST_OPERATOR_PLUS QT_USE_FAST_CONCATENATION
SOURCES += tst_templateengine.cpp

View File

@@ -0,0 +1,7 @@
import qbs
QtcAutotest {
name: "StringUtils autotest"
Depends { name: "Utils" }
files: "tst_templateengine.cpp"
}

View File

@@ -0,0 +1,109 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include <utils/templateengine.h>
#include <QtTest>
//TESTED_COMPONENT=src/libs/utils
class tst_TemplateEngine : public QObject
{
Q_OBJECT
private slots:
void testTemplateEngine_data();
void testTemplateEngine();
};
void tst_TemplateEngine::testTemplateEngine_data()
{
QTest::addColumn<QString>("input");
QTest::addColumn<QString>("expectedOutput");
QTest::addColumn<QString>("expectedErrorMessage");
QTest::newRow("if")
<< QString::fromLatin1("@if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 1\n\n")
<< QString();
QTest::newRow("elsif")
<< QString::fromLatin1("@if 0\nline 1\n@elsif 1\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 2\n\n")
<< QString();
QTest::newRow("else")
<< QString::fromLatin1("@if 0\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n")
<< QString::fromLatin1("line 3\n\n")
<< QString();
QTest::newRow("nested-if")
<< QString::fromLatin1("@if 1\n"
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
"@else\n"
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
"@endif\n")
<< QString::fromLatin1("line 1\n\n")
<< QString();
QTest::newRow("nested-else")
<< QString::fromLatin1("@if 0\n"
" @if 1\nline 1\n@elsif 0\nline 2\n@else\nline 3\n@endif\n"
"@else\n"
" @if 1\nline 4\n@elsif 0\nline 5\n@else\nline 6\n@endif\n"
"@endif\n")
<< QString::fromLatin1("line 4\n\n")
<< QString();
QTest::newRow("twice-nested-if")
<< QString::fromLatin1("@if 0\n"
" @if 1\n"
" @if 1\nline 1\n@else\nline 2\n@endif\n"
" @endif\n"
"@else\n"
" @if 1\n"
" @if 1\nline 3\n@else\nline 4\n@endif\n"
" @endif\n"
"@endif\n")
<< QString::fromLatin1("line 3\n\n")
<< QString();
}
void tst_TemplateEngine::testTemplateEngine()
{
QFETCH(QString, input);
QFETCH(QString, expectedOutput);
QFETCH(QString, expectedErrorMessage);
QString errorMessage;
QString output = Utils::TemplateEngine::processText(Utils::globalMacroExpander(), input, &errorMessage);
QCOMPARE(output, expectedOutput);
QCOMPARE(errorMessage, expectedErrorMessage);
}
QTEST_MAIN(tst_TemplateEngine)
#include "tst_templateengine.moc"

View File

@@ -4,4 +4,5 @@ SUBDIRS = \
fileutils \
ansiescapecodehandler \
stringutils \
templateengine \
treemodel

View File

@@ -6,6 +6,7 @@ Project {
"fileutils/fileutils.qbs",
"ansiescapecodehandler/ansiescapecodehandler.qbs",
"stringutils/stringutils.qbs",
"templateengine/templateengine.qbs",
"treemodel/treemodel.qbs",
]
}