parent
767147c301
commit
adc192c6dc
@ -1,50 +0,0 @@ |
|||||||
#pragma once |
|
||||||
|
|
||||||
#include <string> |
|
||||||
#include <fmt/core.h> |
|
||||||
#include <functional> |
|
||||||
#include <source_location> |
|
||||||
|
|
||||||
using std::string; |
|
||||||
|
|
||||||
namespace dbc { |
|
||||||
class Error { |
|
||||||
public: |
|
||||||
const string message; |
|
||||||
Error(string m) : message{m} {} |
|
||||||
Error(const char *m) : message{m} {} |
|
||||||
}; |
|
||||||
|
|
||||||
class CheckError : public Error {}; |
|
||||||
class SentinelError : public Error {}; |
|
||||||
class PreCondError : public Error {}; |
|
||||||
class PostCondError : public Error {}; |
|
||||||
|
|
||||||
void log(const string &message, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
[[noreturn]] void sentinel(const string &message, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
void pre(const string &message, bool test, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
void pre(const string &message, std::function<bool()> tester, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
void post(const string &message, bool test, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
void post(const string &message, std::function<bool()> tester, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
|
|
||||||
void check(bool test, const string &message, |
|
||||||
const std::source_location location = |
|
||||||
std::source_location::current()); |
|
||||||
} |
|
@ -1,47 +0,0 @@ |
|||||||
#include "guecs/dbc.hpp" |
|
||||||
#include <iostream> |
|
||||||
|
|
||||||
void dbc::log(const string &message, const std::source_location location) { |
|
||||||
std::cout << '[' << location.file_name() << ':' |
|
||||||
<< location.line() << "|" |
|
||||||
<< location.function_name() << "] " |
|
||||||
<< message << std::endl; |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::sentinel(const string &message, const std::source_location location) { |
|
||||||
string err = fmt::format("[SENTINEL!] {}", message); |
|
||||||
dbc::log(err, location); |
|
||||||
throw dbc::SentinelError{err}; |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::pre(const string &message, bool test, const std::source_location location) { |
|
||||||
if(!test) { |
|
||||||
string err = fmt::format("[PRE!] {}", message); |
|
||||||
dbc::log(err, location); |
|
||||||
throw dbc::PreCondError{err}; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) { |
|
||||||
dbc::pre(message, tester(), location); |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::post(const string &message, bool test, const std::source_location location) { |
|
||||||
if(!test) { |
|
||||||
string err = fmt::format("[POST!] {}", message); |
|
||||||
dbc::log(err, location); |
|
||||||
throw dbc::PostCondError{err}; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) { |
|
||||||
dbc::post(message, tester(), location); |
|
||||||
} |
|
||||||
|
|
||||||
void dbc::check(bool test, const string &message, const std::source_location location) { |
|
||||||
if(!test) { |
|
||||||
string err = fmt::format("[CHECK!] {}\n", message); |
|
||||||
dbc::log(err, location); |
|
||||||
throw dbc::CheckError{err}; |
|
||||||
} |
|
||||||
} |
|
@ -1,18 +0,0 @@ |
|||||||
#include <catch2/catch_test_macros.hpp> |
|
||||||
#include "guecs/dbc.hpp" |
|
||||||
|
|
||||||
using namespace dbc; |
|
||||||
|
|
||||||
TEST_CASE("basic feature tests", "[dbc]") { |
|
||||||
log("Logging a message."); |
|
||||||
|
|
||||||
pre("confirm positive cases work", 1 == 1); |
|
||||||
|
|
||||||
pre("confirm positive lambda", [&]{ return 1 == 1;}); |
|
||||||
|
|
||||||
post("confirm positive post", 1 == 1); |
|
||||||
|
|
||||||
post("confirm postitive post with lamdba", [&]{ return 1 == 1;}); |
|
||||||
|
|
||||||
check(1 == 1, "one equals 1"); |
|
||||||
} |
|
Loading…
Reference in new issue