diff --git a/builder.cpp b/builder.cpp index 660029a..214929b 100644 --- a/builder.cpp +++ b/builder.cpp @@ -1,5 +1,4 @@ #include "builder.hpp" -#include "dbc.hpp" #include // for milliseconds #include #include @@ -15,6 +14,7 @@ #include #include #include +#include "dbc.hpp" using std::string; using namespace fmt; diff --git a/dbc.cpp b/dbc.cpp new file mode 100644 index 0000000..c25d32a --- /dev/null +++ b/dbc.cpp @@ -0,0 +1,40 @@ +#include "dbc.hpp" + +void dbc::log(const string &message) { + fmt::print("{}\n", message); +} + +void dbc::sentinel(const string &message) { + string err = fmt::format("[SENTINEL!] {}\n", message); + throw dbc::SentinelError{err}; +} + +void dbc::pre(const string &message, bool test) { + if(!test) { + string err = fmt::format("[PRE!] {}\n", message); + throw dbc::PreCondError{err}; + } +} + +void dbc::pre(const string &message, std::function tester) { + dbc::pre(message, tester()); +} + +void dbc::post(const string &message, bool test) { + if(!test) { + string err = fmt::format("[POST!] {}\n", message); + throw dbc::PostCondError{err}; + } +} + +void dbc::post(const string &message, std::function tester) { + dbc::post(message, tester()); +} + +void dbc::check(bool test, const string &message) { + if(!test) { + string err = fmt::format("[CHECK!] {}\n", message); + fmt::println("{}", err); + throw dbc::CheckError{err}; + } +} diff --git a/dbc.hpp b/dbc.hpp index cc4d71c..919d729 100644 --- a/dbc.hpp +++ b/dbc.hpp @@ -15,39 +15,15 @@ namespace dbc { }; class CheckError : public Error {}; - class SentinelError : public Error {}; class PreCondError : public Error {}; class PostCondError : public Error {}; - void log(const string &message) { - fmt::print("{}\n", message); - } - - void sentinel(const string &message) { - string err = fmt::format("[SENTINEL!] {}\n", message); - throw SentinelError{err}; - } - - void pre(const string &message, std::function tester) { - if(!tester()) { - string err = fmt::format("[PRE!] {}\n", message); - throw PreCondError{err}; - } - } - - void post(const string &message, std::function tester) { - if(!tester()) { - string err = fmt::format("[POST!] {}\n", message); - throw PostCondError{err}; - } - } - - void check(bool test, const string &message) { - if(!test) { - string err = fmt::format("[CHECK!] {}\n", message); - fmt::println("{}", err); - throw CheckError{err}; - } - } + void log(const string &message); + void sentinel(const string &message); + void pre(const string &message, bool test); + void pre(const string &message, std::function tester); + void post(const string &message, bool test); + void post(const string &message, std::function tester); + void check(bool test, const string &message); } diff --git a/game_engine.cpp b/game_engine.cpp index 7589cd8..3bf573b 100644 --- a/game_engine.cpp +++ b/game_engine.cpp @@ -5,6 +5,7 @@ #include #include "game_engine.hpp" #include +#include "dbc.hpp" const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); diff --git a/meson.build b/meson.build index aa39278..01ecd57 100644 --- a/meson.build +++ b/meson.build @@ -29,6 +29,7 @@ subdir('scratchpad') executable('escape_turings_tarpit', ['game_engine.cpp', + 'dbc.cpp', 'gui.cpp', 'watcher.cpp', 'builder.cpp', @@ -37,9 +38,11 @@ executable('escape_turings_tarpit', dependencies: dependencies) runtests = executable('runtests', [ + 'dbc.cpp', 'game_engine.cpp', 'tests/game_engine.cpp', 'tests/fsm.cpp', + 'tests/dbc.cpp', ], dependencies: dependencies + [catch2]) diff --git a/sfmlbackend.cpp b/sfmlbackend.cpp index 64bc203..5c2c0f9 100644 --- a/sfmlbackend.cpp +++ b/sfmlbackend.cpp @@ -162,6 +162,13 @@ SFMLBackend::SFMLBackend(GameEngine &g) : window(sf::VideoMode(X_DIM, Y_DIM), "T } +/* + * This makes my sould hurt. Make it stop. + * + * TODO: Make this more efficient, and don't display + * more than 10 or so errors since more than that is + * not very useful. + */ void SFMLBackend::update_log(std::vector &lines) { log.clear(); for(string &line : lines) { diff --git a/status.txt b/status.txt index bd2b672..ab37297 100644 --- a/status.txt +++ b/status.txt @@ -1,14 +1,14 @@ * Using that to redesign the starter UI. BUGS: -* BUG: Log doesn't scroll. * BUG: lots of errors crash it * BUG: doesn't play you_died sound. TODO: * Rewrite dbc.hpp to actually work. -* Use parameter pack in FSM template + * Add a timer to the game engine so you can set a kind of pomodoro timer and if you don't meet the goal it costs you. + * Track the deaths. https://en.cppreference.com/w/cpp/language/parameter_pack diff --git a/tests/dbc.cpp b/tests/dbc.cpp new file mode 100644 index 0000000..fa45b86 --- /dev/null +++ b/tests/dbc.cpp @@ -0,0 +1,39 @@ +#include +#include "dbc.hpp" + +using namespace dbc; + +TEST_CASE("basic feature tests", "[utils]") { + log("Logging a message."); + + try { + sentinel("This shouldn't happen."); + } catch(SentinelError) { + log("Sentinel happened."); + } + + 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"); + + try { + check(1 == 2, "this should fail"); + } catch(CheckError err) { + log("check fail worked"); + } + + try { + pre("failing pre", 1 == 3); + } catch(PreCondError err) { + log("pre fail worked"); + } + + try { + post("failing post", 1 == 4); + } catch(PostCondError err) { + log("post faile worked"); + } +}