From 7ed99b5a1d0a88c3ebda90410546650cd6d6f690 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Wed, 25 Dec 2024 12:38:18 +0100 Subject: [PATCH 1/3] Implement data_variable --- src/dev/blocks/variableblocks.cpp | 20 +++++++++++ src/dev/blocks/variableblocks.h | 3 ++ test/dev/blocks/variable_blocks_test.cpp | 43 +++++++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/dev/blocks/variableblocks.cpp b/src/dev/blocks/variableblocks.cpp index 6d0f464f..545a68ea 100644 --- a/src/dev/blocks/variableblocks.cpp +++ b/src/dev/blocks/variableblocks.cpp @@ -1,5 +1,12 @@ // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include +#include +#include +#include + #include "variableblocks.h" using namespace libscratchcpp; @@ -16,4 +23,17 @@ std::string VariableBlocks::description() const void VariableBlocks::registerBlocks(IEngine *engine) { + engine->addCompileFunction(this, "data_variable", &compileVariable); +} + +CompilerValue *VariableBlocks::compileVariable(Compiler *compiler) +{ + Field *varField = compiler->field("VARIABLE"); + Variable *var = static_cast(varField->valuePtr().get()); + assert(var); + + if (var) + return compiler->addVariableValue(var); + else + return compiler->addConstValue(Value()); } diff --git a/src/dev/blocks/variableblocks.h b/src/dev/blocks/variableblocks.h index 6c4144ac..3f579880 100644 --- a/src/dev/blocks/variableblocks.h +++ b/src/dev/blocks/variableblocks.h @@ -14,6 +14,9 @@ class VariableBlocks : public IExtension std::string description() const override; void registerBlocks(IEngine *engine) override; + + private: + static CompilerValue *compileVariable(Compiler *compiler); }; } // namespace libscratchcpp diff --git a/test/dev/blocks/variable_blocks_test.cpp b/test/dev/blocks/variable_blocks_test.cpp index 0a480f33..a21343bd 100644 --- a/test/dev/blocks/variable_blocks_test.cpp +++ b/test/dev/blocks/variable_blocks_test.cpp @@ -1,15 +1,56 @@ +#include +#include +#include +#include +#include +#include #include #include "../common.h" #include "dev/blocks/variableblocks.h" using namespace libscratchcpp; +using namespace libscratchcpp::test; class VariableBlocksTest : public testing::Test { public: - void SetUp() override { m_extension = std::make_unique(); } + void SetUp() override + { + m_extension = std::make_unique(); + m_engine = m_project.engine().get(); + m_extension->registerBlocks(m_engine); + } std::unique_ptr m_extension; + Project m_project; + IEngine *m_engine = nullptr; EngineMock m_engineMock; }; + +TEST_F(VariableBlocksTest, Variable) +{ + auto target = std::make_shared(); + auto var1 = std::make_shared("", "", 835.21); + target->addVariable(var1); + auto var2 = std::make_shared("", "", "Hello world"); + target->addVariable(var2); + ScriptBuilder builder(m_extension.get(), m_engine, target); + + builder.addBlock("data_variable"); + builder.addEntityField("VARIABLE", var1); + builder.captureBlockReturnValue(); + + builder.addBlock("data_variable"); + builder.addEntityField("VARIABLE", var2); + builder.captureBlockReturnValue(); + + builder.build(); + builder.run(); + + List *valueList = builder.capturedValues(); + ValueData *values = valueList->data(); + ASSERT_EQ(valueList->size(), 2); + ASSERT_EQ(Value(values[0]), 835.21); + ASSERT_EQ(Value(values[1]), "Hello world"); +} From 5d2b4253c45bb5a4aba7a5715477a453cbe8a0f9 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Wed, 25 Dec 2024 12:38:59 +0100 Subject: [PATCH 2/3] Implement data_setvariableto --- src/dev/blocks/variableblocks.cpp | 13 +++++++++++++ src/dev/blocks/variableblocks.h | 1 + test/dev/blocks/variable_blocks_test.cpp | 23 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/dev/blocks/variableblocks.cpp b/src/dev/blocks/variableblocks.cpp index 545a68ea..7701502c 100644 --- a/src/dev/blocks/variableblocks.cpp +++ b/src/dev/blocks/variableblocks.cpp @@ -24,6 +24,7 @@ std::string VariableBlocks::description() const void VariableBlocks::registerBlocks(IEngine *engine) { engine->addCompileFunction(this, "data_variable", &compileVariable); + engine->addCompileFunction(this, "data_setvariableto", &compileSetVariableTo); } CompilerValue *VariableBlocks::compileVariable(Compiler *compiler) @@ -37,3 +38,15 @@ CompilerValue *VariableBlocks::compileVariable(Compiler *compiler) else return compiler->addConstValue(Value()); } + +CompilerValue *VariableBlocks::compileSetVariableTo(Compiler *compiler) +{ + Field *varField = compiler->field("VARIABLE"); + Variable *var = static_cast(varField->valuePtr().get()); + assert(var); + + if (var) + compiler->createVariableWrite(var, compiler->addInput("VALUE")); + + return nullptr; +} diff --git a/src/dev/blocks/variableblocks.h b/src/dev/blocks/variableblocks.h index 3f579880..cb563fca 100644 --- a/src/dev/blocks/variableblocks.h +++ b/src/dev/blocks/variableblocks.h @@ -17,6 +17,7 @@ class VariableBlocks : public IExtension private: static CompilerValue *compileVariable(Compiler *compiler); + static CompilerValue *compileSetVariableTo(Compiler *compiler); }; } // namespace libscratchcpp diff --git a/test/dev/blocks/variable_blocks_test.cpp b/test/dev/blocks/variable_blocks_test.cpp index a21343bd..c7cc5694 100644 --- a/test/dev/blocks/variable_blocks_test.cpp +++ b/test/dev/blocks/variable_blocks_test.cpp @@ -54,3 +54,26 @@ TEST_F(VariableBlocksTest, Variable) ASSERT_EQ(Value(values[0]), 835.21); ASSERT_EQ(Value(values[1]), "Hello world"); } + +TEST_F(VariableBlocksTest, SetVariableTo) +{ + auto target = std::make_shared(); + auto var1 = std::make_shared("", "", 835.21); + target->addVariable(var1); + auto var2 = std::make_shared("", "", "Hello world"); + target->addVariable(var2); + ScriptBuilder builder(m_extension.get(), m_engine, target); + + builder.addBlock("data_setvariableto"); + builder.addEntityField("VARIABLE", var1); + builder.addValueInput("VALUE", "test"); + + builder.addBlock("data_setvariableto"); + builder.addEntityField("VARIABLE", var2); + builder.addValueInput("VALUE", 123); + + builder.build(); + builder.run(); + ASSERT_EQ(var1->value(), "test"); + ASSERT_EQ(var2->value(), 123); +} From f09dce90d755125d4c8c615f8216f2a60fdb1fe8 Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Wed, 25 Dec 2024 12:52:15 +0100 Subject: [PATCH 3/3] Implement data_changevariableby --- src/dev/blocks/variableblocks.cpp | 15 +++++++++++ src/dev/blocks/variableblocks.h | 1 + test/dev/blocks/variable_blocks_test.cpp | 32 ++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/dev/blocks/variableblocks.cpp b/src/dev/blocks/variableblocks.cpp index 7701502c..177d8dab 100644 --- a/src/dev/blocks/variableblocks.cpp +++ b/src/dev/blocks/variableblocks.cpp @@ -25,6 +25,7 @@ void VariableBlocks::registerBlocks(IEngine *engine) { engine->addCompileFunction(this, "data_variable", &compileVariable); engine->addCompileFunction(this, "data_setvariableto", &compileSetVariableTo); + engine->addCompileFunction(this, "data_changevariableby", &compileChangeVariableBy); } CompilerValue *VariableBlocks::compileVariable(Compiler *compiler) @@ -50,3 +51,17 @@ CompilerValue *VariableBlocks::compileSetVariableTo(Compiler *compiler) return nullptr; } + +CompilerValue *VariableBlocks::compileChangeVariableBy(Compiler *compiler) +{ + Field *varField = compiler->field("VARIABLE"); + Variable *var = static_cast(varField->valuePtr().get()); + assert(var); + + if (var) { + CompilerValue *value = compiler->createAdd(compiler->addVariableValue(var), compiler->addInput("VALUE")); + compiler->createVariableWrite(var, value); + } + + return nullptr; +} diff --git a/src/dev/blocks/variableblocks.h b/src/dev/blocks/variableblocks.h index cb563fca..9cb0bcc4 100644 --- a/src/dev/blocks/variableblocks.h +++ b/src/dev/blocks/variableblocks.h @@ -18,6 +18,7 @@ class VariableBlocks : public IExtension private: static CompilerValue *compileVariable(Compiler *compiler); static CompilerValue *compileSetVariableTo(Compiler *compiler); + static CompilerValue *compileChangeVariableBy(Compiler *compiler); }; } // namespace libscratchcpp diff --git a/test/dev/blocks/variable_blocks_test.cpp b/test/dev/blocks/variable_blocks_test.cpp index c7cc5694..2789f289 100644 --- a/test/dev/blocks/variable_blocks_test.cpp +++ b/test/dev/blocks/variable_blocks_test.cpp @@ -77,3 +77,35 @@ TEST_F(VariableBlocksTest, SetVariableTo) ASSERT_EQ(var1->value(), "test"); ASSERT_EQ(var2->value(), 123); } + +TEST_F(VariableBlocksTest, ChangeVariableBy) +{ + auto target = std::make_shared(); + auto var1 = std::make_shared("", "", 835.21); + target->addVariable(var1); + auto var2 = std::make_shared("", "", "Hello world"); + target->addVariable(var2); + ScriptBuilder builder(m_extension.get(), m_engine, target); + + builder.addBlock("data_changevariableby"); + builder.addEntityField("VARIABLE", var1); + builder.addValueInput("VALUE", "5.12"); + + builder.addBlock("data_changevariableby"); + builder.addEntityField("VARIABLE", var2); + builder.addValueInput("VALUE", -2.5); + + builder.build(); + + builder.run(); + ASSERT_EQ(var1->value(), 840.33); + ASSERT_EQ(var2->value(), -2.5); + + builder.run(); + ASSERT_EQ(var1->value(), 845.45); + ASSERT_EQ(var2->value(), -5); + + builder.run(); + ASSERT_EQ(var1->value(), 850.57); + ASSERT_EQ(var2->value(), -7.5); +}