@@ -52,9 +52,11 @@ TEST_F(MotionBlocksTest, RegisterBlocks)
5252{
5353 // Blocks
5454 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " motion_movesteps" , &MotionBlocks::compileMoveSteps));
55+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " motion_turnright" , &MotionBlocks::compileTurnRight));
5556
5657 // Inputs
5758 EXPECT_CALL (m_engineMock, addInput (m_section.get (), " STEPS" , MotionBlocks::STEPS));
59+ EXPECT_CALL (m_engineMock, addInput (m_section.get (), " DEGREES" , MotionBlocks::DEGREES));
5860
5961 m_section->registerBlocks (&m_engineMock);
6062}
@@ -100,3 +102,42 @@ TEST_F(MotionBlocksTest, MoveStepsImpl)
100102 ASSERT_EQ (std::round (sprite.x () * 100 ) / 100 , -21.36 );
101103 ASSERT_EQ (std::round (sprite.y () * 100 ) / 100 , 14.22 );
102104}
105+
106+ TEST_F (MotionBlocksTest, TurnRight)
107+ {
108+ Compiler compiler (&m_engineMock);
109+
110+ // turn right (12.05) degrees
111+ auto block = std::make_shared<Block>(" a" , " motion_turnright" );
112+ addValueInput (block, " DEGREES" , MotionBlocks::DEGREES, 12.05 );
113+
114+ EXPECT_CALL (m_engineMock, functionIndex (&MotionBlocks::turnRight)).WillOnce (Return (0 ));
115+
116+ compiler.init ();
117+ compiler.setBlock (block);
118+ MotionBlocks::compileTurnRight (&compiler);
119+ compiler.end ();
120+
121+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm::OP_HALT }));
122+ ASSERT_EQ (compiler.constValues ().size (), 1 );
123+ ASSERT_EQ (compiler.constValues ()[0 ].toDouble (), 12.05 );
124+ }
125+
126+ TEST_F (MotionBlocksTest, TurnRightImpl)
127+ {
128+ static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm::OP_HALT };
129+ static BlockFunc functions[] = { &MotionBlocks::turnRight };
130+ static Value constValues[] = { 12.05 };
131+
132+ Sprite sprite;
133+ sprite.setDirection (124.37 );
134+
135+ VirtualMachine vm (&sprite, nullptr , nullptr );
136+ vm.setBytecode (bytecode);
137+ vm.setFunctions (functions);
138+ vm.setConstValues (constValues);
139+ vm.run ();
140+
141+ ASSERT_EQ (vm.registerCount (), 0 );
142+ ASSERT_EQ (std::round (sprite.direction () * 100 ) / 100 , 136.42 );
143+ }
0 commit comments