Skip to content

Commit 3211938

Browse files
committed
Add looks blocks test
1 parent 0ee4905 commit 3211938

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/blocks/looksblocks.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ namespace libscratchcpp
1111
class LooksBlocks : public IBlockSection
1212
{
1313
public:
14+
enum Inputs
15+
{
16+
};
17+
18+
enum Fields
19+
{
20+
};
21+
22+
enum FieldValues
23+
{
24+
};
25+
1426
std::string name() const override;
1527

1628
void registerBlocks(IEngine *engine) override;

test/blocks/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,19 @@ target_link_libraries(
125125
)
126126

127127
gtest_discover_tests(motion_blocks_test)
128+
129+
# looks_blocks_test
130+
add_executable(
131+
looks_blocks_test
132+
looks_blocks_test.cpp
133+
)
134+
135+
target_link_libraries(
136+
looks_blocks_test
137+
GTest::gtest_main
138+
GTest::gmock_main
139+
scratchcpp
140+
scratchcpp_mocks
141+
)
142+
143+
gtest_discover_tests(looks_blocks_test)

test/blocks/looks_blocks_test.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)