|
1 | 1 | #include <scratchcpp/compiler.h> |
2 | 2 | #include <scratchcpp/block.h> |
3 | 3 | #include <scratchcpp/input.h> |
| 4 | +#include <penlayer.h> |
4 | 5 | #include <blocks/penblocks.h> |
5 | 6 | #include <enginemock.h> |
| 7 | +#include <penlayermock.h> |
6 | 8 |
|
7 | 9 | #include "../common.h" |
8 | 10 |
|
9 | 11 | using namespace scratchcpprender; |
10 | 12 | using namespace libscratchcpp; |
11 | 13 |
|
| 14 | +using ::testing::Return; |
| 15 | + |
12 | 16 | class PenBlocksTest : public testing::Test |
13 | 17 | { |
14 | 18 | public: |
@@ -38,5 +42,45 @@ TEST_F(PenBlocksTest, CategoryVisible) |
38 | 42 |
|
39 | 43 | TEST_F(PenBlocksTest, RegisterBlocks) |
40 | 44 | { |
| 45 | + // Blocks |
| 46 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_clear", &PenBlocks::compileClear)); |
| 47 | + |
41 | 48 | m_section->registerBlocks(&m_engineMock); |
42 | 49 | } |
| 50 | + |
| 51 | +TEST_F(PenBlocksTest, Clear) |
| 52 | +{ |
| 53 | + Compiler compiler(&m_engineMock); |
| 54 | + |
| 55 | + auto block = std::make_shared<Block>("a", "pen_clear"); |
| 56 | + |
| 57 | + EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::clear)).WillOnce(Return(2)); |
| 58 | + compiler.init(); |
| 59 | + compiler.setBlock(block); |
| 60 | + PenBlocks::compileClear(&compiler); |
| 61 | + compiler.end(); |
| 62 | + |
| 63 | + ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT })); |
| 64 | + ASSERT_TRUE(compiler.constValues().empty()); |
| 65 | + ASSERT_TRUE(compiler.variables().empty()); |
| 66 | + ASSERT_TRUE(compiler.lists().empty()); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(PenBlocksTest, ClearImpl) |
| 70 | +{ |
| 71 | + static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 72 | + static BlockFunc functions[] = { &PenBlocks::clear }; |
| 73 | + |
| 74 | + PenLayerMock penLayer; |
| 75 | + PenLayer::addPenLayer(&m_engineMock, &penLayer); |
| 76 | + |
| 77 | + VirtualMachine vm(nullptr, &m_engineMock, nullptr); |
| 78 | + vm.setBytecode(bytecode); |
| 79 | + vm.setFunctions(functions); |
| 80 | + |
| 81 | + EXPECT_CALL(penLayer, clear()); |
| 82 | + EXPECT_CALL(m_engineMock, requestRedraw()); |
| 83 | + vm.run(); |
| 84 | + |
| 85 | + ASSERT_EQ(vm.registerCount(), 0); |
| 86 | +} |
0 commit comments