forked from qt-creator/qt-creator
QmlJs code model: added test cases.
Added test cases and modified the test suite in order to make it data-driven. Each sample file can now declare the number of expected messages. If there is no declaration, zero is assumed. Change-Id: Ife3daa10a258f51ea8f896156f6f6af783406b84 Reviewed-by: Thomas Hartmann <Thomas.Hartmann@theqtcompany.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
import QtQuick 2.4
|
||||||
|
import QtQuick.Controls 1.4
|
||||||
|
|
||||||
|
Button {
|
||||||
|
text: "Button"
|
||||||
|
antialiasing: true
|
||||||
|
scale: 1.0
|
||||||
|
}
|
@@ -0,0 +1,43 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
import QtQuick 2.4
|
||||||
|
|
||||||
|
ListModel {
|
||||||
|
id: fruitModel
|
||||||
|
|
||||||
|
ListElement {
|
||||||
|
name: "Apple"
|
||||||
|
cost: 2.45
|
||||||
|
}
|
||||||
|
ListElement {
|
||||||
|
name: "Orange"
|
||||||
|
cost: 3.25
|
||||||
|
}
|
||||||
|
ListElement {
|
||||||
|
name: "Banana"
|
||||||
|
cost: 1.95
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,35 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
import QtQuick 2.4
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
anchors.fill: parent
|
||||||
|
model: fruitModel
|
||||||
|
delegate: Row {
|
||||||
|
Text { text: "Fruit: " + name }
|
||||||
|
Text { text: "Cost: $" + cost }
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,37 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
import QtQuick 2.4
|
||||||
|
import QtQuick.Dialogs 1.1
|
||||||
|
|
||||||
|
MessageDialog {
|
||||||
|
id: messageDialog
|
||||||
|
title: "May I have your attention please"
|
||||||
|
text: "It's so cool that you are using Qt Quick."
|
||||||
|
onAccepted: {
|
||||||
|
console.log("And of course you could only agree.")
|
||||||
|
Qt.quit()
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,41 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
**
|
||||||
|
** Copyright (C) 2016 The Qt Company Ltd.
|
||||||
|
** Contact: https://www.qt.io/licensing/
|
||||||
|
**
|
||||||
|
** This file is part of Qt Creator.
|
||||||
|
**
|
||||||
|
** Commercial License Usage
|
||||||
|
** Licensees holding valid commercial Qt licenses may use this file in
|
||||||
|
** accordance with the commercial license agreement provided with the
|
||||||
|
** Software or, alternatively, in accordance with the terms contained in
|
||||||
|
** a written agreement between you and The Qt Company. For licensing terms
|
||||||
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||||
|
** information use the contact form at https://www.qt.io/contact-us.
|
||||||
|
**
|
||||||
|
** GNU General Public License Usage
|
||||||
|
** Alternatively, this file may be used under the terms of the GNU
|
||||||
|
** General Public License version 3 as published by the Free Software
|
||||||
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||||
|
** included in the packaging of this file. Please review the following
|
||||||
|
** information to ensure the GNU General Public License requirements will
|
||||||
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||||
|
**
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
import QtQuick 2.4
|
||||||
|
import QtQml.Models 2.2
|
||||||
|
|
||||||
|
Rectangle {
|
||||||
|
ObjectModel {
|
||||||
|
id: itemModel
|
||||||
|
Rectangle { height: 30; width: 80; color: "red" }
|
||||||
|
Rectangle { height: 30; width: 80; color: "green" }
|
||||||
|
Rectangle { height: 30; width: 80; color: "blue" }
|
||||||
|
}
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
anchors.fill: parent
|
||||||
|
model: itemModel
|
||||||
|
}
|
||||||
|
}
|
@@ -23,6 +23,9 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
// ExpectedSemanticMessages: 0
|
||||||
|
// ExpectedStaticMessages: 1
|
||||||
|
|
||||||
import QtQuick 2.4
|
import QtQuick 2.4
|
||||||
import QtQuick.Controls 1.3
|
import QtQuick.Controls 1.3
|
||||||
|
|
@@ -52,15 +52,48 @@ using namespace QmlJS;
|
|||||||
using namespace QmlJS::AST;
|
using namespace QmlJS::AST;
|
||||||
using namespace QmlJS::StaticAnalysis;
|
using namespace QmlJS::StaticAnalysis;
|
||||||
|
|
||||||
static Document::MutablePtr readDocument(const QString &path)
|
static QString getValue(const QString &data,
|
||||||
|
const QString &re,
|
||||||
|
const QString &defaultValue = QString::number(0)) {
|
||||||
|
const QRegularExpressionMatch m = QRegularExpression(re).match(data);
|
||||||
|
return m.hasMatch() ? m.captured(1) : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TestData
|
||||||
{
|
{
|
||||||
Document::MutablePtr doc = Document::create(path, Dialect::Qml);
|
TestData(Document::MutablePtr document, int nSemanticMessages, int nStaticMessages)
|
||||||
QFile file(doc->fileName());
|
: doc(document)
|
||||||
|
, semanticMessages(nSemanticMessages)
|
||||||
|
, staticMessages(nStaticMessages)
|
||||||
|
{}
|
||||||
|
Document::MutablePtr doc;
|
||||||
|
const int semanticMessages;
|
||||||
|
const int staticMessages;
|
||||||
|
};
|
||||||
|
|
||||||
|
static TestData testData(const QString &path) {
|
||||||
|
QFile file(path);
|
||||||
file.open(QFile::ReadOnly | QFile::Text);
|
file.open(QFile::ReadOnly | QFile::Text);
|
||||||
doc->setSource(file.readAll());
|
const QString content = QString(file.readAll());
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
|
Document::MutablePtr doc = Document::create(path, Dialect::Qml);
|
||||||
|
doc->setSource(content);
|
||||||
doc->parse();
|
doc->parse();
|
||||||
return doc;
|
const QString nSemantic = getValue(content, "//\\s*ExpectedSemanticMessages: (\\d+)");
|
||||||
|
const QString nStatic = getValue(content, "//\\s*ExpectedStaticMessages: (\\d+)");
|
||||||
|
return TestData(doc, nSemantic.toInt(), nStatic.toInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
void printUnexpectedMessages(const QmlJSTools::SemanticInfo &info, int nSemantic, int nStatic)
|
||||||
|
{
|
||||||
|
if (nSemantic == 0 && info.semanticMessages.length() > 0)
|
||||||
|
for (auto msg: info.semanticMessages)
|
||||||
|
qDebug() << msg.message;
|
||||||
|
if (nStatic == 0 && info.staticAnalysisMessages.length() > 0)
|
||||||
|
for (auto msg: info.staticAnalysisMessages)
|
||||||
|
qDebug() << msg.message;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
class tst_Dependencies : public QObject
|
class tst_Dependencies : public QObject
|
||||||
@@ -97,21 +130,17 @@ void tst_Dependencies::test_data()
|
|||||||
QTest::addColumn<int>("nSemanticMessages");
|
QTest::addColumn<int>("nSemanticMessages");
|
||||||
QTest::addColumn<int>("nStaticMessages");
|
QTest::addColumn<int>("nStaticMessages");
|
||||||
|
|
||||||
QTest::newRow("ApplicationWindow")
|
QDirIterator it(m_path, QDir::Files);
|
||||||
<< QString(m_path + QLatin1String("/001_ApplicationWindow.qml"))
|
while (it.hasNext()) {
|
||||||
<< 0
|
const QString &filepath = it.next();
|
||||||
<< 0;
|
const QString name = getValue(filepath, ".*/(.*).qml", filepath);
|
||||||
QTest::newRow("Window")
|
QTest::newRow(name.toLatin1().data()) << filepath;
|
||||||
<< QString(m_path + QLatin1String("/002_Window.qml"))
|
}
|
||||||
<< 0
|
|
||||||
<< 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_Dependencies::test()
|
void tst_Dependencies::test()
|
||||||
{
|
{
|
||||||
QFETCH(QString, filename);
|
QFETCH(QString, filename);
|
||||||
QFETCH(int, nSemanticMessages);
|
|
||||||
QFETCH(int, nStaticMessages);
|
|
||||||
|
|
||||||
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
|
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
|
||||||
|
|
||||||
@@ -124,8 +153,10 @@ void tst_Dependencies::test()
|
|||||||
ModelManagerInterface::importScan(result, ModelManagerInterface::workingCopy(), lPaths,
|
ModelManagerInterface::importScan(result, ModelManagerInterface::workingCopy(), lPaths,
|
||||||
ModelManagerInterface::instance(), false);
|
ModelManagerInterface::instance(), false);
|
||||||
|
|
||||||
|
TestData data = testData(filename);
|
||||||
Document::MutablePtr doc = readDocument(filename);
|
Document::MutablePtr doc = data.doc;
|
||||||
|
int nExpectedSemanticMessages = data.semanticMessages;
|
||||||
|
int nExpectedStaticMessages = data.staticMessages;
|
||||||
QVERIFY(!doc->source().isEmpty());
|
QVERIFY(!doc->source().isEmpty());
|
||||||
|
|
||||||
Snapshot snapshot = modelManager->snapshot();
|
Snapshot snapshot = modelManager->snapshot();
|
||||||
@@ -134,7 +165,8 @@ void tst_Dependencies::test()
|
|||||||
semanticInfo.document = doc;
|
semanticInfo.document = doc;
|
||||||
semanticInfo.snapshot = snapshot;
|
semanticInfo.snapshot = snapshot;
|
||||||
|
|
||||||
Link link(semanticInfo.snapshot, modelManager->defaultVContext(doc->language(), doc), modelManager->builtins(doc));
|
Link link(semanticInfo.snapshot, modelManager->defaultVContext(doc->language(), doc),
|
||||||
|
modelManager->builtins(doc));
|
||||||
|
|
||||||
semanticInfo.context = link(doc, &semanticInfo.semanticMessages);
|
semanticInfo.context = link(doc, &semanticInfo.semanticMessages);
|
||||||
|
|
||||||
@@ -144,8 +176,10 @@ void tst_Dependencies::test()
|
|||||||
Check checker(doc, semanticInfo.context);
|
Check checker(doc, semanticInfo.context);
|
||||||
semanticInfo.staticAnalysisMessages = checker();
|
semanticInfo.staticAnalysisMessages = checker();
|
||||||
|
|
||||||
QCOMPARE(semanticInfo.semanticMessages.length(), nSemanticMessages);
|
printUnexpectedMessages(semanticInfo, nExpectedSemanticMessages, nExpectedStaticMessages);
|
||||||
QCOMPARE(semanticInfo.staticAnalysisMessages.length(), nStaticMessages);
|
|
||||||
|
QCOMPARE(semanticInfo.semanticMessages.length(), nExpectedSemanticMessages);
|
||||||
|
QCOMPARE(semanticInfo.staticAnalysisMessages.length(), nExpectedStaticMessages);
|
||||||
}
|
}
|
||||||
|
|
||||||
QTEST_MAIN(tst_Dependencies)
|
QTEST_MAIN(tst_Dependencies)
|
||||||
|
Reference in New Issue
Block a user