| 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 QtScxml module 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 <QtTest> | 
| 30 | #include <QObject> | 
| 31 | #include <QXmlStreamReader> | 
| 32 | #include <QtScxml/qscxmlcompiler.h> | 
| 33 | #include <QtScxml/qscxmlstatemachine.h> | 
| 34 | |
| 35 | class tst_Parser: public QObject | 
| 36 | { | 
| 37 | Q_OBJECT | 
| 38 | |
| 39 | private Q_SLOTS: | 
| 40 | void error_data(); | 
| 41 | void error(); | 
| 42 | }; | 
| 43 | |
| 44 | void tst_Parser::error_data() | 
| 45 | { | 
| 46 |     QTest::addColumn<QString>(name: "scxmlFileName");  | 
| 47 |     QTest::addColumn<QString>(name: "errorFileName");  | 
| 48 | |
| 49 |     QDir dir(QLatin1String(":/tst_parser/data/"));  | 
| 50 | const auto dirEntries = dir.entryList(); | 
| 51 | for (const QString &entry : dirEntries) { | 
| 52 |         if (!entry.endsWith(s: QLatin1String(".errors"))) {  | 
| 53 | QString scxmlFileName = dir.filePath(fileName: entry); | 
| 54 | QTest::newRow(dataTag: entry.toLatin1().constData()) | 
| 55 |                     << scxmlFileName << (scxmlFileName + QLatin1String(".errors"));  | 
| 56 | } | 
| 57 | } | 
| 58 | } | 
| 59 | |
| 60 | void tst_Parser::error() | 
| 61 | { | 
| 62 | QFETCH(QString, scxmlFileName); | 
| 63 | QFETCH(QString, errorFileName); | 
| 64 | |
| 65 | QFile errorFile(errorFileName); | 
| 66 | errorFile.open(flags: QIODevice::ReadOnly | QIODevice::Text); | 
| 67 | const QStringList expectedErrors = | 
| 68 | QString::fromUtf8(str: errorFile.readAll()).split(sep: '\n', behavior: Qt::SkipEmptyParts); | 
| 69 | |
| 70 | if (!expectedErrors.isEmpty()) | 
| 71 |         QTest::ignoreMessage(type: QtWarningMsg, message: "SCXML document has errors");  | 
| 72 | |
| 73 | QScopedPointer<QScxmlStateMachine> stateMachine(QScxmlStateMachine::fromFile(fileName: scxmlFileName)); | 
| 74 | QVERIFY(!stateMachine.isNull()); | 
| 75 | |
| 76 | const QVector<QScxmlError> errors = stateMachine->parseErrors(); | 
| 77 | if (errors.count() != expectedErrors.count()) { | 
| 78 | for (const QScxmlError &error : errors) { | 
| 79 | qDebug() << error.toString(); | 
| 80 | } | 
| 81 | } | 
| 82 | QCOMPARE(errors.count(), expectedErrors.count()); | 
| 83 | |
| 84 | for (int i = 0; i < errors.count(); ++i) | 
| 85 | QCOMPARE(errors.at(i).toString(), expectedErrors.at(i)); | 
| 86 | } | 
| 87 | |
| 88 | QTEST_MAIN(tst_Parser) | 
| 89 | |
| 90 | #include "tst_parser.moc" | 
| 91 | |
| 92 | |
| 93 | 
