Skip to content

Commit 6d81d7c

Browse files
authored
Merge pull request #138 from scratchcpp/target_test
Complete Target test
2 parents a573ce6 + fe76032 commit 6d81d7c

File tree

3 files changed

+232
-64
lines changed

3 files changed

+232
-64
lines changed

include/scratchcpp/target.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class LIBSCRATCHCPP_EXPORT Target
5353

5454
const std::vector<Costume> &costumes() const;
5555
int addCostume(const Costume &costume);
56-
Costume costumeAt(int index) const;
56+
const Costume &costumeAt(int index) const;
5757
int findCostume(const std::string &costumeName) const;
5858

5959
const std::vector<Sound> &sounds() const;
6060
int addSound(const Sound &sound);
61-
Sound soundAt(int index) const;
61+
const Sound &soundAt(int index) const;
6262
int findSound(const std::string &soundName) const;
6363

6464
int layerOrder() const;

src/scratch/target.cpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@ const std::vector<std::shared_ptr<Variable>> &Target::variables() const
3636
/*! Adds a variable and returns its index. */
3737
int Target::addVariable(std::shared_ptr<Variable> variable)
3838
{
39+
auto it = std::find(impl->variables.begin(), impl->variables.end(), variable);
40+
41+
if (it != impl->variables.end())
42+
return it - impl->variables.begin();
43+
3944
impl->variables.push_back(variable);
4045
return impl->variables.size() - 1;
4146
}
4247

4348
/*! Returns the variable at index. */
4449
std::shared_ptr<Variable> Target::variableAt(int index) const
4550
{
51+
if (index < 0 || index >= impl->variables.size())
52+
return nullptr;
53+
4654
return impl->variables[index];
4755
}
4856

@@ -79,13 +87,21 @@ const std::vector<std::shared_ptr<List>> &Target::lists() const
7987
/*! Adds a list and returns its index. */
8088
int Target::addList(std::shared_ptr<List> list)
8189
{
90+
auto it = std::find(impl->lists.begin(), impl->lists.end(), list);
91+
92+
if (it != impl->lists.end())
93+
return it - impl->lists.begin();
94+
8295
impl->lists.push_back(list);
8396
return impl->lists.size() - 1;
8497
}
8598

8699
/*! Returns the list at index. */
87100
std::shared_ptr<List> Target::listAt(int index) const
88101
{
102+
if (index < 0 || index >= impl->lists.size())
103+
return nullptr;
104+
89105
return impl->lists[index];
90106
}
91107

@@ -122,13 +138,21 @@ const std::vector<std::shared_ptr<Block>> &Target::blocks() const
122138
/*! Adds a block and returns its index. */
123139
int Target::addBlock(std::shared_ptr<Block> block)
124140
{
141+
auto it = std::find(impl->blocks.begin(), impl->blocks.end(), block);
142+
143+
if (it != impl->blocks.end())
144+
return it - impl->blocks.begin();
145+
125146
impl->blocks.push_back(block);
126147
return impl->blocks.size() - 1;
127148
}
128149

129150
/*! Returns the block at index. */
130151
std::shared_ptr<Block> Target::blockAt(int index) const
131152
{
153+
if (index < 0 || index >= impl->blocks.size())
154+
return nullptr;
155+
132156
return impl->blocks[index];
133157
}
134158

@@ -177,13 +201,29 @@ const std::vector<Costume> &Target::costumes() const
177201
/*! Adds a costume and returns its index. */
178202
int Target::addCostume(const Costume &costume)
179203
{
204+
// TODO: Use shared_ptr for assets
205+
/*auto it = std::find(impl->costumes.begin(), impl->costumes.end(), costume);
206+
207+
if (it != impl->blocks.end())
208+
return it - impl->blocks.begin();*/
209+
210+
int i = 0;
211+
212+
for (const auto &c : impl->costumes) {
213+
if (c.name() == costume.name())
214+
return i;
215+
216+
i++;
217+
}
218+
180219
impl->costumes.push_back(costume);
181220
return impl->costumes.size() - 1;
182221
}
183222

184223
/*! Returns the costume at index. */
185-
Costume Target::costumeAt(int index) const
224+
const libscratchcpp::Costume &Target::costumeAt(int index) const
186225
{
226+
// TODO: Add range check
187227
return impl->costumes[index];
188228
}
189229

@@ -208,13 +248,28 @@ const std::vector<Sound> &Target::sounds() const
208248
/*! Adds a sound and returns its index. */
209249
int Target::addSound(const Sound &sound)
210250
{
251+
// TODO: Use shared_ptr for assets
252+
/*auto it = std::find(impl->sounds.begin(), impl->sounds.end(), sound);
253+
254+
if (it != impl->sounds.end())
255+
return it - impl->sounds.begin();*/
256+
257+
int i = 0;
258+
259+
for (const auto &s : impl->sounds) {
260+
if (s.name() == sound.name())
261+
return i;
262+
263+
i++;
264+
}
211265
impl->sounds.push_back(sound);
212266
return impl->sounds.size() - 1;
213267
}
214268

215269
/*! Returns the sound at index. */
216-
Sound Target::soundAt(int index) const
270+
const libscratchcpp::Sound &Target::soundAt(int index) const
217271
{
272+
// TODO: Add range check
218273
return impl->sounds[index];
219274
}
220275

Lines changed: 173 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,196 @@
1+
#include <scratchcpp/target.h>
12
#include <scratchcpp/variable.h>
23
#include <scratchcpp/list.h>
3-
#include <scratchcpp/stage.h>
4+
#include <scratchcpp/block.h>
45
#include <scratchcpp/costume.h>
56
#include <scratchcpp/sound.h>
67

78
#include "../common.h"
89

910
using namespace libscratchcpp;
1011

11-
TEST(TargetTest, FindCostume)
12+
TEST(TargetTest, IsStage)
1213
{
13-
Stage stage;
14-
Costume c1("costume1", "", "svg");
15-
Costume c2("costume2", "", "png");
16-
Costume c3("costume3", "", "svg");
17-
stage.addCostume(c1);
18-
stage.addCostume(c2);
19-
stage.addCostume(c3);
20-
21-
ASSERT_EQ(stage.findCostume("invalid"), -1);
22-
ASSERT_EQ(stage.findCostume("costume1"), 0);
23-
ASSERT_EQ(stage.findCostume("costume2"), 1);
24-
ASSERT_EQ(stage.findCostume("costume3"), 2);
14+
Target target;
15+
ASSERT_FALSE(target.isStage());
2516
}
2617

27-
TEST(TargetTest, FindSound)
18+
TEST(TargetTest, Name)
2819
{
29-
Stage stage;
30-
Sound s1("sound1", "", "svg");
31-
Sound s2("sound2", "", "png");
32-
Sound s3("sound3", "", "svg");
33-
stage.addSound(s1);
34-
stage.addSound(s2);
35-
stage.addSound(s3);
36-
37-
ASSERT_EQ(stage.findSound("invalid"), -1);
38-
ASSERT_EQ(stage.findSound("sound1"), 0);
39-
ASSERT_EQ(stage.findSound("sound2"), 1);
40-
ASSERT_EQ(stage.findSound("sound3"), 2);
20+
Target target;
21+
target.setName("Test");
22+
ASSERT_EQ(target.name(), "Test");
4123
}
4224

43-
TEST(TargetTest, FindVariable)
25+
TEST(TargetTest, Variables)
4426
{
45-
Stage stage;
46-
auto v1 = std::make_shared<Variable>("a", "variable1");
47-
auto v2 = std::make_shared<Variable>("b", "variable2");
48-
auto v3 = std::make_shared<Variable>("c", "variable3");
49-
stage.addVariable(v1);
50-
stage.addVariable(v2);
51-
stage.addVariable(v3);
52-
53-
ASSERT_EQ(stage.findVariable("invalid"), -1);
54-
ASSERT_EQ(stage.findVariable("variable1"), 0);
55-
ASSERT_EQ(stage.findVariable("variable2"), 1);
56-
ASSERT_EQ(stage.findVariable("variable3"), 2);
57-
58-
ASSERT_EQ(stage.findVariableById("d"), -1);
59-
ASSERT_EQ(stage.findVariableById("a"), 0);
60-
ASSERT_EQ(stage.findVariableById("b"), 1);
61-
ASSERT_EQ(stage.findVariableById("c"), 2);
27+
auto v1 = std::make_shared<Variable>("a", "var1");
28+
auto v2 = std::make_shared<Variable>("b", "var2");
29+
auto v3 = std::make_shared<Variable>("c", "var3");
30+
31+
Target target;
32+
ASSERT_EQ(target.addVariable(v1), 0);
33+
ASSERT_EQ(target.addVariable(v2), 1);
34+
ASSERT_EQ(target.addVariable(v3), 2);
35+
ASSERT_EQ(target.addVariable(v2), 1); // add existing variable
36+
37+
ASSERT_EQ(target.variables(), std::vector<std::shared_ptr<Variable>>({ v1, v2, v3 }));
38+
ASSERT_EQ(target.variableAt(0), v1);
39+
ASSERT_EQ(target.variableAt(1), v2);
40+
ASSERT_EQ(target.variableAt(2), v3);
41+
ASSERT_EQ(target.variableAt(3), nullptr);
42+
ASSERT_EQ(target.variableAt(-1), nullptr);
43+
44+
ASSERT_EQ(target.findVariable("invalid"), -1);
45+
ASSERT_EQ(target.findVariable("var1"), 0);
46+
ASSERT_EQ(target.findVariable("var2"), 1);
47+
ASSERT_EQ(target.findVariable("var3"), 2);
48+
49+
ASSERT_EQ(target.findVariableById("d"), -1);
50+
ASSERT_EQ(target.findVariableById("a"), 0);
51+
ASSERT_EQ(target.findVariableById("b"), 1);
52+
ASSERT_EQ(target.findVariableById("c"), 2);
6253
}
6354

64-
TEST(TargetTest, FindList)
55+
TEST(TargetTest, Lists)
6556
{
66-
Stage stage;
6757
auto l1 = std::make_shared<List>("a", "list1");
6858
auto l2 = std::make_shared<List>("b", "list2");
6959
auto l3 = std::make_shared<List>("c", "list3");
70-
stage.addList(l1);
71-
stage.addList(l2);
72-
stage.addList(l3);
73-
74-
ASSERT_EQ(stage.findList("invalid"), -1);
75-
ASSERT_EQ(stage.findList("list1"), 0);
76-
ASSERT_EQ(stage.findList("list2"), 1);
77-
ASSERT_EQ(stage.findList("list3"), 2);
78-
79-
ASSERT_EQ(stage.findListById("d"), -1);
80-
ASSERT_EQ(stage.findListById("a"), 0);
81-
ASSERT_EQ(stage.findListById("b"), 1);
82-
ASSERT_EQ(stage.findListById("c"), 2);
60+
61+
Target target;
62+
ASSERT_EQ(target.addList(l1), 0);
63+
ASSERT_EQ(target.addList(l2), 1);
64+
ASSERT_EQ(target.addList(l3), 2);
65+
ASSERT_EQ(target.addList(l2), 1); // add existing list
66+
67+
ASSERT_EQ(target.lists(), std::vector<std::shared_ptr<List>>({ l1, l2, l3 }));
68+
ASSERT_EQ(target.listAt(0), l1);
69+
ASSERT_EQ(target.listAt(1), l2);
70+
ASSERT_EQ(target.listAt(2), l3);
71+
ASSERT_EQ(target.listAt(3), nullptr);
72+
ASSERT_EQ(target.listAt(-1), nullptr);
73+
74+
ASSERT_EQ(target.findList("invalid"), -1);
75+
ASSERT_EQ(target.findList("list1"), 0);
76+
ASSERT_EQ(target.findList("list2"), 1);
77+
ASSERT_EQ(target.findList("list3"), 2);
78+
79+
ASSERT_EQ(target.findListById("d"), -1);
80+
ASSERT_EQ(target.findListById("a"), 0);
81+
ASSERT_EQ(target.findListById("b"), 1);
82+
ASSERT_EQ(target.findListById("c"), 2);
83+
}
84+
85+
TEST(TargetTest, Blocks)
86+
{
87+
auto b1 = std::make_shared<Block>("a", "event_whenflagclicked");
88+
auto b2 = std::make_shared<Block>("b", "motion_gotoxy");
89+
auto b3 = std::make_shared<Block>("c", "motion_ifonedgebounce");
90+
auto b4 = std::make_shared<Block>("d", "event_whenflagclicked");
91+
92+
Target target;
93+
ASSERT_EQ(target.addBlock(b1), 0);
94+
ASSERT_EQ(target.addBlock(b2), 1);
95+
ASSERT_EQ(target.addBlock(b3), 2);
96+
ASSERT_EQ(target.addBlock(b4), 3);
97+
ASSERT_EQ(target.addBlock(b2), 1); // add existing block
98+
99+
ASSERT_EQ(target.blocks(), std::vector<std::shared_ptr<Block>>({ b1, b2, b3, b4 }));
100+
ASSERT_EQ(target.blockAt(0), b1);
101+
ASSERT_EQ(target.blockAt(1), b2);
102+
ASSERT_EQ(target.blockAt(2), b3);
103+
ASSERT_EQ(target.blockAt(3), b4);
104+
ASSERT_EQ(target.blockAt(4), nullptr);
105+
ASSERT_EQ(target.blockAt(-1), nullptr);
106+
107+
ASSERT_EQ(target.findBlock("e"), -1);
108+
ASSERT_EQ(target.findBlock("a"), 0);
109+
ASSERT_EQ(target.findBlock("b"), 1);
110+
ASSERT_EQ(target.findBlock("c"), 2);
111+
ASSERT_EQ(target.findBlock("d"), 3);
112+
113+
ASSERT_EQ(target.greenFlagBlocks(), std::vector<std::shared_ptr<Block>>({ b1, b4 }));
114+
}
115+
116+
TEST(TargetTest, CurrentCostume)
117+
{
118+
Target target;
119+
ASSERT_EQ(target.currentCostume(), 1);
120+
target.setCurrentCostume(5);
121+
ASSERT_EQ(target.currentCostume(), 5);
122+
}
123+
124+
TEST(TargetTest, Costumes)
125+
{
126+
Costume c1("costume1", "", "svg");
127+
Costume c2("costume2", "", "png");
128+
Costume c3("costume3", "", "svg");
129+
130+
Target target;
131+
ASSERT_EQ(target.addCostume(c1), 0);
132+
ASSERT_EQ(target.addCostume(c2), 1);
133+
ASSERT_EQ(target.addCostume(c3), 2);
134+
ASSERT_EQ(target.addCostume(c2), 1); // add existing costume
135+
136+
// TODO: Use shared_ptr for assets
137+
// ASSERT_EQ(target.costumes(), std::vector<std::shared_ptr<Costume>>({ c1, c2, c3 }));
138+
ASSERT_EQ(target.costumes().size(), 3);
139+
ASSERT_EQ(target.costumes()[0].name(), c1.name());
140+
ASSERT_EQ(target.costumes()[1].name(), c2.name());
141+
ASSERT_EQ(target.costumes()[2].name(), c3.name());
142+
143+
ASSERT_EQ(target.costumeAt(0).name(), c1.name());
144+
ASSERT_EQ(target.costumeAt(1).name(), c2.name());
145+
ASSERT_EQ(target.costumeAt(2).name(), c3.name());
146+
147+
ASSERT_EQ(target.findCostume("invalid"), -1);
148+
ASSERT_EQ(target.findCostume("costume1"), 0);
149+
ASSERT_EQ(target.findCostume("costume2"), 1);
150+
ASSERT_EQ(target.findCostume("costume3"), 2);
151+
}
152+
153+
TEST(TargetTest, Sounds)
154+
{
155+
Sound s1("sound1", "", "mp3");
156+
Sound s2("sound2", "", "wav");
157+
Sound s3("sound3", "", "mp3");
158+
159+
Target target;
160+
ASSERT_EQ(target.addSound(s1), 0);
161+
ASSERT_EQ(target.addSound(s2), 1);
162+
ASSERT_EQ(target.addSound(s3), 2);
163+
ASSERT_EQ(target.addSound(s2), 1); // add existing Sound
164+
165+
// TODO: Use shared_ptr for assets
166+
// ASSERT_EQ(target.sounds(), std::vector<std::shared_ptr<Sound>>({ s1, s2, s3 }));
167+
ASSERT_EQ(target.sounds().size(), 3);
168+
ASSERT_EQ(target.sounds()[0].name(), s1.name());
169+
ASSERT_EQ(target.sounds()[1].name(), s2.name());
170+
ASSERT_EQ(target.sounds()[2].name(), s3.name());
171+
172+
ASSERT_EQ(target.soundAt(0).name(), s1.name());
173+
ASSERT_EQ(target.soundAt(1).name(), s2.name());
174+
ASSERT_EQ(target.soundAt(2).name(), s3.name());
175+
176+
ASSERT_EQ(target.findSound("invalid"), -1);
177+
ASSERT_EQ(target.findSound("sound1"), 0);
178+
ASSERT_EQ(target.findSound("sound2"), 1);
179+
ASSERT_EQ(target.findSound("sound3"), 2);
180+
}
181+
182+
TEST(TargetTest, LayerOrder)
183+
{
184+
Target target;
185+
ASSERT_EQ(target.layerOrder(), 0);
186+
target.setLayerOrder(2);
187+
ASSERT_EQ(target.layerOrder(), 2);
188+
}
189+
190+
TEST(TargetTest, Volume)
191+
{
192+
Target target;
193+
ASSERT_EQ(target.volume(), 100);
194+
target.setVolume(50);
195+
ASSERT_EQ(target.volume(), 50);
83196
}

0 commit comments

Comments
 (0)