Skip to content

Commit

Permalink
Add assert_bool
Browse files Browse the repository at this point in the history
  • Loading branch information
nluka committed Dec 30, 2022
1 parent db0c234 commit 1c88e44
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Table of contents:
- Simple but useful markdown reports
- Built-in assertion functions for:
- integral types:
- `bool`
- `int8_t`, `uint8_t`
- `int16_t`, `uint16_t`
- `int32_t`, `uint32_t`
Expand Down
4 changes: 4 additions & 0 deletions advanced-example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ int main()
ntest::config::set_max_arr_preview_len(2);
ntest::config::set_max_str_preview_len(10);

// bool
ntest::assert_bool(true, true); // pass
ntest::assert_bool(true, false); // fail

// int8
ntest::assert_int8(INT8_MIN, INT8_MIN); // pass
ntest::assert_int8(INT8_MIN, INT8_MIN + 100); // fail
Expand Down
28 changes: 28 additions & 0 deletions ntest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ void assert_integral(
}
}

static
char const *bool_to_string(bool const b)
{
if (b == true) {
return "true";
} else {
return "false";
}
}

void ntest::assert_bool(
bool const expected, bool const actual,
std::source_location const loc)
{
bool const passed = actual == expected;

stringstream serialized_vals{};
serialized_vals << "bool | " << bool_to_string(expected);

if (passed)
ntest::internal::emplace_passed_assertion(serialized_vals, loc);
else // failed
{
serialized_vals << " | " << bool_to_string(actual);
ntest::internal::emplace_failed_assertion(serialized_vals, loc);
}
}

void ntest::assert_int8(
int8_t const expected, int8_t const actual,
source_location const loc)
Expand Down
5 changes: 5 additions & 0 deletions ntest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ namespace internal {

void init();

void assert_bool(
bool expected, bool actual,
std::source_location loc = std::source_location::current()
);

void assert_int8(
int8_t expected, int8_t actual,
std::source_location loc = std::source_location::current()
Expand Down

0 comments on commit 1c88e44

Please sign in to comment.