@micro-os-plus/micro-test-plus 3.2.3 → 3.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -105,9 +105,15 @@ namespace micro_os_plus::micro_test_plus
105
105
  */
106
106
  struct colors
107
107
  {
108
- const char* none = "\033[0m"; /**< @brief Default colour. */
109
- const char* pass = "\033[32m"; /**< @brief Green colour. */
110
- const char* fail = "\033[31m"; /**< @brief Red colour. */
108
+ const char* none = ""; /**< @brief Default colour. */
109
+ const char* pass = ""; /**< @brief Green colour. */
110
+ const char* fail = ""; /**< @brief Red colour. */
111
+ };
112
+
113
+ const colors colors_red_green = {
114
+ "\033[0m", /**< @brief Default colour. */
115
+ "\033[32m", /**< @brief Green colour. */
116
+ "\033[31m" /**< @brief Red colour. */
111
117
  };
112
118
 
113
119
  /**
@@ -162,6 +168,8 @@ namespace micro_os_plus::micro_test_plus
162
168
 
163
169
  // Requires events::assertion_* for and detailed operators.
164
170
 
171
+ class test_runner;
172
+
165
173
  /**
166
174
  * @brief Reporter to display test results, including operand values and
167
175
  * types for failures.
@@ -197,7 +205,7 @@ namespace micro_os_plus::micro_test_plus
197
205
  /**
198
206
  * @brief Default constructor for the test_reporter class.
199
207
  */
200
- test_reporter () = default;
208
+ virtual ~test_reporter ();
201
209
 
202
210
  /**
203
211
  * @brief Selects the appropriate colour code based on a condition.
@@ -611,8 +619,9 @@ namespace micro_os_plus::micro_test_plus
611
619
  * @par Returns
612
620
  * Nothing.
613
621
  */
614
- void
615
- endline (void);
622
+ virtual void
623
+ endline (void)
624
+ = 0;
616
625
 
617
626
  // ------------------------------------------------------------------------
618
627
 
@@ -654,8 +663,9 @@ namespace micro_os_plus::micro_test_plus
654
663
  * @par Returns
655
664
  * Nothing.
656
665
  */
657
- void
658
- begin_test_case (const char* name);
666
+ virtual void
667
+ begin_test_case (const char* name)
668
+ = 0;
659
669
 
660
670
  /**
661
671
  * @brief Mark the end of a test case.
@@ -664,8 +674,9 @@ namespace micro_os_plus::micro_test_plus
664
674
  * @par Returns
665
675
  * Nothing.
666
676
  */
667
- void
668
- end_test_case (const char* name);
677
+ virtual void
678
+ end_test_case (const char* name)
679
+ = 0;
669
680
 
670
681
  /**
671
682
  * @brief Mark the beginning of a test suite.
@@ -674,8 +685,9 @@ namespace micro_os_plus::micro_test_plus
674
685
  * @par Returns
675
686
  * Nothing.
676
687
  */
677
- void
678
- begin_test_suite (const char* name);
688
+ virtual void
689
+ begin_test_suite (const char* name)
690
+ = 0;
679
691
 
680
692
  /**
681
693
  * @brief Mark the end of a test suite.
@@ -684,8 +696,31 @@ namespace micro_os_plus::micro_test_plus
684
696
  * @par Returns
685
697
  * Nothing.
686
698
  */
687
- void
688
- end_test_suite (test_suite_base& suite);
699
+ virtual void
700
+ end_test_suite (test_suite_base& suite)
701
+ = 0;
702
+
703
+ /**
704
+ * @brief Mark the beginning of a test.
705
+ *
706
+ * @param test_suites_count The number of test suites.
707
+ * @par Returns
708
+ * Nothing.
709
+ */
710
+ virtual void
711
+ begin_test (size_t test_suites_count)
712
+ = 0;
713
+
714
+ /**
715
+ * @brief Mark the end of a test.
716
+ *
717
+ * @param runner Reference to the test runner.
718
+ * @par Returns
719
+ * Nothing.
720
+ */
721
+ virtual void
722
+ end_test (test_runner& runner)
723
+ = 0;
689
724
 
690
725
  /**
691
726
  * @brief Flush the current buffered content.
@@ -695,8 +730,9 @@ namespace micro_os_plus::micro_test_plus
695
730
  * @par Returns
696
731
  * Nothing.
697
732
  */
698
- void
699
- flush (void);
733
+ virtual void
734
+ flush (void)
735
+ = 0;
700
736
 
701
737
  /**
702
738
  * @brief Output the current buffered content.
@@ -706,8 +742,9 @@ namespace micro_os_plus::micro_test_plus
706
742
  * @par Returns
707
743
  * Nothing.
708
744
  */
709
- void
710
- output (void);
745
+ virtual void
746
+ output (void)
747
+ = 0;
711
748
 
712
749
  /**
713
750
  * @brief Controls whether to add an empty line between successful test
@@ -731,8 +768,9 @@ namespace micro_os_plus::micro_test_plus
731
768
  * @par Returns
732
769
  * Nothing.
733
770
  */
734
- void
735
- output_pass_prefix_ (std::string& message);
771
+ virtual void
772
+ output_pass_prefix_ (std::string& message)
773
+ = 0;
736
774
 
737
775
  /**
738
776
  * @brief Outputs the suffix for a passing condition.
@@ -742,30 +780,37 @@ namespace micro_os_plus::micro_test_plus
742
780
  * @par Returns
743
781
  * Nothing.
744
782
  */
745
- void
746
- output_pass_suffix_ (void);
783
+ virtual void
784
+ output_pass_suffix_ (void)
785
+ = 0;
747
786
 
748
787
  /**
749
788
  * @brief Outputs the prefix for a failing condition.
750
789
  *
751
790
  * @param message The message to display.
791
+ * @param hasExpression Whether the failure is associated with an
792
+ * expression.
752
793
  * @param location The source location of the failure.
753
794
  * @par Returns
754
795
  * Nothing.
755
796
  */
756
- void
757
- output_fail_prefix_ (std::string& message,
758
- const reflection::source_location& location);
797
+ virtual void
798
+ output_fail_prefix_ (std::string& message, const bool hasExpression,
799
+ const reflection::source_location& location)
800
+ = 0;
759
801
 
760
802
  /**
761
803
  * @brief Outputs the suffix for a failing condition.
762
804
  *
805
+ * @param location The source location of the failure.
763
806
  * @param abort Whether to abort execution after failure.
764
807
  * @par Returns
765
808
  * Nothing.
766
809
  */
767
- void
768
- output_fail_suffix_ (bool abort);
810
+ virtual void
811
+ output_fail_suffix_ (const reflection::source_location& location,
812
+ bool abort)
813
+ = 0;
769
814
 
770
815
  /**
771
816
  * @brief ANSI colour codes for output formatting.
@@ -53,6 +53,8 @@
53
53
 
54
54
  #include <functional>
55
55
 
56
+ #include <time.h>
57
+
56
58
  // ----------------------------------------------------------------------------
57
59
 
58
60
  #if defined(__GNUC__)
@@ -166,6 +168,12 @@ namespace micro_os_plus::micro_test_plus
166
168
  void
167
169
  register_test_suite (test_suite_base* suite);
168
170
 
171
+ size_t
172
+ test_suites_count (void)
173
+ {
174
+ return (test_suites != nullptr) ? test_suites->size () + 1 : 1;
175
+ }
176
+
169
177
  /**
170
178
  * @brief Retrieves the name of the default test suite.
171
179
  *
@@ -191,36 +199,68 @@ namespace micro_os_plus::micro_test_plus
191
199
  [[noreturn]] void
192
200
  abort (void);
193
201
 
194
- protected:
202
+ // ------------------------------------------------------------------------
203
+ public:
195
204
  /**
196
- * @brief Stores the argument count passed to the test runner.
205
+ * @brief Pointer to the array of registered test suites.
206
+ *
207
+ * @details
208
+ * Statically initialised to zero as BSS, such that test suites defined as
209
+ * static objects in different compilation units can be automatically
210
+ * executed.
197
211
  */
198
- int argc_ = 0;
212
+ std::vector<test_suite_base*>* test_suites; // DO NOT INITIALISE!
199
213
 
214
+ struct
215
+ {
216
+ /**
217
+ * @brief Total number of successful checks in the test suite.
218
+ */
219
+ size_t successful_checks = 0;
220
+
221
+ /**
222
+ * @brief Total number of failed checks in the test suite.
223
+ */
224
+ size_t failed_checks = 0;
225
+
226
+ /**
227
+ * @brief Total number of test cases in the test suite.
228
+ */
229
+ size_t test_cases_count = 0;
230
+ } totals;
231
+
232
+ #if defined(_WIN32) || defined(CLOCK_MONOTONIC)
200
233
  /**
201
- * @brief Stores the argument vector passed to the test runner.
234
+ * @brief The timestamp recorded at the beginning of the test suite.
202
235
  */
203
- char** argv_ = nullptr;
236
+ timespec begin_time{};
204
237
 
205
238
  /**
206
- * @brief The name of the default test suite.
239
+ * @brief The timestamp recorded at the end of the test suite.
207
240
  */
208
- const char* default_suite_name_ = "Test";
241
+ timespec end_time{};
242
+ #endif
209
243
 
210
244
  /**
211
245
  * @brief Pointer to the default test suite which groups the main tests.
212
246
  */
213
- test_suite_base* default_test_suite_;
247
+ test_suite_base* default_test_suite = nullptr;
214
248
 
249
+ protected:
215
250
  /**
216
- * @brief Pointer to the array of registered test suites.
217
- *
218
- * @details
219
- * Statically initialised to zero as BSS, such that test suites defined as
220
- * static objects in different compilation units can be automatically
221
- * executed.
251
+ * @brief Stores the argument count passed to the test runner.
222
252
  */
223
- std::vector<test_suite_base*>* suites_;
253
+ int argc_ = 0;
254
+
255
+ /**
256
+ * @brief Stores the argument vector passed to the test runner.
257
+ */
258
+ char** argv_ = nullptr;
259
+
260
+ /**
261
+ * @brief The name of the default test suite.
262
+ */
263
+ const char* default_suite_name_ = "Test";
224
264
  };
225
265
 
226
266
  // --------------------------------------------------------------------------
@@ -50,6 +50,8 @@
50
50
 
51
51
  // ----------------------------------------------------------------------------
52
52
 
53
+ #include <time.h>
54
+
53
55
  #ifdef __cplusplus
54
56
 
55
57
  // ----------------------------------------------------------------------------
@@ -209,7 +211,7 @@ namespace micro_os_plus::micro_test_plus
209
211
  * None.
210
212
  * @return An integer with the number of checks that passed.
211
213
  */
212
- [[nodiscard]] constexpr int
214
+ [[nodiscard]] constexpr size_t
213
215
  successful_checks (void)
214
216
  {
215
217
  return successful_checks_;
@@ -222,7 +224,7 @@ namespace micro_os_plus::micro_test_plus
222
224
  * None.
223
225
  * @return An integer with the number of checks that failed.
224
226
  */
225
- [[nodiscard]] constexpr int
227
+ [[nodiscard]] constexpr size_t
226
228
  failed_checks (void)
227
229
  {
228
230
  return failed_checks_;
@@ -235,10 +237,10 @@ namespace micro_os_plus::micro_test_plus
235
237
  * None.
236
238
  * @return An integer with the number of test cases.
237
239
  */
238
- [[nodiscard]] constexpr int
239
- test_cases (void)
240
+ [[nodiscard]] constexpr size_t
241
+ test_cases_count (void)
240
242
  {
241
- return test_cases_;
243
+ return test_cases_count_;
242
244
  }
243
245
 
244
246
  /**
@@ -263,6 +265,21 @@ namespace micro_os_plus::micro_test_plus
263
265
  void
264
266
  end_test_suite (void);
265
267
 
268
+ #if defined(_WIN32) || defined(CLOCK_MONOTONIC)
269
+ /**
270
+ * @brief Computes the elapsed time between `begin_time` and `end_time`.
271
+ *
272
+ * @param [out] milliseconds The elapsed time in whole milliseconds.
273
+ * @param [out] microseconds The sub-millisecond remainder in microseconds
274
+ * (0–999).
275
+ * @par Returns
276
+ * Nothing.
277
+ */
278
+ void
279
+ compute_elapsed_time (timespec& begin_time, timespec& end_time,
280
+ long& milliseconds, long& microseconds);
281
+ #endif
282
+
266
283
  /**
267
284
  * @brief Gets the test suite result.
268
285
  *
@@ -288,7 +305,7 @@ namespace micro_os_plus::micro_test_plus
288
305
  unused (void)
289
306
  {
290
307
  return (failed_checks_ == 0 && successful_checks_ == 0
291
- && test_cases_ == 0);
308
+ && test_cases_count_ == 0);
292
309
  }
293
310
 
294
311
  protected:
@@ -305,19 +322,36 @@ namespace micro_os_plus::micro_test_plus
305
322
  /**
306
323
  * @brief Count of test conditions that passed.
307
324
  */
308
- int successful_checks_ = 0;
325
+ size_t successful_checks_ = 0;
309
326
 
310
327
  /**
311
328
  * @brief Count of test conditions that failed.
312
329
  */
313
- int failed_checks_ = 0;
330
+ size_t failed_checks_ = 0;
314
331
 
315
332
  /**
316
333
  * @brief Count of test cases in the test suite.
317
334
  */
318
- int test_cases_ = 0;
335
+ size_t test_cases_count_ = 0;
319
336
 
320
337
  public:
338
+ /**
339
+ * @brief The test suite index, counting from 1.
340
+ */
341
+ size_t index = 1;
342
+
343
+ #if defined(_WIN32) || defined(CLOCK_MONOTONIC)
344
+ /**
345
+ * @brief The timestamp recorded at the beginning of the test suite.
346
+ */
347
+ timespec begin_time{};
348
+
349
+ /**
350
+ * @brief The timestamp recorded at the end of the test suite.
351
+ */
352
+ timespec end_time{};
353
+ #endif
354
+
321
355
  /**
322
356
  * @brief Indicates whether to process deferred begin for test cases.
323
357
  */
@@ -335,12 +369,14 @@ namespace micro_os_plus::micro_test_plus
335
369
  /**
336
370
  * @brief Number of successful checks in the current test case.
337
371
  */
338
- int successful_checks;
372
+ size_t successful_checks = 0;
339
373
 
340
374
  /**
341
375
  * @brief Number of failed checks in the current test case.
342
376
  */
343
- int failed_checks;
377
+ size_t failed_checks = 0;
378
+
379
+ size_t index = 0;
344
380
  } current_test_case{};
345
381
  };
346
382
 
@@ -76,6 +76,8 @@
76
76
  #include "micro-test-plus/test-suite.h"
77
77
  #include "micro-test-plus/test-runner.h"
78
78
  #include "micro-test-plus/test-reporter.h"
79
+ #include "micro-test-plus/test-reporter-basic.h"
80
+ #include "micro-test-plus/test-reporter-tap.h"
79
81
 
80
82
  // ----------------------------------------------------------------------------
81
83
 
@@ -119,7 +121,7 @@ namespace micro_os_plus::micro_test_plus
119
121
  // --------------------------------------------------------------------------
120
122
 
121
123
  extern test_runner runner;
122
- extern test_reporter reporter;
124
+ extern test_reporter* reporter;
123
125
  extern test_suite_base* current_test_suite;
124
126
 
125
127
  // --------------------------------------------------------------------------
@@ -178,7 +180,7 @@ namespace micro_os_plus::micro_test_plus
178
180
  * @par SFINAE
179
181
  * Enabled only if `Expr_T` is derived from `detail::op` or
180
182
  * is convertible to `bool`.
181
- *
183
+ *
182
184
 
183
185
  * @param [in] expr Logical expression to evaluate.
184
186
  * @param [in] sl Optional source location, defaulting to the current line.
package/meson.build CHANGED
@@ -46,6 +46,8 @@ _local_sources += [
46
46
  'src/micro-test-plus.cpp',
47
47
  'src/test-runner.cpp',
48
48
  'src/test-reporter.cpp',
49
+ 'src/test-reporter-basic.cpp',
50
+ 'src/test-reporter-tap.cpp',
49
51
  'src/test-suite.cpp',
50
52
  ]
51
53
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@micro-os-plus/micro-test-plus",
3
- "version": "3.2.3",
3
+ "version": "3.3.1",
4
4
  "description": "A source code library with µTest++, a lightweight testing framework for embedded platforms",
5
5
  "repository": {
6
6
  "type": "git",
@@ -246,7 +246,7 @@ namespace micro_os_plus::micro_test_plus
246
246
  if (abort_ && !value_)
247
247
  {
248
248
  printf ("\n");
249
- reporter.output ();
249
+ reporter->output ();
250
250
  abort ();
251
251
  }
252
252
  }
@@ -278,18 +278,19 @@ namespace micro_os_plus::micro_test_plus
278
278
  test_runner runner;
279
279
 
280
280
  /**
281
- * @brief Global instance of `test_reporter`.
281
+ * @brief Global pointer to `test_reporter`.
282
282
  *
283
283
  * @details
284
- * This global instance of `test_reporter` is responsible for collecting,
284
+ * This global pointer to `test_reporter` is responsible for collecting,
285
285
  * formatting, and outputting the results of test execution within the
286
286
  * µTest++ framework. It manages the reporting of test outcomes, including
287
287
  * successes and failures, and ensures that all relevant information is
288
288
  * presented clearly to the user. By maintaining a single shared reporter,
289
289
  * the framework provides consistent and centralised reporting across all
290
- * test cases and folders.
290
+ * test cases and folders. The reporter is initialized in
291
+ * test_runner::initialize().
291
292
  */
292
- test_reporter reporter;
293
+ test_reporter* reporter = nullptr;
293
294
 
294
295
  /**
295
296
  * @brief Global pointer references the currently active test suite.