aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/autotest/catch/catchcodeparser.cpp
blob: 09c85d5600309e7baf2c049e6afbb8ca753d0804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "catchcodeparser.h"

#include <cplusplus/Token.h>

#include <utils/qtcassert.h>

#include <QRegularExpression>

namespace Autotest {
namespace Internal {

using namespace CPlusPlus;

CatchCodeParser::CatchCodeParser(const QByteArray &source, const LanguageFeatures &features)
    : m_source(source)
    , m_features(features)
{
}

static CatchTestCodeLocationAndType locationAndTypeFromToken(const Token &tkn)
{
    CatchTestCodeLocationAndType locationAndType;
    locationAndType.m_type = TestTreeItem::TestCase;
    locationAndType.m_line = tkn.lineno;
    locationAndType.m_column = 0;
    return locationAndType;
}

static Tokens tokensForSource(const QByteArray &source, const LanguageFeatures &features)
{
    SimpleLexer lexer;
    lexer.setPreprocessorMode(false); // or true? does not make a difference so far..
    lexer.setLanguageFeatures(features);
    return lexer(QString::fromUtf8(source));
}

static QStringList parseTags(const QString &tagsString)
{
    QStringList tagsList;

    const QRegularExpression tagRegEx("\\[(.*?)\\]",QRegularExpression::CaseInsensitiveOption);
    int pos = 0;
    QRegularExpressionMatch it = tagRegEx.match(tagsString, pos);
    while (it.hasMatch()) {
        tagsList.append(it.captured(1));
        pos += it.capturedLength();
        it = tagRegEx.match(tagsString, pos);
    }
    return tagsList;
}

CatchTestCodeLocationList CatchCodeParser::findTests()
{
    m_tokens = tokensForSource(m_source, m_features);
    m_currentIndex = 0;
    for ( ; m_currentIndex < m_tokens.size(); ++m_currentIndex) {
        if (m_tokens.at(m_currentIndex).kind() == T_IDENTIFIER)
            handleIdentifier();
    }
    return m_testCases;
}

void CatchCodeParser::handleIdentifier()
{
    QTC_ASSERT(m_currentIndex < m_tokens.size(), return);
    const Token &token = m_tokens.at(m_currentIndex);
    const QByteArray &identifier = m_source.mid(int(token.bytesBegin()), int(token.bytes()));
    const QByteArray unprefixed = identifier.startsWith("CATCH_") ? identifier.mid(6) : identifier;

    if (unprefixed == "TEST_CASE") {
        handleTestCase(false);
    } else if (unprefixed == "SCENARIO") {
        handleTestCase(true);
    } else if (unprefixed == "TEMPLATE_TEST_CASE" || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE"
               || unprefixed == "TEMPLATE_LIST_TEST_CASE" || unprefixed == "TEMPLATE_TEST_CASE_SIG"
               || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE_SIG") {
        handleParameterizedTestCase(false);
    } else if (unprefixed == "TEST_CASE_METHOD") {
        handleFixtureOrRegisteredTestCase(true);
    } else if (unprefixed == "TEMPLATE_TEST_CASE_METHOD_SIG"
               || unprefixed == "TEMPLATE_PRODUCT_TEST_CASE_METHOD_SIG"
               || unprefixed == "TEMPLATE_TEST_CASE_METHOD"
               || unprefixed == "TEMPLATE_LIST_TEST_CASE_METHOD") {
        handleParameterizedTestCase(true);
    } else if (unprefixed == "METHOD_AS_TEST_CASE" || unprefixed == "REGISTER_TEST_CASE") {
        handleFixtureOrRegisteredTestCase(false);
    }
}

void CatchCodeParser::handleTestCase(bool isScenario)
{
    if (!skipCommentsUntil(T_LPAREN))
        return;

    CatchTestCodeLocationAndType locationAndType
            = locationAndTypeFromToken(m_tokens.at(m_currentIndex));

    Kind stoppedAt;
    ++m_currentIndex;
    QString testCaseName = getStringLiteral(stoppedAt);
    QString tagsString;  // TODO: use them

    if (stoppedAt == T_COMMA) {
        ++m_currentIndex;
        tagsString = getStringLiteral(stoppedAt);
    }

    if (stoppedAt != T_RPAREN)
        return;

    if (isScenario)
        testCaseName.prepend("Scenario: "); // use a flag?

    locationAndType.m_name = testCaseName;
    locationAndType.tags = parseTags(tagsString);
    m_testCases.append(locationAndType);
}

void CatchCodeParser::handleParameterizedTestCase(bool isFixture)
{
    if (!skipCommentsUntil(T_LPAREN))
        return;

    if (isFixture && !skipFixtureParameter())
        return;

    CatchTestCodeLocationAndType locationAndType
            = locationAndTypeFromToken(m_tokens.at(m_currentIndex));

    Kind stoppedAt;
    ++m_currentIndex;
    QString testCaseName = getStringLiteral(stoppedAt);
    QString tagsString;

    if (stoppedAt != T_COMMA)
        return;

    ++m_currentIndex;
    tagsString = getStringLiteral(stoppedAt);

    if (stoppedAt == T_COMMA)
        stoppedAt = skipUntilCorrespondingRParen();

    if (stoppedAt != T_RPAREN)
        return;
    locationAndType.m_name = testCaseName;
    locationAndType.tags = parseTags(tagsString);
    locationAndType.states = CatchTreeItem::Parameterized;
    if (isFixture)
        locationAndType.states |= CatchTreeItem::Fixture;
    m_testCases.append(locationAndType);
}

void CatchCodeParser::handleFixtureOrRegisteredTestCase(bool isFixture)
{
    if (!skipCommentsUntil(T_LPAREN))
        return;

    if (isFixture) {
        if (!skipFixtureParameter())
            return;
    } else {
        if (!skipFunctionParameter())
            return;
    }

    CatchTestCodeLocationAndType locationAndType
            = locationAndTypeFromToken(m_tokens.at(m_currentIndex));

    Kind stoppedAt;
    ++m_currentIndex;
    QString testCaseName = getStringLiteral(stoppedAt);
    QString tagsString;

    if (stoppedAt == T_COMMA) {
        ++m_currentIndex;
        tagsString = getStringLiteral(stoppedAt);
    }

    if (stoppedAt != T_RPAREN)
        return;

    locationAndType.m_name = testCaseName;
    locationAndType.tags = parseTags(tagsString);
    if (isFixture)
        locationAndType.states = CatchTreeItem::Fixture;
    m_testCases.append(locationAndType);
}

QString CatchCodeParser::getStringLiteral(Kind &stoppedAtKind)
{
    QByteArray captured;
    int end = m_tokens.size();
    while (m_currentIndex < end) {
        const Token token = m_tokens.at(m_currentIndex);
        Kind kind = token.kind();
        if (kind == T_STRING_LITERAL) {
            // store the string without its quotes
            captured.append(m_source.mid(token.bytesBegin() + 1, token.bytes() - 2));
        } else if (kind == T_RPAREN || kind == T_COMMA) {
            stoppedAtKind = kind;
            return QString::fromUtf8(captured);
        } else if (!token.isComment()) { // comments are okay - but anything else will cancel
            stoppedAtKind = kind;
            return {};
        }
        ++m_currentIndex;
    }
    stoppedAtKind = T_ERROR;
    return {};
}

bool CatchCodeParser::skipCommentsUntil(Kind nextExpectedKind)
{
    for (int index = m_currentIndex + 1, end = m_tokens.size(); index < end; ++index) {
        const Token &token = m_tokens.at(index);
        if (token.isComment())
            continue;
        if (token.kind() != nextExpectedKind)
            break;
        m_currentIndex = index;
        return true;
    }
    return false;
}

Kind CatchCodeParser::skipUntilCorrespondingRParen()
{
    int openParens = 1;  // we have already one open, looking for the corresponding closing
    int end = m_tokens.size();
    while (m_currentIndex < end) {
        Kind kind = m_tokens.at(m_currentIndex).kind();
        if (kind == T_LPAREN) {
            ++openParens;
        } else if (kind == T_RPAREN) {
            --openParens;
            if (openParens == 0)
                return T_RPAREN;
        }
        ++m_currentIndex;
    }
    return T_ERROR;
}

bool CatchCodeParser::skipFixtureParameter()
{
    if (!skipCommentsUntil(T_IDENTIFIER))
        return false;
    return skipCommentsUntil(T_COMMA);
}

bool CatchCodeParser::skipFunctionParameter()
{
    if (!skipCommentsUntil(T_IDENTIFIER))
        return false;
    if (skipCommentsUntil(T_COLON_COLON))
        return skipFunctionParameter();

    return skipCommentsUntil(T_COMMA);
}

} // namespace Internal
} // namespace Autotest