Skip to content

Commit d08feb6

Browse files
committed
Implement data_lengthoflist
1 parent a82f3fa commit d08feb6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/dev/blocks/listblocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void ListBlocks::registerBlocks(IEngine *engine)
2929
engine->addCompileFunction(this, "data_replaceitemoflist", &compileReplaceItemOfList);
3030
engine->addCompileFunction(this, "data_itemoflist", &compileItemOfList);
3131
engine->addCompileFunction(this, "data_itemnumoflist", &compileItemNumOfList);
32+
engine->addCompileFunction(this, "data_lengthoflist", &compileLengthOfList);
3233
}
3334

3435
CompilerValue *ListBlocks::compileAddToList(Compiler *compiler)
@@ -184,3 +185,14 @@ CompilerValue *ListBlocks::compileItemNumOfList(Compiler *compiler)
184185

185186
return nullptr;
186187
}
188+
189+
CompilerValue *ListBlocks::compileLengthOfList(Compiler *compiler)
190+
{
191+
List *list = static_cast<List *>(compiler->field("LIST")->valuePtr().get());
192+
assert(list);
193+
194+
if (list)
195+
return compiler->addListSize(list);
196+
197+
return nullptr;
198+
}

src/dev/blocks/listblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ListBlocks : public IExtension
2626
static CompilerValue *compileReplaceItemOfList(Compiler *compiler);
2727
static CompilerValue *compileItemOfList(Compiler *compiler);
2828
static CompilerValue *compileItemNumOfList(Compiler *compiler);
29+
static CompilerValue *compileLengthOfList(Compiler *compiler);
2930
};
3031

3132
} // namespace libscratchcpp

test/dev/blocks/list_blocks_test.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,48 @@ TEST_F(ListBlocksTest, ItemNumOfList)
379379
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
380380
ASSERT_EQ(list->toString(), "Lorem ipsum dolor 123 true dolor");
381381
}
382+
383+
TEST_F(ListBlocksTest, LengthOfList)
384+
{
385+
auto target = std::make_shared<Sprite>();
386+
387+
auto list1 = std::make_shared<List>("list1", "");
388+
list1->append("Lorem");
389+
list1->append("ipsum");
390+
list1->append("dolor");
391+
list1->append(123);
392+
list1->append(true);
393+
target->addList(list1);
394+
395+
auto list2 = std::make_shared<List>("list2", "");
396+
list2->append(1);
397+
list2->append(false);
398+
target->addList(list2);
399+
400+
ScriptBuilder builder(m_extension.get(), m_engine, target);
401+
402+
auto addTest = [&builder](std::shared_ptr<List> list) {
403+
builder.addBlock("data_lengthoflist");
404+
builder.addEntityField("LIST", list);
405+
auto block = builder.takeBlock();
406+
407+
builder.addBlock("test_print");
408+
builder.addObscuredInput("STRING", block);
409+
return builder.currentBlock();
410+
};
411+
412+
auto block = addTest(list1);
413+
addTest(list2);
414+
415+
builder.build();
416+
417+
static const std::string expected =
418+
"5\n"
419+
"2\n";
420+
421+
testing::internal::CaptureStdout();
422+
builder.run();
423+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
424+
ASSERT_EQ(list1->toString(), "Lorem ipsum dolor 123 true");
425+
ASSERT_EQ(list2->toString(), "1 false");
426+
}

0 commit comments

Comments
 (0)