|
3 | 3 | #include <scratchcpp/input.h> |
4 | 4 | #include <scratchcpp/field.h> |
5 | 5 | #include <scratchcpp/variable.h> |
| 6 | +#include <scratchcpp/stage.h> |
| 7 | +#include <scratchcpp/monitor.h> |
6 | 8 | #include <enginemock.h> |
7 | 9 |
|
8 | 10 | #include "../common.h" |
|
11 | 13 |
|
12 | 14 | using namespace libscratchcpp; |
13 | 15 |
|
| 16 | +using ::testing::Return; |
| 17 | + |
14 | 18 | class VariableBlocksTest : public testing::Test |
15 | 19 | { |
16 | 20 | public: |
@@ -70,6 +74,7 @@ TEST_F(VariableBlocksTest, RegisterBlocks) |
70 | 74 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_variable", &VariableBlocks::compileVariable)).Times(1); |
71 | 75 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_setvariableto", &VariableBlocks::compileSetVariable)).Times(1); |
72 | 76 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_changevariableby", &VariableBlocks::compileChangeVariableBy)).Times(1); |
| 77 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_showvariable", &VariableBlocks::compileShowVariable)).Times(1); |
73 | 78 |
|
74 | 79 | // Monitor names |
75 | 80 | EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "data_variable", &VariableBlocks::variableMonitorName)); |
@@ -208,3 +213,106 @@ TEST_F(VariableBlocksTest, ChangeVariableBy) |
208 | 213 | })); |
209 | 214 | ASSERT_TRUE(compiler.lists().empty()); |
210 | 215 | } |
| 216 | + |
| 217 | +TEST_F(VariableBlocksTest, ShowVariable) |
| 218 | +{ |
| 219 | + Compiler compiler(&m_engineMock); |
| 220 | + Stage stage; |
| 221 | + Target target; |
| 222 | + |
| 223 | + // show variable [var1] |
| 224 | + auto var1 = std::make_shared<Variable>("b", "var1"); |
| 225 | + var1->setTarget(&stage); |
| 226 | + auto block1 = createVariableBlock("a", "data_showvariable", var1); |
| 227 | + |
| 228 | + // show variable [var2] |
| 229 | + auto var2 = std::make_shared<Variable>("d", "var2"); |
| 230 | + var2->setTarget(&target); |
| 231 | + auto block2 = createVariableBlock("c", "data_showvariable", var2); |
| 232 | + |
| 233 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 234 | + EXPECT_CALL(m_engineMock, functionIndex(&VariableBlocks::showGlobalVariable)).WillOnce(Return(0)); |
| 235 | + compiler.init(); |
| 236 | + compiler.setBlock(block1); |
| 237 | + VariableBlocks::compileShowVariable(&compiler); |
| 238 | + |
| 239 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 240 | + EXPECT_CALL(m_engineMock, functionIndex(&VariableBlocks::showVariable)).WillOnce(Return(1)); |
| 241 | + compiler.setBlock(block2); |
| 242 | + VariableBlocks::compileShowVariable(&compiler); |
| 243 | + compiler.end(); |
| 244 | + |
| 245 | + ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_CONST, 1, vm::OP_EXEC, 1, vm::OP_HALT })); |
| 246 | + ASSERT_EQ(compiler.constValues(), std::vector<Value>({ "b", "d" })); |
| 247 | + ASSERT_TRUE(compiler.variables().empty()); |
| 248 | + ASSERT_TRUE(compiler.lists().empty()); |
| 249 | +} |
| 250 | + |
| 251 | +TEST_F(VariableBlocksTest, ShowVariableImpl) |
| 252 | +{ |
| 253 | + static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 254 | + static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 255 | + static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 256 | + static unsigned int bytecode4[] = { vm::OP_START, vm::OP_CONST, 3, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 257 | + static BlockFunc functions[] = { &VariableBlocks::showGlobalVariable, &VariableBlocks::showVariable }; |
| 258 | + static Value constValues[] = { "a", "b", "c", "d" }; |
| 259 | + |
| 260 | + auto var1 = std::make_shared<Variable>("b", ""); |
| 261 | + Monitor monitor1("b", ""); |
| 262 | + monitor1.setVisible(false); |
| 263 | + var1->setMonitor(&monitor1); |
| 264 | + |
| 265 | + auto var2 = std::make_shared<Variable>("d", ""); |
| 266 | + Monitor monitor2("d", ""); |
| 267 | + monitor2.setVisible(false); |
| 268 | + var2->setMonitor(&monitor2); |
| 269 | + |
| 270 | + Stage stage; |
| 271 | + stage.addVariable(var1); |
| 272 | + |
| 273 | + Target target; |
| 274 | + target.addVariable(var2); |
| 275 | + |
| 276 | + // Global |
| 277 | + VirtualMachine vm1(&stage, &m_engineMock, nullptr); |
| 278 | + vm1.setBytecode(bytecode1); |
| 279 | + vm1.setFunctions(functions); |
| 280 | + vm1.setConstValues(constValues); |
| 281 | + |
| 282 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 283 | + vm1.run(); |
| 284 | + |
| 285 | + ASSERT_EQ(vm1.registerCount(), 0); |
| 286 | + ASSERT_FALSE(monitor1.visible()); |
| 287 | + ASSERT_FALSE(monitor2.visible()); |
| 288 | + |
| 289 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 290 | + vm1.reset(); |
| 291 | + vm1.setBytecode(bytecode2); |
| 292 | + vm1.run(); |
| 293 | + |
| 294 | + ASSERT_EQ(vm1.registerCount(), 0); |
| 295 | + ASSERT_TRUE(monitor1.visible()); |
| 296 | + ASSERT_FALSE(monitor2.visible()); |
| 297 | + |
| 298 | + monitor1.setVisible(false); |
| 299 | + |
| 300 | + // Local |
| 301 | + VirtualMachine vm2(&target, &m_engineMock, nullptr); |
| 302 | + vm2.setBytecode(bytecode3); |
| 303 | + vm2.setFunctions(functions); |
| 304 | + vm2.setConstValues(constValues); |
| 305 | + vm2.run(); |
| 306 | + |
| 307 | + ASSERT_EQ(vm2.registerCount(), 0); |
| 308 | + ASSERT_FALSE(monitor1.visible()); |
| 309 | + ASSERT_FALSE(monitor2.visible()); |
| 310 | + |
| 311 | + vm2.reset(); |
| 312 | + vm2.setBytecode(bytecode4); |
| 313 | + vm2.run(); |
| 314 | + |
| 315 | + ASSERT_EQ(vm2.registerCount(), 0); |
| 316 | + ASSERT_FALSE(monitor1.visible()); |
| 317 | + ASSERT_TRUE(monitor2.visible()); |
| 318 | +} |
0 commit comments