File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ std::string ControlBlocks::description() const
2323void ControlBlocks::registerBlocks (IEngine *engine)
2424{
2525 engine->addCompileFunction (this , " control_forever" , &compileForever);
26+ engine->addCompileFunction (this , " control_repeat" , &compileRepeat);
2627}
2728
2829CompilerValue *ControlBlocks::compileForever (Compiler *compiler)
@@ -32,3 +33,10 @@ CompilerValue *ControlBlocks::compileForever(Compiler *compiler)
3233 compiler->moveToWhileLoop (compiler->addConstValue (true ), substack ? substack->valueBlock () : nullptr );
3334 return nullptr ;
3435}
36+
37+ CompilerValue *ControlBlocks::compileRepeat (Compiler *compiler)
38+ {
39+ auto substack = compiler->input (" SUBSTACK" );
40+ compiler->moveToRepeatLoop (compiler->addInput (" TIMES" ), substack ? substack->valueBlock () : nullptr );
41+ return nullptr ;
42+ }
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ class ControlBlocks : public IExtension
1717
1818 private:
1919 static CompilerValue *compileForever (Compiler *compiler);
20+ static CompilerValue *compileRepeat (Compiler *compiler);
2021};
2122
2223} // namespace libscratchcpp
Original file line number Diff line number Diff line change @@ -68,3 +68,40 @@ TEST_F(ControlBlocksTest, Forever)
6868 }
6969 }
7070}
71+
72+ TEST_F (ControlBlocksTest, Repeat)
73+ {
74+ auto target = std::make_shared<Sprite>();
75+
76+ {
77+ ScriptBuilder builder (m_extension.get (), m_engine, target);
78+
79+ builder.addBlock (" control_repeat" );
80+ auto substack = std::make_shared<Block>(" " , " test_print_test" );
81+ builder.addObscuredInput (" SUBSTACK" , substack);
82+ builder.addValueInput (" TIMES" , 5 );
83+
84+ builder.build ();
85+
86+ testing::internal::CaptureStdout ();
87+ builder.run ();
88+ ASSERT_EQ (testing::internal::GetCapturedStdout (), " test\n test\n test\n test\n test\n " );
89+ }
90+
91+ m_engine->clear ();
92+ target = std::make_shared<Sprite>();
93+
94+ {
95+ ScriptBuilder builder (m_extension.get (), m_engine, target);
96+ builder.addBlock (" control_repeat" );
97+ builder.addValueInput (" TIMES" , " Infinity" );
98+
99+ builder.build ();
100+ m_engine->start ();
101+
102+ for (int i = 0 ; i < 2 ; i++) {
103+ m_engine->step ();
104+ ASSERT_TRUE (m_engine->isRunning ());
105+ }
106+ }
107+ }
You can’t perform that action at this time.
0 commit comments