Skip to content

Commit

Permalink
Add a test for ensuring default behaviors on objects (#7211)
Browse files Browse the repository at this point in the history
  • Loading branch information
4ian authored Nov 29, 2024
1 parent 689bc01 commit 13bdfa4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Core/GDCore/Extensions/Metadata/ObjectMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ class GD_CORE_API ObjectMetadata : public InstructionOrExpressionContainerMetada
return *this;
}

ObjectMetadata& ResetDefaultBehaviorsJustForTesting() {
defaultBehaviorTypes.clear();
return *this;
}

const gd::String& GetName() const override { return name; }
const gd::String& GetFullName() const override { return fullname; }
const gd::String& GetCategoryFullName() const { return categoryFullName; }
Expand Down
74 changes: 74 additions & 0 deletions Core/tests/Project-EnsureObjectDefaultBehaviors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival ([email protected]). All rights
* reserved. This project is released under the MIT License.
*/
/**
* @file Tests covering common features of GDevelop Core.
*/
#include "GDCore/Project/Project.h"

#include <algorithm>

#include "DummyPlatform.h"
#include "GDCore/Extensions/Metadata/MetadataProvider.h"
#include "GDCore/Extensions/Platform.h"
#include "GDCore/Extensions/PlatformExtension.h"
#include "GDCore/Project/Object.h"
#include "GDCore/String.h"
#include "catch.hpp"

TEST_CASE("Project::EnsureObjectDefaultBehaviors", "[common]") {
SECTION("Check that default behaviors are added to an object") {
gd::Platform platform;
gd::Project project;
SetupProjectWithDummyPlatform(project, platform);

auto myObject = project.CreateObject("MyExtension::Sprite", "MyObject");
REQUIRE(myObject->GetType() == "MyExtension::Sprite");
REQUIRE(myObject->GetAllBehaviorNames().size() == 0);

project.EnsureObjectDefaultBehaviors(*myObject);
REQUIRE(myObject->GetAllBehaviorNames().size() == 0);

// Modify the "Sprite" extension to add a default behavior to the object.
const auto& allExtensions = platform.GetAllPlatformExtensions();
auto spriteExtensionIt = std::find_if(
allExtensions.begin(),
allExtensions.end(),
[&](const std::shared_ptr<gd::PlatformExtension>& extension) {
return extension->GetName() == "MyExtension";
});
REQUIRE(spriteExtensionIt != allExtensions.end());
auto spriteExtension = *spriteExtensionIt;

auto& spriteObjectMetadata =
spriteExtension->GetObjectMetadata("MyExtension::Sprite");
REQUIRE(gd::MetadataProvider::IsBadObjectMetadata(spriteObjectMetadata) ==
false);

spriteObjectMetadata.AddDefaultBehavior(
"FlippableCapability::FlippableBehavior");

// Ensure the default behavior is added.
project.EnsureObjectDefaultBehaviors(*myObject);
REQUIRE(myObject->GetAllBehaviorNames().size() == 1);
REQUIRE(myObject->GetAllBehaviorNames()[0] == "Flippable");
REQUIRE(myObject->GetBehavior("Flippable").GetTypeName() == "FlippableCapability::FlippableBehavior");

// Ensure default behaviors are adapted if the object default behaviors are modified.
// While this can not happen with pre-coded extensions, it can happen with custom objects.
spriteObjectMetadata.ResetDefaultBehaviorsJustForTesting();
spriteObjectMetadata.AddDefaultBehavior(
"ResizableCapability::ResizableBehavior");
spriteObjectMetadata.AddDefaultBehavior(
"ScalableCapability::ScalableBehavior");

project.EnsureObjectDefaultBehaviors(*myObject);
REQUIRE(myObject->GetAllBehaviorNames().size() == 2);
REQUIRE(myObject->GetAllBehaviorNames()[0] == "Resizable");
REQUIRE(myObject->GetAllBehaviorNames()[1] == "Scale");
REQUIRE(myObject->GetBehavior("Resizable").GetTypeName() == "ResizableCapability::ResizableBehavior");
REQUIRE(myObject->GetBehavior("Scale").GetTypeName() == "ScalableCapability::ScalableBehavior");
}
}

0 comments on commit 13bdfa4

Please sign in to comment.