StormByte C++ Library 1.2.0
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
Namespaces | Macros | Functions
test_handlers.h File Reference
#include <exception>
#include <filesystem>
#include <iostream>
#include <ostream>
#include <string_view>
Include dependency graph for test_handlers.h:

Go to the source code of this file.

Namespaces

namespace  StormByte
 Root namespace of the StormByte suite.
 
namespace  StormByte::Test
 
namespace  StormByte::Test::Detail
 

Macros

#define RETURN_TEST(fn_name, fn_result)
 Prints FAILED to stderr when fn_result is not 0, then returns that value.
 
#define CurrentFileDirectory   std::filesystem::path(__FILE__).parent_path()
 Directory of the translation unit (std::filesystem::path of __FILE__).
 
#define ASSERT_EQUAL(fn_name, expected, actual)
 Fails the test (return 1) when expected != actual.
 
#define ASSERT_NOT_EQUAL(fn_name, expected, actual)
 Fails the test (return 1) when expected == actual.
 
#define ASSERT_FALSE(fn_name, condition)
 Fails the test (return 1) when condition is true.
 
#define ASSERT_TRUE(fn_name, condition)
 Fails the test (return 1) when condition is false.
 
#define ASSERT_THROWS(fn_name, expression, exception_type)
 Fails when expression does not throw exception_type.
 
#define ASSERT_NO_THROW(fn_name, expression)
 Fails when expression throws any exception.
 
#define ASSERT_NEAR(fn_name, expected, actual, tolerance)
 Fails when two arithmetic values differ by more than tolerance.
 
#define ASSERT_CONTAINS(fn_name, haystack, needle)
 Fails when haystack does not contain needle.
 
#define ASSERT_NOT_NULL(fn_name, pointer)
 Fails when pointer is null.
 

Functions

template<typename T >
void StormByte::Test::Detail::PrintValue (std::ostream &stream, const T &value)
 

Macro Definition Documentation

◆ ASSERT_CONTAINS

#define ASSERT_CONTAINS (   fn_name,
  haystack,
  needle 
)
Value:
do { \
const auto& stormbyte_haystack = (haystack); \
const auto& stormbyte_needle = (needle); \
if (stormbyte_haystack.find(stormbyte_needle) == std::string_view::npos) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected text to contain \"" << stormbyte_needle << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails when haystack does not contain needle.

◆ ASSERT_EQUAL

#define ASSERT_EQUAL (   fn_name,
  expected,
  actual 
)
Value:
do { \
const auto& stormbyte_expected = (expected); \
const auto& stormbyte_actual = (actual); \
if (!(stormbyte_expected == stormbyte_actual)) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_expected); \
std::cerr << "\", got \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
std::cerr << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when expected != actual.

Parameters
fn_nameTest name written to the log.
expectedExpected value.
actualObserved value.

◆ ASSERT_FALSE

#define ASSERT_FALSE (   fn_name,
  condition 
)
Value:
do { \
if ((condition)) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is true, expected false" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when condition is true.

Parameters
fn_nameTest name written to the log.
conditionExpression that must be false.

◆ ASSERT_NEAR

#define ASSERT_NEAR (   fn_name,
  expected,
  actual,
  tolerance 
)
Value:
do { \
const auto stormbyte_expected = (expected); \
const auto stormbyte_actual = (actual); \
const auto stormbyte_tolerance = (tolerance); \
const auto stormbyte_difference = stormbyte_expected > stormbyte_actual ? \
stormbyte_expected - stormbyte_actual : stormbyte_actual - stormbyte_expected; \
if (stormbyte_difference > stormbyte_tolerance) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values differ beyond tolerance" << std::endl; \
return 1; \
} \
} while (false)

Fails when two arithmetic values differ by more than tolerance.

◆ ASSERT_NO_THROW

#define ASSERT_NO_THROW (   fn_name,
  expression 
)
Value:
do { \
try { \
(void)(expression); \
} catch (const std::exception& stormbyte_exception) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected exception: " << stormbyte_exception.what() << std::endl; \
return 1; \
} catch (...) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected non-standard exception" << std::endl; \
return 1; \
} \
} while (false)

Fails when expression throws any exception.

◆ ASSERT_NOT_EQUAL

#define ASSERT_NOT_EQUAL (   fn_name,
  expected,
  actual 
)
Value:
do { \
const auto& stormbyte_expected = (expected); \
const auto& stormbyte_actual = (actual); \
if (stormbyte_expected == stormbyte_actual) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values should differ; both were \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
std::cerr << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when expected == actual.

Parameters
fn_nameTest name written to the log.
expectedValue that must differ.
actualObserved value.

◆ ASSERT_NOT_NULL

#define ASSERT_NOT_NULL (   fn_name,
  pointer 
)
Value:
do { \
if ((pointer) == nullptr) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": pointer is null" << std::endl; \
return 1; \
} \
} while (false)

Fails when pointer is null.

◆ ASSERT_THROWS

#define ASSERT_THROWS (   fn_name,
  expression,
  exception_type 
)
Value:
do { \
bool stormbyte_threw = false; \
try { \
(void)(expression); \
} catch (const exception_type&) { \
stormbyte_threw = true; \
} catch (...) { \
} \
if (!stormbyte_threw) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected " << #exception_type << " to be thrown" << std::endl; \
return 1; \
} \
} while (false)

Fails when expression does not throw exception_type.

◆ ASSERT_TRUE

#define ASSERT_TRUE (   fn_name,
  condition 
)
Value:
do { \
if (!(condition)) { \
std::cerr << fn_name << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is false, expected true" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when condition is false.

Parameters
fn_nameTest name written to the log.
conditionExpression that must be true.

◆ CurrentFileDirectory

#define CurrentFileDirectory   std::filesystem::path(__FILE__).parent_path()

Directory of the translation unit (std::filesystem::path of __FILE__).

◆ RETURN_TEST

#define RETURN_TEST (   fn_name,
  fn_result 
)
Value:
do { \
const int stormbyte_test_result = (fn_result); \
if (stormbyte_test_result != 0) { \
std::cerr << "Test " << fn_name << " FAILED!" << std::endl; \
} \
return stormbyte_test_result; \
} while (false)

Prints FAILED to stderr when fn_result is not 0, then returns that value.

Parameters
fn_nameTest name written to the log.
fn_resultInteger status (0 = pass).