Skip to content

Commit

Permalink
Add tests for property renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Nov 18, 2024
1 parent 22c9ead commit e11207c
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 12 deletions.
75 changes: 63 additions & 12 deletions Core/tests/DummyPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,41 +263,81 @@ void SetupProjectWithDummyPlatform(gd::Project& project,
extension->SetExtensionInformation(
"BuiltinVariables", "My testing extension for variables", "", "", "");

extension
->AddCondition("NumberVariable",
"Variable value",
"Compare the number value of a variable.",
"The variable _PARAM0_",
"",
"",
"")
.AddParameter("variableOrPropertyOrParameter", _("Variable"))
.UseStandardRelationalOperatorParameters(
"number", gd::ParameterOptions::MakeNewOptions());

extension
->AddCondition("StringVariable",
"Variable value",
"Compare the text (string) of a variable.",
"The variable _PARAM0_",
"",
"",
"")
.AddParameter("variableOrPropertyOrParameter", _("Variable"))
.UseStandardRelationalOperatorParameters(
"string", gd::ParameterOptions::MakeNewOptions());

extension
->AddCondition(
"BooleanVariable",
"Variable value",
"Compare the boolean value of a variable.",
"The variable _PARAM0_ is _PARAM1_",
"",
"",
"")
.AddParameter("variableOrPropertyOrParameter", _("Variable"))
.AddParameter("trueorfalse", _("Check if the value is"))
.SetDefaultValue("true")
// This parameter allows to keep the operand expression
// when the editor switch between variable instructions.
.AddCodeOnlyParameter("trueorfalse", "");

extension
->AddAction("SetNumberVariable",
"Do something with number variables",
"This does something with variables",
"Do something with variables",
"Change variable value",
"Modify the number value of a variable.",
"the variable _PARAM0_",
"",
"",
"")
.AddParameter("variable", "Variable")
.AddParameter("variableOrProperty", "Variable")
.AddParameter("operator", "Operator", "number")
.AddParameter("number", "Value")
.SetFunctionName("setNumberVariable");

extension
->AddAction("SetStringVariable",
"Do something with string variables",
"This does something with variables",
"Do something with variables",
"Change text variable",
"Modify the text (string) of a variable.",
"the variable _PARAM0_",
"",
"",
"")
.AddParameter("variable", "Variable")
.AddParameter("variableOrProperty", "Variable")
.AddParameter("operator", "Operator", "string")
.AddParameter("string", "Value")
.SetFunctionName("setStringVariable");

extension
->AddAction("SetBooleanVariable",
"Do something with boolean variables",
"This does something with variables",
"Do something with variables",
"Change boolean variable",
"Modify the boolean value of a variable.",
"Change the variable _PARAM0_: _PARAM1_",
"",
"",
"")
.AddParameter("variable", "Variable")
.AddParameter("variableOrProperty", "Variable")
.AddParameter("operator", "Operator", "boolean")
// This parameter allows to keep the operand expression
// when the editor switch between variable instructions.
Expand Down Expand Up @@ -358,6 +398,17 @@ void SetupProjectWithDummyPlatform(gd::Project& project,
.AddParameter("soundfile", "Parameter 3 (an audio resource)")
.SetFunctionName("doSomethingWithResources");

extension
->AddAction("DoSomethingWithAnyVariable",
"Do something with variables",
"This does something with variables",
"Do something with variables please",
"",
"",
"")
.AddParameter("variable", "Any variable")
.SetFunctionName("doSomethingWithAnyVariable");

extension
->AddAction("DoSomethingWithLegacyPreScopedVariables",
"Do something with variables",
Expand Down
196 changes: 196 additions & 0 deletions Core/tests/WholeProjectRefactorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,54 @@ CreateInstructionWithNumberParameter(gd::Project &project,
return event.GetActions().Insert(instruction);
}

const gd::Instruction &
CreateInstructionWithVariableParameter(gd::Project &project,
gd::EventsList &events,
const gd::String &expression) {
gd::StandardEvent &event = dynamic_cast<gd::StandardEvent &>(
events.InsertNewEvent(project, "BuiltinCommonInstructions::Standard"));

gd::Instruction instruction;
instruction.SetType("MyExtension::DoSomethingWithAnyVariable");
instruction.SetParametersCount(1);
instruction.SetParameter(0, expression);
return event.GetActions().Insert(instruction);
}

const gd::Instruction &
CreateNumberVariableSetterAction(gd::Project &project,
gd::EventsList &events,
const gd::String &variableName,
const gd::String &expression) {
gd::StandardEvent &event = dynamic_cast<gd::StandardEvent &>(
events.InsertNewEvent(project, "BuiltinCommonInstructions::Standard"));

gd::Instruction instruction;
instruction.SetType("BuiltinVariables::SetNumberVariable");
instruction.SetParametersCount(3);
instruction.SetParameter(0, variableName);
instruction.SetParameter(1, "=");
instruction.SetParameter(2, expression);
return event.GetActions().Insert(instruction);
}

const gd::Instruction &
CreateNumberVariableGetterCondition(gd::Project &project,
gd::EventsList &events,
const gd::String &variableName,
const gd::String &expression) {
gd::StandardEvent &event = dynamic_cast<gd::StandardEvent &>(
events.InsertNewEvent(project, "BuiltinCommonInstructions::Standard"));

gd::Instruction instruction;
instruction.SetType("BuiltinVariables::NumberVariable");
instruction.SetParametersCount(3);
instruction.SetParameter(0, variableName);
instruction.SetParameter(1, "=");
instruction.SetParameter(2, expression);
return event.GetConditions().Insert(instruction);
}

enum TestEvent {
FreeFunctionAction,
FreeFunctionWithExpression,
Expand Down Expand Up @@ -3080,6 +3128,80 @@ TEST_CASE("WholeProjectRefactorer", "[common]") {
"MyRenamedProperty");
}

SECTION("(Events based behavior) property renamed (in variable setter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedBehavior =
eventsExtension.GetEventsBasedBehaviors().Get("MyEventsBasedBehavior");

auto &behaviorAction =
eventsBasedBehavior.GetEventsFunctions().InsertNewEventsFunction(
"MyBehaviorEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureBehaviorEventsFunctionsProperParameters(
eventsExtension, eventsBasedBehavior);
auto &instruction = CreateNumberVariableSetterAction(
project, behaviorAction.GetEvents(), "MyProperty", "123");

gd::WholeProjectRefactorer::RenameEventsBasedBehaviorProperty(
project, eventsExtension, eventsBasedBehavior, "MyProperty",
"MyRenamedProperty");

REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyRenamedProperty");
}

SECTION("(Events based behavior) property renamed (in variable getter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedBehavior =
eventsExtension.GetEventsBasedBehaviors().Get("MyEventsBasedBehavior");

auto &behaviorAction =
eventsBasedBehavior.GetEventsFunctions().InsertNewEventsFunction(
"MyBehaviorEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureBehaviorEventsFunctionsProperParameters(
eventsExtension, eventsBasedBehavior);
auto &instruction = CreateNumberVariableGetterCondition(
project, behaviorAction.GetEvents(), "MyProperty", "123");

gd::WholeProjectRefactorer::RenameEventsBasedBehaviorProperty(
project, eventsExtension, eventsBasedBehavior, "MyProperty",
"MyRenamedProperty");

REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyRenamedProperty");
}

SECTION("(Events based behavior) property not renamed (in variable parameter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedBehavior =
eventsExtension.GetEventsBasedBehaviors().Get("MyEventsBasedBehavior");

auto &behaviorAction =
eventsBasedBehavior.GetEventsFunctions().InsertNewEventsFunction(
"MyBehaviorEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureBehaviorEventsFunctionsProperParameters(
eventsExtension, eventsBasedBehavior);
// Properties can't actually be used in "variable" parameters.
auto &instruction = CreateInstructionWithVariableParameter(
project, behaviorAction.GetEvents(), "MyProperty");

gd::WholeProjectRefactorer::RenameEventsBasedBehaviorProperty(
project, eventsExtension, eventsBasedBehavior, "MyProperty",
"MyRenamedProperty");

// "variable" parameters are left untouched.
REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyProperty");
}

SECTION("(Events based behavior) shared property renamed") {
gd::Project project;
gd::Platform platform;
Expand Down Expand Up @@ -3208,6 +3330,80 @@ TEST_CASE("WholeProjectRefactorer", "[common]") {
REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyRenamedProperty");
}

SECTION("(Events based object) property renamed (in variable setter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedObject =
eventsExtension.GetEventsBasedObjects().Get("MyEventsBasedObject");

auto &behaviorAction =
eventsBasedObject.GetEventsFunctions().InsertNewEventsFunction(
"MyObjectEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureObjectEventsFunctionsProperParameters(
eventsExtension, eventsBasedObject);
auto &instruction = CreateNumberVariableSetterAction(
project, behaviorAction.GetEvents(), "MyProperty", "123");

gd::WholeProjectRefactorer::RenameEventsBasedObjectProperty(
project, eventsExtension, eventsBasedObject, "MyProperty",
"MyRenamedProperty");

REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyRenamedProperty");
}

SECTION("(Events based object) property renamed (in variable getter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedObject =
eventsExtension.GetEventsBasedObjects().Get("MyEventsBasedObject");

auto &behaviorAction =
eventsBasedObject.GetEventsFunctions().InsertNewEventsFunction(
"MyObjectEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureObjectEventsFunctionsProperParameters(
eventsExtension, eventsBasedObject);
auto &instruction = CreateNumberVariableGetterCondition(
project, behaviorAction.GetEvents(), "MyProperty", "123");

gd::WholeProjectRefactorer::RenameEventsBasedObjectProperty(
project, eventsExtension, eventsBasedObject, "MyProperty",
"MyRenamedProperty");

REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyRenamedProperty");
}

SECTION("(Events based object) property renamed (in variable parameter)") {
gd::Project project;
gd::Platform platform;
SetupProjectWithDummyPlatform(project, platform);
auto &eventsExtension = SetupProjectWithEventsFunctionExtension(project);
auto &eventsBasedObject =
eventsExtension.GetEventsBasedObjects().Get("MyEventsBasedObject");

auto &behaviorAction =
eventsBasedObject.GetEventsFunctions().InsertNewEventsFunction(
"MyObjectEventsFunction", 0);
gd::WholeProjectRefactorer::EnsureObjectEventsFunctionsProperParameters(
eventsExtension, eventsBasedObject);
// Properties can't actually be used in "variable" parameters.
auto &instruction = CreateInstructionWithVariableParameter(
project, behaviorAction.GetEvents(), "MyProperty");

gd::WholeProjectRefactorer::RenameEventsBasedObjectProperty(
project, eventsExtension, eventsBasedObject, "MyProperty",
"MyRenamedProperty");

// "variable" parameters are left untouched.
REQUIRE(instruction.GetParameter(0).GetPlainString() ==
"MyProperty");
}
}
// TODO: Check that this works when behaviors are attached to a child-object.
TEST_CASE("WholeProjectRefactorer (FindInvalidRequiredBehaviorProperties)",
Expand Down

0 comments on commit e11207c

Please sign in to comment.