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:
Marco Benelli
2016-06-15 13:07:10 +02:00
parent 28c8412797
commit 2d03eb8bb9
9 changed files with 246 additions and 20 deletions

View File

@@ -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
}

View File

@@ -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
}
}

View File

@@ -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 }
}
}

View File

@@ -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()
}
}

View File

@@ -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
}
}

View File

@@ -23,6 +23,9 @@
**
****************************************************************************/
// ExpectedSemanticMessages: 0
// ExpectedStaticMessages: 1
import QtQuick 2.4
import QtQuick.Controls 1.3

View File

@@ -52,15 +52,48 @@ using namespace QmlJS;
using namespace QmlJS::AST;
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);
QFile file(doc->fileName());
TestData(Document::MutablePtr document, int nSemanticMessages, int nStaticMessages)
: 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);
doc->setSource(file.readAll());
const QString content = QString(file.readAll());
file.close();
Document::MutablePtr doc = Document::create(path, Dialect::Qml);
doc->setSource(content);
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
@@ -97,21 +130,17 @@ void tst_Dependencies::test_data()
QTest::addColumn<int>("nSemanticMessages");
QTest::addColumn<int>("nStaticMessages");
QTest::newRow("ApplicationWindow")
<< QString(m_path + QLatin1String("/001_ApplicationWindow.qml"))
<< 0
<< 0;
QTest::newRow("Window")
<< QString(m_path + QLatin1String("/002_Window.qml"))
<< 0
<< 1;
QDirIterator it(m_path, QDir::Files);
while (it.hasNext()) {
const QString &filepath = it.next();
const QString name = getValue(filepath, ".*/(.*).qml", filepath);
QTest::newRow(name.toLatin1().data()) << filepath;
}
}
void tst_Dependencies::test()
{
QFETCH(QString, filename);
QFETCH(int, nSemanticMessages);
QFETCH(int, nStaticMessages);
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
@@ -124,8 +153,10 @@ void tst_Dependencies::test()
ModelManagerInterface::importScan(result, ModelManagerInterface::workingCopy(), lPaths,
ModelManagerInterface::instance(), false);
Document::MutablePtr doc = readDocument(filename);
TestData data = testData(filename);
Document::MutablePtr doc = data.doc;
int nExpectedSemanticMessages = data.semanticMessages;
int nExpectedStaticMessages = data.staticMessages;
QVERIFY(!doc->source().isEmpty());
Snapshot snapshot = modelManager->snapshot();
@@ -134,7 +165,8 @@ void tst_Dependencies::test()
semanticInfo.document = doc;
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);
@@ -144,8 +176,10 @@ void tst_Dependencies::test()
Check checker(doc, semanticInfo.context);
semanticInfo.staticAnalysisMessages = checker();
QCOMPARE(semanticInfo.semanticMessages.length(), nSemanticMessages);
QCOMPARE(semanticInfo.staticAnalysisMessages.length(), nStaticMessages);
printUnexpectedMessages(semanticInfo, nExpectedSemanticMessages, nExpectedStaticMessages);
QCOMPARE(semanticInfo.semanticMessages.length(), nExpectedSemanticMessages);
QCOMPARE(semanticInfo.staticAnalysisMessages.length(), nExpectedStaticMessages);
}
QTEST_MAIN(tst_Dependencies)