Skip to content

Commit 56fc4e4

Browse files
authored
Merge pull request #169 from scratchcpp/block_test
Add Block test
2 parents 3f29771 + 706665a commit 56fc4e4

File tree

6 files changed

+304
-24
lines changed

6 files changed

+304
-24
lines changed

include/scratchcpp/block.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
5353
void setShadow(bool newShadow);
5454

5555
bool topLevel() const;
56-
void setTopLevel(bool newTopLevel);
5756

5857
void setEngine(IEngine *newEngine);
58+
IEngine *engine() const;
5959

6060
void setTarget(Target *newTarget);
61+
Target *target() const;
6162

6263
BlockComp compileFunction() const;
6364
void setCompileFunction(BlockComp newCompileFunction);

src/internal/scratch3reader.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ bool Scratch3Reader::load()
177177
READER_STEP(step, "target -> block -> shadow");
178178
block->setShadow(blockInfo["shadow"]);
179179

180-
// topLevel
181-
READER_STEP(step, "target -> block -> topLevel");
182-
block->setTopLevel(blockInfo["topLevel"]);
183-
184180
target->addBlock(block);
185181
}
186182

src/scratch/block.cpp

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Block::Block(const std::string &id, const std::string &opcode) :
1818
/*! Calls the compile function. */
1919
void Block::compile(Compiler *compiler)
2020
{
21-
return impl->compileFunction(compiler);
21+
if (impl->compileFunction)
22+
return impl->compileFunction(compiler);
2223
}
2324

2425
/*! Returns the opcode. */
@@ -66,16 +67,18 @@ std::shared_ptr<Block> Block::next() const
6667
/*! Returns the ID of the next block. */
6768
std::string Block::nextId() const
6869
{
69-
if (impl->next)
70-
return impl->next->id();
71-
else
72-
return impl->nextId;
70+
return impl->nextId;
7371
}
7472

7573
/*! Sets the next block. */
7674
void Block::setNext(std::shared_ptr<Block> block)
7775
{
7876
impl->next = block;
77+
78+
if (block)
79+
impl->nextId = block->id();
80+
else
81+
impl->nextId = "";
7982
}
8083

8184
/*! Sets the next block by ID. */
@@ -94,16 +97,18 @@ std::shared_ptr<Block> Block::parent() const
9497
/*! Returns the ID of the parent block. */
9598
std::string Block::parentId() const
9699
{
97-
if (impl->parent)
98-
return impl->parent->id();
99-
else
100-
return impl->parentId;
100+
return impl->parentId;
101101
}
102102

103103
/*! Sets the parent block. */
104104
void Block::setParent(std::shared_ptr<Block> block)
105105
{
106106
impl->parent = block;
107+
108+
if (block)
109+
impl->parentId = block->id();
110+
else
111+
impl->parentId = "";
107112
}
108113

109114
/*! Sets the parent block by ID. */
@@ -122,13 +127,21 @@ std::vector<std::shared_ptr<Input>> Block::inputs() const
122127
/*! Adds an input and returns its index. */
123128
int Block::addInput(std::shared_ptr<Input> input)
124129
{
130+
auto it = std::find(impl->inputs.begin(), impl->inputs.end(), input);
131+
132+
if (it != impl->inputs.end())
133+
return it - impl->inputs.begin();
134+
125135
impl->inputs.push_back(input);
126136
return impl->inputs.size() - 1;
127137
}
128138

129139
/*! Returns the input at index. */
130140
std::shared_ptr<Input> Block::inputAt(int index) const
131141
{
142+
if (index < 0 || index >= impl->inputs.size())
143+
return nullptr;
144+
132145
return impl->inputs[index];
133146
}
134147

@@ -169,13 +182,21 @@ std::vector<std::shared_ptr<Field>> Block::fields() const
169182
/*! Adds a field and returns its index. */
170183
int Block::addField(std::shared_ptr<Field> field)
171184
{
185+
auto it = std::find(impl->fields.begin(), impl->fields.end(), field);
186+
187+
if (it != impl->fields.end())
188+
return it - impl->fields.begin();
189+
172190
impl->fields.push_back(field);
173191
return impl->fields.size() - 1;
174192
}
175193

176194
/*! Returns the field at index. */
177195
std::shared_ptr<Field> Block::fieldAt(int index) const
178196
{
197+
if (index < 0 || index >= impl->fields.size())
198+
return nullptr;
199+
179200
return impl->fields[index];
180201
}
181202

@@ -222,15 +243,7 @@ void Block::setShadow(bool newShadow)
222243
/*! Returns true if this is a top level block. */
223244
bool Block::topLevel() const
224245
{
225-
// TODO: Return true if parentId() == ""
226-
// and remove the setter
227-
return impl->topLevel;
228-
}
229-
230-
/*! Toggles whether this block is a top level block. */
231-
void Block::setTopLevel(bool newTopLevel)
232-
{
233-
impl->topLevel = newTopLevel;
246+
return (impl->parentId == "" && !impl->parent);
234247
}
235248

236249
/*! Sets the Engine. */
@@ -239,8 +252,20 @@ void Block::setEngine(IEngine *newEngine)
239252
impl->engine = newEngine;
240253
}
241254

255+
/*! Returns the Engine. */
256+
IEngine *Block::engine() const
257+
{
258+
return impl->engine;
259+
}
260+
242261
/*! Sets the Target. */
243262
void Block::setTarget(Target *newTarget)
244263
{
245264
impl->target = newTarget;
246265
}
266+
267+
/*! Returns the Target. */
268+
Target *Block::target() const
269+
{
270+
return impl->target;
271+
}

src/scratch/block_p.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ struct BlockPrivate
2929
std::vector<std::shared_ptr<Field>> fields;
3030
std::unordered_map<int, Field *> fieldMap;
3131
bool shadow = false;
32-
bool topLevel = false;
3332
IEngine *engine = nullptr;
3433
Target *target = nullptr;
3534
BlockPrototype mutationPrototype;

test/scratch_classes/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,19 @@ target_link_libraries(
153153
)
154154

155155
gtest_discover_tests(field_test)
156+
157+
# block_test
158+
add_executable(
159+
block_test
160+
block_test.cpp
161+
)
162+
163+
target_link_libraries(
164+
block_test
165+
GTest::gtest_main
166+
GTest::gmock_main
167+
scratchcpp
168+
scratchcpp_mocks
169+
)
170+
171+
gtest_discover_tests(block_test)

0 commit comments

Comments
 (0)