Skip to content

Commit c1004e9

Browse files
authored
Merge pull request #414 from scratchcpp/reserved_target_names
Add reserved target names
2 parents c7501c7 + ea9ec3c commit c1004e9

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/scratch/target.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
#include <scratchcpp/comment.h>
88
#include <scratchcpp/iengine.h>
99

10+
#include <unordered_set>
11+
1012
#include "target_p.h"
1113

1214
using namespace libscratchcpp;
1315

16+
static const std::unordered_set<std::string> RESERVED_NAMES = { "_mouse_", "_stage_", "_edge_", "_myself_", "_random_" };
17+
1418
/*! Constructs target. */
1519
Target::Target() :
1620
impl(spimpl::make_unique_impl<TargetPrivate>())
@@ -23,10 +27,14 @@ const std::string &Target::name() const
2327
return impl->name;
2428
}
2529

26-
/*! Sets the name of the target. */
30+
/*!
31+
* Sets the name of the target.
32+
* \note Setting the name to one of the reserved names (_mouse_, _stage_, _edge_, _myself_, _random_) won't do anything.
33+
*/
2734
void Target::setName(const std::string &name)
2835
{
29-
impl->name = name;
36+
if (RESERVED_NAMES.find(name) == RESERVED_NAMES.cend())
37+
impl->name = name;
3038
}
3139

3240
/*! Returns the list of variables. */

test/scratch_classes/target_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ TEST(TargetTest, Name)
2929
Target target;
3030
target.setName("Test");
3131
ASSERT_EQ(target.name(), "Test");
32+
33+
target.setName("_mouse_");
34+
ASSERT_EQ(target.name(), "Test");
35+
36+
target.setName("_stage_");
37+
ASSERT_EQ(target.name(), "Test");
38+
39+
target.setName("_edge_");
40+
ASSERT_EQ(target.name(), "Test");
41+
42+
target.setName("_myself_");
43+
ASSERT_EQ(target.name(), "Test");
44+
45+
target.setName("_random_");
46+
ASSERT_EQ(target.name(), "Test");
47+
48+
target.setName("Sprite1");
49+
ASSERT_EQ(target.name(), "Sprite1");
3250
}
3351

3452
TEST(TargetTest, Variables)

0 commit comments

Comments
 (0)