1// Copyright (C) 2022 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#ifndef QTESTCOREELEMENT_P_H
5#define QTESTCOREELEMENT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtTest/qttestglobal.h>
19#include <QtTest/private/qtestelementattribute_p.h>
20
21#include <vector>
22
23QT_BEGIN_NAMESPACE
24
25
26template <class ElementType>
27class QTestCoreElement
28{
29 public:
30 QTestCoreElement(QTest::LogElementType type = QTest::LET_Undefined);
31 virtual ~QTestCoreElement();
32
33 void addAttribute(const QTest::AttributeIndex index, const char *value);
34 const std::vector<QTestElementAttribute*> &attributes() const;
35 const char *attributeValue(QTest::AttributeIndex index) const;
36 const char *attributeName(QTest::AttributeIndex index) const;
37 const QTestElementAttribute *attribute(QTest::AttributeIndex index) const;
38
39 const char *elementName() const;
40 QTest::LogElementType elementType() const;
41
42 private:
43 std::vector<QTestElementAttribute*> listOfAttributes;
44 QTest::LogElementType type;
45};
46
47template<class ElementType>
48QTestCoreElement<ElementType>::QTestCoreElement(QTest::LogElementType t)
49 : type(t)
50{
51}
52
53template<class ElementType>
54QTestCoreElement<ElementType>::~QTestCoreElement()
55{
56 for (auto *attribute : listOfAttributes)
57 delete attribute;
58}
59
60template <class ElementType>
61void QTestCoreElement<ElementType>::addAttribute(const QTest::AttributeIndex attributeIndex, const char *value)
62{
63 if (attributeIndex == -1 || attribute(index: attributeIndex))
64 return;
65
66 QTestElementAttribute *testAttribute = new QTestElementAttribute;
67 testAttribute->setPair(attributeIndex, value);
68 listOfAttributes.push_back(x: testAttribute);
69}
70
71template <class ElementType>
72const std::vector<QTestElementAttribute*> &QTestCoreElement<ElementType>::attributes() const
73{
74 return listOfAttributes;
75}
76
77template <class ElementType>
78const char *QTestCoreElement<ElementType>::attributeValue(QTest::AttributeIndex index) const
79{
80 const QTestElementAttribute *attrb = attribute(index);
81 if (attrb)
82 return attrb->value();
83
84 return nullptr;
85}
86
87template <class ElementType>
88const char *QTestCoreElement<ElementType>::attributeName(QTest::AttributeIndex index) const
89{
90 const QTestElementAttribute *attrb = attribute(index);
91 if (attrb)
92 return attrb->name();
93
94 return nullptr;
95}
96
97template <class ElementType>
98const char *QTestCoreElement<ElementType>::elementName() const
99{
100 const char *xmlElementNames[] =
101 {
102 "property",
103 "properties",
104 "failure",
105 "error",
106 "testcase",
107 "testsuite",
108 "message",
109 "system-err",
110 "system-out",
111 "skipped"
112 };
113
114 if (type != QTest::LET_Undefined)
115 return xmlElementNames[type];
116
117 return nullptr;
118}
119
120template <class ElementType>
121QTest::LogElementType QTestCoreElement<ElementType>::elementType() const
122{
123 return type;
124}
125
126template <class ElementType>
127const QTestElementAttribute *QTestCoreElement<ElementType>::attribute(QTest::AttributeIndex index) const
128{
129 for (auto *attribute : listOfAttributes) {
130 if (attribute->index() == index)
131 return attribute;
132 }
133
134 return nullptr;
135}
136
137QT_END_NAMESPACE
138
139#endif
140

source code of qtbase/src/testlib/qtestcoreelement_p.h