This is a header-only assertions library for C++. The intention is provide more general assertions that offer multiple improvements over the standard asserts. These include improved debug information where applicable, greater customization options, greater readability, and a collection of functions to utilize with assertions.
Including the header file via #include "fluassert.h"
should be sufficient in order to make use of fluasserts. For example:
#include "fluassert.h"
int main() {
int x = 1;
fluassert(x, should.be, 0);
}
Outputs: FLUASSERT| example.cpp's main (line 5): x should be 0 - but is 1
before exiting the program early.
There are several defines that can be used to change the behaviour of fluassert.
NDEBUG
can be defined in order to disable fluassertsFLUASSERT_NOABORT
can be defined in order to continue program execution after a fluassert failsFLUASSERT_HANDLER(x)
can be defined to determine how fluasserts are printed. By default this isstd::cerr << (x) << '\n';
FLUASSERT_PRESERVE_ASSERT
can be defined in order to allow usage ofassert.h
alongside fluassert. Only matters whenassert.h
is included beforefluassert.h
, to avoid redefining theassert(x)
macro.
There are several different conditionals that can be used in fluasserts. The format is fluassert(x, conditional, y, z, etc.)
.
x should.be y
-- checks ifx == y
x should.be_greater_than y
-- checks ifx > y
x should.be_lesser_than y
-- checks ifx < y
x should.be_greater_or_equal_to y
-- checks ifx >= y
x should.be_lesser_or_equal_to y
-- checks ifx <= y
x should.be_between y, z
-- checks ify <= x <= z
orz <= x <= y
x should.be_positive
-- checks ifx > 0
x should.be_negative
-- checks ifx < 0
x should.contain y
-- checks thatx
, as a container (std::set
,std::unordered_set
,std::map
,std::unordered_map
,std::vector
,std::string
) containsy
as a valuex should.contain_key y
-- forstd::map
andstd::unordered_map
, does the same asshould.contain
x should.contain_value y
-- forstd::map
andstd::unordered_map
, checks if the value exists within the mapx should.begin_with y
-- forstd::string
, checks ifx
begins with the character or stringy
x should.end_with y
-- forstd::string
, checks ifx
ends with the character or stringy
x should.match_regex y
-- forstd::string
, checks ifx
fully matches the regular expressiony
x should.contain_regex y
-- forstd::string
, checks ifx
has a substring (potentially the full string) that matches the regular expressiony
x should.throw_error
-- for a zero-argument function or lambdax
, check if an exception gets thrownx should.be_size y
-- for a container with.size()
, check if its size is equal toy