diff --git a/Core/tests/DummyPlatform.cpp b/Core/tests/DummyPlatform.cpp index 190c61e5fab0..7986263e1fcf 100644 --- a/Core/tests/DummyPlatform.cpp +++ b/Core/tests/DummyPlatform.cpp @@ -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. @@ -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", diff --git a/Core/tests/WholeProjectRefactorer.cpp b/Core/tests/WholeProjectRefactorer.cpp index 6001aed01d34..56c43cae3aa4 100644 --- a/Core/tests/WholeProjectRefactorer.cpp +++ b/Core/tests/WholeProjectRefactorer.cpp @@ -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( + 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( + 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( + 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, @@ -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; @@ -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)",