Skip to content

Commit

Permalink
Add some tests on structure instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed May 10, 2024
1 parent 3090bdc commit 9891ba2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
14 changes: 14 additions & 0 deletions GDevelop.js/TestUtils/GDJSMocks.js
Expand Up @@ -183,6 +183,10 @@ class Variable {
return Object.keys(this._children).length;
}

hasChild(childName) {
return !!this._children[childName];
}

castTo() {}

isPrimitive() {
Expand All @@ -193,6 +197,10 @@ class Variable {
return this.isPrimitive() ? 'number' : 'structure';
}

removeChild(childName) {
delete this._children[childName];
}

clearChildren() {
this._children = {};
this._childrenArray = [];
Expand Down Expand Up @@ -894,6 +902,12 @@ function makeMinimalGDJSMock(options) {
array.pushVariableCopy(variable),
valuePush: (array, value) => array.pushValue(value),
getVariableChildCount: (variable) => variable.getChildrenCount(),
variableChildExists: (variable, childName) =>
variable.hasChild(childName),
variableRemoveChild: (variable, childName) =>
variable.removeChild(childName),
variableClearChildren: (variable) =>
variable.clearChildren(),
},
object: {
createObjectOnScene,
Expand Down
Expand Up @@ -489,7 +489,7 @@ describe('libGD.js - GDJS Code Generation integration tests', function () {
expect(runtimeScene.getVariables().get('MyVariable').getAsNumber()).toBe(1);
});

// TODO Move this tests with legacy ones.
// TODO Move this test with legacy ones.
it('can generate a VariableChildCount expression for an undeclared variable', function () {
scene.getVariables().insertNew('MyVariable', 0).setValue(0);
const runtimeScene = generateAndRunActionsForLayout(
Expand All @@ -515,4 +515,69 @@ describe('libGD.js - GDJS Code Generation integration tests', function () {
);
expect(runtimeScene.getVariables().get('MyVariable').getAsNumber()).toBe(1);
});

it('can generate a child existence condition that is true', function () {
scene
.getVariables()
.insertNew('MyStructureVariable', 0)
.getChild('MyChild')
.setValue(123);
const runtimeScene = generateAndRunVariableAffectationWithConditions([
{
type: { inverted: false, value: 'VariableChildExists2' },
parameters: ['MyStructureVariable', '"MyChild"'],
},
]);
expect(
runtimeScene.getVariables().get('SuccessVariable').getAsNumber()
).toBe(1);
});

it('can generate a child removing action', function () {
const variable = scene.getVariables().insertNew('MyStructureVariable', 0);
variable.getChild('MyChildA').setValue(123);
variable.getChild('MyChildB').setValue(456);
const runtimeScene = generateAndRunActionsForLayout([
{
type: { value: 'RemoveVariableChild' },
parameters: ['MyStructureVariable', '"MyChildA"'],
},
]);
expect(
runtimeScene
.getVariables()
.get('MyStructureVariable')
.hasChild('MyChildA')
).toBe(false);
expect(
runtimeScene
.getVariables()
.get('MyStructureVariable')
.hasChild('MyChildB')
).toBe(true);
});

it('can generate a children clearing action', function () {
const variable = scene.getVariables().insertNew('MyStructureVariable', 0);
variable.getChild('MyChildA').setValue(123);
variable.getChild('MyChildB').setValue(123);
const runtimeScene = generateAndRunActionsForLayout([
{
type: { value: 'ClearVariableChildren' },
parameters: ['MyStructureVariable'],
},
]);
expect(
runtimeScene
.getVariables()
.get('MyStructureVariable')
.hasChild('MyChildA')
).toBe(false);
expect(
runtimeScene
.getVariables()
.get('MyStructureVariable')
.hasChild('MyChildB')
).toBe(false);
});
});

0 comments on commit 9891ba2

Please sign in to comment.