|
1 | 1 | #include <scratchcpp/dev/compiler.h> |
2 | 2 | #include <scratchcpp/dev/compilerconstant.h> |
| 3 | +#include <scratchcpp/dev/compilerlocalvariable.h> |
3 | 4 | #include <scratchcpp/block.h> |
4 | 5 | #include <scratchcpp/variable.h> |
5 | 6 | #include <scratchcpp/list.h> |
@@ -180,6 +181,29 @@ TEST_F(CompilerTest, AddLoopIndex) |
180 | 181 | compile(compiler, block); |
181 | 182 | } |
182 | 183 |
|
| 184 | +TEST_F(CompilerTest, AddLocalVariableValue) |
| 185 | +{ |
| 186 | + Compiler compiler(&m_engine, &m_target); |
| 187 | + auto block = std::make_shared<Block>("a", ""); |
| 188 | + block->setCompileFunction([](Compiler *compiler) -> CompilerValue * { |
| 189 | + CompilerValue ret(Compiler::StaticType::Number); |
| 190 | + CompilerValue ptr1(Compiler::StaticType::Number); |
| 191 | + CompilerValue ptr2(Compiler::StaticType::Bool); |
| 192 | + CompilerLocalVariable var1(&ptr1); |
| 193 | + CompilerLocalVariable var2(&ptr2); |
| 194 | + |
| 195 | + EXPECT_CALL(*m_builder, addLocalVariableValue(&var1)).WillOnce(Return(&ret)); |
| 196 | + EXPECT_EQ(compiler->addLocalVariableValue(&var1), &ret); |
| 197 | + |
| 198 | + EXPECT_CALL(*m_builder, addLocalVariableValue(&var2)).WillOnce(Return(nullptr)); |
| 199 | + EXPECT_EQ(compiler->addLocalVariableValue(&var2), nullptr); |
| 200 | + |
| 201 | + return nullptr; |
| 202 | + }); |
| 203 | + |
| 204 | + compile(compiler, block); |
| 205 | +} |
| 206 | + |
183 | 207 | TEST_F(CompilerTest, AddVariableValue) |
184 | 208 | { |
185 | 209 | Compiler compiler(&m_engine, &m_target); |
@@ -905,6 +929,46 @@ TEST_F(CompilerTest, CreateSelect) |
905 | 929 | compile(compiler, block); |
906 | 930 | } |
907 | 931 |
|
| 932 | +TEST_F(CompilerTest, CreateLocalVariable) |
| 933 | +{ |
| 934 | + Compiler compiler(&m_engine, &m_target); |
| 935 | + auto block = std::make_shared<Block>("", ""); |
| 936 | + |
| 937 | + block->setCompileFunction([](Compiler *compiler) -> CompilerValue * { |
| 938 | + CompilerValue ptr1(Compiler::StaticType::Number); |
| 939 | + CompilerLocalVariable var1(&ptr1); |
| 940 | + EXPECT_CALL(*m_builder, createLocalVariable(var1.type())).WillOnce(Return(&var1)); |
| 941 | + EXPECT_EQ(compiler->createLocalVariable(var1.type()), &var1); |
| 942 | + |
| 943 | + CompilerValue ptr2(Compiler::StaticType::Number); |
| 944 | + CompilerLocalVariable var2(&ptr2); |
| 945 | + EXPECT_CALL(*m_builder, createLocalVariable(var2.type())).WillOnce(Return(&var2)); |
| 946 | + EXPECT_EQ(compiler->createLocalVariable(var2.type()), &var2); |
| 947 | + |
| 948 | + return nullptr; |
| 949 | + }); |
| 950 | + |
| 951 | + compile(compiler, block); |
| 952 | +} |
| 953 | + |
| 954 | +TEST_F(CompilerTest, CreateLocalVariableWrite) |
| 955 | +{ |
| 956 | + Compiler compiler(&m_engine, &m_target); |
| 957 | + auto block = std::make_shared<Block>("", ""); |
| 958 | + |
| 959 | + block->setCompileFunction([](Compiler *compiler) -> CompilerValue * { |
| 960 | + CompilerValue ptr(Compiler::StaticType::Number); |
| 961 | + CompilerLocalVariable var(&ptr); |
| 962 | + CompilerValue arg(Compiler::StaticType::Number); |
| 963 | + EXPECT_CALL(*m_builder, createLocalVariableWrite(&var, &arg)); |
| 964 | + compiler->createLocalVariableWrite(&var, &arg); |
| 965 | + |
| 966 | + return nullptr; |
| 967 | + }); |
| 968 | + |
| 969 | + compile(compiler, block); |
| 970 | +} |
| 971 | + |
908 | 972 | TEST_F(CompilerTest, CreateVariableWrite) |
909 | 973 | { |
910 | 974 | Compiler compiler(&m_engine, &m_target); |
|
0 commit comments