@@ -54,6 +54,14 @@ class SensingBlocksTest : public testing::Test
54
54
return block;
55
55
}
56
56
57
+ void addValueInput (std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id, const Value &value) const
58
+ {
59
+ auto input = std::make_shared<Input>(name, Input::Type::Shadow);
60
+ input->setPrimaryValue (value);
61
+ input->setInputId (id);
62
+ block->addInput (input);
63
+ }
64
+
57
65
void addObscuredInput (std::shared_ptr<Block> block, const std::string &name, SensingBlocks::Inputs id, std::shared_ptr<Block> valueBlock) const
58
66
{
59
67
auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow);
@@ -117,6 +125,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
117
125
{
118
126
// Blocks
119
127
EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_touchingobject" , &SensingBlocks::compileTouchingObject));
128
+ EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_touchingcolor" , &SensingBlocks::compileTouchingColor));
120
129
EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_distanceto" , &SensingBlocks::compileDistanceTo));
121
130
EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_askandwait" , &SensingBlocks::compileAskAndWait));
122
131
EXPECT_CALL (m_engineMock, addCompileFunction (m_section.get (), " sensing_answer" , &SensingBlocks::compileAnswer));
@@ -144,6 +153,7 @@ TEST_F(SensingBlocksTest, RegisterBlocks)
144
153
145
154
// Inputs
146
155
EXPECT_CALL (m_engineMock, addInput (m_section.get (), " TOUCHINGOBJECTMENU" , SensingBlocks::TOUCHINGOBJECTMENU));
156
+ EXPECT_CALL (m_engineMock, addInput (m_section.get (), " COLOR" , SensingBlocks::COLOR));
147
157
EXPECT_CALL (m_engineMock, addInput (m_section.get (), " DISTANCETOMENU" , SensingBlocks::DISTANCETOMENU));
148
158
EXPECT_CALL (m_engineMock, addInput (m_section.get (), " QUESTION" , SensingBlocks::QUESTION));
149
159
EXPECT_CALL (m_engineMock, addInput (m_section.get (), " KEY_OPTION" , SensingBlocks::KEY_OPTION));
@@ -371,6 +381,78 @@ TEST_F(SensingBlocksTest, TouchingObjectImpl)
371
381
ASSERT_FALSE (vm.getInput (0 , 1 )->toBool ());
372
382
}
373
383
384
+ TEST_F (SensingBlocksTest, TouchingColor)
385
+ {
386
+ Compiler compiler (&m_engineMock);
387
+
388
+ // touching color (#FFFF00)
389
+ auto block1 = std::make_shared<Block>(" a" , " sensing_touchingcolor" );
390
+ addValueInput (block1, " COLOR" , SensingBlocks::COLOR, " #FFFF00" );
391
+
392
+ // touching color (null block)
393
+ auto block2 = std::make_shared<Block>(" b" , " sensing_touchingcolor" );
394
+ addDropdownInput (block2, " COLOR" , SensingBlocks::COLOR, " " , createNullBlock (" c" ));
395
+
396
+ compiler.init ();
397
+
398
+ EXPECT_CALL (m_engineMock, functionIndex (&SensingBlocks::touchingColor)).WillOnce (Return (1 ));
399
+ compiler.setBlock (block1);
400
+ SensingBlocks::compileTouchingColor (&compiler);
401
+
402
+ EXPECT_CALL (m_engineMock, functionIndex (&SensingBlocks::touchingColor)).WillOnce (Return (1 ));
403
+ compiler.setBlock (block2);
404
+ SensingBlocks::compileTouchingColor (&compiler);
405
+
406
+ compiler.end ();
407
+
408
+ ASSERT_EQ (compiler.bytecode (), std::vector<unsigned int >({ vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 1 , vm::OP_NULL, vm::OP_EXEC, 1 , vm::OP_HALT }));
409
+ ASSERT_EQ (compiler.constValues ().size (), 1 );
410
+ ASSERT_EQ (compiler.constValues ()[0 ].toString (), " #FFFF00" );
411
+ }
412
+
413
+ TEST_F (SensingBlocksTest, TouchingColorImpl)
414
+ {
415
+ static unsigned int bytecode1[] = { vm::OP_START, vm::OP_CONST, 0 , vm::OP_EXEC, 0 , vm::OP_HALT };
416
+ static unsigned int bytecode2[] = { vm::OP_START, vm::OP_CONST, 1 , vm::OP_EXEC, 0 , vm::OP_HALT };
417
+ static BlockFunc functions[] = { &SensingBlocks::touchingColor };
418
+ static Value constValues[] = { " #FFFF00" , 1946195606 };
419
+
420
+ TargetMock target;
421
+ Sprite sprite;
422
+ VirtualMachine vm (&target, nullptr , nullptr );
423
+ vm.setFunctions (functions);
424
+ vm.setConstValues (constValues);
425
+
426
+ EXPECT_CALL (target, touchingColor (constValues[0 ])).WillOnce (Return (false ));
427
+ vm.setBytecode (bytecode1);
428
+ vm.run ();
429
+
430
+ ASSERT_EQ (vm.registerCount (), 1 );
431
+ ASSERT_FALSE (vm.getInput (0 , 1 )->toBool ());
432
+
433
+ EXPECT_CALL (target, touchingColor (constValues[0 ])).WillOnce (Return (true ));
434
+ vm.reset ();
435
+ vm.run ();
436
+
437
+ ASSERT_EQ (vm.registerCount (), 1 );
438
+ ASSERT_TRUE (vm.getInput (0 , 1 )->toBool ());
439
+
440
+ EXPECT_CALL (target, touchingColor (constValues[1 ])).WillOnce (Return (false ));
441
+ vm.reset ();
442
+ vm.setBytecode (bytecode2);
443
+ vm.run ();
444
+
445
+ ASSERT_EQ (vm.registerCount (), 1 );
446
+ ASSERT_FALSE (vm.getInput (0 , 1 )->toBool ());
447
+
448
+ EXPECT_CALL (target, touchingColor (constValues[1 ])).WillOnce (Return (true ));
449
+ vm.reset ();
450
+ vm.run ();
451
+
452
+ ASSERT_EQ (vm.registerCount (), 1 );
453
+ ASSERT_TRUE (vm.getInput (0 , 1 )->toBool ());
454
+ }
455
+
374
456
TEST_F (SensingBlocksTest, DistanceTo)
375
457
{
376
458
Compiler compiler (&m_engineMock);
0 commit comments