DBC now works and has a test.
This commit is contained in:
parent
f632f2d5af
commit
2035a6dd00
8 changed files with 100 additions and 34 deletions
|
@ -1,5 +1,4 @@
|
||||||
#include "builder.hpp"
|
#include "builder.hpp"
|
||||||
#include "dbc.hpp"
|
|
||||||
#include <chrono> // for milliseconds
|
#include <chrono> // for milliseconds
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -15,6 +14,7 @@
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include "dbc.hpp"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
|
|
40
dbc.cpp
Normal file
40
dbc.cpp
Normal file
|
@ -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};
|
||||||
|
}
|
||||||
|
}
|
38
dbc.hpp
38
dbc.hpp
|
@ -15,39 +15,15 @@ namespace dbc {
|
||||||
};
|
};
|
||||||
|
|
||||||
class CheckError : public Error {};
|
class CheckError : public Error {};
|
||||||
|
|
||||||
class SentinelError : public Error {};
|
class SentinelError : public Error {};
|
||||||
class PreCondError : public Error {};
|
class PreCondError : public Error {};
|
||||||
class PostCondError : public Error {};
|
class PostCondError : public Error {};
|
||||||
|
|
||||||
void log(const string &message) {
|
void log(const string &message);
|
||||||
fmt::print("{}\n", message);
|
void sentinel(const string &message);
|
||||||
}
|
void pre(const string &message, bool test);
|
||||||
|
void pre(const string &message, std::function<bool()> tester);
|
||||||
void sentinel(const string &message) {
|
void post(const string &message, bool test);
|
||||||
string err = fmt::format("[SENTINEL!] {}\n", message);
|
void post(const string &message, std::function<bool()> tester);
|
||||||
throw SentinelError{err};
|
void check(bool test, const string &message);
|
||||||
}
|
|
||||||
|
|
||||||
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};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <fmt/color.h>
|
#include <fmt/color.h>
|
||||||
#include "game_engine.hpp"
|
#include "game_engine.hpp"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include "dbc.hpp"
|
||||||
|
|
||||||
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
|
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ subdir('scratchpad')
|
||||||
|
|
||||||
executable('escape_turings_tarpit',
|
executable('escape_turings_tarpit',
|
||||||
['game_engine.cpp',
|
['game_engine.cpp',
|
||||||
|
'dbc.cpp',
|
||||||
'gui.cpp',
|
'gui.cpp',
|
||||||
'watcher.cpp',
|
'watcher.cpp',
|
||||||
'builder.cpp',
|
'builder.cpp',
|
||||||
|
@ -37,9 +38,11 @@ executable('escape_turings_tarpit',
|
||||||
dependencies: dependencies)
|
dependencies: dependencies)
|
||||||
|
|
||||||
runtests = executable('runtests', [
|
runtests = executable('runtests', [
|
||||||
|
'dbc.cpp',
|
||||||
'game_engine.cpp',
|
'game_engine.cpp',
|
||||||
'tests/game_engine.cpp',
|
'tests/game_engine.cpp',
|
||||||
'tests/fsm.cpp',
|
'tests/fsm.cpp',
|
||||||
|
'tests/dbc.cpp',
|
||||||
],
|
],
|
||||||
dependencies: dependencies + [catch2])
|
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) {
|
void SFMLBackend::update_log(std::vector<string> &lines) {
|
||||||
log.clear();
|
log.clear();
|
||||||
for(string &line : lines) {
|
for(string &line : lines) {
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
* Using that to redesign the starter UI.
|
* Using that to redesign the starter UI.
|
||||||
|
|
||||||
BUGS:
|
BUGS:
|
||||||
* BUG: Log doesn't scroll.
|
|
||||||
* BUG: lots of errors crash it
|
* BUG: lots of errors crash it
|
||||||
* BUG: doesn't play you_died sound.
|
* BUG: doesn't play you_died sound.
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
* Rewrite dbc.hpp to actually work.
|
* 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.
|
* 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.
|
* Track the deaths.
|
||||||
|
|
||||||
https://en.cppreference.com/w/cpp/language/parameter_pack
|
https://en.cppreference.com/w/cpp/language/parameter_pack
|
||||||
|
|
39
tests/dbc.cpp
Normal file
39
tests/dbc.cpp
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue