From 5a6855564fce967ad8a51fd5901947305fcf3ced Mon Sep 17 00:00:00 2001 From: Stepland <10530295-Buggyroom@users.noreply.gitlab.com> Date: Thu, 14 Apr 2022 01:26:31 +0200 Subject: [PATCH] Stuck in boilerplate hell --- src/better_chart.cpp | 6 +++- src/better_chart.hpp | 34 +++++++++++++++++++- src/better_notes.cpp | 6 ++++ src/better_notes.hpp | 29 +++++++++++++++-- src/better_timing.hpp | 20 ++++++++++-- src/main.cpp | 5 ++- src/tests/rapidcheck/generators.hpp | 49 ++++++++++++++++++++++++++--- src/tests/rapidcheck/main.cpp | 12 +++++++ src/tests/rapidcheck/meson.build | 2 ++ 9 files changed, 151 insertions(+), 12 deletions(-) diff --git a/src/better_chart.cpp b/src/better_chart.cpp index fa39ab2..3cd2eda 100644 --- a/src/better_chart.cpp +++ b/src/better_chart.cpp @@ -3,7 +3,6 @@ #include #include #include -#include "src/better_hakus.hpp" namespace better { nlohmann::ordered_json Chart::dump_to_memon_1_0_0( @@ -69,6 +68,11 @@ namespace better { }; } + std::ostream& operator<<(std::ostream& out, const better::Chart& c) { + out << fmt::format("{}", c); + return out; + }; + nlohmann::ordered_json remove_keys_already_in_fallback( const nlohmann::ordered_json& object, const nlohmann::ordered_json& fallback diff --git a/src/better_chart.hpp b/src/better_chart.hpp index 983ec99..3ab0dfa 100644 --- a/src/better_chart.hpp +++ b/src/better_chart.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include "better_hakus.hpp" @@ -26,6 +27,8 @@ namespace better { static Chart load_from_memon_1_0_0(const nlohmann::json& json, const nlohmann::json& fallback_timing); static Chart load_from_memon_legacy(const nlohmann::json& json); + + friend std::ostream& operator<<(std::ostream& out, const Chart& n); }; /* @@ -42,4 +45,33 @@ namespace better { const std::optional& hakus, const nlohmann::ordered_json& fallback_timing_object ); -} \ No newline at end of file +} + +template +struct fmt::formatter>: formatter { + // parse is inherited from formatter. + template + auto format(const std::optional& opt, FormatContext& ctx) { + if (opt) { + return format_to(ctx.out(), "{}", *opt); + } else { + return format_to(ctx.out(), "∅"); + } + } +}; + +template <> +struct fmt::formatter: formatter { + // parse is inherited from formatter. + template + auto format(const better::Chart& c, FormatContext& ctx) { + return format_to( + ctx.out(), + "LongNote(level: {}, timing: {}, hakus: {}, notes: {})", + c.level, + c.timing, + c.hakus, + c.notes + ); + } +}; \ No newline at end of file diff --git a/src/better_notes.cpp b/src/better_notes.cpp index 87c6c0e..fb65514 100644 --- a/src/better_notes.cpp +++ b/src/better_notes.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "fmt/core.h" #include "json.hpp" namespace better { @@ -153,4 +154,9 @@ namespace better { } return notes; }; + + std::ostream& operator<<(std::ostream& out, const Notes& n) { + out << fmt::format("{}", n); + return out; + }; } \ No newline at end of file diff --git a/src/better_notes.hpp b/src/better_notes.hpp index 48a6fae..93f6613 100644 --- a/src/better_notes.hpp +++ b/src/better_notes.hpp @@ -3,11 +3,14 @@ #include #include #include -#include -#include #include #include +#include +#include +#include +#include + #include "better_note.hpp" #include "better_timing.hpp" #include "generic_interval.hpp" @@ -40,5 +43,25 @@ namespace better { static Notes load_from_memon_1_0_0(const nlohmann::json& json, std::uint64_t resolution = 240); static Notes load_from_memon_legacy(const nlohmann::json& json, std::uint64_t resolution); + + friend std::ostream& operator<<(std::ostream& out, const Notes& n); }; -} \ No newline at end of file +} + +template <> +struct fmt::formatter: formatter { + // parse is inherited from formatter. + template + auto format(const better::Notes& n, FormatContext& ctx) { + std::vector notes; + std::transform(n.begin(), n.end(), notes.begin(), [](const auto& p){return p.second;}); + return format_to( + ctx.out(), + "[{}]", + fmt::join( + notes, + ", " + ) + ); + } +}; \ No newline at end of file diff --git a/src/better_timing.hpp b/src/better_timing.hpp index f138b1d..73e79a6 100644 --- a/src/better_timing.hpp +++ b/src/better_timing.hpp @@ -8,9 +8,9 @@ #include #include +#include #include -#include "json.hpp" #include "special_numeric_types.hpp" namespace better { @@ -87,4 +87,20 @@ namespace better { std::set events_by_beats; std::set events_by_seconds; }; -} \ No newline at end of file +} + +template <> +struct fmt::formatter: formatter { + // parse is inherited from formatter. + template + auto format(const better::Chart& c, FormatContext& ctx) { + return format_to( + ctx.out(), + "LongNote(level: {}, timing: {}, hakus: {}, notes: {})", + c.level, + c.timing, + c.hakus, + c.notes + ); + } +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2e97220..02dc162 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #define IMGUI_USER_CONFIG "imconfig-SFML.h" #include @@ -26,7 +27,7 @@ int main() { // TODO : Make the linear preview display the end of the chart // TODO : Make the linear preview timebar height movable - auto executable_folder = std::filesystem::path{whereami::executable_dir()}; + auto executable_folder = std::filesystem::u8path(whereami::executable_dir()); auto assets_folder = executable_folder / "assets"; auto settings_folder = executable_folder / "settings"; @@ -98,6 +99,8 @@ int main() { if (editor_state->save_if_needed_and_user_wants_to() != EditorState::SaveOutcome::UserCanceled) { window.close(); } + } else { + window.close(); } break; case sf::Event::Resized: diff --git a/src/tests/rapidcheck/generators.hpp b/src/tests/rapidcheck/generators.hpp index fefc61a..a438d5d 100644 --- a/src/tests/rapidcheck/generators.hpp +++ b/src/tests/rapidcheck/generators.hpp @@ -1,18 +1,24 @@ #pragma once #include +#include + #include #include +#include +#include +#include #include +#include +#include +#include "../../better_chart.hpp" +#include "../../better_hakus.hpp" #include "../../better_note.hpp" #include "../../better_notes.hpp" #include "../../better_timing.hpp" #include "../../variant_visitor.hpp" -#include "rapidcheck/gen/Build.h" -#include "rapidcheck/gen/Container.h" -#include "rapidcheck/gen/Exec.h" -#include "rapidcheck/gen/Predicate.h" + namespace rc { template<> @@ -160,4 +166,39 @@ namespace rc { ); } }; + + template<> + struct Arbitrary { + static Gen arbitrary() { + return gen::container>(gen::positive()); + } + }; + + template + struct Arbitrary> { + static Gen> arbitrary() { + return gen::mapcat( + gen::arbitrary(), + [](bool engaged) { + if (not engaged) { + return gen::construct>(); + } else { + return gen::construct>(gen::arbitrary()); + } + } + ); + } + }; + + template<> + struct Arbitrary { + static Gen arbitrary() { + return gen::construct( + gen::arbitrary>(), + gen::arbitrary>(), + gen::arbitrary>(), + gen::arbitrary() + ); + } + }; } \ No newline at end of file diff --git a/src/tests/rapidcheck/main.cpp b/src/tests/rapidcheck/main.cpp index 315d3ea..7db069b 100644 --- a/src/tests/rapidcheck/main.cpp +++ b/src/tests/rapidcheck/main.cpp @@ -8,6 +8,7 @@ #include "../../better_note.hpp" #include "../../better_notes.hpp" #include "../../better_timing.hpp" +#include "json.hpp" int main() { @@ -41,5 +42,16 @@ int main() { RC_ASSERT(original == recovered); } ); + + rc::check( + "A Chart object survives being converted to json and back", + [](const better::Chart& original) { + const auto fallback_timing = nlohmann::ordered_json::object(); + const auto j = original.dump_to_memon_1_0_0(fallback_timing); + RC_LOG("json dump : "+j.dump()); + const auto recovered = better::Chart::load_from_memon_1_0_0(j, fallback_timing); + RC_ASSERT(original == recovered); + } + ); return 0; } \ No newline at end of file diff --git a/src/tests/rapidcheck/meson.build b/src/tests/rapidcheck/meson.build index 1da7e4e..8de0cc1 100644 --- a/src/tests/rapidcheck/meson.build +++ b/src/tests/rapidcheck/meson.build @@ -3,6 +3,8 @@ rapidcheck_tests = executable( 'generators.cpp', 'main.cpp', '../../better_beats.cpp', + '../../better_chart.cpp', + '../../better_hakus.cpp', '../../better_note.cpp', '../../better_notes.cpp', '../../better_timing.cpp',