Files
GDevelop/GDevelop.js/__tests__/GDJSAdvancedExtensionCodeGenerationIntegrationTests.js
D8H 75bfa5cb45 Unify variable instructions and handle local variables (#6459)
- Global or scene variables can be used with a unique action and condition.
- Object variables can be used with a unique action and condition.
- Variables need to be declared following the same logic as the new expression syntax.
- Local variable can be declared on events
- Extensions have their own variables
- Show a diagnostic report when a preview is launched and there are missing scene variables, object variables or behaviors.
  - This is especially useful if external events are shared between several scenes.
2024-05-20 23:14:07 +02:00

219 lines
6.5 KiB
JavaScript

const initializeGDevelopJs = require('../../Binaries/embuild/GDevelop.js/libGD.js');
const { makeMinimalGDJSMock } = require('../TestUtils/GDJSMocks');
const {
generateCompiledEventsFromSerializedEvents,
} = require('../TestUtils/CodeGenerationHelpers.js');
describe('libGD.js - GDJS Code Generation integration tests', function () {
let gd = null;
beforeAll(async () => {
gd = await initializeGDevelopJs();
});
const generateAndRunVariableAffectationWithConditions = (
parameterTypes,
parameterValues,
conditions
) => {
const serializerElement = gd.Serializer.fromJSObject([
{
type: 'BuiltinCommonInstructions::Standard',
conditions,
actions: [
{
type: { value: 'ModVarScene' },
parameters: ['SuccessVariable', '=', '1'],
},
],
events: [],
},
]);
const runCompiledEvents = generateCompiledEventsFromSerializedEvents(
gd,
serializerElement,
{ parameterTypes, logCode: false }
);
const { gdjs, runtimeScene } = makeMinimalGDJSMock();
runCompiledEvents(gdjs, runtimeScene, parameterValues);
return runtimeScene;
};
it('can generate a number parameter condition that is true', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'number' },
[123],
[
{
type: { value: 'CompareArgumentAsNumber' },
parameters: ['"MyParameter"', '=', '123'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(true);
expect(
runtimeScene.getVariables().get('SuccessVariable').getAsNumber()
).toBe(1);
});
it('can generate a number parameter condition that is false', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'number' },
[123],
[
{
type: { value: 'BuiltinAdvanced::CompareArgumentAsNumber' },
parameters: ['MyParameter', '=', '456'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(false);
});
it('can generate a string parameter condition that is true', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'number' },
['123'],
[
{
type: { value: 'CompareArgumentAsString' },
parameters: ['"MyParameter"', '=', '"123"'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(true);
expect(
runtimeScene.getVariables().get('SuccessVariable').getAsNumber()
).toBe(1);
});
it('can generate a string parameter condition that is false', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'number' },
['123'],
[
{
type: { value: 'BuiltinAdvanced::CompareArgumentAsString' },
parameters: ['MyParameter', '=', '"456"'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(false);
});
it('can generate a string parameter condition that is true with a contains operator', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'string' },
['Hello word!'],
[
{
type: { value: 'CompareArgumentAsString' },
parameters: ['"MyParameter"', 'contains', '"word"'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(true);
expect(
runtimeScene.getVariables().get('SuccessVariable').getAsNumber()
).toBe(1);
});
it('can generate a string parameter condition that is false with a contains operator', function () {
const runtimeScene = generateAndRunVariableAffectationWithConditions(
{ MyParameter: 'string' },
['Hello word!'],
[
{
type: { value: 'CompareArgumentAsString' },
parameters: ['"MyParameter"', 'contains', '"Hi!"'],
},
]
);
expect(runtimeScene.getVariables().has('SuccessVariable')).toBe(false);
});
it('can copy a variable parameter variable', function () {
const serializerElement = gd.Serializer.fromJSObject([
{
type: 'BuiltinCommonInstructions::Standard',
conditions: [],
actions: [
{
type: { value: 'CopyArgumentToVariable' },
parameters: ['"MyParameter"', '__MyExtensionVariable'],
},
],
events: [],
},
]);
const runCompiledEvents = generateCompiledEventsFromSerializedEvents(
gd,
serializerElement,
{ parameterTypes: { MyParameter: 'scenevar' }, logCode: false }
);
const { gdjs, runtimeScene } = makeMinimalGDJSMock();
const myVariable = runtimeScene.getVariables().get('MyVariable');
myVariable.getChild('MyChildVariable').setNumber(123);
runCompiledEvents(gdjs, runtimeScene, [myVariable]);
// The user variable is copied into a variable with the extension namespace.
expect(runtimeScene.getVariables().has('__MyExtensionVariable')).toBe(true);
expect(
runtimeScene
.getVariables()
.get('__MyExtensionVariable')
.getChild('MyChildVariable')
.getAsNumber()
).toBe(123);
});
it('can write a variable parameter variable', function () {
const serializerElement = gd.Serializer.fromJSObject([
{
type: 'BuiltinCommonInstructions::Standard',
conditions: [],
actions: [
{
type: { value: 'ModVarScene' },
parameters: ['__MyExtensionVariable.MyChildVariable', '=', '123'],
},
{
type: { value: 'CopyVariableToArgument' },
parameters: ['"MyParameter"', '__MyExtensionVariable'],
},
],
events: [],
},
]);
const runCompiledEvents = generateCompiledEventsFromSerializedEvents(
gd,
serializerElement,
{ parameterTypes: { MyParameter: 'scenevar' }, logCode: false }
);
const { gdjs, runtimeScene } = makeMinimalGDJSMock();
const myVariable = runtimeScene.getVariables().get('MyVariable');
runCompiledEvents(gdjs, runtimeScene, [myVariable]);
// The extension variable is copied into the user variable.
expect(runtimeScene.getVariables().has('MyVariable')).toBe(true);
expect(
runtimeScene
.getVariables()
.get('MyVariable')
.getChild('MyChildVariable')
.getAsNumber()
).toBe(123);
});
});