Skip to content

Commit 9796e68

Browse files
committed
Add clone method to Variable
1 parent 94ccf7e commit 9796e68

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

include/scratchcpp/variable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class LIBSCRATCHCPP_EXPORT Variable : public Entity
2929
bool isCloudVariable() const;
3030
void setIsCloudVariable(bool isCloudVariable);
3131

32+
std::shared_ptr<Variable> clone();
33+
3234
private:
3335
spimpl::unique_impl_ptr<VariablePrivate> impl;
3436
};

src/scratch/sprite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ std::shared_ptr<Sprite> Sprite::clone()
5757
const auto &vars = variables();
5858

5959
for (auto var : vars)
60-
clone->addVariable(std::make_shared<Variable>(var->id(), var->name(), var->value()));
60+
clone->addVariable(var->clone());
6161

6262
const auto &l = lists();
6363

src/scratch/variable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ void Variable::setIsCloudVariable(bool isCloudVariable)
4848
{
4949
impl->isCloudVariable = isCloudVariable;
5050
}
51+
52+
/*! Creates a copy of the variable. */
53+
std::shared_ptr<Variable> Variable::clone()
54+
{
55+
return std::make_shared<Variable>(id(), impl->name, impl->value, impl->isCloudVariable);
56+
}

test/scratch_classes/variable_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,23 @@ TEST(VariableTest, IsCloudVariable)
5656
var.setIsCloudVariable(true);
5757
ASSERT_TRUE(var.isCloudVariable());
5858
}
59+
60+
TEST(VariableTest, Clone)
61+
{
62+
std::shared_ptr<Variable> clone;
63+
std::vector<std::shared_ptr<Variable>> vars;
64+
65+
vars.push_back(std::make_shared<Variable>("abc", "var1"));
66+
vars.push_back(std::make_shared<Variable>("abc", "var2", "test"));
67+
vars.push_back(std::make_shared<Variable>("abc", "var3", "test", true));
68+
vars.push_back(std::make_shared<Variable>("abc", "var4", "test", false));
69+
70+
for (auto var : vars) {
71+
clone = var->clone();
72+
ASSERT_TRUE(clone);
73+
ASSERT_EQ(clone->id(), var->id());
74+
ASSERT_EQ(clone->name(), var->name());
75+
ASSERT_EQ(clone->value(), var->value());
76+
ASSERT_EQ(clone->isCloudVariable(), var->isCloudVariable());
77+
}
78+
}

0 commit comments

Comments
 (0)