Skip to content

Commit 157bda5

Browse files
committed
Implement monitor methods
1 parent c0e7399 commit 157bda5

File tree

5 files changed

+19
-38
lines changed

5 files changed

+19
-38
lines changed

include/scratchcpp/imonitorhandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class LIBSCRATCHCPP_EXPORT IMonitorHandler
1717

1818
virtual void init(Monitor *monitor) = 0;
1919

20-
// TODO: Add onValueChanged()
21-
// virtual void onValueChanged(const VirtualMachine *vm) = 0;
20+
virtual void onValueChanged(const Value &value) = 0;
2221
virtual void onXChanged(int x) = 0;
2322
virtual void onYChanged(int y) = 0;
2423
virtual void onVisibleChanged(bool visible) = 0;

include/scratchcpp/monitor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class LIBSCRATCHCPP_EXPORT Monitor : public Entity
5656

5757
const std::string &opcode() const;
5858

59-
// TODO: Add updateValue()
60-
// void updateValue(const VirtualMachine *vm);
59+
void updateValue(const Value &value);
6160

6261
void setValueChangeFunction(MonitorChangeFunc f);
6362
void changeValue(const Value &newValue);

src/scratch/monitor.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,15 @@ const std::string &Monitor::opcode() const
115115
return impl->block->opcode();
116116
}
117117

118-
// TODO: Add updateValue()
119118
/*!
120119
* Notifies the monitor's interface about value change.
121120
* The interaface is supposed to read it from the VirtualMachine.
122121
*/
123-
/*void Monitor::updateValue(const VirtualMachine *vm)
122+
void Monitor::updateValue(const Value &value)
124123
{
125124
if (impl->iface)
126-
impl->iface->onValueChanged(vm);
127-
}*/
125+
impl->iface->onValueChanged(value);
126+
}
128127

129128
/*!
130129
* Sets the function which is called to change the monitor's value.
@@ -145,10 +144,8 @@ void Monitor::changeValue(const Value &newValue)
145144
if (impl->changeFunc)
146145
impl->changeFunc(impl->block.get(), newValue);
147146

148-
if (impl->iface) {
149-
// TODO: Implement this
150-
// impl->iface->onValueChanged(...);
151-
}
147+
if (impl->iface)
148+
impl->iface->onValueChanged(newValue);
152149
}
153150

154151
/*! Returns the monitor's width. */

test/mocks/monitorhandlermock.h

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

13-
// TODO: Add onValueChanged()
14-
// MOCK_METHOD(void, onValueChanged, (const VirtualMachine *), (override));
13+
MOCK_METHOD(void, onValueChanged, (const Value &), (override));
1514
MOCK_METHOD(void, onXChanged, (int), (override));
1615
MOCK_METHOD(void, onYChanged, (int), (override));
1716
MOCK_METHOD(void, onVisibleChanged, (bool), (override));

test/scratch_classes/monitor_test.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using namespace libscratchcpp;
1313

1414
using ::testing::Return;
15-
using ::testing::SaveArg;
1615
using ::testing::_;
1716

1817
static const int PADDING = 5;
@@ -111,26 +110,20 @@ TEST(MonitorTest, Sprite)
111110
ASSERT_EQ(block->target(), nullptr);
112111
}
113112

114-
/*TEST(MonitorTest, UpdateValue)
113+
TEST(MonitorTest, UpdateValue)
115114
{
116115
Monitor monitor("", "");
117-
VirtualMachine vm1, vm2;
118116
monitor.updateValue(nullptr);
119-
monitor.updateValue(&vm1);
117+
monitor.updateValue("test");
120118

121119
MonitorHandlerMock handler;
122120
EXPECT_CALL(handler, init);
123121
monitor.setInterface(&handler);
124122

125-
EXPECT_CALL(handler, onValueChanged(&vm1));
126-
monitor.updateValue(&vm1);
127-
128-
EXPECT_CALL(handler, onValueChanged(nullptr));
129-
monitor.updateValue(nullptr);
130-
131-
EXPECT_CALL(handler, onValueChanged(&vm2));
132-
monitor.updateValue(&vm2);
133-
}*/
123+
Value v = 123;
124+
EXPECT_CALL(handler, onValueChanged(v));
125+
monitor.updateValue(v);
126+
}
134127

135128
TEST(MonitorTest, ChangeValue)
136129
{
@@ -141,20 +134,14 @@ TEST(MonitorTest, ChangeValue)
141134
EXPECT_CALL(handler, init);
142135
monitor.setInterface(&handler);
143136

144-
/*const VirtualMachine *vm = nullptr;
145-
EXPECT_CALL(handler, onValueChanged(_)).WillOnce(SaveArg<0>(&vm));*/
146-
monitor.changeValue(0.25);
147-
/*ASSERT_TRUE(vm);
148-
ASSERT_EQ(vm->registerCount(), 1);
149-
ASSERT_EQ(vm->getInput(0, 1)->toDouble(), 0.25);*/
137+
Value v = "test";
138+
EXPECT_CALL(handler, onValueChanged(v));
139+
monitor.changeValue(v);
150140

151141
monitor.setValueChangeFunction([](Block *block, const Value &newValue) { std::cout << block->opcode() + " " + newValue.toString() << std::endl; });
152-
// EXPECT_CALL(handler, onValueChanged(_)).WillOnce(SaveArg<0>(&vm));
142+
EXPECT_CALL(handler, onValueChanged(v));
153143
testing::internal::CaptureStdout();
154-
monitor.changeValue("test");
155-
/*ASSERT_TRUE(vm);
156-
ASSERT_EQ(vm->registerCount(), 1);
157-
ASSERT_EQ(vm->getInput(0, 1)->toString(), "test");*/
144+
monitor.changeValue(v);
158145
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test_block test\n");
159146
}
160147

0 commit comments

Comments
 (0)