From ea9ec3c3e4524d9edc62b6825179aba3c50a34db Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Sun, 7 Jan 2024 12:35:23 +0100 Subject: [PATCH] Add reserved target names --- src/scratch/target.cpp | 12 ++++++++++-- test/scratch_classes/target_test.cpp | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/scratch/target.cpp b/src/scratch/target.cpp index 8d6ca0d3..289e415e 100644 --- a/src/scratch/target.cpp +++ b/src/scratch/target.cpp @@ -7,10 +7,14 @@ #include #include +#include + #include "target_p.h" using namespace libscratchcpp; +static const std::unordered_set RESERVED_NAMES = { "_mouse_", "_stage_", "_edge_", "_myself_", "_random_" }; + /*! Constructs target. */ Target::Target() : impl(spimpl::make_unique_impl()) @@ -23,10 +27,14 @@ const std::string &Target::name() const return impl->name; } -/*! Sets the name of the target. */ +/*! + * Sets the name of the target. + * \note Setting the name to one of the reserved names (_mouse_, _stage_, _edge_, _myself_, _random_) won't do anything. + */ void Target::setName(const std::string &name) { - impl->name = name; + if (RESERVED_NAMES.find(name) == RESERVED_NAMES.cend()) + impl->name = name; } /*! Returns the list of variables. */ diff --git a/test/scratch_classes/target_test.cpp b/test/scratch_classes/target_test.cpp index e3f54f92..9feabeb6 100644 --- a/test/scratch_classes/target_test.cpp +++ b/test/scratch_classes/target_test.cpp @@ -29,6 +29,24 @@ TEST(TargetTest, Name) Target target; target.setName("Test"); ASSERT_EQ(target.name(), "Test"); + + target.setName("_mouse_"); + ASSERT_EQ(target.name(), "Test"); + + target.setName("_stage_"); + ASSERT_EQ(target.name(), "Test"); + + target.setName("_edge_"); + ASSERT_EQ(target.name(), "Test"); + + target.setName("_myself_"); + ASSERT_EQ(target.name(), "Test"); + + target.setName("_random_"); + ASSERT_EQ(target.name(), "Test"); + + target.setName("Sprite1"); + ASSERT_EQ(target.name(), "Sprite1"); } TEST(TargetTest, Variables)