Cutelee  6.1.0
testcachingloader.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2015 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.1 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  Lesser 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 #include <QtCore/QDebug>
22 #include <QtCore/QFileInfo>
23 #include <QtTest/QTest>
24 
25 #include "cachingloaderdecorator.h"
26 #include "context.h"
27 #include "coverageobject.h"
28 #include "engine.h"
29 #include "filterexpression.h"
30 #include "cutelee_paths.h"
31 #include "template.h"
32 #include <metaenumvariable_p.h>
33 
34 typedef QHash<QString, QVariant> Dict;
35 
36 Q_DECLARE_METATYPE(Cutelee::Error)
37 
38 using namespace Cutelee;
39 
41 {
42  Q_OBJECT
43 
44 private Q_SLOTS:
45  void testRenderAfterError();
46 };
47 
48 void TestCachingLoader::testRenderAfterError()
49 {
50  Engine engine;
51  engine.setPluginPaths({QStringLiteral(CUTELEE_PLUGIN_PATH)});
52 
53  std::shared_ptr<InMemoryTemplateLoader> loader(new InMemoryTemplateLoader);
54  loader->setTemplate(QStringLiteral("template1"),
55  QStringLiteral("This template has an error {{ va>r }}"));
56  loader->setTemplate(QStringLiteral("template2"), QStringLiteral("Ok"));
57  loader->setTemplate(QStringLiteral("main"),
58  QStringLiteral("{% include template_var %}"));
59 
60  std::shared_ptr<Cutelee::CachingLoaderDecorator> cache(
62 
63  engine.addTemplateLoader(cache);
64 
65  Context c;
66  Template t;
67  t = engine.loadByName(QStringLiteral("main"));
68 
69  c.insert(QStringLiteral("template_var"), QLatin1String("template1"));
70  QCOMPARE(t->render(&c), QString());
71  QCOMPARE(t->error(), TagSyntaxError);
72 
73  c.insert(QStringLiteral("template_var"), QLatin1String("template2"));
74  QCOMPARE(t->render(&c), QLatin1String("Ok"));
75  QCOMPARE(t->error(), NoError);
76 }
77 
78 QTEST_MAIN(TestCachingLoader)
79 #include "testcachingloader.moc"
Implements a loader decorator which caches compiled Template objects.
The Context class holds the context to render a Template with.
Definition: context.h:119
void insert(const QString &name, QObject *object)
Definition: context.cpp:145
Cutelee::Engine is the main entry point for creating Cutelee Templates.
Definition: engine.h:121
Template loadByName(const QString &name) const
Definition: engine.cpp:370
void setPluginPaths(const QStringList &dirs)
Definition: engine.cpp:87
void addTemplateLoader(std::shared_ptr< AbstractTemplateLoader > loader)
Definition: engine.cpp:68
The InMemoryTemplateLoader loads Templates set dynamically in memory.
The Template class is a tree of nodes which may be rendered.
Definition: template.h:95
QString render(Context *c) const
Definition: template.cpp:74
Error error() const
Definition: template.cpp:122
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8