11#include < scratchcpp/script.h>
22#include < scratchcpp/virtualmachine.h>
33#include < scratchcpp/target.h>
4+ #include < scratchcpp/sprite.h>
5+ #include < scratchcpp/variable.h>
46#include < scratchcpp/list.h>
57#include < enginemock.h>
68
79#include " ../common.h"
810
911using namespace libscratchcpp ;
1012
13+ using ::testing::Return;
14+
1115class ScriptTest : public testing ::Test
1216{
1317 public:
@@ -45,11 +49,13 @@ TEST_F(ScriptTest, Start)
4549 static std::vector<BlockFunc> functions = { &testFunction };
4650 static std::vector<Value> constValues = { " test" };
4751
48- std::unique_ptr<Value> var = std::make_unique<Value>();
49- static std::vector<Value *> variables = { var.get () };
52+ std::unique_ptr<Variable> var1 = std::make_unique<Variable>(" a" , " " , Value ());
53+ std::unique_ptr<Variable> var2 = std::make_unique<Variable>(" b" , " " , Value ());
54+ static std::vector<Variable *> variables = { var1.get (), var2.get () };
5055
51- std::unique_ptr<List> list = std::make_unique<List>(" " , " " );
52- static std::vector<List *> lists = { list.get () };
56+ std::unique_ptr<List> list1 = std::make_unique<List>(" c" , " " );
57+ std::unique_ptr<List> list2 = std::make_unique<List>(" d" , " " );
58+ static std::vector<List *> lists = { list1.get (), list2.get () };
5359
5460 Script script1 (nullptr , nullptr );
5561
@@ -85,7 +91,7 @@ TEST_F(ScriptTest, Start)
8591 ASSERT_EQ (vm->procedures ()[0 ], procedures[0 ]);
8692 ASSERT_EQ (vm->functions ()[0 ], functions[0 ]);
8793 ASSERT_EQ (vm->constValues ()[0 ].toString (), constValues[0 ].toString ());
88- ASSERT_EQ (vm->variables ()[0 ], variables[0 ]);
94+ ASSERT_EQ (vm->variables ()[0 ], variables[0 ]-> valuePtr () );
8995 ASSERT_EQ (vm->lists ()[0 ], lists[0 ]);
9096
9197 Target target;
@@ -96,6 +102,39 @@ TEST_F(ScriptTest, Start)
96102 ASSERT_EQ (vm->procedures ()[0 ], procedures[0 ]);
97103 ASSERT_EQ (vm->functions ()[0 ], functions[0 ]);
98104 ASSERT_EQ (vm->constValues ()[0 ].toString (), constValues[0 ].toString ());
99- ASSERT_EQ (vm->variables ()[0 ], variables[0 ]);
105+ ASSERT_EQ (vm->variables ()[0 ], variables[0 ]->valuePtr ());
106+ ASSERT_EQ (vm->lists ()[0 ], lists[0 ]);
107+
108+ Sprite root;
109+ root.setEngine (&m_engine);
110+ root.addVariable (std::make_shared<Variable>(" b" , " " , Value ()));
111+ root.addList (std::make_shared<List>(" d" , " " ));
112+
113+ EXPECT_CALL (m_engine, initClone).Times (1 );
114+ auto clone = root.clone ();
115+
116+ Script script4 (&root, &m_engine);
117+ script4.setBytecode (bytecode);
118+ script4.setProcedures (procedures);
119+ script4.setFunctions (functions);
120+ script4.setConstValues (constValues);
121+ script4.setVariables (variables);
122+ script4.setLists (lists);
123+
124+ EXPECT_CALL (m_engine, variableOwner (var1.get ())).WillOnce (Return (&target));
125+ EXPECT_CALL (m_engine, variableOwner (var2.get ())).WillOnce (Return (&root));
126+ EXPECT_CALL (m_engine, listOwner (list1.get ())).WillOnce (Return (&target));
127+ EXPECT_CALL (m_engine, listOwner (list2.get ())).WillOnce (Return (&root));
128+ vm = script4.start (clone.get ());
129+
130+ ASSERT_TRUE (vm);
131+ ASSERT_EQ (vm->target (), clone.get ());
132+ ASSERT_EQ (vm->bytecode ()[0 ], bytecode[0 ]);
133+ ASSERT_EQ (vm->procedures ()[0 ], procedures[0 ]);
134+ ASSERT_EQ (vm->functions ()[0 ], functions[0 ]);
135+ ASSERT_EQ (vm->constValues ()[0 ].toString (), constValues[0 ].toString ());
136+ ASSERT_EQ (vm->variables ()[0 ], variables[0 ]->valuePtr ());
137+ ASSERT_EQ (vm->variables ()[1 ], clone->variableAt (clone->findVariableById (" b" ))->valuePtr ());
100138 ASSERT_EQ (vm->lists ()[0 ], lists[0 ]);
139+ ASSERT_EQ (vm->lists ()[1 ], clone->listAt (clone->findListById (" d" )).get ());
101140}
0 commit comments