Utils: Fix logic in TemplateEngine and adjust auto test

Broke with 8641277121.

Change-Id: I49174428bf940ef4c3fa7f6ebd3124a907a555b8
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Christian Stenger
2016-08-02 07:40:28 +02:00
parent 734c348700
commit 6f963fd56e
2 changed files with 10 additions and 7 deletions

View File

@@ -139,6 +139,7 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
const QChar newLine = QLatin1Char('\n');
const QStringList lines = in.split(newLine, QString::KeepEmptyParts);
const int lineCount = lines.size();
bool first = true;
for (int l = 0; l < lineCount; l++) {
// Check for element of the stack (be it dummy, else something is wrong).
if (m_sectionStack.isEmpty()) {
@@ -206,7 +207,9 @@ bool PreprocessContext::process(const QString &in, QString *out, QString *errorM
break;
case OtherSection: // Rest: Append according to current condition.
if (top.condition) {
if (l != 0)
if (first)
first = false;
else
out->append(newLine);
out->append(lines.at(l));
}

View File

@@ -45,15 +45,15 @@ void tst_TemplateEngine::testTemplateEngine_data()
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::fromLatin1("line 1\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::fromLatin1("line 2\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::fromLatin1("line 3\n")
<< QString();
QTest::newRow("nested-if")
<< QString::fromLatin1("@if 1\n"
@@ -61,7 +61,7 @@ void tst_TemplateEngine::testTemplateEngine_data()
"@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::fromLatin1("line 1\n")
<< QString();
QTest::newRow("nested-else")
<< QString::fromLatin1("@if 0\n"
@@ -69,7 +69,7 @@ void tst_TemplateEngine::testTemplateEngine_data()
"@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::fromLatin1("line 4\n")
<< QString();
QTest::newRow("twice-nested-if")
<< QString::fromLatin1("@if 0\n"
@@ -81,7 +81,7 @@ void tst_TemplateEngine::testTemplateEngine_data()
" @if 1\nline 3\n@else\nline 4\n@endif\n"
" @endif\n"
"@endif\n")
<< QString::fromLatin1("line 3\n\n")
<< QString::fromLatin1("line 3\n")
<< QString();
}