-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathiutest_default_xml_generator.ipp
More file actions
433 lines (396 loc) · 13.7 KB
/
Copy pathiutest_default_xml_generator.ipp
File metadata and controls
433 lines (396 loc) · 13.7 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//======================================================================
//-----------------------------------------------------------------------
/**
* @file iutest_default_xml_generator.ipp
* @brief output xml event listener implementation
*
* @author t.shirayanagi
* @par copyright
* Copyright (C) 2011-2021, Takazumi Shirayanagi\n
* This software is released under the new BSD License,
* see LICENSE
*/
//-----------------------------------------------------------------------
//======================================================================
#ifndef INCG_IRIS_IUTEST_DEFAULT_XML_GENERATOR_IPP_791DCB98_05CC_49BA_8518_0EC9CA2B5450_
#define INCG_IRIS_IUTEST_DEFAULT_XML_GENERATOR_IPP_791DCB98_05CC_49BA_8518_0EC9CA2B5450_
//======================================================================
// include
#include "../listener/iutest_default_xml_generator.hpp"
namespace iutest
{
IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_BEGIN()
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnTestIterationStart(const UnitTest& test, int iteration)
{
if(!IsReportable(test))
{
return;
}
if( !m_output_path_format.empty() )
{
m_output_path = m_output_path_format;
::std::string strIte = detail::iu_to_string(iteration);
detail::StringReplace(m_output_path, "%d", 2, strIte.c_str());
detail::StringReplace(m_output_path, "{I}", 3, strIte.c_str());
if( m_output_path == m_output_path_format)
{
m_output_path_format.clear();
}
if( m_fp != NULL )
{
OnReportTest(m_fp, test);
FileClose();
}
}
if( m_fp == NULL )
{
FileOpen(m_output_path.c_str());
}
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnTestProgramEnd(const UnitTest& test)
{
if(!IsReportable(test))
{
return;
}
if( m_fp == NULL )
{
FileOpen(m_output_path.c_str());
if( m_fp == NULL )
{
return;
}
}
OnReportTest(m_fp, test);
FileClose();
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnListup(int total_test_num, const ::std::vector<TestSuite*>& tests)
{
FileOpen(m_output_path.c_str());
m_fp->Printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
m_fp->Printf("<testsuites tests=\"%d\" name=\"AllTests\">\n", total_test_num);
for( ::std::vector<TestSuite*>::const_iterator it = tests.begin(), end=tests.end(); it != end; ++it )
{
m_fp->Printf(" <testsuite name=\"%s\" tests=\"%d\">\n"
, (*it)->testsuite_name_with_default_package_name().c_str()
, (*it)->total_test_count());
for( int i = 0; i < (*it)->total_test_count(); ++i )
{
const TestInfo* testinfo = (*it)->GetTestInfo(i);
std::string value_param, type_param;
if ( testinfo->value_param() != NULL )
{
value_param = " value_param=\"" + EscapeXmlAttribute(testinfo->value_param()) + "\"";
}
if ( testinfo->type_param() != NULL )
{
type_param = " type_param=\"" + EscapeXmlAttribute(testinfo->type_param()) + "\"";
}
m_fp->Printf(" <testcase name=\"%s\"%s%s file=\"%s\" line=\"%d\" />\n"
, testinfo->name()
, value_param.c_str()
, type_param.c_str()
, testinfo->file()
, testinfo->line());
}
m_fp->Printf(" </testsuite>\n");
}
m_fp->Printf("</testsuites>\n");
FileClose();
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnReportTest(IFile* file, const UnitTest& test)
{
file->Printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
file->Printf("<testsuites tests=\"%d\" failures=\"%d\" disabled=\"%d\" "
, test.reportable_test_count()
, test.failed_test_count()
, test.reportable_disabled_test_count()
);
#if IUTEST_HAS_REPORT_SKIPPED
file->Printf("skipped=\"%d\" ", test.reportable_skip_test_count());
#endif
file->Printf("errors=\"0\" time=\"%s\" timestamp=\"%s\" "
, detail::FormatTimeInMillisecAsSecond(test.elapsed_time()).c_str()
, detail::FormatTimeInMillisecAsIso8601(test.start_timestamp()).c_str()
);
if( TestFlag::IsEnableFlag(TestFlag::SHUFFLE_TESTS) )
{
file->Printf("random_seed=\"%u\" ", test.random_seed());
}
file->Printf("name=\"AllTests\"");
// propertys
OnReportTestProperty(file, *test.ad_hoc_test_result(), UnitTest::ValidateTestPropertyName);
file->Printf(">\n");
for( int i=0, count=test.total_test_suite_count(); i < count; ++i )
{
OnReportTestSuite(file, *test.GetTestSuite(i));
}
file->Printf("</testsuites>\n");
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnReportTestSuite(IFile* file, const TestSuite& test_suite)
{
if( test_suite.reportable_test_count() <= 0 )
{
return;
}
file->Printf(" <testsuite ");
OutputXmlAttribute(file, "name"
, EscapeXmlAttribute(test_suite.testsuite_name_with_default_package_name()).c_str());
file->Printf("tests=\"%d\" failures=\"%d\" disabled=\"%d\" "
, test_suite.reportable_test_count()
, test_suite.failed_test_count()
, test_suite.reportable_disabled_test_count()
);
#if IUTEST_HAS_REPORT_SKIPPED
file->Printf("skipped=\"%d\" ", test_suite.reportable_skip_test_count() );
#endif
file->Printf("errors=\"0\" time=\"%s\" timestamp=\"%s\""
, detail::FormatTimeInMillisecAsSecond(test_suite.elapsed_time()).c_str()
, detail::FormatTimeInMillisecAsIso8601(test_suite.start_timestamp()).c_str()
);
// propertys
OnReportTestProperty(file, *test_suite.ad_hoc_test_result(), TestSuite::ValidateTestPropertyName);
file->Printf(">\n");
for( int i=0, count=test_suite.total_test_count(); i < count; ++i )
{
OnReportTestInfo(file, *test_suite.GetTestInfo(i));
}
file->Printf(" </testsuite>\n");
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnReportTestInfo(IFile* file, const TestInfo& test_info)
{
if( !test_info.is_reportable() )
{
return;
}
file->Printf(" <testcase ");
OutputXmlAttribute(file, "name", EscapeXmlAttribute(test_info.name()).c_str() );
{
const char* type_param = test_info.type_param();
if( type_param != NULL )
{
OutputXmlAttribute(file, "type_param"
, EscapeXmlAttribute(type_param).c_str() );
}
}
{
const char* value_param = test_info.value_param();
if( value_param != NULL )
{
OutputXmlAttribute(file, "value_param"
, EscapeXmlAttribute(value_param).c_str() );
}
}
if( test_info.is_ran() )
{
file->Printf("status=\"run\" ");
}
else
{
file->Printf("status=\"notrun\" ");
}
file->Printf("time=\"%s\" "
, detail::FormatTimeInMillisecAsSecond(test_info.elapsed_time()).c_str()
);
OutputXmlAttribute(file, "classname"
, EscapeXmlAttribute(test_info.testsuite_name_with_default_package_name()).c_str());
// propertys
OnReportTestProperty(file, *test_info.result(), TestInfo::ValidateTestPropertyName);
const bool notrun = test_info.should_run() && !test_info.is_ran();
if( test_info.HasFailure() || notrun )
{
file->Printf(">\n");
for( int i=0, count=test_info.result()->total_part_count(); i < count; ++i )
{
const TestPartResult& part = test_info.result()->GetTestPartResult(i);
if( part.passed() )
{
continue;
}
file->Printf(" <failure ");
OutputXmlAttribute(file, "message"
, EscapeXmlAttribute(part.summary()).c_str());
file->Printf("type=\"\">");
::std::string message = detail::FormatCompilerIndependentFileLocation(part.file_name(), part.line_number());
message += "\n";
message += detail::MultiByteStringToUTF8(part.summary());
OutputXmlCDataSection(file, message.c_str());
file->Printf("\n </failure>\n");
}
if( notrun )
{
file->Printf(" <failure message=\"Not Run\" type=\"\">");
OutputXmlCDataSection(file, "Not Run");
file->Printf("\n </failure>\n");
}
file->Printf(" </testcase>\n");
}
else
{
#if IUTEST_HAS_REPORT_SKIPPED
const bool skipped = test_info.is_skipped() || !test_info.should_run();
if( skipped )
{
file->Printf(">\n");
OnReportTestSkipped(file, test_info);
file->Printf(" </testcase>\n");
}
else
#endif
{
file->Printf(" />\n");
}
}
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnReportTestSkipped(IFile* file, const TestInfo& test_info)
{
file->Printf(" <skipped type=\"iutest.skip\" ");
const TestResult* tr = test_info.result();
const int count = tr->total_part_count();
for( int i=0; i < count; ++i )
{
const TestPartResult& part = tr->GetTestPartResult(i);
if( part.skipped() )
{
::std::string message = detail::FormatCompilerIndependentFileLocation(part.file_name(), part.line_number());
#if 0
OutputXmlAttribute(file, "message"
, EscapeXmlAttribute(part.summary()).c_str());
file->Printf(">\n");
message += "\n";
message += detail::MultiByteStringToUTF8(part.summary());
OutputXmlCDataSection(file, message.c_str());
file->Printf("\n </skipped>\n");
#else
message += ": ";
message += detail::MultiByteStringToUTF8(part.summary());
OutputXmlAttribute(file, "message"
, EscapeXmlAttribute(message.c_str()).c_str());
file->Printf(" />\n");
#endif
return;
}
}
if( test_info.is_disabled_test() )
{
OutputXmlAttribute(file, "message", "disabled test.");
}
file->Printf("/>\n");
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OnReportTestProperty(IFile* file, const TestResult& test_result
, bool (*pfnValidate)(const ::std::string&))
{
for( int i=0, count=test_result.test_property_count(); i < count; ++i )
{
const TestProperty& prop = test_result.GetTestProperty(i);
if( (*pfnValidate)(prop.key()) )
{
file->Printf(" %s=\"%s\""
, EscapeXmlAttribute(prop.key()).c_str()
, EscapeXmlAttribute(prop.value()).c_str()
);
}
}
}
IUTEST_IPP_INLINE bool DefaultXmlGeneratorListener::FileOpen(const char* path)
{
m_fp = detail::IFileSystem::New();
if( m_fp == NULL )
{
return false;
}
if( !m_fp->Open(path, IFile::OpenWrite) )
{
fprintf(stderr, "Unable to open file \"%s\".\n", m_output_path.c_str());
fflush(stderr);
detail::IFileSystem::Free(m_fp);
m_fp = NULL;
return false;
}
return true;
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::FileClose()
{
if( m_fp == NULL )
{
return;
}
m_fp->Close();
detail::IFileSystem::Free(m_fp);
m_fp = NULL;
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OutputXmlCDataSection(IFile* file, const char* data)
{
file->Printf("<![CDATA[");
file->Write(data, strlen(data), 1);
file->Printf("]]>");
}
IUTEST_IPP_INLINE void DefaultXmlGeneratorListener::OutputXmlAttribute(IFile* file, const char* name, const char* value)
{
file->Printf("%s=\"", name);
file->Write(value, strlen(value), 1 );
file->Printf("\" ");
}
IUTEST_PRAGMA_CONSTEXPR_CALLED_AT_RUNTIME_WARN_DISABLE_BEGIN()
IUTEST_IPP_INLINE ::std::string DefaultXmlGeneratorListener::EscapeXml(const char* str, bool is_attribute)
{
::std::string msg;
if( str != NULL )
{
for( const char* src = str; *src; ++src )
{
const char s = *src;
switch(s)
{
case '<':
msg += "<";
break;
case '>':
msg += ">";
break;
case '&':
msg += "&";
break;
case '\'':
msg += is_attribute ? "'" : "\'";
break;
case '\"':
msg += is_attribute ? """ : "\"";
break;
default:
{
#if !defined(IUTEST_OS_WINDOWS_MOBILE)
wchar_t wc = 0;
const int len = detail::iu_mbtowc(&wc, src, static_cast<size_t>(MB_CUR_MAX));
if( len > 1 )
{
msg += detail::AnyStringToUTF8(&wc, 1);
src += len-1;
}
else if( IsValidXmlCharacter(s) )
#else
if( IsValidXmlCharacter(s) )
#endif
{
if( is_attribute && IsWhitespace(s) )
{
char tmp[8];
detail::iu_snprintf(tmp, sizeof(tmp), "&#x%02X;", static_cast<unsigned int>(s));
msg += tmp;
}
else
{
msg += s;
}
}
}
break;
}
}
}
return msg;
}
IUTEST_PRAGMA_CONSTEXPR_CALLED_AT_RUNTIME_WARN_DISABLE_END()
IUTEST_PRAGMA_CRT_SECURE_WARN_DISABLE_END()
} // end of namespace iutest
#endif // INCG_IRIS_IUTEST_DEFAULT_XML_GENERATOR_IPP_791DCB98_05CC_49BA_8518_0EC9CA2B5450_