|
3 | 3 | #include <scratchcpp/input.h> |
4 | 4 | #include <scratchcpp/field.h> |
5 | 5 | #include <scratchcpp/list.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 ListBlocksTest : public testing::Test |
15 | 19 | { |
16 | 20 | public: |
@@ -104,6 +108,7 @@ TEST_F(ListBlocksTest, RegisterBlocks) |
104 | 108 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_itemnumoflist", &ListBlocks::compileItemNumberInList)).Times(1); |
105 | 109 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_lengthoflist", &ListBlocks::compileLengthOfList)).Times(1); |
106 | 110 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_listcontainsitem", &ListBlocks::compileListContainsItem)).Times(1); |
| 111 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_showlist", &ListBlocks::compileShowList)).Times(1); |
107 | 112 |
|
108 | 113 | // Monitor names |
109 | 114 | EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "data_listcontents", &ListBlocks::listContentsMonitorName)); |
@@ -435,3 +440,106 @@ TEST_F(ListBlocksTest, ListContainsItem) |
435 | 440 | list2.get(), |
436 | 441 | })); |
437 | 442 | } |
| 443 | + |
| 444 | +TEST_F(ListBlocksTest, ShowList) |
| 445 | +{ |
| 446 | + Compiler compiler(&m_engineMock); |
| 447 | + Stage stage; |
| 448 | + Target target; |
| 449 | + |
| 450 | + // show list [list1] |
| 451 | + auto list1 = std::make_shared<List>("b", "list1"); |
| 452 | + list1->setTarget(&stage); |
| 453 | + auto block1 = createListBlock("a", "data_showlist", list1); |
| 454 | + |
| 455 | + // show list [list2] |
| 456 | + auto list2 = std::make_shared<List>("d", "list2"); |
| 457 | + list2->setTarget(&target); |
| 458 | + auto block2 = createListBlock("c", "data_showlist", list2); |
| 459 | + |
| 460 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 461 | + EXPECT_CALL(m_engineMock, functionIndex(&ListBlocks::showGlobalList)).WillOnce(Return(0)); |
| 462 | + compiler.init(); |
| 463 | + compiler.setBlock(block1); |
| 464 | + ListBlocks::compileShowList(&compiler); |
| 465 | + |
| 466 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 467 | + EXPECT_CALL(m_engineMock, functionIndex(&ListBlocks::showList)).WillOnce(Return(1)); |
| 468 | + compiler.setBlock(block2); |
| 469 | + ListBlocks::compileShowList(&compiler); |
| 470 | + compiler.end(); |
| 471 | + |
| 472 | + 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 })); |
| 473 | + ASSERT_EQ(compiler.constValues(), std::vector<Value>({ "b", "d" })); |
| 474 | + ASSERT_TRUE(compiler.variables().empty()); |
| 475 | + ASSERT_TRUE(compiler.lists().empty()); |
| 476 | +} |
| 477 | + |
| 478 | +TEST_F(ListBlocksTest, ShowListImpl) |
| 479 | +{ |
| 480 | + static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 481 | + static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 482 | + static unsigned int bytecode3[] = { vm::OP_START, vm::OP_CONST, 2, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 483 | + static unsigned int bytecode4[] = { vm::OP_START, vm::OP_CONST, 3, vm::OP_EXEC, 1, vm::OP_HALT }; |
| 484 | + static BlockFunc functions[] = { &ListBlocks::showGlobalList, &ListBlocks::showList }; |
| 485 | + static Value constValues[] = { "a", "b", "c", "d" }; |
| 486 | + |
| 487 | + auto list1 = std::make_shared<List>("b", ""); |
| 488 | + Monitor monitor1("b", ""); |
| 489 | + monitor1.setVisible(false); |
| 490 | + list1->setMonitor(&monitor1); |
| 491 | + |
| 492 | + auto list2 = std::make_shared<List>("d", ""); |
| 493 | + Monitor monitor2("d", ""); |
| 494 | + monitor2.setVisible(false); |
| 495 | + list2->setMonitor(&monitor2); |
| 496 | + |
| 497 | + Stage stage; |
| 498 | + stage.addList(list1); |
| 499 | + |
| 500 | + Target target; |
| 501 | + target.addList(list2); |
| 502 | + |
| 503 | + // Global |
| 504 | + VirtualMachine vm1(&stage, &m_engineMock, nullptr); |
| 505 | + vm1.setBytecode(bytecode1); |
| 506 | + vm1.setFunctions(functions); |
| 507 | + vm1.setConstValues(constValues); |
| 508 | + |
| 509 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 510 | + vm1.run(); |
| 511 | + |
| 512 | + ASSERT_EQ(vm1.registerCount(), 0); |
| 513 | + ASSERT_FALSE(monitor1.visible()); |
| 514 | + ASSERT_FALSE(monitor2.visible()); |
| 515 | + |
| 516 | + EXPECT_CALL(m_engineMock, stage()).WillOnce(Return(&stage)); |
| 517 | + vm1.reset(); |
| 518 | + vm1.setBytecode(bytecode2); |
| 519 | + vm1.run(); |
| 520 | + |
| 521 | + ASSERT_EQ(vm1.registerCount(), 0); |
| 522 | + ASSERT_TRUE(monitor1.visible()); |
| 523 | + ASSERT_FALSE(monitor2.visible()); |
| 524 | + |
| 525 | + monitor1.setVisible(false); |
| 526 | + |
| 527 | + // Local |
| 528 | + VirtualMachine vm2(&target, &m_engineMock, nullptr); |
| 529 | + vm2.setBytecode(bytecode3); |
| 530 | + vm2.setFunctions(functions); |
| 531 | + vm2.setConstValues(constValues); |
| 532 | + vm2.run(); |
| 533 | + |
| 534 | + ASSERT_EQ(vm2.registerCount(), 0); |
| 535 | + ASSERT_FALSE(monitor1.visible()); |
| 536 | + ASSERT_FALSE(monitor2.visible()); |
| 537 | + |
| 538 | + vm2.reset(); |
| 539 | + vm2.setBytecode(bytecode4); |
| 540 | + vm2.run(); |
| 541 | + |
| 542 | + ASSERT_EQ(vm2.registerCount(), 0); |
| 543 | + ASSERT_FALSE(monitor1.visible()); |
| 544 | + ASSERT_TRUE(monitor2.visible()); |
| 545 | +} |
0 commit comments