Skip to content

Commit 33f519c

Browse files
committed
ScriptBuilder: Refactor reporter blocks
1 parent 3380e7c commit 33f519c

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

include/scratchcpp/dev/test/scriptbuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
3131
~ScriptBuilder();
3232

3333
void addBlock(const std::string &opcode);
34-
void addReporterBlock(const std::string &opcode);
3534
void captureBlockReturnValue();
3635

3736
void addValueInput(const std::string &name, const Value &value);
@@ -47,6 +46,7 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
4746
void addEntityField(const std::string &name, std::shared_ptr<Entity> entity);
4847

4948
std::shared_ptr<Block> currentBlock();
49+
std::shared_ptr<Block> takeBlock();
5050

5151
void build();
5252
void run();

src/dev/test/scriptbuilder.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,13 @@ void ScriptBuilder::addBlock(const std::string &opcode)
5959
addBlock(impl->lastBlock);
6060
}
6161

62-
/*! Creates a reporter block with the given opcode to be used with captureBlockReturnValue() later. */
63-
void ScriptBuilder::addReporterBlock(const std::string &opcode)
64-
{
65-
impl->lastBlock = std::make_shared<Block>(std::to_string(impl->blockId++), opcode);
66-
}
67-
6862
/*! Captures the return value of the created reporter block. It can be retrieved using capturedValues() later. */
6963
void ScriptBuilder::captureBlockReturnValue()
7064
{
7165
if (!impl->lastBlock)
7266
return;
7367

74-
auto valueBlock = impl->lastBlock;
68+
auto valueBlock = takeBlock();
7569
addBlock("script_builder_capture");
7670
addObscuredInput("VALUE", valueBlock);
7771
}
@@ -104,6 +98,7 @@ void ScriptBuilder::addObscuredInput(const std::string &name, std::shared_ptr<Bl
10498
return;
10599

106100
auto block = valueBlock;
101+
block->setParent(impl->lastBlock);
107102

108103
while (block) {
109104
block->setId(std::to_string(impl->blockId++));
@@ -112,7 +107,7 @@ void ScriptBuilder::addObscuredInput(const std::string &name, std::shared_ptr<Bl
112107
auto parent = block->parent();
113108
auto next = block->next();
114109

115-
if (parent)
110+
if (parent && block != valueBlock)
116111
parent->setNext(block);
117112

118113
if (next)
@@ -228,6 +223,24 @@ std::shared_ptr<Block> ScriptBuilder::currentBlock()
228223
return impl->lastBlock;
229224
}
230225

226+
/*! Removes the current block from the script and returns it. Can be used in inputs later. */
227+
std::shared_ptr<Block> ScriptBuilder::takeBlock()
228+
{
229+
if (!impl->lastBlock)
230+
return nullptr;
231+
232+
auto block = impl->lastBlock;
233+
impl->blocks.pop_back();
234+
235+
if (!impl->blocks.empty())
236+
impl->blocks.back()->setNext(nullptr);
237+
238+
block->setParent(nullptr);
239+
block->setNext(nullptr);
240+
241+
return block;
242+
}
243+
231244
/*! Builds and compiles the script. */
232245
void ScriptBuilder::build()
233246
{

test/dev/test_api/scriptbuilder_test.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ TEST_F(ScriptBuilderTest, AddObscuredInputMultipleBlocks)
129129
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\n");
130130
}
131131

132+
TEST_F(ScriptBuilderTest, AdvancedObscuredInput)
133+
{
134+
for (int i = 1; i <= 3; i++) {
135+
m_builder->addBlock("test_input");
136+
m_builder->addValueInput("INPUT", i);
137+
auto valueBlock = m_builder->takeBlock();
138+
139+
m_builder->addBlock("test_input");
140+
m_builder->addObscuredInput("INPUT", valueBlock);
141+
valueBlock = m_builder->takeBlock();
142+
143+
m_builder->addBlock("test_print");
144+
m_builder->addObscuredInput("STRING", valueBlock);
145+
}
146+
147+
m_builder->build();
148+
149+
testing::internal::CaptureStdout();
150+
m_builder->run();
151+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "1\n2\n3\n");
152+
}
153+
132154
TEST_F(ScriptBuilderTest, AddNullObscuredInput)
133155
{
134156
m_builder->addBlock("test_print");
@@ -221,15 +243,15 @@ TEST_F(ScriptBuilderTest, AddEntityField)
221243
m_builder->build();
222244
}
223245

224-
TEST_F(ScriptBuilderTest, ReporterBlocks)
246+
TEST_F(ScriptBuilderTest, CaptureBlockReturnValue)
225247
{
226-
m_builder->addReporterBlock("test_teststr");
248+
m_builder->addBlock("test_teststr");
227249
auto block = m_builder->currentBlock();
228250
ASSERT_TRUE(block);
229251
ASSERT_EQ(block->opcode(), "test_teststr");
230252
m_builder->captureBlockReturnValue();
231253

232-
m_builder->addReporterBlock("test_input");
254+
m_builder->addBlock("test_input");
233255
m_builder->addValueInput("INPUT", -93.4);
234256
block = m_builder->currentBlock();
235257
ASSERT_TRUE(block);

0 commit comments

Comments
 (0)