@@ -37,6 +37,18 @@ class VariableBlocksTest : public testing::Test
3737 return block;
3838 }
3939
40+ // For read variable
41+ std::shared_ptr<Block> createVariableBlock (const std::string &id, const std::string &opcode, std::shared_ptr<Variable> variable) const
42+ {
43+ auto block = std::make_shared<Block>(id, opcode);
44+
45+ auto variableField = std::make_shared<Field>(" VARIABLE" , Value (), variable);
46+ variableField->setFieldId (VariableBlocks::VARIABLE);
47+ block->addField (variableField);
48+
49+ return block;
50+ }
51+
4052 std::unique_ptr<IBlockSection> m_section;
4153 EngineMock m_engineMock;
4254 Engine m_engine;
@@ -55,6 +67,7 @@ TEST_F(VariableBlocksTest, CategoryVisible)
5567TEST_F (VariableBlocksTest, RegisterBlocks)
5668{
5769 // Blocks
70+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " data_variable" , &VariableBlocks::compileVariable)).Times (1 );
5871 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " data_setvariableto" , &VariableBlocks::compileSetVariable)).Times (1 );
5972 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " data_changevariableby" , &VariableBlocks::compileChangeVariableBy)).Times (1 );
6073
@@ -67,6 +80,36 @@ TEST_F(VariableBlocksTest, RegisterBlocks)
6780 m_section->registerBlocks (&m_engineMock);
6881}
6982
83+ TEST_F (VariableBlocksTest, Variable)
84+ {
85+ Compiler compiler (&m_engine);
86+
87+ // [var1]
88+ auto var1 = std::make_shared<Variable>(" b" , " var1" );
89+ auto block1 = createVariableBlock (" a" , " data_variable" , var1);
90+
91+ // [var2]
92+ auto var2 = std::make_shared<Variable>(" d" , " var2" );
93+ auto block2 = createVariableBlock (" c" , " data_variable" , var2);
94+
95+ compiler.init ();
96+ compiler.setBlock (block1);
97+ VariableBlocks::compileVariable (&compiler);
98+ compiler.setBlock (block2);
99+ VariableBlocks::compileVariable (&compiler);
100+ compiler.end ();
101+
102+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_READ_VAR, 0 , vm::OP_READ_VAR, 1 , vm::OP_HALT }));
103+ ASSERT_TRUE (compiler.constValues ().empty ());
104+ ASSERT_EQ (
105+ compiler.variables (),
106+ std::vector<Variable *>({
107+ var1.get (),
108+ var2.get (),
109+ }));
110+ ASSERT_TRUE (compiler.lists ().empty ());
111+ }
112+
70113TEST_F (VariableBlocksTest, SetVariableTo)
71114{
72115 Compiler compiler (&m_engine);
0 commit comments