|
| 1 | +#include <scratchcpp/compiler.h> |
| 2 | +#include <scratchcpp/block.h> |
| 3 | +#include <scratchcpp/input.h> |
| 4 | +#include <scratchcpp/field.h> |
| 5 | +#include <scratchcpp/variable.h> |
| 6 | +#include <enginemock.h> |
| 7 | + |
| 8 | +#include "../common.h" |
| 9 | +#include "blocks/variableblocks.h" |
| 10 | +#include "engine/engine.h" |
| 11 | + |
| 12 | +using namespace libscratchcpp; |
| 13 | + |
| 14 | +class VariableBlocksTest : public testing::Test |
| 15 | +{ |
| 16 | + public: |
| 17 | + void SetUp() override |
| 18 | + { |
| 19 | + m_section = std::make_unique<VariableBlocks>(); |
| 20 | + m_section->registerBlocks(&m_engine); |
| 21 | + } |
| 22 | + |
| 23 | + // For set variable to and change variable by |
| 24 | + std::shared_ptr<Block> createVariableBlock(const std::string &id, const std::string &opcode, std::shared_ptr<Variable> variable, const Value &value) const |
| 25 | + { |
| 26 | + auto block = std::make_shared<Block>(id, opcode); |
| 27 | + |
| 28 | + auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable); |
| 29 | + variableField->setFieldId(VariableBlocks::VARIABLE); |
| 30 | + block->addField(variableField); |
| 31 | + block->updateFieldMap(); |
| 32 | + |
| 33 | + auto valueInput = std::make_shared<Input>("VALUE", Input::Type::Shadow); |
| 34 | + valueInput->setPrimaryValue(value); |
| 35 | + valueInput->setInputId(VariableBlocks::VALUE); |
| 36 | + block->addInput(valueInput); |
| 37 | + block->updateInputMap(); |
| 38 | + |
| 39 | + return block; |
| 40 | + } |
| 41 | + |
| 42 | + std::unique_ptr<IBlockSection> m_section; |
| 43 | + EngineMock m_engineMock; |
| 44 | + Engine m_engine; |
| 45 | +}; |
| 46 | + |
| 47 | +TEST_F(VariableBlocksTest, Name) |
| 48 | +{ |
| 49 | + ASSERT_EQ(m_section->name(), "Variables"); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(VariableBlocksTest, CategoryVisible) |
| 53 | +{ |
| 54 | + ASSERT_TRUE(m_section->categoryVisible()); |
| 55 | +} |
| 56 | + |
| 57 | +TEST_F(VariableBlocksTest, RegisterBlocks) |
| 58 | +{ |
| 59 | + // Blocks |
| 60 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_setvariableto", &VariableBlocks::compileSetVariable)).Times(1); |
| 61 | + EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_changevariableby", &VariableBlocks::compileChangeVariableBy)).Times(1); |
| 62 | + |
| 63 | + // Inputs |
| 64 | + EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VALUE", VariableBlocks::VALUE)); |
| 65 | + |
| 66 | + // Fields |
| 67 | + EXPECT_CALL(m_engineMock, addField(m_section.get(), "VARIABLE", VariableBlocks::VARIABLE)); |
| 68 | + |
| 69 | + m_section->registerBlocks(&m_engineMock); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_F(VariableBlocksTest, SetVariableTo) |
| 73 | +{ |
| 74 | + Compiler compiler(&m_engine); |
| 75 | + |
| 76 | + // set variable [var1] to "new value" |
| 77 | + auto var1 = std::make_shared<Variable>("b", "var1", "old value"); |
| 78 | + auto block1 = createVariableBlock("a", "data_setvariableto", var1, "new value"); |
| 79 | + |
| 80 | + // set variable [var2] to 5 |
| 81 | + auto var2 = std::make_shared<Variable>("d", "var2", 2.5); |
| 82 | + auto block2 = createVariableBlock("c", "data_setvariableto", var2, 5); |
| 83 | + |
| 84 | + compiler.init(); |
| 85 | + compiler.setBlock(block1); |
| 86 | + VariableBlocks::compileSetVariable(&compiler); |
| 87 | + compiler.setBlock(block2); |
| 88 | + VariableBlocks::compileSetVariable(&compiler); |
| 89 | + compiler.end(); |
| 90 | + |
| 91 | + ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_SET_VAR, 0, vm::OP_CONST, 1, vm::OP_SET_VAR, 1, vm::OP_HALT })); |
| 92 | + ASSERT_EQ(compiler.constValues(), std::vector<Value>({ "new value", 5 })); |
| 93 | + ASSERT_EQ( |
| 94 | + compiler.variables(), |
| 95 | + std::vector<Variable *>({ |
| 96 | + var1.get(), |
| 97 | + var2.get(), |
| 98 | + })); |
| 99 | + ASSERT_TRUE(compiler.lists().empty()); |
| 100 | +} |
| 101 | + |
| 102 | +TEST_F(VariableBlocksTest, ChangeVariableBy) |
| 103 | +{ |
| 104 | + Compiler compiler(&m_engine); |
| 105 | + |
| 106 | + // change variable [var1] by 10 |
| 107 | + auto var1 = std::make_shared<Variable>("b", "var1", 2.5); |
| 108 | + auto block1 = createVariableBlock("a", "data_changevariableby", var1, 10); |
| 109 | + |
| 110 | + // change variable [var2] by 3.25 |
| 111 | + auto var2 = std::make_shared<Variable>("d", "var2", 1.2); |
| 112 | + auto block2 = createVariableBlock("c", "data_changevariableby", var2, 3.25); |
| 113 | + |
| 114 | + compiler.init(); |
| 115 | + compiler.setBlock(block1); |
| 116 | + VariableBlocks::compileChangeVariableBy(&compiler); |
| 117 | + compiler.setBlock(block2); |
| 118 | + VariableBlocks::compileChangeVariableBy(&compiler); |
| 119 | + compiler.end(); |
| 120 | + |
| 121 | + ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_CHANGE_VAR, 0, vm::OP_CONST, 1, vm::OP_CHANGE_VAR, 1, vm::OP_HALT })); |
| 122 | + ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 10, 3.25 })); |
| 123 | + ASSERT_EQ( |
| 124 | + compiler.variables(), |
| 125 | + std::vector<Variable *>({ |
| 126 | + var1.get(), |
| 127 | + var2.get(), |
| 128 | + })); |
| 129 | + ASSERT_TRUE(compiler.lists().empty()); |
| 130 | +} |
0 commit comments