1212using namespace libscratchcpp ;
1313
1414using ::testing::Return;
15+ using ::testing::ReturnRef;
1516using ::testing::_;
1617
1718class ScriptTest : public testing ::Test
@@ -73,10 +74,13 @@ TEST_F(ScriptTest, HatPredicate)
7374 return 0 ;
7475 };
7576
77+ std::vector<BlockFunc> functions1 = { f1 };
78+ std::vector<BlockFunc> functions2 = { f1, f2, f3 };
79+
80+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions1));
7681 stageTest = nullptr ;
7782 engineTest = nullptr ;
7883 scriptTest = nullptr ;
79- script.setFunctions ({ f1 });
8084 script.setConstValues ({ " test" });
8185 script.setHatPredicateBytecode ({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_PRINT, vm::OP_EXEC, 0 , vm::OP_HALT });
8286 testing::internal::CaptureStdout ();
@@ -86,11 +90,11 @@ TEST_F(ScriptTest, HatPredicate)
8690 ASSERT_EQ (engineTest, &m_engine);
8791 ASSERT_EQ (scriptTest, &script);
8892
93+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions2));
8994 stageTest = nullptr ;
9095 engineTest = nullptr ;
9196 scriptTest = nullptr ;
9297 script.setHatPredicateBytecode ({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_PRINT, vm::OP_EXEC, 1 , vm::OP_HALT });
93- script.setFunctions ({ f1, f2, f3 });
9498 script.setConstValues ({ 5 });
9599 testing::internal::CaptureStdout ();
96100 ASSERT_TRUE (script.runHatPredicate ());
@@ -99,6 +103,7 @@ TEST_F(ScriptTest, HatPredicate)
99103 ASSERT_EQ (engineTest, &m_engine);
100104 ASSERT_EQ (scriptTest, &script);
101105
106+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions2));
102107 stageTest = nullptr ;
103108 engineTest = nullptr ;
104109 scriptTest = nullptr ;
@@ -119,6 +124,7 @@ TEST_F(ScriptTest, Start)
119124 static std::vector<unsigned int > bytecode = { vm::OP_START, vm::OP_HALT };
120125 static std::vector<unsigned int *> procedures = { bytecode.data () };
121126 static std::vector<BlockFunc> functions = { &testFunction };
127+ static std::vector<BlockFunc> noFunctions;
122128 static std::vector<Value> constValues = { " test" };
123129
124130 std::shared_ptr<Variable> var1 = std::make_unique<Variable>(" a" , " " , Value ());
@@ -144,6 +150,7 @@ TEST_F(ScriptTest, Start)
144150
145151 Script script2 (&m_target, nullptr , &m_engine);
146152
153+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (noFunctions));
147154 vm = script2.start ();
148155 ASSERT_TRUE (vm);
149156 ASSERT_EQ (vm->target (), &m_target);
@@ -152,11 +159,11 @@ TEST_F(ScriptTest, Start)
152159 Script script3 (&m_target, nullptr , &m_engine);
153160 script3.setBytecode (bytecode);
154161 script3.setProcedures (procedures);
155- script3.setFunctions (functions);
156162 script3.setConstValues (constValues);
157163 script3.setVariables (variables);
158164 script3.setLists (lists);
159165
166+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions));
160167 vm = script3.start ();
161168 ASSERT_TRUE (vm);
162169 ASSERT_EQ (vm->bytecode ()[0 ], bytecode[0 ]);
@@ -166,6 +173,7 @@ TEST_F(ScriptTest, Start)
166173 ASSERT_EQ (vm->variables ()[0 ], variables[0 ]->valuePtr ());
167174 ASSERT_EQ (vm->lists ()[0 ], lists[0 ]);
168175
176+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions));
169177 Target target;
170178 target.addVariable (var1);
171179 target.addList (list1);
@@ -194,11 +202,11 @@ TEST_F(ScriptTest, Start)
194202 Script script4 (&root, nullptr , &m_engine);
195203 script4.setBytecode (bytecode);
196204 script4.setProcedures (procedures);
197- script4.setFunctions (functions);
198205 script4.setConstValues (constValues);
199206 script4.setVariables (variables);
200207 script4.setLists (lists);
201208
209+ EXPECT_CALL (m_engine, blockFunctions ()).WillOnce (ReturnRef (functions));
202210 vm = script4.start (clone.get ());
203211
204212 ASSERT_TRUE (vm);
0 commit comments