////////////////////////////////////////////////////////////////////////////////
//
// Assert
//
#include <sstream>
#include <kcpp_assert.hpp>
#undef assertEquals
#undef assertTrue
#undef assertFalse
#undef assertNull
#undef assertNotNull
#undef fail
namespace kcpp
{
AssertError::AssertError() noexcept : Error(), errorFile(""), errorFunc(""), errorLine(0)
{
// NOP
}
AssertError::AssertError(const AssertError& t) noexcept : Error(t), errorFile(t.errorFile), errorFunc(t.errorFunc), errorLine(t.errorLine)
{
// NOP
}
AssertError::AssertError(const std::string& msg) noexcept : Error(msg), errorFile(""), errorFunc(""), errorLine(0)
{
// NOP
}
AssertError::AssertError(const std::string& msg, const char* file, const char* func, int line) noexcept
: Error(msg), errorFile(file), errorFunc(func), errorLine(line)
{
// NOP
}
AssertError::~AssertError() noexcept
{
// NOP
}
const std::string& AssertError::getFile() const noexcept
{
return errorFile;
}
const std::string& AssertError::getFunc() const noexcept
{
return errorFunc;
}
int AssertError::getLine() const noexcept
{
return errorLine;
}
namespace Assert
{
void assertEquals(bool expected, bool actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
const char* msg = (expected)
? "expected:<true> but was:<false>"
: "expected:<false> but was:<true>";
throw AssertError(msg, file, func, line);
}
}
void assertEquals(char expected, char actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
std::ostringstream msg;
msg << "expected:<" << expected << "> but was:<" << actual << ">";
throw AssertError(msg.str(), file, func, line);
}
}
void assertEquals(int expected, int actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
std::ostringstream msg;
msg << "expected:<" << expected << "> but was:<" << actual << ">";
throw AssertError(msg.str(), file, func, line);
}
}
void assertEquals(long expected, long actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
std::ostringstream msg;
msg << "expected:<" << expected << "> but was:<" << actual << ">";
throw AssertError(msg.str(), file, func, line);
}
}
void assertEquals(double expected, double actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
std::ostringstream msg;
msg << "expected:<" << expected << "> but was:<" << actual << ">";
throw AssertError(msg.str(), file, func, line);
}
}
void assertEquals(const std::string& expected, const std::string& actual, const char* file, const char* func, int line)
{
if (expected != actual)
{
std::ostringstream msg;
msg << "expected:<" << expected << "> but was:<" << actual << ">";
throw AssertError(msg.str(), file, func, line);
}
}
void assertEquals(const char* expected, const std::string& actual, const char* file, const char* func, int line)
{
std::string expectedStr = expected;
assertEquals(expectedStr, actual, file, func, line);
}
void assertEquals(const std::string& expected, const char* actual, const char* file, const char* func, int line)
{
std::string actualStr = actual;
assertEquals(expected, actualStr, file, func, line);
}
void assertEquals(const char* expected, const char* actual, const char* file, const char* func, int line)
{
std::string expectedStr = expected;
std::string actualStr = actual;
assertEquals(expectedStr, actualStr, file, func, line);
}
void assertTrue(bool condition, const char* file, const char* func, int line)
{
assertEquals(true, condition, file, func, line);
}
void assertFalse(bool condition, const char* file, const char* func, int line)
{
assertEquals(false, condition, file, func, line);
}
void assertNull(void* obj, const char* file, const char* func, int line)
{
if (obj != nullptr)
{
throw AssertError("expected:<null> but was:<not null>", file, func, line);
}
}
void assertNotNull(void* obj, const char* file, const char* func, int line)
{
if (obj == nullptr)
{
throw AssertError("expected:<not null> but was:<not>", file, func, line);
}
}
void fail(const char* file, const char* func, int line)
{
throw AssertError("fail()", file, func, line);
}
}
}