Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/scratchcpp/iblocksection.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class LIBSCRATCHCPP_EXPORT IBlockSection

/*! Override this method to register blocks. */
virtual void registerBlocks(IEngine *engine) = 0;

/*! This method is called when a project is loaded. */
virtual void onInit(IEngine *engine) { }
};

} // namespace libscratchcpp
6 changes: 6 additions & 0 deletions src/blocks/soundblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ void SoundBlocks::registerBlocks(IEngine *engine)
engine->addInput(this, "VOLUME", VOLUME);
}

void SoundBlocks::onInit(IEngine *engine)
{
m_waitingSounds.clear();
// TODO: Remove stopped threads from m_waitingSounds
}

bool SoundBlocks::compilePlayCommon(Compiler *compiler, bool untilDone, bool *byIndex)
{
Target *target = compiler->target();
Expand Down
1 change: 1 addition & 0 deletions src/blocks/soundblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SoundBlocks : public IBlockSection
std::string name() const override;

void registerBlocks(IEngine *engine) override;
void onInit(IEngine *engine) override;

static bool compilePlayCommon(Compiler *compiler, bool untilDone, bool *byIndex = nullptr);
static void compilePlay(Compiler *compiler);
Expand Down
1 change: 1 addition & 0 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ void Engine::registerSection(std::shared_ptr<IBlockSection> section)

m_sections[section] = std::make_unique<BlockSectionContainer>();
section->registerBlocks(this);
section->onInit(this);
}
}

Expand Down
2 changes: 0 additions & 2 deletions test/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
add_executable(
engine_test
engine_test.cpp
testsection.cpp
testsection.h
)

target_link_libraries(
Expand Down
173 changes: 152 additions & 21 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#include <audiooutputmock.h>
#include <audioplayermock.h>
#include <monitorhandlermock.h>
#include <blocksectionmock.h>
#include <thread>

#include "../common.h"
#include "testsection.h"
#include "engine/internal/engine.h"
#include "engine/internal/clock.h"

Expand Down Expand Up @@ -77,7 +77,9 @@ TEST(EngineTest, Clear)
auto broadcast2 = std::make_shared<Broadcast>("", "");
engine.setBroadcasts({ broadcast1, broadcast2 });

auto section = std::make_shared<TestSection>();
auto section = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section, registerBlocks);
EXPECT_CALL(*section, onInit);
engine.registerSection(section);

auto monitor1 = std::make_shared<Monitor>("", "");
Expand Down Expand Up @@ -126,7 +128,9 @@ TEST(EngineTest, CompileAndExecuteMonitors)
m2->setSprite(sprite.get());
engine.setMonitors({ m1, m2 });

auto section = std::make_shared<TestSection>();
auto section = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section, registerBlocks);
EXPECT_CALL(*section, onInit);
engine.registerSection(section);
engine.addCompileFunction(section.get(), m1->opcode(), [](Compiler *compiler) { compiler->addConstValue(5.4); });
engine.addCompileFunction(section.get(), m2->opcode(), [](Compiler *compiler) { compiler->addConstValue("test"); });
Expand Down Expand Up @@ -859,12 +863,19 @@ TEST(EngineTest, Sections)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks(&engine));
EXPECT_CALL(*section1, onInit(&engine));
engine.registerSection(section1);

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks(&engine));
EXPECT_CALL(*section2, onInit(&engine));
engine.registerSection(section2);

EXPECT_CALL(*section1, name()).WillOnce(Return("test"));
EXPECT_CALL(*section1, registerBlocks).Times(0);
EXPECT_CALL(*section1, onInit).Times(0);
engine.registerSection(section1); // register existing section

ASSERT_EQ(engine.registeredSections().size(), 2);
Expand Down Expand Up @@ -908,15 +919,19 @@ TEST(EngineTest, CompileFunctions)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

TestSection section3;
BlockSectionMock section3;

engine.addCompileFunction(section1.get(), "test1", &compileTest1);
engine.addCompileFunction(section2.get(), "test2", &compileTest2);
Expand All @@ -929,19 +944,123 @@ TEST(EngineTest, CompileFunctions)
ASSERT_EQ(container2->resolveBlockCompileFunc("test2"), &compileTest2);
}

TEST(EngineTest, HatPredicateCompileFunctions)
{
Engine engine;

auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

BlockSectionMock section3;

engine.addHatPredicateCompileFunction(section1.get(), "test1", &compileTest1);
engine.addHatPredicateCompileFunction(section2.get(), "test2", &compileTest2);
engine.addHatPredicateCompileFunction(section1.get(), "test1", &compileTest1);
engine.addHatPredicateCompileFunction(&section3, "test1", &compileTest1);

ASSERT_EQ(container1->resolveHatPredicateCompileFunc("test1"), &compileTest1);
ASSERT_EQ(container1->resolveHatPredicateCompileFunc("test2"), nullptr);
ASSERT_EQ(container2->resolveHatPredicateCompileFunc("test1"), nullptr);
ASSERT_EQ(container2->resolveHatPredicateCompileFunc("test2"), &compileTest2);
}

TEST(EngineTest, MonitorNameFunctions)
{
Engine engine;

auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

BlockSectionMock section3;

MonitorNameFunc f1 = [](Block *) -> const std::string & {
static const std::string ret;
return ret;
};

MonitorNameFunc f2 = [](Block *) -> const std::string & {
static const std::string ret;
return ret;
};

engine.addMonitorNameFunction(section1.get(), "test1", f1);
engine.addMonitorNameFunction(section2.get(), "test2", f2);
engine.addMonitorNameFunction(section1.get(), "test1", f1);
engine.addMonitorNameFunction(&section3, "test1", f1);

ASSERT_EQ(container1->resolveMonitorNameFunc("test1"), f1);
ASSERT_EQ(container1->resolveMonitorNameFunc("test2"), nullptr);
ASSERT_EQ(container2->resolveMonitorNameFunc("test1"), nullptr);
ASSERT_EQ(container2->resolveMonitorNameFunc("test2"), f2);
}

TEST(EngineTest, MonitorChangeFunctions)
{
Engine engine;

auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

BlockSectionMock section3;

MonitorChangeFunc f1 = [](Block *, const Value &) {};
MonitorChangeFunc f2 = [](Block *, const Value &) {};

engine.addMonitorChangeFunction(section1.get(), "test1", f1);
engine.addMonitorChangeFunction(section2.get(), "test2", f2);
engine.addMonitorChangeFunction(section1.get(), "test1", f1);
engine.addMonitorChangeFunction(&section3, "test1", f1);

ASSERT_EQ(container1->resolveMonitorChangeFunc("test1"), f1);
ASSERT_EQ(container1->resolveMonitorChangeFunc("test2"), nullptr);
ASSERT_EQ(container2->resolveMonitorChangeFunc("test1"), nullptr);
ASSERT_EQ(container2->resolveMonitorChangeFunc("test2"), f2);
}

TEST(EngineTest, HatBlocks)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

TestSection section3;
BlockSectionMock section3;

engine.addHatBlock(section1.get(), "test1");
engine.addHatBlock(section2.get(), "test2");
Expand All @@ -958,15 +1077,19 @@ TEST(EngineTest, Inputs)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

TestSection section3;
BlockSectionMock section3;

engine.addInput(section1.get(), "VALUE1", 1);
engine.addInput(section2.get(), "VALUE2", 2);
Expand All @@ -985,15 +1108,19 @@ TEST(EngineTest, Fields)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

TestSection section3;
BlockSectionMock section3;

engine.addField(section1.get(), "VALUE1", 1);
engine.addField(section2.get(), "VALUE2", 2);
Expand All @@ -1012,19 +1139,23 @@ TEST(EngineTest, FieldValues)
{
Engine engine;

auto section1 = std::make_shared<TestSection>();
auto section1 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section1, registerBlocks);
EXPECT_CALL(*section1, onInit);
engine.registerSection(section1);
auto container1 = engine.blockSectionContainer(section1.get());

auto section2 = std::make_shared<TestSection>();
auto section2 = std::make_shared<BlockSectionMock>();
EXPECT_CALL(*section2, registerBlocks);
EXPECT_CALL(*section2, onInit);
engine.registerSection(section2);
auto container2 = engine.blockSectionContainer(section2.get());

TestSection section3;
BlockSectionMock section3;

engine.addFieldValue(section1.get(), "value1", 1);
engine.addFieldValue(section2.get(), "value2", 2);
engine.addFieldValue(section1.get(), "value1", 3); // change ID of existing field
engine.addFieldValue(section1.get(), "value1", 3); // change ID of existing field value
engine.addFieldValue(&section3, "value3", 4);

ASSERT_EQ(container1->resolveFieldValue("value1"), 3);
Expand Down
14 changes: 0 additions & 14 deletions test/engine/testsection.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions test/engine/testsection.h

This file was deleted.

16 changes: 16 additions & 0 deletions test/mocks/blocksectionmock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <scratchcpp/iblocksection.h>
#include <gmock/gmock.h>

using namespace libscratchcpp;

class BlockSectionMock : public IBlockSection
{
public:
MOCK_METHOD(std::string, name, (), (const, override));
MOCK_METHOD(bool, categoryVisible, (), (const, override));

MOCK_METHOD(void, registerBlocks, (IEngine *), (override));
MOCK_METHOD(void, onInit, (IEngine *), (override));
};