@@ -95,6 +95,7 @@ TEST_F(SoundBlocksTest, RegisterBlocks)
9595{
9696 // Blocks
9797 EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sound_changevolumeby" , &SoundBlocks::compileChangeVolumeBy));
98+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sound_setvolumeto" , &SoundBlocks::compileSetVolumeTo));
9899
99100 // Inputs
100101 EXPECT_CALL (m_engineMock, addInput (m_section.get (), " VOLUME" , SoundBlocks::VOLUME));
@@ -157,3 +158,43 @@ TEST_F(SoundBlocksTest, ChangeVolumeByImpl)
157158 ASSERT_EQ (vm.registerCount (), 0 );
158159 ASSERT_EQ (std::round (target.volume () * 100 ) / 100 , 93.32 );
159160}
161+
162+ TEST_F (SoundBlocksTest, SetVolumeTo)
163+ {
164+ Compiler compiler (&m_engineMock);
165+
166+ // set volume to (43.409) %
167+ auto block = std::make_shared<Block>(" a" , " sound_setvolumeto" );
168+ addValueInput (block, " VOLUME" , SoundBlocks::VOLUME, 43.409 );
169+
170+ EXPECT_CALL (m_engineMock, functionIndex (&SoundBlocks::setVolumeTo)).WillOnce (Return (0 ));
171+
172+ compiler.init ();
173+ compiler.setBlock (block);
174+ SoundBlocks::compileSetVolumeTo (&compiler);
175+ compiler.end ();
176+
177+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm::OP_HALT }));
178+ ASSERT_EQ (compiler.constValues ().size (), 1 );
179+ ASSERT_EQ (compiler.constValues ()[0 ].toDouble (), 43.409 );
180+ }
181+
182+ TEST_F (SoundBlocksTest, SetVolumeToImpl)
183+ {
184+ static unsigned int bytecode[] = { vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm::OP_HALT };
185+ static BlockFunc functions[] = { &SoundBlocks::setVolumeTo };
186+ static Value constValues[] = { 43.409 };
187+
188+ Target target;
189+ target.setVolume (42.4 );
190+
191+ VirtualMachine vm (&target, nullptr , nullptr );
192+
193+ vm.setBytecode (bytecode);
194+ vm.setFunctions (functions);
195+ vm.setConstValues (constValues);
196+ vm.run ();
197+
198+ ASSERT_EQ (vm.registerCount (), 0 );
199+ ASSERT_EQ (target.volume (), 43.409 );
200+ }
0 commit comments