• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thetic / mutiny / 24541716104

17 Apr 2026 12:44AM UTC coverage: 98.686% (+0.04%) from 98.642%
24541716104

Pull #58

github

web-flow
Merge 092199ca5 into 405a5edbc
Pull Request #58: Junity

63 of 63 new or added lines in 2 files covered. (100.0%)

4 existing lines in 1 file now uncovered.

5109 of 5177 relevant lines covered (98.69%)

4000.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.71
/src/test/JUnitOutput.cpp
1
#include "mutiny/test/JUnitOutput.hpp"
2

3
#include "mutiny/test/Failure.hpp"
4
#include "mutiny/test/Output.hpp"
5
#include "mutiny/test/Result.hpp"
6
#include "mutiny/test/Shell.hpp"
7

8
#include "mutiny/time.hpp"
9

10
#include <stdint.h>
11

12
namespace mu {
13
namespace tiny {
14
namespace test {
15

16
namespace {
17

18
class TestProperty
19
{
20
public:
21
  String name;
22
  String value;
23
  TestProperty* next{ nullptr };
24
};
25

26
} // namespace
27

28
class JUnitTestCaseResultNode
29
{
30
public:
31
  JUnitTestCaseResultNode() = default;
60 ✔
32

33
  String name;
34
  uint_least64_t exec_time{ 0 };
35
  Failure* failure{ nullptr };
36
  bool failure_is_error{ false };
37
  bool ignored{ false };
38
  String skip_message;
39
  String file;
40
  size_t line_number{ 0 };
41
  size_t check_count{ 0 };
42
  TestProperty* properties{ nullptr };
43
  TestProperty* properties_tail{ nullptr };
44
  JUnitTestCaseResultNode* next{ nullptr };
45
};
46

47
class JUnitTestGroupResult
48
{
49
public:
50
  JUnitTestGroupResult() = default;
46 ✔
51

52
  size_t test_count{ 0 };
53
  size_t failure_count{ 0 };
54
  size_t error_count{ 0 };
55
  size_t skip_count{ 0 };
56
  size_t total_check_count{ 0 };
57
  uint_least64_t start_time{ 0 };
58
  uint_least64_t group_exec_time{ 0 };
59
  String group;
60
  JUnitTestCaseResultNode* head{ nullptr };
61
  JUnitTestCaseResultNode* tail{ nullptr };
62
};
63

64
class JUnitTestOutputImpl
65
{
66
public:
67
  JUnitTestGroupResult results;
68
  String current_group_xml;
69
  String accumulated_xml;
70
  String package;
71
  size_t total_test_count{ 0 };
72
  size_t total_failure_count{ 0 };
73
  size_t total_error_count{ 0 };
74
  size_t total_skip_count{ 0 };
75
  uint_least64_t total_exec_time{ 0 };
76
  String start_timestamp;
77
};
78

79
JUnitOutput::JUnitOutput()
46 ✔
80
  : impl_(new JUnitTestOutputImpl)
46 ✔
81
{
82
}
46 ✔
83

84
JUnitOutput::~JUnitOutput()
92 ✔
85
{
86
  reset_test_group_result();
46 ✔
87
  delete impl_;
46 ✔
88
}
92 ✔
89

90
void JUnitOutput::reset_test_group_result()
93 ✔
91
{
92
  impl_->results.test_count = 0;
93 ✔
93
  impl_->results.failure_count = 0;
93 ✔
94
  impl_->results.error_count = 0;
93 ✔
95
  impl_->results.skip_count = 0;
93 ✔
96
  impl_->results.group = "";
93 ✔
97
  JUnitTestCaseResultNode* cur = impl_->results.head;
93 ✔
98
  while (cur) {
153 ✔
99
    JUnitTestCaseResultNode* tmp = cur->next;
60 ✔
100
    delete cur->failure;
60 ✔
101
    TestProperty* prop = cur->properties;
60 ✔
102
    while (prop) {
65 ✔
103
      TestProperty* prop_tmp = prop->next;
5 ✔
104
      delete prop;
5 ✔
105
      prop = prop_tmp;
5 ✔
106
    }
107
    delete cur;
60 ✔
108
    cur = tmp;
60 ✔
109
  }
110
  impl_->results.head = nullptr;
93 ✔
111
  impl_->results.tail = nullptr;
93 ✔
112
}
93 ✔
113

114
void JUnitOutput::print_tests_started()
41 ✔
115
{
116
  impl_->accumulated_xml.clear();
41 ✔
117
  impl_->total_test_count = 0;
41 ✔
118
  impl_->total_failure_count = 0;
41 ✔
119
  impl_->total_error_count = 0;
41 ✔
120
  impl_->total_skip_count = 0;
41 ✔
121
  impl_->total_exec_time = 0;
41 ✔
122
  impl_->start_timestamp = get_time_string();
41 ✔
123
}
41 ✔
124

125
void JUnitOutput::print_current_group_started(const Shell& /*test*/) {}
47 ✔
126

127
void JUnitOutput::print_current_test_ended(const Result& result)
60 ✔
128
{
129
  impl_->results.tail->exec_time =
120 ✔
130
      result.get_current_test_total_execution_time();
60 ✔
131
  impl_->results.tail->check_count = result.get_check_count();
60 ✔
132
}
60 ✔
133

134
void JUnitOutput::print_tests_ended(const Result& /*result*/)
41 ✔
135
{
136
  Output::File file = fopen_(create_file_name().c_str(), "w");
41 ✔
137
  String header = string_from_format(
138
      "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
139
      "<testsuites tests=\"%d\" failures=\"%d\" errors=\"%d\" "
140
      "skipped=\"%d\" time=\"%d.%03d\" timestamp=\"%s\">\n",
141
      static_cast<int>(impl_->total_test_count),
41 ✔
142
      static_cast<int>(impl_->total_failure_count),
41 ✔
143
      static_cast<int>(impl_->total_error_count),
41 ✔
144
      static_cast<int>(impl_->total_skip_count),
41 ✔
145
      static_cast<int>(impl_->total_exec_time / 1000),
41 ✔
146
      static_cast<int>(impl_->total_exec_time % 1000),
41 ✔
147
      impl_->start_timestamp.c_str()
41 ✔
148
  );
41 ✔
149
  fputs_(header.c_str(), file);
41 ✔
150
  fputs_(impl_->accumulated_xml.c_str(), file);
41 ✔
151
  fputs_("</testsuites>\n", file);
41 ✔
152
  fclose_(file);
41 ✔
153
}
41 ✔
154

155
void JUnitOutput::print_current_group_ended(const Result& result)
47 ✔
156
{
157
  impl_->results.group_exec_time =
94 ✔
158
      result.get_current_group_total_execution_time();
47 ✔
159
  impl_->total_test_count += impl_->results.test_count;
47 ✔
160
  impl_->total_failure_count += impl_->results.failure_count;
47 ✔
161
  impl_->total_error_count += impl_->results.error_count;
47 ✔
162
  impl_->total_skip_count += impl_->results.skip_count;
47 ✔
163
  impl_->total_exec_time += impl_->results.group_exec_time;
47 ✔
164
  write_test_group_to_file();
47 ✔
165
  reset_test_group_result();
47 ✔
166
}
47 ✔
167

168
void JUnitOutput::print_current_test_started(const Shell& test)
60 ✔
169
{
170
  impl_->results.test_count++;
60 ✔
171
  impl_->results.group = test.get_group();
60 ✔
172
  impl_->results.start_time = get_time_in_millis();
60 ✔
173

174
  if (impl_->results.tail == nullptr) {
60 ✔
175
    impl_->results.head = impl_->results.tail = new JUnitTestCaseResultNode;
47 ✔
176
  } else {
177
    impl_->results.tail->next = new JUnitTestCaseResultNode;
13 ✔
178
    impl_->results.tail = impl_->results.tail->next;
13 ✔
179
  }
180
  impl_->results.tail->name = test.get_name();
60 ✔
181
  impl_->results.tail->file = test.get_file();
60 ✔
182
  impl_->results.tail->line_number = test.get_line_number();
60 ✔
183
  if (!test.will_run()) {
60 ✔
184
    impl_->results.tail->ignored = true;
1 ✔
185
    impl_->results.skip_count++;
1 ✔
186
  }
187
}
60 ✔
188

189
String JUnitOutput::create_file_name()
46 ✔
190
{
191
  if (!impl_->package.empty())
46 ✔
192
    return encode_file_name(impl_->package) + ".xml";
22 ✔
193
  return "mutiny.xml";
35 ✔
194
}
195

196
String JUnitOutput::encode_file_name(const String& file_name)
11 ✔
197
{
198
  // special character list based on: https://en.wikipedia.org/wiki/Filename
199
  static const char* const forbidden_characters = "/\\?%*:|\"<>";
200

201
  String result = file_name;
11 ✔
202
  for (const char* sym = forbidden_characters; *sym; ++sym) {
121 ✔
203
    string_replace(result, *sym, '_');
110 ✔
204
  }
205
  return result;
11 ✔
UNCOV
206
}
×
207

208
void JUnitOutput::set_package_name(const String& package)
11 ✔
209
{
210
  if (impl_ != nullptr) {
11 ✔
211
    impl_->package = package;
11 ✔
212
  }
213
}
11 ✔
214

215
void JUnitOutput::write_test_suite_summary()
47 ✔
216
{
217
  size_t total_assertions = 0;
47 ✔
218
  for (JUnitTestCaseResultNode* n = impl_->results.head; n; n = n->next)
107 ✔
219
    total_assertions = n->check_count;
60 ✔
220

221
  String buf = string_from_format(
222
      "<testsuite errors=\"%d\" failures=\"%d\" skipped=\"%d\" "
223
      "assertions=\"%d\" name=\"%s\" tests=\"%d\" "
224
      "time=\"%d.%03d\" timestamp=\"%s\">\n",
225
      static_cast<int>(impl_->results.error_count),
47 ✔
226
      static_cast<int>(impl_->results.failure_count),
47 ✔
227
      static_cast<int>(impl_->results.skip_count),
47 ✔
228
      static_cast<int>(total_assertions),
229
      impl_->results.group.c_str(),
47 ✔
230
      static_cast<int>(impl_->results.test_count),
47 ✔
231
      static_cast<int>(impl_->results.group_exec_time / 1000),
47 ✔
232
      static_cast<int>(impl_->results.group_exec_time % 1000),
47 ✔
233
      get_time_string()
234
  );
47 ✔
235
  write_to_file(buf.c_str());
47 ✔
236
}
47 ✔
237

238
String JUnitOutput::encode_xml_text(const String& textbody)
45 ✔
239
{
240
  String buf = textbody.c_str();
45 ✔
241
  string_replace(buf, "&", "&amp;");
45 ✔
242
  string_replace(buf, "\"", "&quot;");
45 ✔
243
  string_replace(buf, "<", "&lt;");
45 ✔
244
  string_replace(buf, ">", "&gt;");
45 ✔
245
  string_replace(buf, "\r", "&#13;");
45 ✔
246
  string_replace(buf, "\n", "&#10;");
45 ✔
247
  return buf;
45 ✔
UNCOV
248
}
×
249

250
void JUnitOutput::write_test_cases()
47 ✔
251
{
252
  JUnitTestCaseResultNode* cur = impl_->results.head;
47 ✔
253

254
  while (cur) {
107 ✔
255
    String buf = string_from_format(
256
        "<testcase classname=\"%s%s%s\" name=\"%s\" assertions=\"%d\" "
257
        "time=\"%d.%03d\" file=\"%s\" line=\"%d\">\n",
258
        impl_->package.c_str(),
60 ✔
259
        impl_->package.empty() ? "" : ".",
120 ✔
260
        impl_->results.group.c_str(),
60 ✔
261
        cur->name.c_str(),
262
        static_cast<int>(cur->check_count - impl_->results.total_check_count),
60 ✔
263
        static_cast<int>(cur->exec_time / 1000),
60 ✔
264
        static_cast<int>(cur->exec_time % 1000),
60 ✔
265
        cur->file.c_str(),
266
        static_cast<int>(cur->line_number)
60 ✔
267
    );
60 ✔
268
    write_to_file(buf.c_str());
60 ✔
269

270
    impl_->results.total_check_count = cur->check_count;
60 ✔
271

272
    if (cur->properties) {
60 ✔
273
      write_to_file("<properties>\n");
4 ✔
274
      for (TestProperty* prop = cur->properties; prop; prop = prop->next) {
9 ✔
275
        String prop_buf = string_from_format(
276
            "<property name=\"%s\" value=\"%s\"/>\n",
277
            encode_xml_text(prop->name).c_str(),
5 ✔
278
            encode_xml_text(prop->value).c_str()
10 ✔
279
        );
5 ✔
280
        write_to_file(prop_buf.c_str());
5 ✔
281
      }
5 ✔
282
      write_to_file("</properties>\n");
4 ✔
283
    }
284

285
    if (cur->failure) {
60 ✔
286
      if (cur->failure_is_error)
18 ✔
287
        write_error(cur);
4 ✔
288
      else
289
        write_failure(cur);
14 ✔
290
    } else if (cur->ignored) {
42 ✔
291
      if (cur->skip_message.empty()) {
4 ✔
292
        write_to_file("<skipped />\n");
1 ✔
293
      } else {
294
        write_to_file(string_from_format(
3 ✔
295
                          "<skipped message=\"%s\" />\n",
296
                          encode_xml_text(cur->skip_message).c_str()
6 ✔
297
        )
298
                          .c_str());
299
      }
300
    }
301

302
    write_to_file("</testcase>\n");
60 ✔
303
    cur = cur->next;
60 ✔
304
  }
60 ✔
305
}
47 ✔
306

307
void JUnitOutput::write_failure(JUnitTestCaseResultNode* node)
14 ✔
308
{
309
  String file = encode_xml_text(node->failure->get_file_name());
14 ✔
310
  String msg = encode_xml_text(node->failure->get_message());
14 ✔
311
  String buf = string_from_format(
312
      "<failure message=\"%s:%d: %s\" type=\"AssertionFailedError\">\n"
313
      "%s:%d: %s\n",
314
      file.c_str(),
315
      static_cast<int>(node->failure->get_failure_line_number()),
28 ✔
316
      msg.c_str(),
317
      file.c_str(),
318
      static_cast<int>(node->failure->get_failure_line_number()),
28 ✔
319
      msg.c_str()
320
  );
14 ✔
321
  write_to_file(buf.c_str());
14 ✔
322
  write_to_file("</failure>\n");
14 ✔
323
}
14 ✔
324

325
void JUnitOutput::write_error(JUnitTestCaseResultNode* node)
4 ✔
326
{
327
  String msg = encode_xml_text(node->failure->get_message());
4 ✔
328
  String buf = string_from_format(
329
      "<error message=\"%s\" type=\"UnexpectedException\">\n"
330
      "%s\n",
331
      msg.c_str(),
332
      msg.c_str()
333
  );
4 ✔
334
  write_to_file(buf.c_str());
4 ✔
335
  write_to_file("</error>\n");
4 ✔
336
}
4 ✔
337

338
void JUnitOutput::write_file_ending()
47 ✔
339
{
340
  write_to_file("</testsuite>\n");
47 ✔
341
}
47 ✔
342

343
void JUnitOutput::write_test_group_to_file()
47 ✔
344
{
345
  open_file_for_write();
47 ✔
346
  write_test_suite_summary();
47 ✔
347
  write_test_cases();
47 ✔
348
  write_file_ending();
47 ✔
349
  close_file();
47 ✔
350
}
47 ✔
351

UNCOV
352
void JUnitOutput::print_buffer(const char*) {}
×
353

354
void JUnitOutput::print_test_property(const char* name, const char* value)
5 ✔
355
{
356
  if (impl_->results.tail == nullptr)
5 ✔
357
    return;
×
358
  auto* prop = new TestProperty;
5 ✔
359
  prop->name = name;
5 ✔
360
  prop->value = value;
5 ✔
361
  if (impl_->results.tail->properties == nullptr) {
5 ✔
362
    impl_->results.tail->properties = prop;
4 ✔
363
    impl_->results.tail->properties_tail = prop;
4 ✔
364
  } else {
365
    impl_->results.tail->properties_tail->next = prop;
1 ✔
366
    impl_->results.tail->properties_tail = prop;
1 ✔
367
  }
368
}
369

370
void JUnitOutput::print_skipped(const char* message)
3 ✔
371
{
372
  if (impl_->results.tail == nullptr)
3 ✔
UNCOV
373
    return;
×
374
  impl_->results.tail->ignored = true;
3 ✔
375
  impl_->results.tail->skip_message = message;
3 ✔
376
  impl_->results.skip_count++;
3 ✔
377
}
378

379
void JUnitOutput::print_failure(const Failure& failure)
18 ✔
380
{
381
  if (impl_->results.tail->failure == nullptr) {
18 ✔
382
    if (failure.is_error()) {
18 ✔
383
      impl_->results.error_count++;
4 ✔
384
      impl_->results.tail->failure_is_error = true;
4 ✔
385
    } else {
386
      impl_->results.failure_count++;
14 ✔
387
    }
388
    impl_->results.tail->failure = new Failure(failure);
18 ✔
389
  }
390
}
18 ✔
391

392
void JUnitOutput::open_file_for_write()
47 ✔
393
{
394
  impl_->current_group_xml.clear();
47 ✔
395
}
47 ✔
396

397
void JUnitOutput::write_to_file(const String& buffer)
267 ✔
398
{
399
  impl_->current_group_xml += buffer;
267 ✔
400
}
267 ✔
401

402
void JUnitOutput::close_file()
47 ✔
403
{
404
  impl_->accumulated_xml += impl_->current_group_xml;
47 ✔
405
}
47 ✔
406

407
} // namespace test
408
} // namespace tiny
409
} // namespace mu
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc