Skip to content

Commit 3f4c47d

Browse files
committed
Add onInit() method to IBlockSection
1 parent aa50fad commit 3f4c47d

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

include/scratchcpp/iblocksection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class LIBSCRATCHCPP_EXPORT IBlockSection
3636

3737
/*! Override this method to register blocks. */
3838
virtual void registerBlocks(IEngine *engine) = 0;
39+
40+
/*! This method is called when a project is loaded. */
41+
virtual void onInit(IEngine *engine) { }
3942
};
4043

4144
} // namespace libscratchcpp

src/engine/internal/engine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ void Engine::registerSection(std::shared_ptr<IBlockSection> section)
864864

865865
m_sections[section] = std::make_unique<BlockSectionContainer>();
866866
section->registerBlocks(this);
867+
section->onInit(this);
867868
}
868869
}
869870

test/engine/engine_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ TEST(EngineTest, Clear)
7979

8080
auto section = std::make_shared<BlockSectionMock>();
8181
EXPECT_CALL(*section, registerBlocks);
82+
EXPECT_CALL(*section, onInit);
8283
engine.registerSection(section);
8384

8485
auto monitor1 = std::make_shared<Monitor>("", "");
@@ -129,6 +130,7 @@ TEST(EngineTest, CompileAndExecuteMonitors)
129130

130131
auto section = std::make_shared<BlockSectionMock>();
131132
EXPECT_CALL(*section, registerBlocks);
133+
EXPECT_CALL(*section, onInit);
132134
engine.registerSection(section);
133135
engine.addCompileFunction(section.get(), m1->opcode(), [](Compiler *compiler) { compiler->addConstValue(5.4); });
134136
engine.addCompileFunction(section.get(), m2->opcode(), [](Compiler *compiler) { compiler->addConstValue("test"); });
@@ -863,14 +865,17 @@ TEST(EngineTest, Sections)
863865

864866
auto section1 = std::make_shared<BlockSectionMock>();
865867
EXPECT_CALL(*section1, registerBlocks(&engine));
868+
EXPECT_CALL(*section1, onInit(&engine));
866869
engine.registerSection(section1);
867870

868871
auto section2 = std::make_shared<BlockSectionMock>();
869872
EXPECT_CALL(*section2, registerBlocks(&engine));
873+
EXPECT_CALL(*section2, onInit(&engine));
870874
engine.registerSection(section2);
871875

872876
EXPECT_CALL(*section1, name()).WillOnce(Return("test"));
873877
EXPECT_CALL(*section1, registerBlocks).Times(0);
878+
EXPECT_CALL(*section1, onInit).Times(0);
874879
engine.registerSection(section1); // register existing section
875880

876881
ASSERT_EQ(engine.registeredSections().size(), 2);
@@ -916,11 +921,13 @@ TEST(EngineTest, CompileFunctions)
916921

917922
auto section1 = std::make_shared<BlockSectionMock>();
918923
EXPECT_CALL(*section1, registerBlocks);
924+
EXPECT_CALL(*section1, onInit);
919925
engine.registerSection(section1);
920926
auto container1 = engine.blockSectionContainer(section1.get());
921927

922928
auto section2 = std::make_shared<BlockSectionMock>();
923929
EXPECT_CALL(*section2, registerBlocks);
930+
EXPECT_CALL(*section2, onInit);
924931
engine.registerSection(section2);
925932
auto container2 = engine.blockSectionContainer(section2.get());
926933

@@ -943,11 +950,13 @@ TEST(EngineTest, HatPredicateCompileFunctions)
943950

944951
auto section1 = std::make_shared<BlockSectionMock>();
945952
EXPECT_CALL(*section1, registerBlocks);
953+
EXPECT_CALL(*section1, onInit);
946954
engine.registerSection(section1);
947955
auto container1 = engine.blockSectionContainer(section1.get());
948956

949957
auto section2 = std::make_shared<BlockSectionMock>();
950958
EXPECT_CALL(*section2, registerBlocks);
959+
EXPECT_CALL(*section2, onInit);
951960
engine.registerSection(section2);
952961
auto container2 = engine.blockSectionContainer(section2.get());
953962

@@ -970,11 +979,13 @@ TEST(EngineTest, MonitorNameFunctions)
970979

971980
auto section1 = std::make_shared<BlockSectionMock>();
972981
EXPECT_CALL(*section1, registerBlocks);
982+
EXPECT_CALL(*section1, onInit);
973983
engine.registerSection(section1);
974984
auto container1 = engine.blockSectionContainer(section1.get());
975985

976986
auto section2 = std::make_shared<BlockSectionMock>();
977987
EXPECT_CALL(*section2, registerBlocks);
988+
EXPECT_CALL(*section2, onInit);
978989
engine.registerSection(section2);
979990
auto container2 = engine.blockSectionContainer(section2.get());
980991

@@ -1007,11 +1018,13 @@ TEST(EngineTest, MonitorChangeFunctions)
10071018

10081019
auto section1 = std::make_shared<BlockSectionMock>();
10091020
EXPECT_CALL(*section1, registerBlocks);
1021+
EXPECT_CALL(*section1, onInit);
10101022
engine.registerSection(section1);
10111023
auto container1 = engine.blockSectionContainer(section1.get());
10121024

10131025
auto section2 = std::make_shared<BlockSectionMock>();
10141026
EXPECT_CALL(*section2, registerBlocks);
1027+
EXPECT_CALL(*section2, onInit);
10151028
engine.registerSection(section2);
10161029
auto container2 = engine.blockSectionContainer(section2.get());
10171030

@@ -1037,11 +1050,13 @@ TEST(EngineTest, HatBlocks)
10371050

10381051
auto section1 = std::make_shared<BlockSectionMock>();
10391052
EXPECT_CALL(*section1, registerBlocks);
1053+
EXPECT_CALL(*section1, onInit);
10401054
engine.registerSection(section1);
10411055
auto container1 = engine.blockSectionContainer(section1.get());
10421056

10431057
auto section2 = std::make_shared<BlockSectionMock>();
10441058
EXPECT_CALL(*section2, registerBlocks);
1059+
EXPECT_CALL(*section2, onInit);
10451060
engine.registerSection(section2);
10461061
auto container2 = engine.blockSectionContainer(section2.get());
10471062

@@ -1064,11 +1079,13 @@ TEST(EngineTest, Inputs)
10641079

10651080
auto section1 = std::make_shared<BlockSectionMock>();
10661081
EXPECT_CALL(*section1, registerBlocks);
1082+
EXPECT_CALL(*section1, onInit);
10671083
engine.registerSection(section1);
10681084
auto container1 = engine.blockSectionContainer(section1.get());
10691085

10701086
auto section2 = std::make_shared<BlockSectionMock>();
10711087
EXPECT_CALL(*section2, registerBlocks);
1088+
EXPECT_CALL(*section2, onInit);
10721089
engine.registerSection(section2);
10731090
auto container2 = engine.blockSectionContainer(section2.get());
10741091

@@ -1093,11 +1110,13 @@ TEST(EngineTest, Fields)
10931110

10941111
auto section1 = std::make_shared<BlockSectionMock>();
10951112
EXPECT_CALL(*section1, registerBlocks);
1113+
EXPECT_CALL(*section1, onInit);
10961114
engine.registerSection(section1);
10971115
auto container1 = engine.blockSectionContainer(section1.get());
10981116

10991117
auto section2 = std::make_shared<BlockSectionMock>();
11001118
EXPECT_CALL(*section2, registerBlocks);
1119+
EXPECT_CALL(*section2, onInit);
11011120
engine.registerSection(section2);
11021121
auto container2 = engine.blockSectionContainer(section2.get());
11031122

@@ -1122,11 +1141,13 @@ TEST(EngineTest, FieldValues)
11221141

11231142
auto section1 = std::make_shared<BlockSectionMock>();
11241143
EXPECT_CALL(*section1, registerBlocks);
1144+
EXPECT_CALL(*section1, onInit);
11251145
engine.registerSection(section1);
11261146
auto container1 = engine.blockSectionContainer(section1.get());
11271147

11281148
auto section2 = std::make_shared<BlockSectionMock>();
11291149
EXPECT_CALL(*section2, registerBlocks);
1150+
EXPECT_CALL(*section2, onInit);
11301151
engine.registerSection(section2);
11311152
auto container2 = engine.blockSectionContainer(section2.get());
11321153

test/mocks/blocksectionmock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class BlockSectionMock : public IBlockSection
1212
MOCK_METHOD(bool, categoryVisible, (), (const, override));
1313

1414
MOCK_METHOD(void, registerBlocks, (IEngine *), (override));
15+
MOCK_METHOD(void, onInit, (IEngine *), (override));
1516
};

0 commit comments

Comments
 (0)