Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/scratch/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
#include <scratchcpp/comment.h>
#include <scratchcpp/iengine.h>

#include <unordered_set>

#include "target_p.h"

using namespace libscratchcpp;

static const std::unordered_set<std::string> RESERVED_NAMES = { "_mouse_", "_stage_", "_edge_", "_myself_", "_random_" };

/*! Constructs target. */
Target::Target() :
impl(spimpl::make_unique_impl<TargetPrivate>())
Expand All @@ -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. */
Expand Down
18 changes: 18 additions & 0 deletions test/scratch_classes/target_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down