Repository: 4ian/GDevelop
File: GDevelop.js/__tests__/Core.js (JavaScript)

Code before:

      parameters.removeParameter('MySpriteObject2');
      expect(parameters.getParametersCount()).toBe(7);

      objectsContainer = new gd.ObjectsContainer(gd.ObjectsContainer.Function);
      gd.ParameterMetadataTools.parametersToObjectsContainer(
        project,
        parameters,
        objectsContainer
      );

      // Check that object parameters are considered as objects in the objects container.
      expect(objectsContainer.getObjectsCount()).toBe(2);
      expect(objectsContainer.hasObjectNamed('MyObjectWithoutType')).toBe(true);
      expect(objectsContainer.hasObjectNamed('MySpriteObject')).toBe(true);

      const objectWithoutType = objectsContainer.getObject('MyObjectWithoutType');
      expect(objectWithoutType.getType()).toBe(
        ''
      );
      const mySpriteObject = objectsContainer.getObject('MySpriteObject');
      expect(objectsContainer.getObject('MySpriteObject').getType()).toBe(
        'Sprite'
      );

      // Check that behaviors were also added.
      expect(objectsContainer.getObject('MySpriteObject').getAllBehaviorNames().toJSArray()).toEqual(
        ['MyFirstBehavior', 'MySecondBehavior']
      );

      // Call a second time and verify no changes.
      gd.ParameterMetadataTools.parametersToObjectsContainer(
        project,
        parameters,
        objectsContainer
      );
      expect(objectsContainer.getObjectsCount()).toBe(2);
      expect(objectsContainer.hasObjectNamed('MyObjectWithoutType')).toBe(true);
      expect(objectsContainer.hasObjectNamed('MySpriteObject')).toBe(true);
      expect(objectsContainer.getObject('MySpriteObject').getAllBehaviorNames().toJSArray()).toEqual(
        ['MyFirstBehavior', 'MySecondBehavior']
      );

      // Verify that objects are even stable in memory.
      expect(objectWithoutType).toBe(objectsContainer.getObject('MyObjectWithoutType'));
      expect(gd.getPointer(objectWithoutType)).toBe(gd.getPointer(objectsContainer.getObject('MyObjectWithoutType')));
      expect(mySpriteObject).toBe(objectsContainer.getObject('MySpriteObject'));
      expect(gd.getPointer(mySpriteObject)).toBe(gd.getPointer(objectsContainer.getObject('MySpriteObject')));

      // Change an object type, rename a behavior and add a new object.
      parameters.getParameter("MyObjectWithoutType").setExtraInfo('Sprite');

Review comment (suggestion):
Maybe `ObjectMetadata::AddDefaultBehavior` can be used to simulate a change of capability in a custom object.

Code after:

      parameters.removeParameter('MySpriteObject2');
      expect(parameters.getParametersCount()).toBe(7);

      objectsContainer = new gd.ObjectsContainer();
      gd.ParameterMetadataTools.parametersToObjectsContainer(
        project,
        parameters,
        objectsContainer
      );

      // Check that object parameters are considered as objects in the objects container.
      expect(objectsContainer.getObjectsCount()).toBe(2);
      expect(objectsContainer.hasObjectNamed('MyObjectWithoutType')).toBe(true);
      expect(objectsContainer.hasObjectNamed('MySpriteObject')).toBe(true);

      const objectWithoutType = objectsContainer.getObject(
        'MyObjectWithoutType'
      );
      expect(objectWithoutType.getType()).toBe('');
      const mySpriteObject = objectsContainer.getObject('MySpriteObject');
      expect(objectsContainer.getObject('MySpriteObject').getType()).toBe(
        'Sprite'
      );

      // Check that behaviors were also added AND that default behaviors are also present.
      expect(
        objectsContainer
          .getObject('MySpriteObject')
          .getAllBehaviorNames()
          .toJSArray()
      ).toEqual([
        'Animation',
        'Effect',
        'Flippable',
        'MyFirstBehavior',
        'MySecondBehavior',
        'Opacity',
        'Resizable',
        'Scale',
      ]);
      expect(
        objectsContainer
          .getObject('MyObjectWithoutType')
          .getAllBehaviorNames()
          .toJSArray()
      ).toEqual([]);

      // Call a second time and verify no changes.
      gd.ParameterMetadataTools.parametersToObjectsContainer(
        project,