Skip to content

Commit 3f2cfd4

Browse files
committed
Add new tests
1 parent 608c890 commit 3f2cfd4

File tree

2 files changed

+422
-2
lines changed

2 files changed

+422
-2
lines changed

GDevelop.js/TestUtils/GDJSMocks.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,16 @@ class Variable {
190190
return !!this._children[childName];
191191
}
192192

193-
castTo() {}
193+
castTo(newType) {
194+
if (newType === 'string' || newType === 'number' || newType === 'boolean') {
195+
this._children = {};
196+
this._childrenArray = [];
197+
} else if (newType === 'structure') {
198+
this._childrenArray = [];
199+
} else if (newType === 'array') {
200+
this._children = {};
201+
}
202+
}
194203

195204
isPrimitive() {
196205
return (
@@ -218,6 +227,43 @@ class Variable {
218227
this._childrenArray = [];
219228
}
220229

230+
/**
231+
* Replaces all the children with a new map of children.
232+
* @param {Object.<string, Variable>} newChildren
233+
*/
234+
replaceChildren(newChildren) {
235+
this._children = newChildren;
236+
this._childrenArray = [];
237+
}
238+
239+
/**
240+
* Replaces all the children with a new array of children.
241+
* @param {Variable[]} newChildren
242+
*/
243+
replaceChildrenArray(newChildren) {
244+
this._childrenArray = newChildren;
245+
this._children = {};
246+
}
247+
248+
/**
249+
* Converts this variable into an equivalent JavaScript object.
250+
* @returns {any}
251+
*/
252+
toJSObject() {
253+
if (this._childrenArray.length > 0) {
254+
return this._childrenArray.map((child) => child.toJSObject());
255+
}
256+
if (Object.keys(this._children).length > 0) {
257+
const obj = {};
258+
for (const name in this._children) {
259+
if (this._children.hasOwnProperty(name))
260+
obj[name] = this._children[name].toJSObject();
261+
}
262+
return obj;
263+
}
264+
return this._value;
265+
}
266+
221267
clone() {
222268
return Variable.copy(this, new Variable());
223269
}
@@ -315,7 +361,12 @@ class VariablesContainer {
315361
const childVariable = variable.getChild(childVariableData.name);
316362
setupVariableFromVariableData(childVariable, childVariableData);
317363
});
318-
variable.setString(variableData.value);
364+
} else if (variableData.type === 'array') {
365+
variableData.children.forEach((childVariableData) => {
366+
const childVariable = new Variable();
367+
setupVariableFromVariableData(childVariable, childVariableData);
368+
variable._pushVariable(childVariable);
369+
});
319370
} else {
320371
throw new Error(
321372
'Unsupported variable type in GDJS Mock:',
@@ -978,6 +1029,10 @@ function makeMinimalGDJSMock(options) {
9781029
wait: () => new FakeAsyncTask(),
9791030
noop: () => {},
9801031
},
1032+
network: {
1033+
variableStructureToJSON: (variable) =>
1034+
JSON.stringify(variable.toJSObject()),
1035+
},
9811036
common: {
9821037
resolveAsyncEventsFunction: ({ task }) => task.resolve(),
9831038
},

0 commit comments

Comments
 (0)