File indexing completed on 2024-11-17 03:42:07

0001 /*
0002   This file is part of the KTextTemplate library
0003 
0004   SPDX-FileCopyrightText: 2015 Stephen Kelly <steveire@gmail.com>
0005 
0006   SPDX-License-Identifier: LGPL-2.1-or-later
0007 
0008 */
0009 
0010 #include <QDebug>
0011 #include <QFileInfo>
0012 #include <QTest>
0013 
0014 #include "cachingloaderdecorator.h"
0015 #include "context.h"
0016 #include "engine.h"
0017 #include "ktexttemplate_paths.h"
0018 #include "template.h"
0019 #include <metaenumvariable_p.h>
0020 
0021 using Dict = QHash<QString, QVariant>;
0022 
0023 Q_DECLARE_METATYPE(KTextTemplate::Error)
0024 
0025 using namespace KTextTemplate;
0026 
0027 class TestCachingLoader : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 private Q_SLOTS:
0032     void testRenderAfterError();
0033 };
0034 
0035 void TestCachingLoader::testRenderAfterError()
0036 {
0037     Engine engine;
0038     engine.setPluginPaths({QStringLiteral(KTEXTTEMPLATE_PLUGIN_PATH)});
0039 
0040     QSharedPointer<InMemoryTemplateLoader> loader(new InMemoryTemplateLoader);
0041     loader->setTemplate(QStringLiteral("template1"), QStringLiteral("This template has an error {{ va>r }}"));
0042     loader->setTemplate(QStringLiteral("template2"), QStringLiteral("Ok"));
0043     loader->setTemplate(QStringLiteral("main"), QStringLiteral("{% include template_var %}"));
0044 
0045     QSharedPointer<KTextTemplate::CachingLoaderDecorator> cache(new KTextTemplate::CachingLoaderDecorator(loader));
0046 
0047     engine.addTemplateLoader(cache);
0048 
0049     Context c;
0050     Template t;
0051     t = engine.loadByName(QStringLiteral("main"));
0052 
0053     c.insert(QStringLiteral("template_var"), QLatin1String("template1"));
0054     QCOMPARE(t->render(&c), QString());
0055     QCOMPARE(t->error(), TagSyntaxError);
0056 
0057     c.insert(QStringLiteral("template_var"), QLatin1String("template2"));
0058     QCOMPARE(t->render(&c), QLatin1String("Ok"));
0059     QCOMPARE(t->error(), NoError);
0060 }
0061 
0062 QTEST_MAIN(TestCachingLoader)
0063 #include "testcachingloader.moc"