LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
TestCasesHandler.cpp
Go to the documentation of this file.
1// Copyright 2002-2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 * @author Benoit Mathieu
8 ***************************************************************************/
9
13
14#include <iostream>
15
16namespace Lima
17{
18namespace Common
19{
20namespace TGV
21{
22
23char const * const FATAL_ERROR_TYPE = "bloquant";
24int const EXIT_FATAL_ERROR = 64;
25
27 : m_reportByType(), m_processor(processor), m_hasFatalError(false) {
28}
29
32
33// -----------------------------------------------------------------------
34// Implementations of the SAX DocumentHandler interface
35// -----------------------------------------------------------------------
36
38{
39 currentTestCase=TestCase();
40 m_inExpl=false;
41 m_inText=false;
42 m_inParam=false;
43 m_inList=false;
44 return true;
45}
46
48 const QString & namespaceURI, const QString & localname, const QString & qname, const QXmlAttributes & attrs)
49{
50 LIMA_UNUSED(namespaceURI);
52 std::string name=getName(localname,qname);
53
54 if (name=="testcase")
55 {
56 LDEBUG << "TestCasesHandler::startElement: new testcase ";
57 currentTestCase=TestCase();
58 currentTestCase.id=attributeValue("id", attrs);
59 std::string typech=attributeValue("type", attrs);
60 currentTestCase.type=(typech.empty()?"undefined":typech);
61 LDEBUG << "TestCasesHandler::startElement: id=" << currentTestCase.id
62 << ", type=" << currentTestCase.type;
63 } else if (name=="text") {
64 // For compatibility with old testTva format
65 LDEBUG << "TestCasesHandler::startElement: start text ";
66 m_inText=true;
67 } else if (name=="expl") {
68 LDEBUG << "TestCasesHandler::startElement: start expl ";
69 m_inExpl=true;
70 } else if (name=="call-parameters") {
71 m_inParam=true;
72 LDEBUG << "TestCasesHandler::startElement: start call-parameters ";
73 } else if (name=="list") {
74 LDEBUG << "TestCasesHandler::startElement: start list ";
75 m_listKey = attributeValue("key", attrs);
76 MultiValCallParams::iterator pos = currentTestCase.multiValCallParams.find(m_listKey);
77 if( pos == currentTestCase.multiValCallParams.end() ) {
78 // typedef std::map<std::string,std::list<std::string> > MultiValCallParams;
79 std::pair<std::string,std::list<std::string> > newElem(m_listKey,std::list<std::string>() );
80// std::pair<MultiValCallParams::iterator,bool> ret =
81// currentTestCase.multiValCallParams.insert(
82// std::pair<std::string,std::list<std::string> >(m_listKey,std::list<std::string>) );
83 /*std::pair<MultiValCallParams::iterator,bool> ret =*/ currentTestCase.multiValCallParams.insert(newElem);
84 }
85 m_inList=true;
86 } else if (name=="map") {
87 LDEBUG << "TestCasesHandler::startElement: start map ";
88 m_mapKey = attributeValue("key",attrs);
89 MapValCallParams::iterator pos = currentTestCase.mapValCallParams.find(m_mapKey);
90 if( pos == currentTestCase.mapValCallParams.end() ) {
91 // typedef std::map<std::string,std::list<std::string> > MultiValCallParams;
92 std::pair<std::string,std::map<std::string,std::string> > newElem(m_mapKey,std::map<std::string,std::string>() );
93// std::pair<MultiValCallParams::iterator,bool> ret =
94// currentTestCase.multiValCallParams.insert(
95// std::pair<std::string,std::list<std::string> >(m_listKey,std::list<std::string>) );
96 /*std::pair<MapValCallParams::iterator,bool> ret =*/ currentTestCase.mapValCallParams.insert(newElem);
97 }
98 m_inMap=true;
99 } else if (name=="item") {
100 LDEBUG << "TestCasesHandler::startElement: start item ";
101 if( m_inList == true ) {
102 MultiValCallParams::iterator pos = currentTestCase.multiValCallParams.find(m_listKey);
103 if( pos != currentTestCase.multiValCallParams.end() ) {
104 std::string val = attributeValue("value", attrs);
105 (*pos).second.push_back(val);
106 }
107 else {
108 LERROR << "!! TestCasesHandler::startElement: no list for param " << m_listKey;
109 }
110 }
111 else if (m_inMap == true) {
112 MapValCallParams::iterator pos = currentTestCase.mapValCallParams.find(m_mapKey);
113 if( pos != currentTestCase.mapValCallParams.end() ) {
114 std::string attr = attributeValue("key", attrs);
115 std::string val = attributeValue("value",attrs);
116 (*pos).second.insert(make_pair(attr,val));
117 }
118 else {
119 LERROR << "!! TestCasesHandler::startElement: no list for param " << m_listKey;
120 }
121 }
122// currentTestCase.multiValCallParams.insert(std::pair<std::string, std::string>(key,val) );
123 } else if (name=="param") {
124 LDEBUG << "TestCasesHandler::startElement: start param ";
125 std::string key = attributeValue("key",attrs);
126 std::string val = attributeValue("value", attrs);
127 currentTestCase.simpleValCallParams.insert(std::pair<std::string, std::string>(key,val) );
128 // TODO: read multiValCallParams "pipelines"
129 // std::string pipch=attributeValue("pipelines");
130 } else if (name=="test") {
131 // build a testUnit
132 //cout << "add test" << std::endl;
134 tu.id=attributeValue("id", attrs);
135 tu.trace=attributeValue("trace", attrs);
136 tu.comment=attributeValue("comment", attrs);
137 tu.left=attributeValue("left", attrs);
138 tu.op=attributeValue("operator", attrs);
139 tu.right=attributeValue("right",attrs);
140 tu.conditional=(attributeValue("conditional",attrs)=="yes");
141 currentTestCase.tests.push_back(tu);
142 LDEBUG << "TestCasesHandler::startElement: push testUnit"
143 << ", id =" << tu.id
144 << ", trace =" << tu.trace
145 << ", comment =" << tu.comment
146 << ", left =" << tu.left
147 << ", op =" << tu.op
148 << ", right =" << tu.right
149 << ", conditional =" << tu.conditional;
150
151 }
152 return true;
153}
154
155bool TestCasesHandler::characters(const QString& chars)
156{
157 if (m_inExpl) {
158 currentTestCase.explanation.append(chars.toUtf8().data());
159 } else if (m_inText) {
160 // For compatibility with old testTva format
161 std::string newTextVal(chars.toUtf8().data());
162
163 SimpleValCallParams::iterator pos = currentTestCase.simpleValCallParams.find("text");
164 if( pos == currentTestCase.simpleValCallParams.end() ) {
165// std::pair<SimpleValCallParams::iterator,bool> ret =
166 currentTestCase.simpleValCallParams.insert(std::pair<std::string, std::string>("text",newTextVal) );
167 }
168 else {
169 std::string& textVal = (*pos).second;
170 textVal.append(chars.toUtf8().data());
171 }
172 }
173 return true;
174
175}
176
177bool TestCasesHandler::endElement (const QString & namespaceURI, const QString & localname, const QString & qname)
178{
179 LIMA_UNUSED(namespaceURI);
181 std::string name=getName(localname,qname);
182 if (name=="testcase")
183 {
184 m_reportByType[currentTestCase.type].nbtests++;
185 LDEBUG << "TestCasesHandler::endElement: call processTestCase(" << currentTestCase.id
186 << "," << currentTestCase.type << ")";
187 TestCaseError e = m_processor.processTestCase(currentTestCase);
188 if (e())
189 {
190 std::cout << currentTestCase.id << " (" << currentTestCase.type << ") got error (type: '"<<e()<<"'): "
191 << std::endl << e.what() << std::endl;
192 if (currentTestCase.type == FATAL_ERROR_TYPE) {
193 m_hasFatalError = true;
194 }
196 {
197 if (e.isConditional())
198 {
199 m_reportByType[currentTestCase.type].conditional++;
200 } else
201 {
202 m_reportByType[currentTestCase.type].failed++;
203 }
204 }
205 else
206 {
207 std::cout << "runtime error: " << e.what() << std::endl;
208 m_reportByType[currentTestCase.type].failed++;
209 throw std::runtime_error(e.what());
210 }
211 }
212 else
213 {
214 std::cout << currentTestCase.id << " (" << currentTestCase.type << ") passed successfully." << std::endl;;
215 m_reportByType[currentTestCase.type].success++;
216 }
217 } else if (name=="text") {
218 // For compatibility with old testTva format
219 m_inText=false;
220 } else if (name=="expl") {
221 m_inExpl=false;
222 } else if (name=="call-parameters") {
223 m_inParam=false;
224 } else if (name=="list") {
225 m_inList=false;
226 } else if (name=="map") {
227 m_inMap=false;
228 }
229 return true;
230}
231
232std::string TestCasesHandler::getName(const QString& localName,
233 const QString& qName) {
234
235 return Common::Misc::limastring2utf8stdstring( (localName.isEmpty())?qName:localName );
236}
237
238std::string TestCasesHandler::attributeValue(const QString& attr, const QXmlAttributes& attrs) const
239{
240 return Common::Misc::limastring2utf8stdstring( attrs.value(attr) );
241}
242
244{
245 return tch.hasFatalError() ? EXIT_FATAL_ERROR : EXIT_SUCCESS;
246}
247
248} // TGV
249} // Common
250} // Lima
#define TGVLOGINIT
Definition LimaCommon.h:216
#define LIMA_UNUSED(x)
Definition LimaCommon.h:224
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
const std::string & what() const
virtual TestCaseError processTestCase(const TestCase &testCase)=0
bool characters(const QString &chars) override
std::map< std::string, TestReport > m_reportByType
bool endElement(const QString &namespaceURI, const QString &name, const QString &qName) override
bool startElement(const QString &namespaceURI, const QString &name, const QString &qName, const QXmlAttributes &attributes) override
TestCasesHandler(TestCaseProcessor &processor)
std::string limastring2utf8stdstring(const Lima::LimaString &phrase, uint32_t size0)
Convert a wide string to a string , in dest up to size bytes.
char const *const FATAL_ERROR_TYPE
int exitCode(TestCasesHandler const &tch)
NAUTITIA.
MultiValCallParams multiValCallParams
Definition TestCase.h:62
SimpleValCallParams simpleValCallParams
Definition TestCase.h:61
MapValCallParams mapValCallParams
Definition TestCase.h:63
std::list< TestUnit > tests
Definition TestCase.h:68