Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/scratch/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ void Input::setValueBlock(std::shared_ptr<Block> block)
/*! Sets the ID of the value block. \see setValueBlock() */
void Input::setValueBlockId(const std::string &id)
{
impl->primaryValue.setValueBlockId(id);
impl->primaryValue.setValueBlock(nullptr);
impl->primaryValue.setValueBlockId(id);
}
26 changes: 25 additions & 1 deletion src/scratch/inputvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

#include <scratchcpp/inputvalue.h>
#include <scratchcpp/compiler.h>
#include <scratchcpp/entity.h>
#include <scratchcpp/block.h>
#include <scratchcpp/broadcast.h>
#include <scratchcpp/variable.h>
#include <scratchcpp/list.h>
#include <map>
#include <iostream>

#include "inputvalue_p.h"

Expand Down Expand Up @@ -82,6 +86,11 @@ const std::shared_ptr<Block> &InputValue::valueBlock() const
void InputValue::setValueBlock(const std::shared_ptr<Block> &newValueBlock)
{
impl->valueBlock = newValueBlock;

if (newValueBlock)
impl->valueBlockId = newValueBlock->id();
else
impl->valueBlockId = "";
}

/*! Returns the ID of the block. \see valueBlock() */
Expand All @@ -94,6 +103,7 @@ const std::string &InputValue::valueBlockId() const
void InputValue::setValueBlockId(const std::string &newValueBlockId)
{
impl->valueBlockId = newValueBlockId;
impl->valueBlock = nullptr;
}

/*! Returns a pointer to the value (e. g. a variable). */
Expand All @@ -105,7 +115,21 @@ std::shared_ptr<Entity> InputValue::valuePtr() const
/*! Sets the value pointer. */
void InputValue::setValuePtr(const std::shared_ptr<Entity> &newValuePtr)
{
if (std::dynamic_pointer_cast<Broadcast>(newValuePtr))
setType(Type::Broadcast);
else if (std::dynamic_pointer_cast<Variable>(newValuePtr))
setType(Type::Variable);
else if (std::dynamic_pointer_cast<List>(newValuePtr))
setType(Type::List);
else {
impl->valuePtr = nullptr;
impl->valueId = "";
std::cout << "warning: unsupported input value type (use InputValue::setValueBlock() to set the block)" << std::endl;
return;
}

impl->valuePtr = newValuePtr;
impl->valueId = newValuePtr->id();
}

/*! Returns the ID of the value. */
Expand Down
14 changes: 14 additions & 0 deletions test/scratch_classes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,17 @@ target_link_libraries(
)

gtest_discover_tests(input_test)

# inputvalue_test
add_executable(
inputvalue_test
inputvalue_test.cpp
)

target_link_libraries(
inputvalue_test
GTest::gtest_main
scratchcpp
)

gtest_discover_tests(inputvalue_test)
5 changes: 5 additions & 0 deletions test/scratch_classes/input_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ TEST(InputTest, ValueBlock)
input.setValueBlockId("hello");
ASSERT_EQ(input.valueBlockId(), "hello");
ASSERT_EQ(input.valueBlock(), nullptr);

input.setValueBlock(block);
input.setValueBlock(nullptr);
ASSERT_EQ(input.valueBlock(), nullptr);
ASSERT_EQ(input.valueBlockId(), "");
}
104 changes: 104 additions & 0 deletions test/scratch_classes/inputvalue_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include <scratchcpp/inputvalue.h>
#include <scratchcpp/block.h>
#include <scratchcpp/broadcast.h>
#include <scratchcpp/variable.h>
#include <scratchcpp/list.h>

#include "../common.h"

using namespace libscratchcpp;

TEST(InputValueTest, Constructors)
{
InputValue value1;
ASSERT_EQ(value1.type(), InputValue::Type::Number);

InputValue value2(InputValue::Type::String);
ASSERT_EQ(value2.type(), InputValue::Type::String);
}

TEST(InputTest, Type)
{
InputValue value;

value.setType(InputValue::Type::Color);
ASSERT_EQ(value.type(), InputValue::Type::Color);
}

TEST(InputTest, Value)
{
InputValue value;
ASSERT_EQ(value.value(), Value());

value.setValue("test");
ASSERT_EQ(value.value().toString(), "test");
}

TEST(InputTest, ValueBlock)
{
InputValue value;
ASSERT_EQ(value.valueBlock(), nullptr);
ASSERT_EQ(value.valueBlockId(), "");

auto block = std::make_shared<Block>("abc", "");
value.setValueBlock(block);
ASSERT_EQ(value.valueBlock(), block);
ASSERT_EQ(value.valueBlockId(), "abc");

value.setValueBlock(nullptr);
ASSERT_EQ(value.valueBlock(), nullptr);
ASSERT_EQ(value.valueBlockId(), "");

value.setValueBlockId("hello");
ASSERT_EQ(value.valueBlockId(), "hello");
ASSERT_EQ(value.valueBlock(), nullptr);
}

TEST(InputTest, ValuePtr)
{
InputValue value1;
ASSERT_EQ(value1.valuePtr(), nullptr);
ASSERT_EQ(value1.valueId(), "");

auto broadcast = std::make_shared<Broadcast>("abc", "");
value1.setValuePtr(broadcast);
ASSERT_EQ(value1.valuePtr(), broadcast);
ASSERT_EQ(value1.valueId(), "abc");
ASSERT_EQ(value1.type(), InputValue::Type::Broadcast);

auto variable = std::make_shared<Variable>("def", "");
value1.setValuePtr(variable);
ASSERT_EQ(value1.valuePtr(), variable);
ASSERT_EQ(value1.valueId(), "def");
ASSERT_EQ(value1.type(), InputValue::Type::Variable);

auto list = std::make_shared<List>("ghi", "");
value1.setValuePtr(list);
ASSERT_EQ(value1.valuePtr(), list);
ASSERT_EQ(value1.valueId(), "ghi");
ASSERT_EQ(value1.type(), InputValue::Type::List);

auto block = std::make_shared<Block>("jkl", "");
value1.setValuePtr(block);
ASSERT_EQ(value1.valuePtr(), nullptr);
ASSERT_EQ(value1.valueId(), "");
ASSERT_EQ(value1.type(), InputValue::Type::List);

value1.setValuePtr(nullptr);
ASSERT_EQ(value1.valuePtr(), nullptr);
ASSERT_EQ(value1.valueId(), "");
ASSERT_EQ(value1.type(), InputValue::Type::List);

InputValue value2(InputValue::Type::Integer);

value2.setValuePtr(block);
ASSERT_EQ(value2.valuePtr(), nullptr);
ASSERT_EQ(value1.valueId(), "");
ASSERT_EQ(value2.type(), InputValue::Type::Integer);

value2.setValuePtr(variable);
value2.setValueId("hello");
ASSERT_EQ(value2.valuePtr(), nullptr);
ASSERT_EQ(value2.valueId(), "hello");
ASSERT_EQ(value2.type(), InputValue::Type::Variable);
}