1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtTest/private/qtesttable_p.h>
5#include <QtTest/qtestdata.h>
6#include <QtTest/qtestassert.h>
7
8#include <QtCore/private/qduplicatetracker_p.h>
9#include <QtCore/qmetaobject.h>
10
11#include <string.h>
12#include <vector>
13#include <algorithm>
14
15QT_BEGIN_NAMESPACE
16
17class QTestTablePrivate
18{
19public:
20 ~QTestTablePrivate()
21 {
22 qDeleteAll(begin: dataList.begin(), end: dataList.end());
23 }
24
25 struct Element {
26 Element() = default;
27 Element(const char *n, int t) : name(n), type(t) {}
28
29 const char *name = nullptr;
30 int type = 0;
31 };
32
33 using ElementList = std::vector<Element>;
34 ElementList elementList;
35
36 using DataList = std::vector<QTestData *>;
37 DataList dataList;
38
39 using TagSet = QDuplicateTracker<std::string>;
40 TagSet tagSet;
41
42 void addColumn(int elemType, const char *elemName) { elementList.push_back(x: Element(elemName, elemType)); }
43 void addRow(QTestData *data) { dataList.push_back(x: data); }
44
45 static QTestTable *currentTestTable;
46 static QTestTable *gTable;
47};
48
49QTestTable *QTestTablePrivate::currentTestTable = nullptr;
50QTestTable *QTestTablePrivate::gTable = nullptr;
51
52void QTestTable::addColumn(int type, const char *name)
53{
54 QTEST_ASSERT(type);
55 QTEST_ASSERT(name);
56 if (indexOf(elementName: name) != -1)
57 qWarning() << "Duplicate data column" << name << "- please rename.";
58
59 d->addColumn(elemType: type, elemName: name);
60}
61
62int QTestTable::elementCount() const
63{
64 return int(d->elementList.size());
65}
66
67int QTestTable::dataCount() const
68{
69 return int(d->dataList.size());
70}
71
72bool QTestTable::isEmpty() const
73{
74 return d->elementList.empty();
75}
76
77QTestData *QTestTable::newData(const char *tag)
78{
79 QTEST_ASSERT(tag);
80 if (d->tagSet.hasSeen(s: tag))
81 qWarning(msg: "Duplicate data tag \"%s\" - please rename.", tag);
82
83 QTestData *dt = new QTestData(tag, this);
84 d->addRow(data: dt);
85 return dt;
86}
87
88QTestTable::QTestTable()
89{
90 d = new QTestTablePrivate;
91 QTestTablePrivate::currentTestTable = this;
92}
93
94QTestTable::~QTestTable()
95{
96 QTestTablePrivate::currentTestTable = nullptr;
97 delete d;
98}
99
100int QTestTable::elementTypeId(int index) const
101{
102 return size_t(index) < d->elementList.size() ? d->elementList[index].type : -1;
103}
104
105const char *QTestTable::dataTag(int index) const
106{
107 return size_t(index) < d->elementList.size() ? d->elementList[index].name : nullptr;
108}
109
110QTestData *QTestTable::testData(int index) const
111{
112 return size_t(index) < d->dataList.size() ? d->dataList[index] : nullptr;
113}
114
115class NamePredicate
116{
117public:
118 explicit NamePredicate(const char *needle) : m_needle(needle) {}
119
120 bool operator()(const QTestTablePrivate::Element &e) const
121 { return !strcmp(s1: e.name, s2: m_needle); }
122
123 bool operator()(const QTestData *e) const
124 { return !strcmp(s1: e->dataTag(), s2: m_needle); }
125
126private:
127 const char *m_needle;
128};
129
130int QTestTable::indexOf(const char *elementName) const
131{
132 QTEST_ASSERT(elementName);
133
134 const QTestTablePrivate::ElementList &elementList = d->elementList;
135
136 const auto it = std::find_if(first: elementList.begin(), last: elementList.end(),
137 pred: NamePredicate(elementName));
138 return it != elementList.end() ?
139 int(it - elementList.begin()) : -1;
140}
141
142QTestTable *QTestTable::globalTestTable()
143{
144 if (!QTestTablePrivate::gTable)
145 QTestTablePrivate::gTable = new QTestTable();
146 return QTestTablePrivate::gTable;
147}
148
149void QTestTable::clearGlobalTestTable()
150{
151 delete QTestTablePrivate::gTable;
152 QTestTablePrivate::gTable = nullptr;
153}
154
155QTestTable *QTestTable::currentTestTable()
156{
157 return QTestTablePrivate::currentTestTable;
158}
159
160QT_END_NAMESPACE
161

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/testlib/qtesttable.cpp