2
This file is part of the Grantlee template system.
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2 of the Licence, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
21
#ifndef SCRIPTABLETAGSTEST_H
22
#define SCRIPTABLETAGSTEST_H
25
#include <QtCore/QObject>
30
#include "filterexpression.h"
31
#include "grantlee_paths.h"
33
typedef QHash<QString, QVariant> Dict;
35
Q_DECLARE_METATYPE( Dict )
36
Q_DECLARE_METATYPE( Grantlee::Error )
38
using namespace Grantlee;
40
class TestScriptableTagsSyntax : public QObject
47
void testBasicSyntax_data();
48
void testBasicSyntax() {
52
void cleanupTestCase();
62
void TestScriptableTagsSyntax::initTestCase()
64
m_engine = new Engine( this );
65
QString appDirPath = QFileInfo( QCoreApplication::applicationDirPath() ).absoluteDir().path();
66
m_engine->setPluginPaths( QStringList() << GRANTLEE_PLUGIN_PATH
67
<< ":/plugins/" // For scripteddefaults.qs
69
m_engine->addDefaultLibrary( "grantlee_scriptabletags" );
72
void TestScriptableTagsSyntax::cleanupTestCase()
77
void TestScriptableTagsSyntax::doTest()
79
QFETCH( QString, input );
81
QFETCH( QString, output );
82
QFETCH( Grantlee::Error, error );
84
Template t = m_engine->newTemplate( input, QTest::currentDataTag() );
86
Context context( dict );
88
QString result = t->render( &context );
90
if ( t->error() != NoError ) {
91
if ( t->error() != error )
92
qDebug() << t->errorString();
93
QCOMPARE( t->error(), error );
98
QCOMPARE( t->error(), NoError );
100
// Didn't catch any errors, so make sure I didn't expect any.
101
QCOMPARE( NoError, error );
103
QCOMPARE( t->error(), NoError );
106
QCOMPARE( result, output );
109
void TestScriptableTagsSyntax::testBasicSyntax_data()
111
QTest::addColumn<QString>( "input" );
112
QTest::addColumn<Dict>( "dict" );
113
QTest::addColumn<QString>( "output" );
114
QTest::addColumn<Grantlee::Error>( "error" );
118
dict.insert( "boo", "Far" );
119
dict.insert( "booList", QVariantList() << "Tom" << "Dick" << "Harry" );
121
QTest::newRow( "scriptable-tags01" ) << "{% load scripteddefaults %}{% if2 \"something\\\" stupid\" %}{{ boo }}{% endif2 %}" << dict << "Far" << NoError;
123
// Nest c++ tags inside scripted tags.
124
QTest::newRow( "scriptable-tags02" ) << "{% load scripteddefaults %}{% if2 \"something\\\" stupid\" %}{% for name in booList %}:{{ name }};{% endfor %}{% endif2 %}" << dict << ":Tom;:Dick;:Harry;" << NoError;
126
// Nest c++ tags inside scripted tags.
127
QTest::newRow( "scriptable-tags03" ) << "{% load scripteddefaults %}{% if2 boo %}yes{% else %}no{% endif2 %}" << dict << "yes" << NoError;
128
QTest::newRow( "scriptable-tags04" ) << "{% load scripteddefaults %}{% if2 foo %}yes{% else %}no{% endif2 %}" << dict << "no" << NoError;
130
QTest::newRow( "scriptable-tags05" ) << "{% load scripteddefaults %}{{ boo|upper }}" << dict << "FAR" << NoError;
133
dict.insert( "boo", "Far & away" );
134
// Variables are escaped ...
135
QTest::newRow( "scriptable-tags06" ) << "{% load scripteddefaults %}{{ boo }}" << dict << "Far & away" << NoError;
136
// and scripted filters can mark output as 'safe'.
137
QTest::newRow( "scriptable-tags07" ) << "{% load scripteddefaults %}{{ boo|safe2 }}" << dict << "Far & away" << NoError;
139
// Filters can take arguments
140
QTest::newRow( "scriptable-tags08" ) << "{% load scripteddefaults %}{{ booList|join2:\" \" }}" << dict << "Tom Dick Harry" << NoError;
142
// Literals in tags are compared un-escaped.
143
QTest::newRow( "scriptable-tags09" ) << "{% load scripteddefaults %}{% ifequal2 boo \"Far & away\" %}yes{% endifequal2 %}" << dict << "yes" << NoError;
145
// Literals in arguments to filters are not escaped.
146
QTest::newRow( "scriptable-tags10" ) << "{% load scripteddefaults %}{{ booList|join2:\" & \" }}" << dict << "Tom & Dick & Harry" << NoError;
148
// Nor are variables.
149
dict.insert( "amp", " & " );
150
QTest::newRow( "scriptable-tags11" ) << "{% load scripteddefaults %}{{ booList|join2:amp }}" << dict << "Tom & Dick & Harry" << NoError;
157
QTEST_MAIN( TestScriptableTagsSyntax )
158
#include "testscriptabletags.moc"