|
4 | 4 | #include <scratchcpp/field.h>
|
5 | 5 | #include <penlayer.h>
|
6 | 6 | #include <spritemodel.h>
|
| 7 | +#include <stagemodel.h> |
7 | 8 | #include <blocks/penblocks.h>
|
8 | 9 | #include <enginemock.h>
|
9 | 10 | #include <penlayermock.h>
|
| 11 | +#include <renderedtargetmock.h> |
10 | 12 |
|
11 | 13 | #include "../common.h"
|
12 | 14 |
|
@@ -92,6 +94,7 @@ TEST_F(PenBlocksTest, RegisterBlocks)
|
92 | 94 | {
|
93 | 95 | // Blocks
|
94 | 96 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_clear", &PenBlocks::compileClear));
|
| 97 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_stamp", &PenBlocks::compileStamp)); |
95 | 98 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penDown", &PenBlocks::compilePenDown));
|
96 | 99 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_penUp", &PenBlocks::compilePenUp));
|
97 | 100 | EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "pen_setPenColorToColor", &PenBlocks::compileSetPenColorToColor));
|
@@ -133,6 +136,66 @@ TEST_F(PenBlocksTest, Clear)
|
133 | 136 | ASSERT_TRUE(compiler.lists().empty());
|
134 | 137 | }
|
135 | 138 |
|
| 139 | +TEST_F(PenBlocksTest, Stamp) |
| 140 | +{ |
| 141 | + Compiler compiler(&m_engineMock); |
| 142 | + |
| 143 | + auto block = std::make_shared<Block>("a", "pen_stamp"); |
| 144 | + |
| 145 | + EXPECT_CALL(m_engineMock, functionIndex(&PenBlocks::stamp)).WillOnce(Return(2)); |
| 146 | + compiler.init(); |
| 147 | + compiler.setBlock(block); |
| 148 | + PenBlocks::compileStamp(&compiler); |
| 149 | + compiler.end(); |
| 150 | + |
| 151 | + ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT })); |
| 152 | + ASSERT_TRUE(compiler.constValues().empty()); |
| 153 | + ASSERT_TRUE(compiler.variables().empty()); |
| 154 | + ASSERT_TRUE(compiler.lists().empty()); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F(PenBlocksTest, StampImpl) |
| 158 | +{ |
| 159 | + static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT }; |
| 160 | + static BlockFunc functions[] = { &PenBlocks::stamp }; |
| 161 | + |
| 162 | + PenLayerMock penLayer; |
| 163 | + PenLayer::addPenLayer(&m_engineMock, &penLayer); |
| 164 | + RenderedTargetMock renderedTarget; |
| 165 | + |
| 166 | + // Test sprite |
| 167 | + libscratchcpp::Sprite sprite; |
| 168 | + SpriteModel spriteModel; |
| 169 | + sprite.setInterface(&spriteModel); |
| 170 | + spriteModel.setRenderedTarget(&renderedTarget); |
| 171 | + |
| 172 | + VirtualMachine vm1(&sprite, &m_engineMock, nullptr); |
| 173 | + vm1.setBytecode(bytecode); |
| 174 | + vm1.setFunctions(functions); |
| 175 | + |
| 176 | + EXPECT_CALL(penLayer, stamp(&renderedTarget)); |
| 177 | + EXPECT_CALL(m_engineMock, requestRedraw()); |
| 178 | + vm1.run(); |
| 179 | + |
| 180 | + ASSERT_EQ(vm1.registerCount(), 0); |
| 181 | + |
| 182 | + // Test stage |
| 183 | + libscratchcpp::Stage stage; |
| 184 | + StageModel stageModel; |
| 185 | + stage.setInterface(&stageModel); |
| 186 | + stageModel.setRenderedTarget(&renderedTarget); |
| 187 | + |
| 188 | + VirtualMachine vm2(&stage, &m_engineMock, nullptr); |
| 189 | + vm2.setBytecode(bytecode); |
| 190 | + vm2.setFunctions(functions); |
| 191 | + |
| 192 | + EXPECT_CALL(penLayer, stamp(&renderedTarget)); |
| 193 | + EXPECT_CALL(m_engineMock, requestRedraw()); |
| 194 | + vm2.run(); |
| 195 | + |
| 196 | + ASSERT_EQ(vm2.registerCount(), 0); |
| 197 | +} |
| 198 | + |
136 | 199 | TEST_F(PenBlocksTest, ClearImpl)
|
137 | 200 | {
|
138 | 201 | static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0, vm::OP_HALT };
|
|
0 commit comments