From 8d78727d6e7badca16ba7a1328643928a0039569 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 18 Nov 2024 18:02:09 +0100 Subject: move utilities from loek/audio --- src/test/AssetTest.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/AssetTest.cpp (limited to 'src/test/AssetTest.cpp') diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp new file mode 100644 index 0000000..563a253 --- /dev/null +++ b/src/test/AssetTest.cpp @@ -0,0 +1,33 @@ +#include + +#include +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class AssetTest : public Test { +public: + Config & cfg = Config::get_instance(); + void SetUp() override { + this->cfg.asset.root_pattern = ".crepe-root"; + } +}; + +TEST_F(AssetTest, Existant) { + ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); +} + +TEST_F(AssetTest, Nonexistant) { + ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); +} + +TEST_F(AssetTest, Rootless) { + cfg.asset.root_pattern.clear(); + + string arbitrary = "\\/this is / /../passed through as-is"; + Asset asset{arbitrary}; + ASSERT_EQ(arbitrary, asset.get_path()); +} + -- cgit v1.2.3 From 02845c3d25130e9473604cb2eeee42a7a7a8eadf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 14:01:31 +0100 Subject: `make format` --- src/crepe/api/Asset.cpp | 15 +++++---------- src/crepe/api/Asset.h | 8 ++++---- src/crepe/types.h | 4 ++-- src/crepe/util/OptionalRef.h | 13 ++++++------- src/crepe/util/OptionalRef.hpp | 3 +-- src/example/rendering.cpp | 5 ++--- src/test/AssetTest.cpp | 13 +++---------- src/test/OptionalRefTest.cpp | 1 - 8 files changed, 23 insertions(+), 39 deletions(-) (limited to 'src/test/AssetTest.cpp') diff --git a/src/crepe/api/Asset.cpp b/src/crepe/api/Asset.cpp index 5271cf7..3fe3ceb 100644 --- a/src/crepe/api/Asset.cpp +++ b/src/crepe/api/Asset.cpp @@ -8,8 +8,8 @@ using namespace crepe; using namespace std; -Asset::Asset(const string & src) : src(find_asset(src)) { } -Asset::Asset(const char * src) : src(find_asset(src)) { } +Asset::Asset(const string & src) : src(find_asset(src)) {} +Asset::Asset(const char * src) : src(find_asset(src)) {} const string & Asset::get_path() const noexcept { return this->src; } @@ -22,14 +22,12 @@ string Asset::find_asset(const string & src) const { // absolute paths do not need to be resolved, only canonicalized filesystem::path path = src; - if (path.is_absolute()) - return filesystem::canonical(path); + if (path.is_absolute()) return filesystem::canonical(path); // find directory matching root_pattern filesystem::path root = this->whereami(); while (1) { - if (filesystem::exists(root / root_pattern)) - break; + if (filesystem::exists(root / root_pattern)) break; if (!root.has_parent_path()) throw runtime_error(format("Asset: Cannot find root pattern ({})", root_pattern)); root = root.parent_path(); @@ -48,11 +46,8 @@ string Asset::whereami() const noexcept { return path; } -bool Asset::operator==(const Asset & other) const noexcept { - return this->src == other.src; -} +bool Asset::operator==(const Asset & other) const noexcept { return this->src == other.src; } size_t std::hash::operator()(const Asset & asset) const noexcept { return std::hash{}(asset.get_path()); }; - diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index 685dd3a..77596ac 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -33,7 +33,7 @@ public: * \param other Possibly different instance of \c Asset to test equality against * \return True if \c this and \c other are equal */ - bool operator == (const Asset & other) const noexcept; + bool operator==(const Asset & other) const noexcept; private: //! path to asset @@ -52,7 +52,8 @@ private: namespace std { //! Hash helper struct -template<> struct hash { +template <> +struct hash { /** * \brief Hash operator for crepe::Asset * @@ -64,5 +65,4 @@ template<> struct hash { size_t operator()(const crepe::Asset & asset) const noexcept; }; -} - +} // namespace std diff --git a/src/crepe/types.h b/src/crepe/types.h index 86730cc..914c76c 100644 --- a/src/crepe/types.h +++ b/src/crepe/types.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include namespace crepe { @@ -13,4 +13,4 @@ typedef uint32_t game_object_id_t; template using RefVector = std::vector>; -} +} // namespace crepe diff --git a/src/crepe/util/OptionalRef.h b/src/crepe/util/OptionalRef.h index 8417a25..0a94ae5 100644 --- a/src/crepe/util/OptionalRef.h +++ b/src/crepe/util/OptionalRef.h @@ -23,7 +23,7 @@ public: * * \return Reference to this (required for operator) */ - OptionalRef & operator=(T & ref); + OptionalRef & operator=(T & ref); /** * \brief Check if this reference is not empty * @@ -51,13 +51,13 @@ public: void clear() noexcept; //! Copy constructor - OptionalRef(const OptionalRef &); + OptionalRef(const OptionalRef &); //! Move constructor - OptionalRef(OptionalRef &&); + OptionalRef(OptionalRef &&); //! Copy assignment - OptionalRef & operator=(const OptionalRef &); + OptionalRef & operator=(const OptionalRef &); //! Move assignment - OptionalRef & operator=(OptionalRef &&); + OptionalRef & operator=(OptionalRef &&); private: /** @@ -68,7 +68,6 @@ private: T * ref = nullptr; }; -} +} // namespace crepe #include "OptionalRef.hpp" - diff --git a/src/crepe/util/OptionalRef.hpp b/src/crepe/util/OptionalRef.hpp index 7b201b0..ee41f61 100644 --- a/src/crepe/util/OptionalRef.hpp +++ b/src/crepe/util/OptionalRef.hpp @@ -63,5 +63,4 @@ OptionalRef::operator bool() const noexcept { return this->ref != nullptr; } -} - +} // namespace crepe diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 522ec0f..01794f8 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,9 +30,8 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - Sprite & sprite - = obj.add_component(make_shared("asset/texture/img.png"), - color, FlipSettings{false, false}); + Sprite & sprite = obj.add_component( + make_shared("asset/texture/img.png"), color, FlipSettings{false, false}); sprite.sorting_in_layer = 2; sprite.order_in_layer = 1; obj.add_component(Color::RED); diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 563a253..8aa7629 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -10,18 +10,12 @@ using namespace testing; class AssetTest : public Test { public: Config & cfg = Config::get_instance(); - void SetUp() override { - this->cfg.asset.root_pattern = ".crepe-root"; - } + void SetUp() override { this->cfg.asset.root_pattern = ".crepe-root"; } }; -TEST_F(AssetTest, Existant) { - ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); -} +TEST_F(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } -TEST_F(AssetTest, Nonexistant) { - ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); -} +TEST_F(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } TEST_F(AssetTest, Rootless) { cfg.asset.root_pattern.clear(); @@ -30,4 +24,3 @@ TEST_F(AssetTest, Rootless) { Asset asset{arbitrary}; ASSERT_EQ(arbitrary, asset.get_path()); } - diff --git a/src/test/OptionalRefTest.cpp b/src/test/OptionalRefTest.cpp index 219ccca..2072d56 100644 --- a/src/test/OptionalRefTest.cpp +++ b/src/test/OptionalRefTest.cpp @@ -35,4 +35,3 @@ TEST(OptionalRefTest, Implicit) { EXPECT_TRUE(ref); ASSERT_NO_THROW(ref.get()); } - -- cgit v1.2.3 From 004ee3aafb6beb4e984877186bced560010f4ddb Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 22 Nov 2024 21:57:08 +0100 Subject: fix RenderSystem unit test path resolution + reset Config before each test --- src/crepe/api/Config.h | 13 ++++++------- src/crepe/api/Texture.cpp | 13 ++++--------- src/crepe/api/Texture.h | 12 +++--------- src/test/AssetTest.cpp | 14 +++++--------- src/test/RenderSystemTest.cpp | 8 ++++---- src/test/main.cpp | 24 ++++++++++++++++++++---- 6 files changed, 42 insertions(+), 42 deletions(-) (limited to 'src/test/AssetTest.cpp') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 13eabd1..0c9d116 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -11,19 +11,18 @@ namespace crepe { * modified *before* execution is handed over from the game programmer to the engine (i.e. the * main loop is started). */ -class Config { +class Config final { public: //! Retrieve handle to global Config instance static Config & get_instance(); private: Config() = default; - - // singleton - Config(const Config &) = delete; - Config(Config &&) = delete; - Config & operator=(const Config &) = delete; - Config & operator=(Config &&) = delete; + ~Config() = default; + Config(const Config &) = default; + Config(Config &&) = default; + Config & operator=(const Config &) = default; + Config & operator=(Config &&) = default; public: //! Logging-related settings diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 9be9421..264d7b1 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -9,14 +9,9 @@ using namespace crepe; using namespace std; -Texture::Texture(unique_ptr res) { +Texture::Texture(const Asset & src) { dbg_trace(); - this->load(std::move(res)); -} - -Texture::Texture(const char * src) { - dbg_trace(); - this->load(make_unique(src)); + this->load(src); } Texture::~Texture() { @@ -24,9 +19,9 @@ Texture::~Texture() { this->texture.reset(); } -void Texture::load(unique_ptr res) { +void Texture::load(const Asset & res) { SDLContext & ctx = SDLContext::get_instance(); - this->texture = std::move(ctx.texture_from_path(res->get_path())); + this->texture = ctx.texture_from_path(res.get_path()); } int Texture::get_width() const { diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 6965223..b4f7d07 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -24,17 +24,11 @@ class Animator; class Texture { public: - /** - * \brief Constructs a Texture from a file path. - * \param src Path to the image file to be loaded as a texture. - */ - Texture(const char * src); - /** * \brief Constructs a Texture from an Asset resource. - * \param res Unique pointer to an Asset resource containing texture data. + * \param src Asset with texture data to load. */ - Texture(std::unique_ptr res); + Texture(const Asset & src); /** * \brief Destroys the Texture instance, freeing associated resources. @@ -59,7 +53,7 @@ private: * \brief Loads the texture from an Asset resource. * \param res Unique pointer to an Asset resource to load the texture from. */ - void load(std::unique_ptr res); + void load(const Asset & res); private: //! The texture of the class from the library diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 8aa7629..c3c166c 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -7,20 +7,16 @@ using namespace std; using namespace crepe; using namespace testing; -class AssetTest : public Test { -public: - Config & cfg = Config::get_instance(); - void SetUp() override { this->cfg.asset.root_pattern = ".crepe-root"; } -}; - -TEST_F(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } +TEST(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } -TEST_F(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } +TEST(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } -TEST_F(AssetTest, Rootless) { +TEST(AssetTest, Rootless) { + Config & cfg = Config::get_instance(); cfg.asset.root_pattern.clear(); string arbitrary = "\\/this is / /../passed through as-is"; Asset asset{arbitrary}; ASSERT_EQ(arbitrary, asset.get_path()); } + diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index ac479d3..f37fb56 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -30,7 +30,7 @@ public: void SetUp() override { auto & sprite1 - = entity1.add_component(make_shared("../asset/texture/img.png"), + = entity1.add_component(make_shared("asset/texture/img.png"), Color(0, 0, 0, 0), FlipSettings{false, false}); ASSERT_NE(sprite1.sprite_image.get(), nullptr); sprite1.order_in_layer = 5; @@ -38,7 +38,7 @@ public: EXPECT_EQ(sprite1.order_in_layer, 5); EXPECT_EQ(sprite1.sorting_in_layer, 5); auto & sprite2 - = entity2.add_component(make_shared("../asset/texture/img.png"), + = entity2.add_component(make_shared("asset/texture/img.png"), Color(0, 0, 0, 0), FlipSettings{false, false}); ASSERT_NE(sprite2.sprite_image.get(), nullptr); sprite2.sorting_in_layer = 2; @@ -48,7 +48,7 @@ public: EXPECT_EQ(sprite2.order_in_layer, 1); auto & sprite3 - = entity3.add_component(make_shared("../asset/texture/img.png"), + = entity3.add_component(make_shared("asset/texture/img.png"), Color(0, 0, 0, 0), FlipSettings{false, false}); ASSERT_NE(sprite3.sprite_image.get(), nullptr); sprite3.sorting_in_layer = 1; @@ -58,7 +58,7 @@ public: EXPECT_EQ(sprite3.order_in_layer, 2); auto & sprite4 - = entity4.add_component(make_shared("../asset/texture/img.png"), + = entity4.add_component(make_shared("asset/texture/img.png"), Color(0, 0, 0, 0), FlipSettings{false, false}); ASSERT_NE(sprite4.sprite_image.get(), nullptr); sprite4.sorting_in_layer = 1; diff --git a/src/test/main.cpp b/src/test/main.cpp index 241015d..e03a989 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -1,15 +1,31 @@ -#include - #include +#define protected public +#define private public + +#include + using namespace crepe; using namespace testing; +class GlobalConfigReset : public EmptyTestEventListener { +public: + Config & cfg = Config::get_instance(); + Config cfg_default = Config(); + + // This function is called before each test + void OnTestStart(const TestInfo &) override { + cfg = cfg_default; + cfg.log.level = Log::Level::WARNING; + } +}; + int main(int argc, char ** argv) { InitGoogleTest(&argc, argv); - auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::ERROR; + UnitTest & ut = *UnitTest::GetInstance(); + ut.listeners().Append(new GlobalConfigReset); return RUN_ALL_TESTS(); } + -- cgit v1.2.3 From ba8c5d5feae48036405f3cdd920dac421fdb6184 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 24 Nov 2024 18:56:41 +0100 Subject: `make format` --- src/test/AssetTest.cpp | 1 - src/test/main.cpp | 1 - 2 files changed, 2 deletions(-) (limited to 'src/test/AssetTest.cpp') diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index c3c166c..93fd6a9 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -19,4 +19,3 @@ TEST(AssetTest, Rootless) { Asset asset{arbitrary}; ASSERT_EQ(arbitrary, asset.get_path()); } - diff --git a/src/test/main.cpp b/src/test/main.cpp index e03a989..aece72d 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -28,4 +28,3 @@ int main(int argc, char ** argv) { return RUN_ALL_TESTS(); } - -- cgit v1.2.3