Skip to content

Commit a82f3fa

Browse files
committed
Implement data_itemnumoflist
1 parent 39aba5d commit a82f3fa

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/dev/blocks/listblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void ListBlocks::registerBlocks(IEngine *engine)
2828
engine->addCompileFunction(this, "data_insertatlist", &compileInsertAtList);
2929
engine->addCompileFunction(this, "data_replaceitemoflist", &compileReplaceItemOfList);
3030
engine->addCompileFunction(this, "data_itemoflist", &compileItemOfList);
31+
engine->addCompileFunction(this, "data_itemnumoflist", &compileItemNumOfList);
3132
}
3233

3334
CompilerValue *ListBlocks::compileAddToList(Compiler *compiler)
@@ -170,3 +171,16 @@ CompilerValue *ListBlocks::compileItemOfList(Compiler *compiler)
170171

171172
return nullptr;
172173
}
174+
175+
CompilerValue *ListBlocks::compileItemNumOfList(Compiler *compiler)
176+
{
177+
List *list = static_cast<List *>(compiler->field("LIST")->valuePtr().get());
178+
assert(list);
179+
180+
if (list) {
181+
CompilerValue *item = compiler->addInput("ITEM");
182+
return compiler->createAdd(compiler->addListItemIndex(list, item), compiler->addConstValue(1));
183+
}
184+
185+
return nullptr;
186+
}

src/dev/blocks/listblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ListBlocks : public IExtension
2525
static CompilerValue *compileInsertAtList(Compiler *compiler);
2626
static CompilerValue *compileReplaceItemOfList(Compiler *compiler);
2727
static CompilerValue *compileItemOfList(Compiler *compiler);
28+
static CompilerValue *compileItemNumOfList(Compiler *compiler);
2829
};
2930

3031
} // namespace libscratchcpp

test/dev/blocks/list_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,46 @@ TEST_F(ListBlocksTest, ItemOfList)
336336
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
337337
ASSERT_EQ(list->toString(), "Lorem ipsum dolor 123 true");
338338
}
339+
340+
TEST_F(ListBlocksTest, ItemNumOfList)
341+
{
342+
auto target = std::make_shared<Sprite>();
343+
344+
auto list = std::make_shared<List>("list", "");
345+
list->append("Lorem");
346+
list->append("ipsum");
347+
list->append("dolor");
348+
list->append(123);
349+
list->append(true);
350+
list->append("dolor");
351+
target->addList(list);
352+
353+
ScriptBuilder builder(m_extension.get(), m_engine, target);
354+
355+
auto addTest = [&builder](const Value &item, std::shared_ptr<List> list) {
356+
builder.addBlock("data_itemnumoflist");
357+
builder.addValueInput("ITEM", item);
358+
builder.addEntityField("LIST", list);
359+
auto block = builder.takeBlock();
360+
361+
builder.addBlock("test_print");
362+
builder.addObscuredInput("STRING", block);
363+
return builder.currentBlock();
364+
};
365+
366+
auto block = addTest("dolor", list);
367+
addTest(true, list);
368+
addTest("nonexistent", list);
369+
370+
builder.build();
371+
372+
static const std::string expected =
373+
"3\n"
374+
"5\n"
375+
"0\n";
376+
377+
testing::internal::CaptureStdout();
378+
builder.run();
379+
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
380+
ASSERT_EQ(list->toString(), "Lorem ipsum dolor 123 true dolor");
381+
}

0 commit comments

Comments
 (0)