DBC now works and has a test.

master
Zed A. Shaw 3 months ago
parent f632f2d5af
commit 2035a6dd00
  1. 2
      builder.cpp
  2. 40
      dbc.cpp
  3. 38
      dbc.hpp
  4. 1
      game_engine.cpp
  5. 3
      meson.build
  6. 7
      sfmlbackend.cpp
  7. 4
      status.txt
  8. 39
      tests/dbc.cpp

@ -1,5 +1,4 @@
#include "builder.hpp"
#include "dbc.hpp"
#include <chrono> // for milliseconds
#include <fmt/core.h>
#include <fstream>
@ -15,6 +14,7 @@
#include <fstream>
#include <future>
#include <mutex>
#include "dbc.hpp"
using std::string;
using namespace fmt;

@ -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<bool()> 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<bool()> 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};
}
}

@ -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<bool()> tester) {
if(!tester()) {
string err = fmt::format("[PRE!] {}\n", message);
throw PreCondError{err};
}
}
void post(const string &message, std::function<bool()> 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<bool()> tester);
void post(const string &message, bool test);
void post(const string &message, std::function<bool()> tester);
void check(bool test, const string &message);
}

@ -5,6 +5,7 @@
#include <fmt/color.h>
#include "game_engine.hpp"
#include <cassert>
#include "dbc.hpp"
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);

@ -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])

@ -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<string> &lines) {
log.clear();
for(string &line : lines) {

@ -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

@ -0,0 +1,39 @@
#include <catch2/catch_test_macros.hpp>
#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");
}
}
Loading…
Cancel
Save