Skip to content

Commit 258d028

Browse files
committed
ScriptBuilder: Add addEntityField() method
1 parent 1f9dd99 commit 258d028

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

include/scratchcpp/dev/test/scriptbuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace libscratchcpp
1313
class IExtension;
1414
class IEngine;
1515
class Target;
16+
class Entity;
1617
class List;
1718

1819
} // namespace libscratchcpp
@@ -44,6 +45,8 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
4445
void addDropdownInput(const std::string &name, const std::string &selectedValue);
4546
void addDropdownField(const std::string &name, const std::string &selectedValue);
4647

48+
void addEntityField(const std::string &name, std::shared_ptr<Entity> entity);
49+
4750
std::shared_ptr<Block> currentBlock();
4851

4952
void build();

src/dev/test/scriptbuilder.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ void ScriptBuilder::addDropdownField(const std::string &name, const std::string
152152
impl->lastBlock->addField(field);
153153
}
154154

155+
/*! Adds a field pointing to an entity (variable, list, broadcast, etc.) */
156+
void ScriptBuilder::addEntityField(const std::string &name, std::shared_ptr<Entity> entity)
157+
{
158+
if (!impl->lastBlock)
159+
return;
160+
161+
entity->setId(std::to_string(impl->blockId++));
162+
auto field = std::make_shared<Field>(name, Value(), entity);
163+
impl->lastBlock->addField(field);
164+
}
165+
155166
/*!
156167
* Returns the current block (can be used e. g. with a custom Compiler instance).\n
157168
* The script is automatically built to set the compile function of the block.
@@ -199,8 +210,6 @@ void ScriptBuilder::addBlock(std::shared_ptr<Block> block)
199210

200211
void ScriptBuilder::build(std::shared_ptr<Target> target)
201212
{
202-
impl->engine->clear();
203-
204213
if (target->blocks().empty()) {
205214
for (auto block : impl->blocks)
206215
target->addBlock(block);

test/dev/test_api/scriptbuilder_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/input.h>
88
#include <scratchcpp/field.h>
99
#include <scratchcpp/list.h>
10+
#include <scratchcpp/broadcast.h>
1011

1112
#include "../../common.h"
1213
#include "testextension.h"
@@ -161,6 +162,22 @@ TEST_F(ScriptBuilderTest, AddDropdownField)
161162
ASSERT_EQ(testing::internal::GetCapturedStdout(), "hello\n");
162163
}
163164

165+
TEST_F(ScriptBuilderTest, AddEntityField)
166+
{
167+
auto broadcast = std::make_shared<Broadcast>("", "");
168+
m_engine->setBroadcasts({ broadcast });
169+
170+
m_builder->addBlock("test_print_field");
171+
m_builder->addEntityField("STRING", broadcast);
172+
auto block = m_builder->currentBlock();
173+
ASSERT_TRUE(block);
174+
ASSERT_EQ(block->opcode(), "test_print_field");
175+
ASSERT_TRUE(block->inputs().empty());
176+
ASSERT_EQ(block->fields().size(), 1);
177+
ASSERT_EQ(block->fieldAt(0)->name(), "STRING");
178+
ASSERT_EQ(block->fieldAt(0)->valuePtr(), broadcast);
179+
}
180+
164181
TEST_F(ScriptBuilderTest, ReporterBlocks)
165182
{
166183
m_builder->addReporterBlock("test_teststr");

0 commit comments

Comments
 (0)