|
| 1 | +#include <scratchcpp/compiler.h> |
| 2 | +#include <scratchcpp/block.h> |
| 3 | +#include <scratchcpp/input.h> |
| 4 | +#include <scratchcpp/field.h> |
| 5 | +#include <enginemock.h> |
| 6 | + |
| 7 | +#include "../common.h" |
| 8 | +#include "blocks/looksblocks.h" |
| 9 | +#include "engine/internal/engine.h" |
| 10 | + |
| 11 | +using namespace libscratchcpp; |
| 12 | + |
| 13 | +using ::testing::Return; |
| 14 | + |
| 15 | +class LooksBlocksTest : public testing::Test |
| 16 | +{ |
| 17 | + public: |
| 18 | + void SetUp() override |
| 19 | + { |
| 20 | + m_section = std::make_unique<LooksBlocks>(); |
| 21 | + m_section->registerBlocks(&m_engine); |
| 22 | + } |
| 23 | + |
| 24 | + // For any looks block |
| 25 | + std::shared_ptr<Block> createLooksBlock(const std::string &id, const std::string &opcode) const { return std::make_shared<Block>(id, opcode); } |
| 26 | + |
| 27 | + void addValueInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id, const Value &value) const |
| 28 | + { |
| 29 | + auto input = std::make_shared<Input>(name, Input::Type::Shadow); |
| 30 | + input->setPrimaryValue(value); |
| 31 | + input->setInputId(id); |
| 32 | + block->addInput(input); |
| 33 | + block->updateInputMap(); |
| 34 | + } |
| 35 | + |
| 36 | + void addObscuredInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const |
| 37 | + { |
| 38 | + auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow); |
| 39 | + input->setValueBlock(valueBlock); |
| 40 | + input->setInputId(id); |
| 41 | + block->addInput(input); |
| 42 | + block->updateInputMap(); |
| 43 | + } |
| 44 | + |
| 45 | + std::shared_ptr<Input> addNullInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id) const |
| 46 | + { |
| 47 | + auto input = std::make_shared<Input>(name, Input::Type::Shadow); |
| 48 | + input->setInputId(id); |
| 49 | + block->addInput(input); |
| 50 | + block->updateInputMap(); |
| 51 | + |
| 52 | + return input; |
| 53 | + } |
| 54 | + |
| 55 | + void addDropdownInput(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Inputs id, const std::string &selectedValue, std::shared_ptr<Block> valueBlock = nullptr) const |
| 56 | + { |
| 57 | + if (valueBlock) |
| 58 | + addObscuredInput(block, name, id, valueBlock); |
| 59 | + else { |
| 60 | + auto input = addNullInput(block, name, id); |
| 61 | + auto menu = createLooksBlock(block->id() + "_menu", block->opcode() + "_menu"); |
| 62 | + input->setValueBlock(menu); |
| 63 | + addDropdownField(menu, name, static_cast<LooksBlocks::Fields>(-1), selectedValue, static_cast<LooksBlocks::FieldValues>(-1)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + void addDropdownField(std::shared_ptr<Block> block, const std::string &name, LooksBlocks::Fields id, const std::string &value, LooksBlocks::FieldValues valueId) const |
| 68 | + { |
| 69 | + auto field = std::make_shared<Field>(name, value); |
| 70 | + field->setFieldId(id); |
| 71 | + field->setSpecialValueId(valueId); |
| 72 | + block->addField(field); |
| 73 | + block->updateFieldMap(); |
| 74 | + } |
| 75 | + |
| 76 | + std::unique_ptr<IBlockSection> m_section; |
| 77 | + EngineMock m_engineMock; |
| 78 | + Engine m_engine; |
| 79 | +}; |
| 80 | + |
| 81 | +TEST_F(LooksBlocksTest, Name) |
| 82 | +{ |
| 83 | + ASSERT_EQ(m_section->name(), "Looks"); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(LooksBlocksTest, CategoryVisible) |
| 87 | +{ |
| 88 | + ASSERT_TRUE(m_section->categoryVisible()); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(LooksBlocksTest, RegisterBlocks) |
| 92 | +{ |
| 93 | + m_section->registerBlocks(&m_engineMock); |
| 94 | +} |
0 commit comments