-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapcatch2.hpp
1171 lines (1037 loc) · 32.7 KB
/
snapcatch2.hpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019-2024 Made to Order Software Corp. All Rights Reserved
//
// https://snapwebsites.org/project/snapcatch2
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once
// catch2
//
#define CATCH_CONFIG_PREFIX_ALL
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wctor-dtor-privacy"
#pragma GCC diagnostic ignored "-Wsign-promo"
#include <catch2/catch_session.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#pragma GCC diagnostic pop
// C++
//
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <sstream>
// C lib
//
#include <string.h>
#include <unistd.h>
/** \brief Allow for the literals defined by Catch.
*
* This statement makes all the literals offered by Catch to be usable in
* your tests.
*
* For floating point compare against a literal, one can do:
*
* CATCH_REQUIRE(var == 1.0_a); // equivalent to Approx(1.0)
*
* Below we also offer a floating point REQUIRE like so:
*
* CATCH_REQUIRE_FLOATING_POINT(var, 1.0);
*
* That require uses our implementation of the compare which is limited
* to an epsilon variance.
*/
using namespace Catch::literals;
/** \brief Namespace declaration.
*
* You are expected to reuse this name each time you create a common
* function, class, etc. For example the following creates a counter
* which is common to all your tests:
*
* \code
* namespace SNAP_CATCH2_NAMESPACE
* {
*
* int g_counter = 0;
*
* }
* \endcode
*/
#define SNAP_CATCH2_NAMESPACE unittest
/** \brief The snapcatch2 definitions.
*
* This namespace encompasses the extensions we offer to catch2. We reuse
* these in pretty much all our tests so to make it easy we made it a
* common header file.
*/
namespace SNAP_CATCH2_NAMESPACE
{
/** \brief Print out progress as a section gets executed.
*
* This flag is used along the CATCH_START_SECTION() and CATCH_END_SECTION()
* macros. The flag gets set whenever the `--progress` command line option
* is used and the macros print out the name of the section being run
* (assuming you use our macros).
*
* This is very useful to have whenever you have very many and rather
* slow tests to see which one fails because otherwise you may get
* some invalid line number/names especially when you have an unexpected
* exception or some similar error.
*
* You are welcome to use the parameter in your own code. Just make sure
* to use the parenthesis:
*
* \code
* if(g_progress())
* {
* std::cout << "we're moving!" << std::endl;
* }
* \endcode
*
* \return A read-write reference to the `progress` parameter.
*/
inline bool & g_progress()
{
static bool progress = false;
return progress;
}
/** \brief Retrieve the path to the source directory.
*
* Some tests need access to the source directory in order to read files
* such as the debian/changelog to get the current version or some
* configuration files.
*
* This function returns a reference to that path which can be set by
* the user on the command line using the `--source-dir` option.
*
* \return A reference to the source directory.
*/
inline std::string & g_source_dir()
{
static std::string source_dir = std::string();
return source_dir;
}
/** \brief Retrieve the path to the binary (build) directory.
*
* Some tests need access to the binary directory in order to access
* generated files including tools that were created by cmake.
* Those files will be found under this directory.
*
* This function returns a reference to that path which can be set by
* the user on the command line using the `--binary-dir` option.
*
* \return A reference to the binary directory.
*/
inline std::string & g_binary_dir()
{
static std::string binary_dir = std::string();
return binary_dir;
}
/** \brief Retrieve the path to the dist(ribution) directory.
*
* Some tests need access to the dist directory where dependencies
* installed their files. For example, the eventdispatcher message
* verification process loads files from
* `/usr/share/eventdispatcher/messages`. This folder is likely
* to not exist and if it does, it is even less likely to be up to
* date on a programmer's machine.
*
* This function returns a reference to that path which is used
* to install such files while building all the contribs and
* main snapwebsites files.
*
* The unit test is expected to use the `--dist-dir` option in
* order to define this directory.
*
* \return A reference to the binary directory.
*/
inline std::string & g_dist_dir()
{
static std::string dist_dir = std::string();
return dist_dir;
}
/** \brief Retrieve the temporary directory.
*
* Many tests make use of input and output files. These are expected
* to be saved in a temporary. This function returns the path to that
* directory.
*
* The path will have been cleaned up on initialization. It is available
* in all of our tests, although many use a temporary folder, some do not.
*
* \return A reference to the user specified directory.
*/
inline std::string & g_tmp_dir()
{
static std::string tmp_dir = std::string();
return tmp_dir;
}
/** \brief Generate a random number.
*
* This function uses the rand() function to generate a random number
* for your test. This is very simple and it can easily be reproduced
* since we offer the --seed command line option to re-seed your test
* with the same value.
*
* The template expects to be used to fill the number of bits specified
* in the type. By default, rand() returns less than 32 bits of truely
* random data. This function makes sure that all the bits are set to
* some random value.
*
* Note that it is extremely unlikely that you get 0 or the maximum
* value ("-1" for an unsigned number or "-1 >> 1" for a signed number).
* If you want your test to verify those specific numbers, make sure
* to include special cases in your code.
*
* \note
* This function takes the result as a parameter in order to determine
* the template parameter T.
*
* \exception std::logic_error
* If somehow you have a type which size is not handled, then the function
* emits a logic_error. This may happen if you use intrinsic types which
* can be 256 or 512 bits and these are not handled by this function.
*
* \tparam T The type of integral number to generate.
* \param[out] result The resulting random number.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
template<typename T>
void random(T & result)
{
int const size((sizeof(T) + 1) / 2);
switch(size)
{
case 1: // 8 and 16 bits
result = static_cast<T>(rand());
break;
case 2: // 32 bits
result = static_cast<T>(
(static_cast<std::uint32_t>(rand()) << 16)
^ (static_cast<std::uint32_t>(rand()) << 0));
break;
case 4: // 64 bits
result = static_cast<T>(
(static_cast<std::uint64_t>(rand()) << 48)
^ (static_cast<std::uint64_t>(rand()) << 32)
^ (static_cast<std::uint64_t>(rand()) << 16)
^ (static_cast<std::uint64_t>(rand()) << 0));
break;
case 8: // 128 bits
result = static_cast<T>(
(static_cast<unsigned __int128>(rand()) << 112)
^ (static_cast<unsigned __int128>(rand()) << 96)
^ (static_cast<unsigned __int128>(rand()) << 80)
^ (static_cast<unsigned __int128>(rand()) << 64)
^ (static_cast<unsigned __int128>(rand()) << 48)
^ (static_cast<unsigned __int128>(rand()) << 32)
^ (static_cast<unsigned __int128>(rand()) << 16)
^ (static_cast<unsigned __int128>(rand()) << 0));
break;
default:
CATCH_REQUIRE("unsupported type for random()" == nullptr);
throw std::logic_error("unsupported type for random()");
}
}
#pragma GCC diagnostic pop
enum class character_t
{
CHARACTER_ZUNICODE, // any from 0 to 0x10FFFF
CHARACTER_ZPLAN0, // any from 0 to 0x00FFFF
CHARACTER_UNICODE, // any from 1 to 0x10FFFF
CHARACTER_PLAN0, // any from 1 to 0x00FFFF
CHARACTER_LETTERS, // a-z, A-Z
CHARACTER_ALPHANUMERIC, // a-z, A-Z, 0-9
CHARACTER_DIGITS, // 0-9
CHARACTER_ASCII, // 0x20 to 0x7E
CHARACTER_LABEL, // a-z, A-Z, 0-9, _
};
inline char32_t random_char(character_t category)
{
constexpr std::size_t const SURROGATES_COUNT(0xE000 - 0xD800);
char32_t result(U'\0');
switch(category)
{
case character_t::CHARACTER_ZUNICODE:
random(result);
result %= 0x110000 - SURROGATES_COUNT;
break;
case character_t::CHARACTER_ZPLAN0:
random(result);
result %= 0x010000 - SURROGATES_COUNT;
break;
case character_t::CHARACTER_UNICODE:
random(result);
result = result % (0x110000 - SURROGATES_COUNT - 1) + 1;
break;
case character_t::CHARACTER_PLAN0:
random(result);
result = result % (0x010000 - SURROGATES_COUNT - 1) + 1;
break;
case character_t::CHARACTER_LETTERS:
random(result);
result = result % (26 * 2) + 'A';
if(result > 'Z')
{
// lowercase
//
result += 'a' - 'Z' - 1;
}
break;
case character_t::CHARACTER_ALPHANUMERIC:
random(result);
result = result % (26 * 2 + 10) + '0';
if(result > '9')
{
// uppercase
//
result += 'A' - '9' - 1;
if(result > 'Z')
{
// lowercase
//
result += 'a' - 'Z' - 1;
}
}
break;
case character_t::CHARACTER_DIGITS:
random(result);
result = result % 10 + '0';
break;
case character_t::CHARACTER_ASCII:
random(result);
result = result % ('~' - ' ' + 1) + ' ';
break;
case character_t::CHARACTER_LABEL:
random(result);
result = result % (26 * 2 + 10 + 1) + '0';
if(result > '9')
{
// uppercase
//
result += 'A' - '9' - 1;
if(result > 'Z')
{
// lowercase
//
result += 'a' - 'Z' - 1;
if(result > 'z')
{
result = '_';
}
}
}
break;
}
if(result >= 0xD800)
{
// skip the surrogates (not valid char32_t values)
//
result += SURROGATES_COUNT;
}
return result;
}
/** \brief Convert a char32_t to a UTF-8 chain of bytes.
*
* This function converts a char32_t in a string of UTF-8 bytes. If the input
* is an invalid character (larger than 0x10FFFF) then nothing happens.
*
* This function is expected to only be used in tests. In your code,
* make sure to use the libutf8::to_u8string().
*
* \param[in,out] out The output string where bytes get added.
* \param[in] wc The input wide character.
*/
inline void wctombs(std::string & out, char32_t wc)
{
if(wc < 0x80)
{
out += static_cast<char>(wc);
}
else if(wc < 0x800)
{
out += static_cast<char>((wc >> 6) | 0xC0);
out += static_cast<char>((wc & 0x3F) | 0x80);
}
else if(wc < 0x10000)
{
out += static_cast<char>((wc >> 12) | 0xE0);
out += static_cast<char>(((wc >> 6) & 0x3F) | 0x80);
out += static_cast<char>((wc & 0x3F) | 0x80);
}
else if(wc < 0x110000)
{
out += static_cast<char>((wc >> 18) | 0xF0);
out += static_cast<char>(((wc >> 12) & 0x3F) | 0x80);
out += static_cast<char>(((wc >> 6) & 0x3F) | 0x80);
out += static_cast<char>((wc & 0x3F) | 0x80);
}
}
inline std::string random_string(
std::size_t length_min
, std::size_t length_max // inclusive
, character_t category = character_t::CHARACTER_ASCII)
{
std::size_t length(0);
random(length);
length = length % (length_max + 1 - length_min) + length_min;
std::string result;
for(std::size_t i(0); i < length; ++i)
{
char32_t c(random_char(category));
// labels cannot start with a digit
//
while(result.empty()
&& category == character_t::CHARACTER_LABEL
&& c >= '0'
&& c <= '9')
{
c = random_char(category);
}
wctombs(result, c);
}
return result;
}
inline std::vector<std::uint8_t> random_buffer(
std::size_t size_min // inclusive
, std::size_t size_max) // inclusive
{
std::size_t size(0);
random(size);
size = size % (size_max + 1 - size_min) + size_min;
std::vector<std::uint8_t> result(size);
for(std::size_t i(0); i < size; ++i)
{
random(result[i]);
}
return result;
}
namespace detail
{
inline void init_tmp_dir(std::string const & project_name)
{
std::string & path(g_tmp_dir());
// make sure it's not just "/tmp" since we're about to delete it
// with `rm -rf ...`
//
if(path == "/tmp")
{
std::cerr
<< "fatal error: temporary directory cannot be \"/tmp\"."
" It needs to include a sub-directory (\"/tmp/"
<< project_name
<< "\", for example)."
<< std::endl;
exit(1);
}
// default to /tmp/<project-name> if not defined
//
if(path.empty())
{
// TODO: make sure "project_name" is "clean" (spaces -> '_', slashes?)
//
path = "/tmp/" + project_name;
}
// delete the directory if it exists
//
// this ensure an equivalent state each time we run the tests
{
std::stringstream ss;
ss << "rm -rf \"" << path << "\"";
if(system(ss.str().c_str()) != 0)
{
std::cerr
<< "fatal error: could not delete temporary directory \""
<< path
<< "\".";
exit(1);
}
}
// create the directory
{
std::stringstream ss;
ss << "mkdir -p \"" << path << "\"";
if(system(ss.str().c_str()) != 0)
{
std::cerr
<< "fatal error: could not create temporary directory \""
<< path
<< "\".";
exit(1);
}
}
}
} // detail namespace
/** \brief Print out information to let programmers know what tests are doing.
*
* This flag is used whenever useful information could be outputted. The
* flag let the test know whether to print that information or not.
*
* You are welcome to use the parameter in your own code. Just make sure
* to use the parenthesis:
*
* \code
* if(g_verbose())
* {
* std::cout << "More info about this test!" << std::endl;
* }
* \endcode
*
* \return A read-write reference to the `verbose` parameter.
*/
inline bool & g_verbose()
{
static bool verbose = false;
return verbose;
}
#ifdef CATCH_CONFIG_RUNNER
/** \brief The main function to initialize and run the unit tests.
*
* This inline function initializes and runs the snapcatch2 tests.
*
* The \p init_callback parameter is used to initialize your test before
* we parse the command line. Use the \p callback function to do further
* initialization after the command line is parsed and basic functions
* run (i.e. the `--version` command line option will run and the
* function exit before \p callback gets called. So when the user uses
* that command line option, you do not get \p callback called, however,
* \p init_callback will always be called).
*
* We do it this way because the function will try/catch std::exception
* so if an exception occurs in your \p init_callback, it will always
* be captured and a message printed out before the application
* exits cleanly with an error code (i.e. `exit(1)`).
*
* The \p init_callback can be set to a nullptr if you do not need to
* initialize anything futher early on.
*
* One thing we use the \p init_callback for is to prevent our
* libexcept exceptions from generating a stack trace because that's
* very slow and in a test which generate thousands if not million
* of exception cases, it makes it really very slow.
*
* \code
* snap_catch2_main(
* "libutf8"
* , "1.2.3"
* , argc
* , argv
* , []() { libexcept::set_collect_stack(libexcept::collect_stack_t::COLLECT_STACK_NO); }
* );
* \endcode
*
* The \p add_user_options parameter is a pointer to a function which
* adds command line options supported by your test. This pointer can
* be set to nullptr, For example, if you wanted to support a path to
* a temporary directory, you would write:
*
* \code
* namespace SNAP_CATCH2_NAMESPACE
* {
*
* std::string g_conf_dir = std::string("/etc/config");
*
* Catch::Clara::Parser add_command_line_options(Catch::Clara::Parser const & cli)
* {
* return cli
* | Catch::Clara::Opt(g_conf_dir, "conf-dir")
* ["--conf-dir"]
* ("a path to a configuration directory used by the test.");
* }
*
* }
*
* ...snip...
*
* snap_catch2_main(
* "libutf8"
* , "1.2.3"
* , argc
* , argv
* , nullptr
* , &add_command_line_options
* );
* \endcode
*
* \warning
* It is possible to add a one letter command line option such as ["-t"],
* however, catch2 already uses most of these options. The `-t` is used
* to list tags, for example.
*
* The \p callback function gets called just before the tests run.
* In other words, we give you one more chance to verify command line
* parameters or do additional initilalizations, now that you have access
* to the user defined command line parameters. We pass the session to
* the function, although in many cases you will not use it because your
* parameters are already set as expected.
*
* \code
* namespace SNAP_CATCH2_NAMESPACE
* {
*
* int finish_init(Catch::Session & session)
* {
* std::string cmd("rm -rf ");
* cmd += g_conf_dir;
* cmd += "/my-project";
* if(system(cmd.c_str()) != 0)
* {
* perror(cmd.c_str());
* return 1;
* }
*
* return 0;
* }
*
* }
*
* ...snip...
*
* snap_catch2_main(
* "libutf8"
* , "1.2.3"
* , argc
* , argv
* , nullptr
* , nullptr
* , &finish_init
* );
* \endcode
*
* Once the tests ran, we call the \p finished_callback as well. This gives
* you the ability to test futher things such as making sure that everything
* was cleaned up, resources released, etc. For example, in our advgetopt
* we use a FIFO to handle error messages. If an error never occurs that
* FIFO will not be empty on exit. This is actually an error in the test.
* Only errors that do occur have to be added to the FIFO. So on exit we
* want to verify that the FIFO is indeed empty.
*
* \param[in] project_name The name of the project (i.e. "libutf8").
* \param[in] project_version The version string of the project
* (i.e. "LIBUTF8_VERSION_STRING").
* \param[in] argc The number of arguments on the command line.
* \param[in] argv An array of strings with the command line arguments.
* \param[in] init_callback Callback called before anything else.
* \param[in] add_user_options A callback giving you the opportunity to add
* command line options before argv gets parsed.
* \param[in] callback A callback called just before we run the tests.
* \param[in] finished_callback The callback called just after all the tests
* ran.
*
* \return The exit code, usually 0 on success and 1 on an error.
*/
inline int snap_catch2_main(
char const * project_name
, char const * project_version
, int argc
, char * argv[]
, void (*init_callback)() = nullptr
, void (*add_user_options)(Catch::Clara::Parser & cli) = nullptr
, int (*callback)(Catch::Session & session) = nullptr
, void (*finished_callback)() = nullptr)
{
typedef unsigned int seed_t; // see `man srand(3)` for type
try
{
if(init_callback != nullptr)
{
init_callback();
}
Catch::Session session;
bool version(false);
seed_t seed(static_cast<seed_t>(time(NULL)));
auto cli = session.cli();
cli |= Catch::Clara::Opt(seed, "seed")
["-S"]["--seed"]
("value to seed the randomizer, if not specified, use time()")
| Catch::Clara::Opt(g_progress())
["-p"]["--progress"]
("print name of test section being run")
| Catch::Clara::Opt(g_source_dir(), "source_dir")
["--source-dir"]
("specify the full path to the source directory")
| Catch::Clara::Opt(g_binary_dir(), "binary_dir")
["--binary-dir"]
("specify the full path to the binary directory")
| Catch::Clara::Opt(g_dist_dir(), "dist_dir")
["--dist-dir"]
("specify the full path to the dist directory")
| Catch::Clara::Opt(g_tmp_dir(), "tmp_dir")
["-T"]["--tmp-dir"]
("specify a temporary directory")
| Catch::Clara::Opt(g_verbose())
["--verbose"]
("print additional information from within our own tests")
| Catch::Clara::Opt(version)
["-V"]["--version"]
("print out the libutf8 library version these unit tests pertain to");
if(add_user_options != nullptr)
{
add_user_options(cli);
}
session.cli(cli);
if(session.applyCommandLine(argc, argv) != 0)
{
std::cerr << "fatal error: invalid command line." << std::endl;
return 1;
}
if(version)
{
std::cout << project_version << std::endl;
return 0;
}
// also turn on verbosity if the VERBOSE environment variable
// is set to a value other than 0, false, off
//
char const * verbose(getenv("VERBOSE"));
if(verbose != nullptr)
{
if(strcmp(verbose, "0") != 0
&& strcmp(verbose, "false") != 0
&& strcmp(verbose, "off") != 0)
{
g_verbose() = true;
}
}
if(g_verbose())
{
std::cout << "info: verbosity activated." << std::endl;
}
detail::init_tmp_dir(project_name);
// by default we get a different seed each time; that really helps
// in detecting errors! At least it helped me many times.
//
srand(seed);
// save the seed, it can be practical opposed to searching your
// test output
//
{
std::ofstream seed_file;
seed_file.open("seed.txt");
if(seed_file.is_open())
{
seed_file << seed << std::endl;
}
}
if(callback != nullptr)
{
int const e(callback(session));
if(e != 0)
{
return e;
}
}
std::cout << project_name
<< " v"
<< project_version
<< " ["
<< getpid()
<< "]:unittest: seed is "
<< seed
<< "\n"
"source directory: \""
<< g_source_dir()
<< "\"\n"
"binary directory: \""
<< g_binary_dir()
<< "\"\n"
"dist directory: \""
<< g_dist_dir()
<< "\"\n"
"temporary directory: \""
<< g_tmp_dir()
<< "\""
<< std::endl;
auto const r(session.run());
if(finished_callback != nullptr)
{
finished_callback();
}
return r;
}
catch(std::logic_error const & e)
{
std::cerr << "fatal error: caught a logic error in "
<< project_name
<< " unit tests: "
<< e.what()
<< "\n";
return 1;
}
catch(std::runtime_error const & e)
{
std::cerr << "fatal error: caught a run-time error in "
<< project_name
<< " unit tests: "
<< e.what()
<< "\n";
return 1;
}
catch(std::exception const & e)
{
std::cerr << "fatal error: caught a standard exception in "
<< project_name
<< " unit tests: "
<< e.what()
<< "\n";
return 1;
}
}
#endif
inline void catch_compare_long_strings(std::string const & a, std::string const & b)
{
auto print_char = [](char c)
{
if(c == 0x20)
{
// Possible characters to make spaces visible are:
// U+23B5 BOTTOM SQUARE BRACKET
// U+2420 SYMBOL FOR SPACE
// U+2423 OPEN BOX
//
std::cout << "\xE2\x90\xA3";
}
else if(static_cast<unsigned char>(c) < 0x20)
{
// control character
//
std::cout << "^" << static_cast<char>(c + 0x40);
}
else if(static_cast<unsigned char>(c) < 0x80)
{
// standard character
//
std::cout << c;
}
else if(static_cast<unsigned char>(c) < 0xA0)
{
// graphical control
//
std::cout << "@" << static_cast<char>(c - 0x40);
}
else
{
// UTF-8 characters cannot be written as is since we are breaking
// up the string into bytes instead of characters; so instead show
// the corresponding \xXX value
//
std::cout << "\\x" << std::hex << static_cast<int>(static_cast<std::uint8_t>(c));
}
};
if(a != b)
{
std::cout << "error: long strings do not match.\n"
<< "---------------------------------------------------\n";
// TODO: we may want to look into supporting UTF-8 properly
//
size_t const max(std::min(a.length(), b.length()));
bool err(false);
for(size_t idx(0); idx < max; ++idx)
{
if(a[idx] == b[idx])
{
if(err)
{
err = false;
std::cout << "\033[0m";
}
std::cout << a[idx];
}
else
{
if(!err)
{
err = true;
std::cout << "\033[7m";
}
std::cout << "[";
print_char(a[idx]);
std::cout << "/";
print_char(b[idx]);
std::cout << "]";
}
}
if(err)
{
err = false;
std::cout << "\033[0m";
}
std::cout << std::endl
<< "---------------------------------------------------" << std::endl;
if(a.length() > b.length())
{
std::cout << "Left hand side string is longer ("
<< a.length()
<< " versus "
<< b.length()
<< ")."
<< std::endl;
}
else if(b.length() > a.length())
{
std::cout << "Right hand side string is longer ("
<< a.length()
<< " versus "
<< b.length()
<< ")."
<< std::endl;
}
}
// to generate the standard error too
//
CATCH_REQUIRE(a == b);
}