Cutelee  6.1.0
testinsertlibrary.cpp
1 /*
2  * This file is part of the Cutelee template system.
3  *
4  * Copyright (c) 2020 Matthias Fehring <mf@huessenbergnetz.de>
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 #ifndef INSERTLIBRARYTEST_H
22 #define INSERTLIBRARYTEST_H
23 
24 #include <QTest>
25 #include <QDebug>
26 
27 #include "coverageobject.h"
28 #include "engine.h"
29 #include "cutelee_paths.h"
30 #include "template.h"
31 #include "node.h"
32 #include "parser.h"
33 #include "taglibraryinterface.h"
34 #include "exception.h"
35 
36 #define TESTLIBRARYTAG_RENDER_VALUE "Hello World!"
37 
38 using namespace Cutelee;
39 
41 {
42  Q_OBJECT
43 
44 private Q_SLOTS:
45  void initTestCase();
46  void cleanupTestCase();
47 
48  void testInsertedLibraryTag();
49  void testLoadInsertedLibraryTag();
50 
51 private:
52  std::shared_ptr<InMemoryTemplateLoader> m_loader;
53  Engine *m_engine = nullptr;
54 };
55 
57 {
58  Node *getNode(const QString &tagContent, Parser *p) const override;
59 };
60 
61 class TestLibraryTag : public Node
62 {
63  Q_OBJECT
64 public:
65  explicit TestLibraryTag(Parser *parser = nullptr) : Node(parser) {}
66 
67  void render(Cutelee::OutputStream *stream, Cutelee::Context *gc) const override;
68 };
69 
70 Node *TestLibraryTagFactory::getNode(const QString &tagContent, Parser *p) const
71 {
72  Q_UNUSED(tagContent)
73  return new TestLibraryTag(p);
74 }
75 
77 {
78  Q_UNUSED(gc);
79  *stream << QStringLiteral(TESTLIBRARYTAG_RENDER_VALUE);
80 }
81 
82 class TestLibrary : public QObject, public TagLibraryInterface
83 {
84  Q_OBJECT
85  Q_INTERFACES(Cutelee::TagLibraryInterface)
86 public:
87  explicit TestLibrary(QObject *parent = nullptr) : QObject(parent) {}
88 
89  virtual QHash<QString, AbstractNodeFactory*> nodeFactories(const QString &name = QString()) override
90  {
91  Q_UNUSED(name);
92  QHash<QString, AbstractNodeFactory*> ret{
93  {QStringLiteral("test_library_tag"), new TestLibraryTagFactory()}
94  };
95  return ret;
96  }
97 };
98 
99 class TestLoadLibrary : public QObject, public TagLibraryInterface
100 {
101  Q_OBJECT
102  Q_INTERFACES(Cutelee::TagLibraryInterface)
103 public:
104  explicit TestLoadLibrary(QObject *parent = nullptr) : QObject(parent) {}
105 
106  virtual QHash<QString, AbstractNodeFactory*> nodeFactories(const QString &name = QString()) override
107  {
108  Q_UNUSED(name);
109  QHash<QString, AbstractNodeFactory*> ret{
110  {QStringLiteral("test_load_library_tag"), new TestLibraryTagFactory()}
111  };
112  return ret;
113  }
114 };
115 
116 void TestInsertLibrary::initTestCase()
117 {
118  m_engine = new Engine(this);
119  QVERIFY(m_engine);
120 
121  m_loader = std::shared_ptr<InMemoryTemplateLoader>(new InMemoryTemplateLoader());
122  m_engine->addTemplateLoader(m_loader);
123 
124  m_engine->setPluginPaths({QStringLiteral(CUTELEE_PLUGIN_PATH)});
125 
126  m_engine->insertDefaultLibrary(QStringLiteral("test_library"), new TestLibrary(m_engine));
127 
128  m_engine->insertLibrary(QStringLiteral("test_load_library"), new TestLoadLibrary(m_engine));
129 }
130 
131 void TestInsertLibrary::cleanupTestCase()
132 {
133  delete m_engine;
134 }
135 
136 void TestInsertLibrary::testInsertedLibraryTag()
137 {
138  auto t = m_engine->newTemplate(QStringLiteral("{% test_library_tag %}"), QStringLiteral("testInsertedLibraryTag"));
139 
140  Context context;
141 
142  const QString result = t->render(&context);
143 
144  QCOMPARE(result, QStringLiteral(TESTLIBRARYTAG_RENDER_VALUE));
145 }
146 
147 void TestInsertLibrary::testLoadInsertedLibraryTag()
148 {
149  Context context;
150 
151  auto t1 = m_engine->newTemplate(QStringLiteral("{% test_load_library_tag %}"), QStringLiteral("testLoadInsertedLibraryTag1"));
152 
153  const QString result1 = t1->render(&context);
154 
155  QVERIFY(result1.isEmpty());
156 
157  auto t2 = m_engine->newTemplate(QStringLiteral("{% load test_load_library %}{% test_load_library_tag %}"), QStringLiteral("testLoadInsertedLibraryTag2"));
158 
159  const QString result2 = t2->render(&context);
160 
161  QCOMPARE(result2, QStringLiteral(TESTLIBRARYTAG_RENDER_VALUE));
162 }
163 
164 QTEST_MAIN(TestInsertLibrary)
165 
166 #include "testinsertlibrary.moc"
167 
168 #endif // INSERTLIBRARYTEST_H
Base class for all NodeFactories.
Definition: node.h:300
The Context class holds the context to render a Template with.
Definition: context.h:119
Cutelee::Engine is the main entry point for creating Cutelee Templates.
Definition: engine.h:121
The InMemoryTemplateLoader loads Templates set dynamically in memory.
Base class for all nodes.
Definition: node.h:78
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
The TagLibraryInterface returns available tags and filters from libraries.
void render(Cutelee::OutputStream *stream, Cutelee::Context *gc) const override
virtual QHash< QString, AbstractNodeFactory * > nodeFactories(const QString &name=QString()) override
virtual QHash< QString, AbstractNodeFactory * > nodeFactories(const QString &name=QString()) override
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8