|
8 | 8 | #include <scratchcpp/keyevent.h> |
9 | 9 | #include <scratchcpp/monitor.h> |
10 | 10 | #include <scratchcpp/field.h> |
| 11 | +#include <scratchcpp/compiler.h> |
| 12 | +#include <scratchcpp/script.h> |
| 13 | +#include <scratchcpp/virtualmachine.h> |
11 | 14 | #include <scratch/sound_p.h> |
12 | 15 | #include <timermock.h> |
13 | 16 | #include <clockmock.h> |
@@ -99,6 +102,37 @@ TEST(EngineTest, Clear) |
99 | 102 | ASSERT_TRUE(engine.monitors().empty()); |
100 | 103 | } |
101 | 104 |
|
| 105 | +TEST(EngineTest, CompileMonitors) |
| 106 | +{ |
| 107 | + Engine engine; |
| 108 | + auto stage = std::make_shared<Stage>(); |
| 109 | + auto sprite = std::make_shared<Sprite>(); |
| 110 | + engine.setTargets({ stage, sprite }); |
| 111 | + |
| 112 | + auto m1 = std::make_shared<Monitor>("a", "monitor_test1"); |
| 113 | + auto m2 = std::make_shared<Monitor>("b", "monitor_test2"); |
| 114 | + m2->setSprite(sprite.get()); |
| 115 | + engine.setMonitors({ m1, m2 }); |
| 116 | + |
| 117 | + auto section = std::make_shared<TestSection>(); |
| 118 | + engine.registerSection(section); |
| 119 | + engine.addCompileFunction(section.get(), m1->opcode(), [](Compiler *compiler) { compiler->addConstValue(5.4); }); |
| 120 | + engine.addCompileFunction(section.get(), m2->opcode(), [](Compiler *compiler) { compiler->addConstValue("test"); }); |
| 121 | + |
| 122 | + engine.compile(); |
| 123 | + auto script1 = m1->script(); |
| 124 | + auto script2 = m2->script(); |
| 125 | + ASSERT_TRUE(script1 && script2); |
| 126 | + |
| 127 | + ASSERT_EQ(script1->bytecodeVector(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_HALT })); |
| 128 | + ASSERT_EQ(script1->target(), stage.get()); |
| 129 | + ASSERT_EQ(script1->topBlock(), m1->block()); |
| 130 | + |
| 131 | + ASSERT_EQ(script2->bytecodeVector(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_HALT })); |
| 132 | + ASSERT_EQ(script2->target(), sprite.get()); |
| 133 | + ASSERT_EQ(script2->topBlock(), m2->block()); |
| 134 | +} |
| 135 | + |
102 | 136 | TEST(EngineTest, IsRunning) |
103 | 137 | { |
104 | 138 | Engine engine; |
|
0 commit comments