Skip to content

Commit 078c4ab

Browse files
committed
Implement data_addtolist
1 parent d1a82e4 commit 078c4ab

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/dev/blocks/listblocks.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// SPDX-License-Identifier: Apache-2.0
22

3+
#include <scratchcpp/iengine.h>
4+
#include <scratchcpp/dev/compiler.h>
5+
#include <scratchcpp/dev/compilerconstant.h>
6+
#include <scratchcpp/field.h>
7+
#include <scratchcpp/list.h>
8+
39
#include "listblocks.h"
410

511
using namespace libscratchcpp;
@@ -16,4 +22,16 @@ std::string ListBlocks::description() const
1622

1723
void ListBlocks::registerBlocks(IEngine *engine)
1824
{
25+
engine->addCompileFunction(this, "data_addtolist", &compileAddToList);
26+
}
27+
28+
CompilerValue *ListBlocks::compileAddToList(Compiler *compiler)
29+
{
30+
auto list = compiler->field("LIST")->valuePtr();
31+
assert(list);
32+
33+
if (list)
34+
compiler->createListAppend(static_cast<List *>(list.get()), compiler->addInput("ITEM"));
35+
36+
return nullptr;
1937
}

src/dev/blocks/listblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class ListBlocks : public IExtension
1414
std::string description() const override;
1515

1616
void registerBlocks(IEngine *engine) override;
17+
18+
private:
19+
static CompilerValue *compileAddToList(Compiler *compiler);
1720
};
1821

1922
} // namespace libscratchcpp
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1+
#include <scratchcpp/project.h>
2+
#include <scratchcpp/sprite.h>
3+
#include <scratchcpp/list.h>
4+
#include <scratchcpp/dev/compiler.h>
5+
#include <scratchcpp/dev/test/scriptbuilder.h>
16
#include <enginemock.h>
27

38
#include "../common.h"
49
#include "dev/blocks/listblocks.h"
510

611
using namespace libscratchcpp;
12+
using namespace libscratchcpp::test;
713

814
class ListBlocksTest : public testing::Test
915
{
1016
public:
11-
void SetUp() override { m_extension = std::make_unique<ListBlocks>(); }
17+
void SetUp() override
18+
{
19+
m_extension = std::make_unique<ListBlocks>();
20+
m_engine = m_project.engine().get();
21+
m_extension->registerBlocks(m_engine);
22+
}
1223

1324
std::unique_ptr<IExtension> m_extension;
25+
Project m_project;
26+
IEngine *m_engine = nullptr;
1427
EngineMock m_engineMock;
1528
};
29+
30+
TEST_F(ListBlocksTest, AddToList)
31+
{
32+
auto target = std::make_shared<Sprite>();
33+
auto list1 = std::make_shared<List>("", "");
34+
target->addList(list1);
35+
auto list2 = std::make_shared<List>("", "");
36+
target->addList(list2);
37+
38+
ScriptBuilder builder(m_extension.get(), m_engine, target);
39+
40+
builder.addBlock("data_addtolist");
41+
builder.addValueInput("ITEM", "test");
42+
builder.addEntityField("LIST", list1);
43+
44+
builder.addBlock("data_addtolist");
45+
builder.addValueInput("ITEM", true);
46+
builder.addEntityField("LIST", list1);
47+
48+
builder.addBlock("data_addtolist");
49+
builder.addValueInput("ITEM", 123);
50+
builder.addEntityField("LIST", list2);
51+
52+
builder.addBlock("data_addtolist");
53+
builder.addValueInput("ITEM", "Hello world");
54+
builder.addEntityField("LIST", list2);
55+
56+
builder.build();
57+
58+
builder.run();
59+
ASSERT_EQ(list1->toString(), "test true");
60+
ASSERT_EQ(list2->toString(), "123 Hello world");
61+
}

0 commit comments

Comments
 (0)