Skip to content

Commit 7f5ef8c

Browse files
committed
Add updateValue() method to Monitor
1 parent e0a633d commit 7f5ef8c

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

include/scratchcpp/imonitorhandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace libscratchcpp
88
{
99

1010
class Monitor;
11+
class Value;
1112

1213
class LIBSCRATCHCPP_EXPORT IMonitorHandler
1314
{
@@ -16,6 +17,7 @@ class LIBSCRATCHCPP_EXPORT IMonitorHandler
1617

1718
virtual void init(Monitor *) = 0;
1819

20+
virtual void onValueChanged(const Value &value) = 0;
1921
virtual void onVisibleChanged(bool visible) = 0;
2022
};
2123

include/scratchcpp/monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace libscratchcpp
1212
class IMonitorHandler;
1313
class Block;
1414
class Sprite;
15+
class Value;
1516
class Rect;
1617
class MonitorPrivate;
1718

@@ -44,6 +45,8 @@ class LIBSCRATCHCPP_EXPORT Monitor : public Entity
4445

4546
const std::string &opcode() const;
4647

48+
void updateValue(const Value &value);
49+
4750
unsigned int width() const;
4851
void setWidth(unsigned int width);
4952

src/scratch/monitor.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ const std::string &Monitor::opcode() const
6868
return impl->block->opcode();
6969
}
7070

71+
/*! Updates the monitor's value and notifies its interface about it (if there's any). */
72+
void Monitor::updateValue(const Value &value)
73+
{
74+
if (impl->iface)
75+
impl->iface->onValueChanged(value);
76+
}
77+
7178
/*! Returns the monitor's width. */
7279
unsigned int Monitor::width() const
7380
{

test/mocks/monitorhandlermock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ class MonitorHandlerMock : public IMonitorHandler
1010
public:
1111
MOCK_METHOD(void, init, (Monitor *), (override));
1212

13+
MOCK_METHOD(void, onValueChanged, (const Value &), (override));
1314
MOCK_METHOD(void, onVisibleChanged, (bool), (override));
1415
};

test/scratch_classes/monitor_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ TEST(MonitorTest, Sprite)
7878
ASSERT_EQ(block->target(), nullptr);
7979
}
8080

81+
TEST(MonitorTest, UpdateValue)
82+
{
83+
Monitor monitor("", "");
84+
monitor.updateValue(5);
85+
86+
MonitorHandlerMock handler;
87+
EXPECT_CALL(handler, init);
88+
monitor.setInterface(&handler);
89+
90+
EXPECT_CALL(handler, onValueChanged(Value(6.5)));
91+
monitor.updateValue(6.5);
92+
93+
EXPECT_CALL(handler, onValueChanged(Value("test")));
94+
monitor.updateValue("test");
95+
}
96+
8197
TEST(MonitorTest, Width)
8298
{
8399
Monitor monitor("", "");

0 commit comments

Comments
 (0)