@@ -32,6 +32,8 @@ using namespace libscratchcpp;
3232
3333using ::testing::Return;
3434using ::testing::SaveArg;
35+ using ::testing::WithArgs;
36+ using ::testing::Invoke;
3537using ::testing::_;
3638
3739// NOTE: resolveIds() and compile() are tested in load_project_test
@@ -102,7 +104,7 @@ TEST(EngineTest, Clear)
102104 ASSERT_TRUE (engine.monitors ().empty ());
103105}
104106
105- TEST (EngineTest, CompileMonitors )
107+ TEST (EngineTest, CompileAndExecuteMonitors )
106108{
107109 Engine engine;
108110 auto stage = std::make_shared<Stage>();
@@ -111,6 +113,8 @@ TEST(EngineTest, CompileMonitors)
111113
112114 auto m1 = std::make_shared<Monitor>(" a" , " monitor_test1" );
113115 auto m2 = std::make_shared<Monitor>(" b" , " monitor_test2" );
116+ auto m3 = std::make_shared<Monitor>(" c" , " monitor_test3" );
117+ m1->setVisible (false );
114118 m2->setSprite (sprite.get ());
115119 engine.setMonitors ({ m1, m2 });
116120
@@ -119,18 +123,38 @@ TEST(EngineTest, CompileMonitors)
119123 engine.addCompileFunction (section.get (), m1->opcode (), [](Compiler *compiler) { compiler->addConstValue (5.4 ); });
120124 engine.addCompileFunction (section.get (), m2->opcode (), [](Compiler *compiler) { compiler->addConstValue (" test" ); });
121125
126+ // Compile the monitor blocks
122127 engine.compile ();
123128 auto script1 = m1->script ();
124129 auto script2 = m2->script ();
125- ASSERT_TRUE (script1 && script2);
130+ auto script3 = m3->script ();
131+ ASSERT_TRUE (script1 && script2 && !script3);
126132
127- ASSERT_EQ (script1->bytecodeVector (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_HALT }));
133+ ASSERT_EQ (script1->bytecodeVector (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm:: OP_HALT }));
128134 ASSERT_EQ (script1->target (), stage.get ());
129135 ASSERT_EQ (script1->topBlock (), m1->block ());
130136
131- ASSERT_EQ (script2->bytecodeVector (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_HALT }));
137+ ASSERT_EQ (script2->bytecodeVector (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm:: OP_HALT }));
132138 ASSERT_EQ (script2->target (), sprite.get ());
133139 ASSERT_EQ (script2->topBlock (), m2->block ());
140+
141+ // Execute the monitor blocks
142+ MonitorHandlerMock iface1, iface2, iface3;
143+ EXPECT_CALL (iface1, init);
144+ EXPECT_CALL (iface2, init);
145+ EXPECT_CALL (iface3, init);
146+ m1->setInterface (&iface1);
147+ m2->setInterface (&iface2);
148+ m3->setInterface (&iface3);
149+
150+ EXPECT_CALL (iface1, onValueChanged).Times (0 );
151+ EXPECT_CALL (iface2, onValueChanged (_)).WillOnce (WithArgs<0 >(Invoke ([](const VirtualMachine *vm) {
152+ ASSERT_EQ (vm->registerCount (), 1 );
153+ ASSERT_EQ (vm->getInput (0 , 1 )->toString (), " test" );
154+ ASSERT_FALSE (vm->atEnd ()); // the script shouldn't end because that would spam the console with leak warnings
155+ })));
156+ EXPECT_CALL (iface3, onValueChanged).Times (0 );
157+ engine.updateMonitors ();
134158}
135159
136160TEST (EngineTest, IsRunning)
0 commit comments