~ubuntu-branches/ubuntu/precise/grantlee/precise

« back to all changes in this revision

Viewing changes to tests/testscriptabletags.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
*/
 
20
 
 
21
#ifndef SCRIPTABLETAGSTEST_H
 
22
#define SCRIPTABLETAGSTEST_H
 
23
 
 
24
#include <QtTest>
 
25
#include <QtCore/QObject>
 
26
 
 
27
#include "template.h"
 
28
#include "engine.h"
 
29
#include "context.h"
 
30
#include "filterexpression.h"
 
31
#include "grantlee_paths.h"
 
32
 
 
33
typedef QHash<QString, QVariant> Dict;
 
34
 
 
35
Q_DECLARE_METATYPE( Dict )
 
36
Q_DECLARE_METATYPE( Grantlee::Error )
 
37
 
 
38
using namespace Grantlee;
 
39
 
 
40
class TestScriptableTagsSyntax : public QObject
 
41
{
 
42
  Q_OBJECT
 
43
 
 
44
private Q_SLOTS:
 
45
  void initTestCase();
 
46
 
 
47
  void testBasicSyntax_data();
 
48
  void testBasicSyntax() {
 
49
    doTest();
 
50
  }
 
51
 
 
52
  void cleanupTestCase();
 
53
 
 
54
private:
 
55
 
 
56
  void doTest();
 
57
 
 
58
  Engine *m_engine;
 
59
 
 
60
};
 
61
 
 
62
void TestScriptableTagsSyntax::initTestCase()
 
63
{
 
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
 
68
                         );
 
69
  m_engine->addDefaultLibrary( "grantlee_scriptabletags" );
 
70
}
 
71
 
 
72
void TestScriptableTagsSyntax::cleanupTestCase()
 
73
{
 
74
  delete m_engine;
 
75
}
 
76
 
 
77
void TestScriptableTagsSyntax::doTest()
 
78
{
 
79
  QFETCH( QString, input );
 
80
  QFETCH( Dict, dict );
 
81
  QFETCH( QString, output );
 
82
  QFETCH( Grantlee::Error, error );
 
83
 
 
84
  Template t = m_engine->newTemplate( input, QTest::currentDataTag() );
 
85
 
 
86
  Context context( dict );
 
87
 
 
88
  QString result = t->render( &context );
 
89
 
 
90
  if ( t->error() != NoError ) {
 
91
    if ( t->error() != error )
 
92
      qDebug() << t->errorString();
 
93
    QCOMPARE( t->error(), error );
 
94
    return;
 
95
  }
 
96
 
 
97
 
 
98
  QCOMPARE( t->error(), NoError );
 
99
 
 
100
  // Didn't catch any errors, so make sure I didn't expect any.
 
101
  QCOMPARE( NoError, error );
 
102
 
 
103
  QCOMPARE( t->error(), NoError );
 
104
 
 
105
 
 
106
  QCOMPARE( result, output );
 
107
}
 
108
 
 
109
void TestScriptableTagsSyntax::testBasicSyntax_data()
 
110
{
 
111
  QTest::addColumn<QString>( "input" );
 
112
  QTest::addColumn<Dict>( "dict" );
 
113
  QTest::addColumn<QString>( "output" );
 
114
  QTest::addColumn<Grantlee::Error>( "error" );
 
115
 
 
116
  Dict dict;
 
117
 
 
118
  dict.insert( "boo", "Far" );
 
119
  dict.insert( "booList", QVariantList() << "Tom" << "Dick" << "Harry" );
 
120
 
 
121
  QTest::newRow( "scriptable-tags01" ) << "{% load scripteddefaults %}{% if2 \"something\\\" stupid\" %}{{ boo }}{% endif2 %}" << dict << "Far" << NoError;
 
122
 
 
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;
 
125
 
 
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;
 
129
 
 
130
  QTest::newRow( "scriptable-tags05" ) << "{% load scripteddefaults %}{{ boo|upper }}" << dict << "FAR" << NoError;
 
131
 
 
132
 
 
133
  dict.insert( "boo", "Far & away" );
 
134
  // Variables are escaped ...
 
135
  QTest::newRow( "scriptable-tags06" ) << "{% load scripteddefaults %}{{ boo }}" << dict << "Far &amp; away" << NoError;
 
136
  // and scripted filters can mark output as 'safe'.
 
137
  QTest::newRow( "scriptable-tags07" ) << "{% load scripteddefaults %}{{ boo|safe2 }}" << dict << "Far & away" << NoError;
 
138
 
 
139
  // Filters can take arguments
 
140
  QTest::newRow( "scriptable-tags08" ) << "{% load scripteddefaults %}{{ booList|join2:\" \" }}" << dict << "Tom Dick Harry" << NoError;
 
141
 
 
142
  // Literals in tags are compared un-escaped.
 
143
  QTest::newRow( "scriptable-tags09" ) << "{% load scripteddefaults %}{% ifequal2 boo \"Far & away\" %}yes{% endifequal2 %}" << dict << "yes" << NoError;
 
144
 
 
145
  // Literals in arguments to filters are not escaped.
 
146
  QTest::newRow( "scriptable-tags10" ) << "{% load scripteddefaults %}{{ booList|join2:\" & \" }}" << dict << "Tom & Dick & Harry" << NoError;
 
147
 
 
148
  // Nor are variables.
 
149
  dict.insert( "amp", " & " );
 
150
  QTest::newRow( "scriptable-tags11" ) << "{% load scripteddefaults %}{{ booList|join2:amp }}" << dict << "Tom & Dick & Harry" << NoError;
 
151
 
 
152
 
 
153
 
 
154
  dict.clear();
 
155
}
 
156
 
 
157
QTEST_MAIN( TestScriptableTagsSyntax )
 
158
#include "testscriptabletags.moc"
 
159
 
 
160
#endif
 
161