File: test_miparser.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (221 lines) | stat: -rw-r--r-- 8,402 bytes parent folder | download | duplicates (2)
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
/* This file is part of KDevelop
 *
 * Copyright 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "test_miparser.h"

// SUT
#include <mi/miparser.h>
// Qt
#include <QTest>

struct ResultData
{
    QString name;
    QVariant value;
};

struct ResultRecordData
{
    uint32_t token;
    QString reason;
    QVector<ResultData> results;

    QVariant toVariant() { return QVariant::fromValue<ResultRecordData>(*this); }
};
Q_DECLARE_METATYPE(ResultRecordData)

struct AsyncRecordData
{
    int subkind;
    QString reason;
    QVector<ResultData> results;

    QVariant toVariant() { return QVariant::fromValue<AsyncRecordData>(*this); }
};
Q_DECLARE_METATYPE(AsyncRecordData)

struct StreamRecordData
{
    int subkind;
    QString message;

    QVariant toVariant() { return QVariant::fromValue<StreamRecordData>(*this); }
};
Q_DECLARE_METATYPE(StreamRecordData)


void TestMIParser::testParseLine_data()
{
    QTest::addColumn<QByteArray>("line");
    QTest::addColumn<int>("recordKind");
    QTest::addColumn<QVariant>("recordData");

    // prompt
    QTest::newRow("gdpprompt")
        << QByteArray("(gdb)")
        << (int)KDevMI::MI::Record::Prompt
        << QVariant();

    // result records
    QTest::newRow("done")
        << QByteArray("^done")
        << (int)KDevMI::MI::Record::Result
        << ResultRecordData{0, "done", {}}.toVariant();
    QTest::newRow("doneWToken")
        << QByteArray("11^done")
        << (int)KDevMI::MI::Record::Result
        << ResultRecordData{11, "done", {}}.toVariant();
    QTest::newRow("runningWToken")
        << QByteArray("25^running")
        << (int)KDevMI::MI::Record::Result
        << ResultRecordData{25, "running", {}}.toVariant();

    // Out-of-band records
    QTest::newRow("stopreply")
        << QByteArray("*stop,reason=\"stop\",address=\"0x123\",source=\"a.c:123\"")
        << (int)KDevMI::MI::Record::Async
        << AsyncRecordData{KDevMI::MI::AsyncRecord::Exec, "stop",
                           {{"reason", "stop"}, {"address", "0x123"}, {"source", "a.c:123"}}}.toVariant();
    QTest::newRow("threadgroupadded")
        << QByteArray("=thread-group-added,id=\"i1\"")
        << (int)KDevMI::MI::Record::Async
        << AsyncRecordData{KDevMI::MI::AsyncRecord::Notify, "thread-group-added",
                           {{"id", "i1"}}}.toVariant();
    QTest::newRow("symbolfilereply") << QByteArray("*breakpoint,nr=\"3\",address=\"0x123\",source=\"a.c:123\"")
        << (int)KDevMI::MI::Record::Async
        << AsyncRecordData{KDevMI::MI::AsyncRecord::Exec, "breakpoint",
                           {{"nr", "3"}, {"address", "0x123"}, {"source", "a.c:123"}}}.toVariant();

    // breakpoint creation records
    QTest::newRow("breakreply")
        << QByteArray("&\"break /path/to/some/file.cpp:28\\n\"")
        << (int)KDevMI::MI::Record::Stream
        << StreamRecordData{KDevMI::MI::StreamRecord::Log, "break /path/to/some/file.cpp:28\n"}.toVariant();
    QTest::newRow("breakreply2")
        << QByteArray("~\"Breakpoint 1 at 0x400ab0: file /path/to/some/file.cpp, line 28.\\n\"")
        << (int)KDevMI::MI::Record::Stream
        << StreamRecordData{KDevMI::MI::StreamRecord::Console, "Breakpoint 1 at 0x400ab0: file /path/to/some/file.cpp, line 28.\n"}.toVariant();
    QTest::newRow("breakreply3")
        << QByteArray("=breakpoint-created,bkpt={number=\"1\",type=\"breakpoint\",disp=\"keep\",enabled=\"y\",addr=\"0x0000000000400ab0\",func=\"main(int, char**)\",file=\"/path/to/some/file.cpp\",fullname=\"/path/to/some/file.cpp\",line=\"28\",thread-groups=[\"i1\"],times=\"0\",original-location=\"/path/to/some/file.cpp:28\"}")
        << (int)KDevMI::MI::Record::Async
        << AsyncRecordData{KDevMI::MI::AsyncRecord::Notify, "breakpoint-created",
                           {{"bkpt", QVariantMap{
                               {"number", "1"},
                               {"type", "breakpoint"},
                               {"disp", "keep"},
                               {"enabled", "y"},
                               {"addr", "0x0000000000400ab0"},
                               {"func", "main(int, char**)"},
                               {"file", "/path/to/some/file.cpp"},
                               {"fullname", "/path/to/some/file.cpp"},
                               {"line", "28"},
                               {"thread-groups", QVariantList{"i1"}},
                               {"times", "0"},
                               {"original-location", "/path/to/some/file.cpp:28"}}}}}.toVariant();
}


void TestMIParser::doTestResult(const KDevMI::MI::Value& actualValue, const QVariant& expectedValue)
{
    if (expectedValue.type() == QVariant::String) {
        QCOMPARE((int)actualValue.kind, (int)KDevMI::MI::Value::StringLiteral);
        QCOMPARE(actualValue.literal(), expectedValue.toString());
    } else if (expectedValue.type() == QVariant::List) {
        QCOMPARE(actualValue.kind, KDevMI::MI::Value::List);

        const auto expectedList = expectedValue.toList();
        QCOMPARE(actualValue.size(), expectedList.size());
        for (int i = 0; i < expectedList.size(); ++i) {
            doTestResult(actualValue[i], expectedList[i]);
        }
    } else if (expectedValue.type() == QVariant::Map) {
        QCOMPARE(actualValue.kind, KDevMI::MI::Value::Tuple);

        const auto expectedMap = expectedValue.toMap();

        for (auto it = expectedMap.begin(), end = expectedMap.end(); it != end; ++it) {
            const auto& expectedMapField = it.key();
            QVERIFY(actualValue.hasField(expectedMapField));
            const auto& expectedMapValue = it.value();
            doTestResult(actualValue[expectedMapField], expectedMapValue);
        }
    } else {
        QFAIL("Using unexpected QVariant type");
    }
}


void TestMIParser::testParseLine()
{
    QFETCH(QByteArray, line);
    QFETCH(int, recordKind);
    QFETCH(QVariant, recordData);

    KDevMI::MI::MIParser m_parser;

    KDevMI::MI::FileSymbol file;
    file.contents = line;

    std::unique_ptr<KDevMI::MI::Record> record(m_parser.parse(&file));
    QVERIFY(record != nullptr);
    QCOMPARE((int)record->kind, recordKind);

    switch(recordKind) {
    case KDevMI::MI::Record::Result: {
        const auto& resultRecord = static_cast<KDevMI::MI::ResultRecord&>(*record);
        const auto resultData = recordData.value<ResultRecordData>();

        QCOMPARE(resultRecord.token, resultData.token);
        QCOMPARE(resultRecord.reason, resultData.reason);

        for (const auto& result : resultData.results) {
            QVERIFY(resultRecord.hasField(result.name));
            doTestResult(resultRecord[result.name], result.value);
        }
        break;
    }
    case KDevMI::MI::Record::Async: {
        const auto& asyncRecord = static_cast<KDevMI::MI::AsyncRecord&>(*record);
        const auto asyncData = recordData.value<AsyncRecordData>();

        QCOMPARE((int)asyncRecord.subkind, asyncData.subkind);
        QCOMPARE(asyncRecord.reason, asyncData.reason);

        for (const auto& result : asyncData.results) {
            QVERIFY(asyncRecord.hasField(result.name));
            doTestResult(asyncRecord[result.name], result.value);
        }
        break;
    }
    case KDevMI::MI::Record::Stream: {
        const auto& streamRecord = static_cast<KDevMI::MI::StreamRecord&>(*record);
        const auto streamData = recordData.value<StreamRecordData>();

        QCOMPARE((int)streamRecord.subkind, streamData.subkind);
        QCOMPARE(streamRecord.message, streamData.message);
        break;
    }
    case KDevMI::MI::Record::Prompt:
        break;
    }

}

QTEST_GUILESS_MAIN(TestMIParser)