11#include < scratchcpp/compiler.h>
22#include < scratchcpp/block.h>
33#include < enginemock.h>
4+ #include < timermock.h>
45
56#include " ../common.h"
67#include " blocks/sensingblocks.h"
78#include " engine/internal/engine.h"
89
910using namespace libscratchcpp ;
1011
12+ using ::testing::Return;
13+
1114class SensingBlocksTest : public testing ::Test
1215{
1316 public:
@@ -20,6 +23,7 @@ class SensingBlocksTest : public testing::Test
2023 std::unique_ptr<IBlockSection> m_section;
2124 EngineMock m_engineMock;
2225 Engine m_engine;
26+ TimerMock m_timerMock;
2327};
2428
2529TEST_F (SensingBlocksTest, Name)
@@ -34,5 +38,42 @@ TEST_F(SensingBlocksTest, CategoryVisible)
3438
3539TEST_F (SensingBlocksTest, RegisterBlocks)
3640{
41+ // Blocks
42+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_timer" , &SensingBlocks::compileTimer)).Times (1 );
43+
3744 m_section->registerBlocks (&m_engineMock);
3845}
46+
47+ TEST_F (SensingBlocksTest, Timer)
48+ {
49+ Compiler compiler (&m_engineMock);
50+
51+ auto block = std::make_shared<Block>(" a" , " sensing_timer" );
52+
53+ EXPECT_CALL (m_engineMock, functionIndex (&SensingBlocks::timer)).WillOnce (Return (0 ));
54+
55+ compiler.init ();
56+ compiler.setBlock (block);
57+ SensingBlocks::compileTimer (&compiler);
58+ compiler.end ();
59+
60+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_EXEC, 0 , vm::OP_HALT }));
61+ }
62+
63+ TEST_F (SensingBlocksTest, TimerImpl)
64+ {
65+ static unsigned int bytecode[] = { vm::OP_START, vm::OP_EXEC, 0 , vm::OP_HALT };
66+ static BlockFunc functions[] = { &SensingBlocks::timer };
67+
68+ VirtualMachine vm (nullptr , &m_engineMock, nullptr );
69+ vm.setFunctions (functions);
70+
71+ EXPECT_CALL (m_engineMock, timer ()).WillOnce (Return (&m_timerMock));
72+ EXPECT_CALL (m_timerMock, value ()).WillOnce (Return (2.375 ));
73+
74+ vm.setBytecode (bytecode);
75+ vm.run ();
76+
77+ ASSERT_EQ (vm.registerCount (), 1 );
78+ ASSERT_EQ (vm.getInput (0 , 1 )->toDouble (), 2.375 );
79+ }
0 commit comments