1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QXmlAttributes>
46 #include <QXmlResultItems>
47 #include <QXmlSerializer>
48 #include <private/qxmlquery_p.h>
50 #include "DebugExpressionFactory.h"
51 #include "ExternalSourceLoader.h"
53 #include "TestSuite.h"
54 #include "XMLWriter.h"
58 using namespace QPatternistSDK;
59 using namespace QPatternist;
62 static const DebugExpressionFactory::Ptr s_exprFact(new DebugExpressionFactory());
64 TestCase::TestCase() : m_result(0)
73 TestResult::List TestCase::execute(const ExecutionStage stage,
76 if(name() == QLatin1String("Constr-cont-document-3"))
78 TestResult::List result;
79 result.append(createTestResult(TestResult::Fail, QLatin1String("Skipped this test, because we loop infinitely on it.")));
82 else if(name() == QLatin1String("Axes089"))
84 TestResult::List result;
85 result.append(createTestResult(TestResult::Fail, QLatin1String("Skipped this test, we crash on it.")));
89 qDebug() << "Running test case: " << name();
91 return execute(stage);
94 return TestResult::List();
97 TestResult *TestCase::createTestResult(const TestResult::Status status,
98 const QString &comment) const
100 TestResult *const result = new TestResult(name(),
102 0 /* We don't have an AST. */,
103 ErrorHandler::Message::List(),
104 QPatternist::Item::List(),
106 result->setComment(comment);
110 TestResult::List TestCase::execute(const ExecutionStage stage)
112 ErrorHandler errHandler;
113 ErrorHandler::installQtMessageHandler(&errHandler);
115 pDebug() << "TestCase::execute()";
118 QXmlQuery query(language(), Global::namePoolAsPublic());
120 query.d->setExpressionFactory(s_exprFact);
121 query.setInitialTemplateName(initialTemplateName());
123 QXmlQuery openDoc(query.namePool());
125 if(contextItemSource().isValid())
127 openDoc.setQuery(QString::fromLatin1("doc('") + contextItemSource().toString() + QLatin1String("')"));
128 Q_ASSERT(openDoc.isValid());
129 QXmlResultItems result;
131 openDoc.evaluateTo(&result);
132 const QXmlItem item(result.next());
133 Q_ASSERT(!item.isNull());
134 query.setFocus(item);
137 TestResult::List retval;
139 const Scenario scen(scenario());
140 TestResult::Status resultStatus = TestResult::Unknown;
143 const QString queryString(sourceCode(ok));
147 /* Loading the query file failed, or similar. */
148 resultStatus = TestResult::Fail;
150 m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
151 errHandler.messages(), QPatternist::Item::List(), QString());
152 retval.append(m_result);
153 ErrorHandler::installQtMessageHandler(0);
158 query.setMessageHandler(&errHandler);
159 QXmlNamePool namePool(query.namePool());
161 /* Bind variables. */
162 QPatternist::ExternalVariableLoader::Ptr loader(externalVariableLoader());
166 const ExternalSourceLoader::VariableMap vMap(static_cast<const ExternalSourceLoader *>(loader.data())->variableMap());
167 const QStringList variables(vMap.keys());
169 for(int i = 0; i < variables.count(); ++i)
171 const QXmlName name(namePool, variables.at(i));
172 const QXmlItem val(QPatternist::Item::toPublic(loader->evaluateSingleton(name, QPatternist::DynamicContext::Ptr())));
173 query.bindVariable(name, val);
177 /* We pass in the testCasePath(), such that the base URI is correct fort
178 * XSL-T stylesheets. */
179 query.setQuery(queryString, testCasePath());
183 pDebug() << "Got compilation exception.";
184 resultStatus = TestBaseLine::scanErrors(errHandler.messages(), baseLines());
186 Q_ASSERT(resultStatus != TestResult::Unknown);
187 m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
188 errHandler.messages(), QPatternist::Item::List(), QString());
189 retval.append(m_result);
190 ErrorHandler::installQtMessageHandler(0);
195 if(stage == CompileOnly)
197 m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
198 errHandler.messages(), QPatternist::Item::List(), QString());
199 retval.append(m_result);
203 Q_ASSERT(stage == CompileAndRun);
205 if(scen == ParseError) /* We're supposed to have received an error
208 m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
209 errHandler.messages(), QPatternist::Item::List(), QString());
210 ErrorHandler::installQtMessageHandler(0);
211 retval.append(m_result);
216 QPatternist::Item::List itemList;
219 QBuffer buffer(&output);
220 buffer.open(QIODevice::WriteOnly);
222 QXmlSerializer serializer(query, &buffer);
224 pDebug() << "-------------------------- evaluateToPushCallback() ---------------------------- ";
225 const bool success = query.evaluateTo(&serializer);
226 pDebug() << "------------------------------------------------------------------------------------ ";
230 const QString serialized(QString::fromUtf8(output.constData(), output.size()));
234 resultStatus = TestBaseLine::scanErrors(errHandler.messages(), baseLines());
236 Q_ASSERT(resultStatus != TestResult::Unknown);
237 m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
238 errHandler.messages(), QPatternist::Item::List(), serialized);
239 retval.append(m_result);
240 ErrorHandler::installQtMessageHandler(0);
245 /* It's a regular test. */
246 Q_ASSERT(scen == Standard || scen == RuntimeError);
248 resultStatus = TestBaseLine::scan(serialized, baseLines());
249 Q_ASSERT(resultStatus != TestResult::Unknown);
251 /* Check that errHandler()->messages() at most only contains
252 * warnings, since it shouldn't have errors at this point. */
253 const ErrorHandler::Message::List errors (errHandler.messages());
254 const ErrorHandler::Message::List::const_iterator end(errors.constEnd());
255 ErrorHandler::Message::List::const_iterator it(errors.constBegin());
257 for(; it != end; ++it)
259 const QtMsgType type = (*it).type();
260 if(type == QtFatalMsg)
262 m_result = new TestResult(name(), TestResult::Fail, s_exprFact->astTree(),
263 errHandler.messages(), itemList, serialized);
264 retval.append(m_result);
265 ErrorHandler::installQtMessageHandler(0);
271 m_result = new TestResult(name(), resultStatus, s_exprFact->astTree(),
272 errHandler.messages(), itemList, serialized);
273 retval.append(m_result);
274 ErrorHandler::installQtMessageHandler(0);
279 TestCase::Scenario TestCase::scenarioFromString(const QString &string)
281 if(string == QLatin1String("standard"))
283 else if(string == QLatin1String("parse-error"))
285 else if(string == QLatin1String("runtime-error"))
287 else if(string == QLatin1String("trivial"))
291 Q_ASSERT_X(false, Q_FUNC_INFO,
292 qPrintable(QString::fromLatin1("Invalid string representation for the scenario-enum: %1").arg(string)));
293 return ParseError; /* Silence GCC. */
297 void TestCase::toXML(XMLWriter &receiver) const
300 QXmlAttributes test_caseAtts;
301 test_caseAtts.append(QLatin1String("is-XPath2"), QString(),
302 QLatin1String("is-XPath2"), isXPath() ? QLatin1String("true")
303 : QLatin1String("false"));
304 test_caseAtts.append(QLatin1String("name"), QString(), QLatin1String("name"), name());
305 test_caseAtts.append(QLatin1String("creator"), QString(), QLatin1String("creator"), creator());
311 scen = QLatin1String("standard");
316 scen = QLatin1String("parse-error");
321 scen = QLatin1String("runtime-error");
326 scen = QLatin1String("trivial");
329 default: /* includes 'AnyError' */
332 test_caseAtts.append(QLatin1String("scenario"), QString(), QLatin1String("scenario"), scen);
333 test_caseAtts.append(QLatin1String(QLatin1String("FilePath")), QString(),
334 QLatin1String("FilePath"), QString());
335 receiver.startElement(QLatin1String("test-case"), test_caseAtts);
338 receiver.startElement(QLatin1String("description"), test_caseAtts);
339 receiver.characters(description());
342 receiver.endElement(QLatin1String("description"));
345 QXmlAttributes queryAtts;
346 queryAtts.append(QLatin1String("date"), QString(), QLatin1String("date"), /* This date is a dummy. */
347 QDate::currentDate().toString(Qt::ISODate));
348 queryAtts.append(QLatin1String("name"), QString(), QLatin1String("name"), testCasePath().toString());
349 receiver.startElement(QLatin1String("query"), queryAtts);
352 receiver.endElement(QLatin1String("query"));
354 /* Note: this is invalid, we don't add spec-citation. */
355 TestBaseLine::List bls(baseLines());
356 const TestBaseLine::List::const_iterator end(bls.constEnd());
357 TestBaseLine::List::const_iterator it(bls.constBegin());
359 for(; it != end; ++it)
360 (*it)->toXML(receiver);
363 receiver.endElement(QLatin1String("test-case"));
366 QString TestCase::displayName(const Scenario scen)
371 return QLatin1String("Standard");
373 return QLatin1String("Parse Error");
375 return QLatin1String("Runtime Error");
377 return QLatin1String("Trivial");
389 TestItem::ResultSummary TestCase::resultSummary() const
392 return ResultSummary(m_result->status() == TestResult::Pass ? 1 : 0,
395 return ResultSummary(0, 1);
398 void TestCase::appendChild(TreeItem *)
400 Q_ASSERT_X(false, Q_FUNC_INFO, "Makes no sense to call appendChild() for TestCase.");
403 TreeItem *TestCase::child(const unsigned int) const
405 return 0; /* Silence GCC */
408 TreeItem::List TestCase::children() const
410 return TreeItem::List();
413 unsigned int TestCase::childCount() const
418 TestResult *TestCase::testResult() const
423 bool TestCase::isFinalNode() const
428 QXmlQuery::QueryLanguage TestCase::language() const
430 return QXmlQuery::XQuery10;
433 QXmlName TestCase::initialTemplateName() const
438 // vim: et:ts=4:sw=4:sts=4