Skip to content

Commit

Permalink
Duplicate tests for functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed May 10, 2024
1 parent 7fe65e7 commit 623ddcf
Show file tree
Hide file tree
Showing 9 changed files with 807 additions and 8 deletions.
94 changes: 92 additions & 2 deletions GDevelop.js/TestUtils/CodeGenerationHelpers.js
Expand Up @@ -11,13 +11,39 @@ function generateCompiledEventsForEventsFunction(
project,
eventsFunction,
logCode = false
) {
const extension = new gd.EventsFunctionsExtension();
const runCompiledEventsFunction = generateCompiledEventsForEventsFunctionWithContext(
gd,
project,
extension,
eventsFunction,
logCode
);
extension.delete();
return runCompiledEventsFunction;
}

/**
* Generate the code from events (using GDJS platform)
* and create a JavaScript function that runs it.
*
* The JavaScript function must be called with the `runtimeScene` to be used.
* In this context, GDJS game engine does not exist, so you must pass a mock
* to it to validate that the events are working properly.
*/
function generateCompiledEventsForEventsFunctionWithContext(
gd,
project,
extension,
eventsFunction,
logCode = false
) {
const namespace = 'functionNamespace';
const eventsFunctionsExtensionCodeGenerator =
new gd.EventsFunctionsExtensionCodeGenerator(project);

const includeFiles = new gd.SetString();
const extension = new gd.EventsFunctionsExtension();
const code =
eventsFunctionsExtensionCodeGenerator.generateFreeEventsFunctionCompleteCode(
extension,
Expand All @@ -28,7 +54,6 @@ function generateCompiledEventsForEventsFunction(
);

eventsFunctionsExtensionCodeGenerator.delete();
extension.delete();
includeFiles.delete();

if (logCode) console.log(code);
Expand Down Expand Up @@ -309,6 +334,70 @@ function generateCompiledEventsFromSerializedEvents(
return runCompiledEvents;
}

/**
* Helper to create compiled events from serialized events, creating a project and the events function.
* @param {*} gd
* @param {gdEventsFunctionExtension} extension
* @param {gdSerializerElement} eventsSerializerElement
* @param {{parameterTypes: {[name: string]: string}, groups: {[name: string]: string[]}, logCode: boolean}?} configuration
* @returns
*/
function generateCompiledEventsFunctionFromSerializedEvents(
gd,
extension,
eventsSerializerElement,
configuration
) {
const project = new gd.ProjectHelper.createNewGDJSProject();
const eventsFunction = new gd.EventsFunction();
eventsFunction.getEvents().unserializeFrom(project, eventsSerializerElement);

if (configuration) {
const { parameterTypes, groups } = configuration;
if (groups) {
for (const groupName in groups) {
if (Object.hasOwnProperty.call(groups, groupName)) {
const objectsNames = groups[groupName];

const group = eventsFunction
.getObjectGroups()
.insertNew(groupName, 0);
for (const objectName of objectsNames) {
group.addObject(objectName);
}
}
}
}

if (parameterTypes) {
for (const parameterName in parameterTypes) {
if (Object.hasOwnProperty.call(parameterTypes, parameterName)) {
const parameterType = parameterTypes[parameterName];

const parameter = new gd.ParameterMetadata();
parameter.setType(parameterType);
parameter.setName(parameterName);
eventsFunction.getParameters().push_back(parameter);
parameter.delete();
}
}
}
}

const runCompiledEvents = generateCompiledEventsForEventsFunctionWithContext(
gd,
project,
extension,
eventsFunction,
configuration && configuration.logCode
);

eventsFunction.delete();
project.delete();

return runCompiledEvents;
}

/**
* Generate a function to run the compiled events of a layout.
*/
Expand Down Expand Up @@ -342,6 +431,7 @@ function generateCompiledEventsForLayout(gd, project, layout, logCode = false) {
module.exports = {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
generateCompiledEventsFunctionFromSerializedEvents,
generateCompiledEventsForSerializedEventsBasedExtension,
generateCompiledEventsForLayout,
};
33 changes: 33 additions & 0 deletions GDevelop.js/TestUtils/GDJSMocks.js
Expand Up @@ -682,11 +682,26 @@ class RuntimeGame {
this._variablesContainer = new VariablesContainer(
gameData && gameData.variables
);
this._variablesByExtensionName = new Map();
if (gameData) {
for (const extensionData of gameData.eventsFunctionsExtensions) {
if (extensionData.globalVariables.length > 0) {
this._variablesByExtensionName.set(
extensionData.name,
new VariablesContainer(extensionData.globalVariables)
);
}
}
}
}

getVariables() {
return this._variablesContainer;
}

getVariablesForExtension(extensionName) {
return this._variablesByExtensionName.get(extensionName) || null;
}
}

/** A minimal implementation of gdjs.RuntimeScene for testing. */
Expand All @@ -696,6 +711,16 @@ class RuntimeScene {
this._variablesContainer = new VariablesContainer(
sceneData && sceneData.variables
);
this._variablesByExtensionName = new Map();
if (sceneData && sceneData.usedExtensionsWithVariablesData) {
for (const extensionData of sceneData.usedExtensionsWithVariablesData) {
this._variablesByExtensionName.set(
extensionData.name,
new VariablesContainer(extensionData.sceneVariables)
);
}
}

this._onceTriggers = new OnceTriggers();
this._asyncTasksManager = new FakeAsyncTasksManager();

Expand Down Expand Up @@ -760,6 +785,10 @@ class RuntimeScene {
return this._variablesContainer;
}

getVariablesForExtension(extensionName) {
return this._variablesByExtensionName.get(extensionName) || null;
}

getOnceTriggers() {
return this._onceTriggers;
}
Expand Down Expand Up @@ -883,6 +912,10 @@ function makeMinimalGDJSMock(options) {
const behaviorCtors = {};
const customObjectsCtors = {};
let runtimeScenePreEventsCallbacks = [];
if (options && options.gameData && options.sceneData) {
options.sceneData.usedExtensionsWithVariablesData =
options.gameData.eventsFunctionsExtensions;
}
const runtimeGame = new RuntimeGame(options && options.gameData);
const runtimeScene = new RuntimeScene(
options && options.sceneData,
Expand Down
@@ -1,7 +1,6 @@
const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const { makeMinimalGDJSMock } = require('../TestUtils/GDJSMocks');
const {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
} = require('../TestUtils/CodeGenerationHelpers.js');

Expand Down
@@ -1,7 +1,6 @@
const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const { makeMinimalGDJSMock } = require('../TestUtils/GDJSMocks.js');
const {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
} = require('../TestUtils/CodeGenerationHelpers.js');

Expand Down
@@ -1,7 +1,6 @@
const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const { makeMinimalGDJSMock } = require('../TestUtils/GDJSMocks');
const {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
} = require('../TestUtils/CodeGenerationHelpers.js');

Expand Down
@@ -1,7 +1,6 @@
const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const { makeMinimalGDJSMock } = require('../TestUtils/GDJSMocks.js');
const {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
} = require('../TestUtils/CodeGenerationHelpers.js');

Expand Down
@@ -1,6 +1,5 @@
const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const {
generateCompiledEventsForEventsFunction,
generateCompiledEventsFromSerializedEvents,
generateCompiledEventsForSerializedEventsBasedExtension,
} = require('../TestUtils/CodeGenerationHelpers.js');
Expand Down

0 comments on commit 623ddcf

Please sign in to comment.