2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-05-11 13:02:42 +02:00
|
|
|
|
|
|
|
|
#include "quicktestvisitors.h"
|
|
|
|
|
|
2018-07-09 10:43:31 +02:00
|
|
|
#include <cplusplus/Overview.h>
|
2016-05-11 13:02:42 +02:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2017-02-27 09:30:16 +01:00
|
|
|
#include <qmljs/qmljsbind.h>
|
|
|
|
|
#include <qmljs/qmljslink.h>
|
|
|
|
|
#include <qmljs/qmljsutils.h>
|
|
|
|
|
#include <utils/algorithm.h>
|
2019-07-26 08:28:17 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2016-05-11 13:02:42 +02:00
|
|
|
|
2020-02-28 17:51:32 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
2016-05-11 13:02:42 +02:00
|
|
|
namespace Autotest {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
2017-02-22 15:09:35 +01:00
|
|
|
static QStringList specialFunctions({"initTestCase", "cleanupTestCase", "init", "cleanup"});
|
2016-05-11 13:02:42 +02:00
|
|
|
|
2023-01-18 08:15:08 +01:00
|
|
|
TestQmlVisitor::TestQmlVisitor(QmlJS::Document::Ptr doc,
|
|
|
|
|
const QmlJS::Snapshot &snapshot,
|
|
|
|
|
bool checkForDerivedTest)
|
2016-05-11 13:02:42 +02:00
|
|
|
: m_currentDoc(doc)
|
2017-02-27 09:30:16 +01:00
|
|
|
, m_snapshot(snapshot)
|
2023-01-18 08:15:08 +01:00
|
|
|
, m_checkForDerivedTest(checkForDerivedTest)
|
2016-05-11 13:02:42 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 09:30:16 +01:00
|
|
|
static bool documentImportsQtTest(const QmlJS::Document *doc)
|
|
|
|
|
{
|
|
|
|
|
if (const QmlJS::Bind *bind = doc->bind()) {
|
2022-12-07 14:45:43 +01:00
|
|
|
return Utils::anyOf(bind->imports(), [](const QmlJS::ImportInfo &info) {
|
2017-02-27 09:30:16 +01:00
|
|
|
return info.isValid() && info.name() == "QtTest";
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isDerivedFromTestCase(QmlJS::AST::UiQualifiedId *id, const QmlJS::Document::Ptr &doc,
|
|
|
|
|
const QmlJS::Snapshot &snapshot)
|
|
|
|
|
{
|
|
|
|
|
if (!id)
|
|
|
|
|
return false;
|
|
|
|
|
QmlJS::Link link(snapshot, QmlJS::ViewerContext(), QmlJS::LibraryInfo());
|
|
|
|
|
const QmlJS::ContextPtr context = link();
|
|
|
|
|
|
|
|
|
|
const QmlJS::ObjectValue *value = context->lookupType(doc.data(), id);
|
|
|
|
|
if (!value)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QmlJS::PrototypeIterator protoIterator(value, context);
|
|
|
|
|
const QList<const QmlJS::ObjectValue *> prototypes = protoIterator.all();
|
|
|
|
|
for (const QmlJS::ObjectValue *val : prototypes) {
|
|
|
|
|
if (auto prototype = val->prototype()) {
|
|
|
|
|
if (auto qmlPrototypeRef = prototype->asQmlPrototypeReference()) {
|
|
|
|
|
if (auto qmlTypeName = qmlPrototypeRef->qmlTypeName()) {
|
2020-09-16 15:08:57 +02:00
|
|
|
if (qmlTypeName->name == QLatin1String("TestCase")) {
|
2017-02-27 09:30:16 +01:00
|
|
|
if (auto astObjVal = val->asAstObjectValue())
|
|
|
|
|
return documentImportsQtTest(astObjVal->document());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 13:02:42 +02:00
|
|
|
bool TestQmlVisitor::visit(QmlJS::AST::UiObjectDefinition *ast)
|
|
|
|
|
{
|
2020-09-16 15:08:57 +02:00
|
|
|
const QStringView name = ast->qualifiedTypeNameId->name;
|
2019-07-30 13:17:35 +02:00
|
|
|
m_objectIsTestStack.push(false);
|
2020-09-16 15:08:57 +02:00
|
|
|
if (name != QLatin1String("TestCase")) {
|
2023-01-18 08:15:08 +01:00
|
|
|
if (!m_checkForDerivedTest
|
|
|
|
|
|| !isDerivedFromTestCase(ast->qualifiedTypeNameId, m_currentDoc, m_snapshot)) {
|
2017-02-27 09:30:16 +01:00
|
|
|
return true;
|
2023-01-18 08:15:08 +01:00
|
|
|
}
|
2017-02-27 09:30:16 +01:00
|
|
|
} else if (!documentImportsQtTest(m_currentDoc.data())) {
|
2016-05-11 13:02:42 +02:00
|
|
|
return true; // find nested TestCase items as well
|
2017-02-27 09:30:16 +01:00
|
|
|
}
|
2016-05-11 13:02:42 +02:00
|
|
|
|
2019-07-30 13:17:35 +02:00
|
|
|
m_objectIsTestStack.top() = true;
|
2016-05-11 13:02:42 +02:00
|
|
|
const auto sourceLocation = ast->firstSourceLocation();
|
2019-07-26 08:28:17 +02:00
|
|
|
QuickTestCaseSpec currentSpec;
|
2022-06-20 12:35:13 +02:00
|
|
|
currentSpec.m_locationAndType.m_filePath = m_currentDoc->fileName();
|
2019-07-30 13:17:35 +02:00
|
|
|
currentSpec.m_locationAndType.m_line = sourceLocation.startLine;
|
|
|
|
|
currentSpec.m_locationAndType.m_column = sourceLocation.startColumn - 1;
|
|
|
|
|
currentSpec.m_locationAndType.m_type = TestTreeItem::TestCase;
|
|
|
|
|
m_caseParseStack.push(currentSpec);
|
2016-05-11 13:02:42 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-20 10:12:04 +02:00
|
|
|
void TestQmlVisitor::endVisit(QmlJS::AST::UiObjectDefinition *)
|
|
|
|
|
{
|
2019-07-30 13:17:35 +02:00
|
|
|
if (!m_objectIsTestStack.isEmpty() && m_objectIsTestStack.pop() && !m_caseParseStack.isEmpty())
|
|
|
|
|
m_testCases << m_caseParseStack.pop();
|
2018-06-20 10:12:04 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-11 13:02:42 +02:00
|
|
|
bool TestQmlVisitor::visit(QmlJS::AST::ExpressionStatement *ast)
|
|
|
|
|
{
|
|
|
|
|
const QmlJS::AST::ExpressionNode *expr = ast->expression;
|
|
|
|
|
return expr->kind == QmlJS::AST::Node::Kind_StringLiteral;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TestQmlVisitor::visit(QmlJS::AST::UiScriptBinding *ast)
|
|
|
|
|
{
|
2019-07-30 13:17:35 +02:00
|
|
|
if (m_objectIsTestStack.top())
|
2020-09-16 15:08:57 +02:00
|
|
|
m_expectTestCaseName = ast->qualifiedId->name == QLatin1String("name");
|
2018-06-20 10:12:04 +02:00
|
|
|
return m_expectTestCaseName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TestQmlVisitor::endVisit(QmlJS::AST::UiScriptBinding *)
|
|
|
|
|
{
|
|
|
|
|
if (m_expectTestCaseName)
|
|
|
|
|
m_expectTestCaseName = false;
|
2016-05-11 13:02:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TestQmlVisitor::visit(QmlJS::AST::FunctionDeclaration *ast)
|
|
|
|
|
{
|
2019-07-30 13:17:35 +02:00
|
|
|
if (m_caseParseStack.isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-09-16 15:08:57 +02:00
|
|
|
const QString name = ast->name.toString();
|
|
|
|
|
if (name.startsWith("test_") || name.startsWith("benchmark_") || name.endsWith("_data")
|
2021-06-16 16:06:34 +02:00
|
|
|
|| specialFunctions.contains(name)) {
|
2016-05-11 13:02:42 +02:00
|
|
|
const auto sourceLocation = ast->firstSourceLocation();
|
|
|
|
|
TestCodeLocationAndType locationAndType;
|
2021-06-16 16:06:34 +02:00
|
|
|
locationAndType.m_name = name;
|
2022-06-20 12:35:13 +02:00
|
|
|
locationAndType.m_filePath = m_currentDoc->fileName();
|
2016-05-11 13:02:42 +02:00
|
|
|
locationAndType.m_line = sourceLocation.startLine;
|
|
|
|
|
locationAndType.m_column = sourceLocation.startColumn - 1;
|
2020-09-16 15:08:57 +02:00
|
|
|
if (specialFunctions.contains(name))
|
2016-05-11 13:02:42 +02:00
|
|
|
locationAndType.m_type = TestTreeItem::TestSpecialFunction;
|
2016-09-29 12:15:43 +02:00
|
|
|
else if (name.endsWith("_data"))
|
2016-05-11 13:02:42 +02:00
|
|
|
locationAndType.m_type = TestTreeItem::TestDataFunction;
|
|
|
|
|
else
|
2019-05-21 07:50:34 +02:00
|
|
|
locationAndType.m_type = TestTreeItem::TestFunction;
|
2016-05-11 13:02:42 +02:00
|
|
|
|
2019-07-30 15:14:40 +02:00
|
|
|
// identical test functions inside the same file are not working - will fail at runtime
|
|
|
|
|
if (!Utils::anyOf(m_caseParseStack.top().m_functions,
|
2021-06-16 16:06:34 +02:00
|
|
|
[locationAndType](const TestCodeLocationAndType &func) {
|
|
|
|
|
return func.m_type == locationAndType.m_type
|
|
|
|
|
&& func.m_name == locationAndType.m_name
|
|
|
|
|
&& func.m_filePath == locationAndType.m_filePath;
|
2019-07-30 15:14:40 +02:00
|
|
|
})) {
|
2021-06-16 16:06:34 +02:00
|
|
|
m_caseParseStack.top().m_functions.append(locationAndType);
|
2019-07-30 15:14:40 +02:00
|
|
|
}
|
2016-05-11 13:02:42 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool TestQmlVisitor::visit(QmlJS::AST::StringLiteral *ast)
|
|
|
|
|
{
|
2019-07-26 08:28:17 +02:00
|
|
|
if (m_expectTestCaseName) {
|
2019-07-30 13:17:35 +02:00
|
|
|
QTC_ASSERT(!m_caseParseStack.isEmpty(), return false);
|
|
|
|
|
m_caseParseStack.top().m_caseName = ast->value.toString();
|
2018-06-20 10:12:04 +02:00
|
|
|
m_expectTestCaseName = false;
|
|
|
|
|
}
|
2016-05-11 13:02:42 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 17:51:32 +01:00
|
|
|
void TestQmlVisitor::throwRecursionDepthError()
|
|
|
|
|
{
|
|
|
|
|
qWarning("Warning: Hit maximum recursion depth while visiting AST in TestQmlVisitor");
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-09 10:43:31 +02:00
|
|
|
/************************************** QuickTestAstVisitor *************************************/
|
|
|
|
|
|
|
|
|
|
QuickTestAstVisitor::QuickTestAstVisitor(CPlusPlus::Document::Ptr doc,
|
|
|
|
|
const CPlusPlus::Snapshot &snapshot)
|
|
|
|
|
: ASTVisitor(doc->translationUnit())
|
|
|
|
|
, m_currentDoc(doc)
|
|
|
|
|
, m_snapshot(snapshot)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool QuickTestAstVisitor::visit(CPlusPlus::CallAST *ast)
|
|
|
|
|
{
|
|
|
|
|
if (m_currentDoc.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (const auto expressionAST = ast->base_expression) {
|
|
|
|
|
if (const auto idExpressionAST = expressionAST->asIdExpression()) {
|
|
|
|
|
if (const auto simpleNameAST = idExpressionAST->name->asSimpleName()) {
|
|
|
|
|
const CPlusPlus::Overview o;
|
|
|
|
|
const QString prettyName = o.prettyName(simpleNameAST->name);
|
|
|
|
|
if (prettyName == "quick_test_main" || prettyName == "quick_test_main_with_setup") {
|
|
|
|
|
if (auto expressionListAST = ast->expression_list) {
|
|
|
|
|
// third argument is the one we need, so skip current and next
|
|
|
|
|
expressionListAST = expressionListAST->next; // argv
|
|
|
|
|
expressionListAST = expressionListAST ? expressionListAST->next : nullptr; // testcase literal
|
|
|
|
|
|
|
|
|
|
if (expressionListAST && expressionListAST->value) {
|
|
|
|
|
const auto *stringLitAST = expressionListAST->value->asStringLiteral();
|
2020-04-15 13:33:44 +02:00
|
|
|
if (!stringLitAST)
|
|
|
|
|
return false;
|
2018-07-09 10:43:31 +02:00
|
|
|
const auto *string
|
|
|
|
|
= translationUnit()->stringLiteral(stringLitAST->literal_token);
|
|
|
|
|
if (string) {
|
|
|
|
|
m_testBaseName = QString::fromUtf8(string->chars(),
|
|
|
|
|
int(string->size()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 13:02:42 +02:00
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Autotest
|