1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QColor>
30#include <QFile>
31#include <QFileInfo>
32#include <QVariant>
33#include <QtDebug>
34
35#include "XQTSTestCase.h"
36
37using namespace QPatternistSDK;
38using namespace QPatternist;
39
40XQTSTestCase::XQTSTestCase(const Scenario scen,
41 TreeItem *p,
42 const QXmlQuery::QueryLanguage lang) : m_isXPath(false)
43 , m_scenario(scen)
44 , m_parent(p)
45 , m_lang(lang)
46{
47}
48
49XQTSTestCase::~XQTSTestCase()
50{
51 qDeleteAll(c: m_baseLines);
52}
53
54QVariant XQTSTestCase::data(const Qt::ItemDataRole role, int column) const
55{
56 if(role == Qt::DisplayRole)
57 {
58 if(column == 0)
59 return title();
60
61 const TestResult *const tr = testResult();
62 if(!tr)
63 {
64 if(column == 1)
65 return TestResult::displayName(status: TestResult::NotTested);
66 else
67 return QString();
68 }
69 const TestResult::Status status = tr->status();
70
71 switch(column)
72 {
73 case 1:
74 return status == TestResult::Pass ? QString(QChar::fromLatin1(c: '1'))
75 : QString(QChar::fromLatin1(c: '0'));
76 case 2:
77 return status == TestResult::Fail ? QString(QChar::fromLatin1(c: '1'))
78 : QString(QChar::fromLatin1(c: '0'));
79 default:
80 return QString();
81 }
82 }
83
84 if(role != Qt::BackgroundRole)
85 return QVariant();
86
87 const TestResult *const tr = testResult();
88
89 if(!tr)
90 {
91 if(column == 0)
92 return QColor(Qt::yellow);
93 else
94 return QVariant();
95 }
96
97 const TestResult::Status status = tr->status();
98
99 if(status == TestResult::NotTested || status == TestResult::Unknown)
100 return QColor(Qt::yellow);
101
102 switch(column)
103 {
104 case 1:
105 return status == TestResult::Pass ? QColor(Qt::green) : QVariant();
106 case 2:
107 return status == TestResult::Fail ? QColor(Qt::red) : QVariant();
108 default:
109 return QVariant();
110 }
111}
112
113QString XQTSTestCase::sourceCode(bool &ok) const
114{
115 QFile file(m_queryPath.toLocalFile());
116
117 QString err;
118
119 if(!file.exists())
120 err = QString::fromLatin1(str: "Error: %1 does not exist.").arg(a: file.fileName());
121 else if(!QFileInfo(file.fileName()).isFile())
122 err = QString::fromLatin1(str: "Error: %1 is not a file, cannot display it.").arg(a: file.fileName());
123 else if(!file.open(flags: QIODevice::ReadOnly))
124 err = QString::fromLatin1(str: "Error: Could not open %1. Likely a permission error.")
125 .arg(a: file.fileName());
126
127 if(err.isNull()) /* No errors. */
128 {
129 ok = true;
130 /* Scary, we assume the query is stored in UTF-8. */
131 return QString::fromUtf8(str: file.readAll());
132 }
133 else
134 {
135 ok = false;
136 return err;
137 }
138}
139
140int XQTSTestCase::columnCount() const
141{
142 return 2;
143}
144
145void XQTSTestCase::addBaseLine(TestBaseLine *line)
146{
147 m_baseLines.append(t: line);
148}
149
150QString XQTSTestCase::name() const
151{
152 return m_name;
153}
154
155QString XQTSTestCase::creator() const
156{
157 return m_creator;
158}
159
160QString XQTSTestCase::description() const
161{
162 return m_description;
163}
164
165QDate XQTSTestCase::lastModified() const
166{
167 return m_lastModified;
168}
169
170bool XQTSTestCase::isXPath() const
171{
172 return m_isXPath;
173}
174
175TestCase::Scenario XQTSTestCase::scenario() const
176{
177 return m_scenario;
178}
179
180void XQTSTestCase::setName(const QString &n)
181{
182 m_name = n;
183}
184
185void XQTSTestCase::setCreator(const QString &ctor)
186{
187 m_creator = ctor;
188}
189
190void XQTSTestCase::setDescription(const QString &descriptionP)
191{
192 m_description = descriptionP;
193}
194
195void XQTSTestCase::setLastModified(const QDate &date)
196{
197 m_lastModified = date;
198}
199
200void XQTSTestCase::setIsXPath(const bool isXPathP)
201{
202 m_isXPath = isXPathP;
203}
204
205void XQTSTestCase::setQueryPath(const QUrl &uri)
206{
207 m_queryPath = uri;
208}
209
210TreeItem *XQTSTestCase::parent() const
211{
212 return m_parent;
213}
214
215QString XQTSTestCase::title() const
216{
217 return m_name;
218}
219
220TestBaseLine::List XQTSTestCase::baseLines() const
221{
222 Q_ASSERT_X(!m_baseLines.isEmpty(), Q_FUNC_INFO,
223 qPrintable(QString::fromLatin1("The test %1 has no base lines, it should have at least one.").arg(name())));
224 return m_baseLines;
225}
226
227QUrl XQTSTestCase::testCasePath() const
228{
229 return m_queryPath;
230}
231
232void XQTSTestCase::setExternalVariableLoader(const QPatternist::ExternalVariableLoader::Ptr &loader)
233{
234 m_externalVariableLoader = loader;
235}
236
237QPatternist::ExternalVariableLoader::Ptr XQTSTestCase::externalVariableLoader() const
238{
239 return m_externalVariableLoader;
240}
241
242void XQTSTestCase::setContextItemSource(const QUrl &uri)
243{
244 m_contextItemSource = uri;
245}
246
247QUrl XQTSTestCase::contextItemSource() const
248{
249 return m_contextItemSource;
250}
251
252QXmlQuery::QueryLanguage XQTSTestCase::language() const
253{
254 return m_lang;
255}
256
257void XQTSTestCase::setParent(TreeItem *const p)
258{
259 m_parent = p;
260}
261
262void XQTSTestCase::setInitialTemplateName(const QXmlName &name)
263{
264 m_initialTemplateName = name;
265}
266
267QXmlName XQTSTestCase::initialTemplateName() const
268{
269 return m_initialTemplateName;
270}
271
272// vim: et:ts=4:sw=4:sts=4
273
274

source code of qtxmlpatterns/tests/auto/xmlpatternssdk/XQTSTestCase.cpp