From a83c0808a04ae050e2dfc13b1bd3e702a4f82edf Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Fri, 7 Jul 2017 08:19:32 -0400 Subject: [PATCH] Add public API for costume and sound renaming with tests --- src/virtual-machine.js | 26 ++++++++++++++++++++++++ test/unit/virtual-machine.js | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/virtual-machine.js b/src/virtual-machine.js index 2ca4e21d8..0cc3b7bb6 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -287,6 +287,19 @@ class VirtualMachine extends EventEmitter { }); } + /** + * Rename a costume on the current editing target. + * @param {int} costumeIndex - the index of the costume to be renamed. + * @param {string} newName - the desired new name of the costume (will be modified if already in use). + */ + renameCostume (costumeIndex, newName) { + const usedNames = this.editingTarget.sprite.costumes + .filter((costume, index) => costumeIndex !== index) + .map(costume => costume.name); + this.editingTarget.sprite.costumes[costumeIndex].name = StringUtil.unusedName(newName, usedNames); + this.emitTargetsUpdate(); + } + /** * Delete a costume from the current editing target. * @param {int} costumeIndex - the index of the costume to be removed. @@ -307,6 +320,19 @@ class VirtualMachine extends EventEmitter { }); } + /** + * Rename a sound on the current editing target. + * @param {int} soundIndex - the index of the sound to be renamed. + * @param {string} newName - the desired new name of the sound (will be modified if already in use). + */ + renameSound (soundIndex, newName) { + const usedNames = this.editingTarget.sprite.sounds + .filter((sound, index) => soundIndex !== index) + .map(sound => sound.name); + this.editingTarget.sprite.sounds[soundIndex].name = StringUtil.unusedName(newName, usedNames); + this.emitTargetsUpdate(); + } + /** * Delete a sound from the current editing target. * @param {int} soundIndex - the index of the sound to be removed. diff --git a/test/unit/virtual-machine.js b/test/unit/virtual-machine.js index 37a62ff69..9361ab918 100644 --- a/test/unit/virtual-machine.js +++ b/test/unit/virtual-machine.js @@ -109,3 +109,41 @@ test('renameSprite does not increment when renaming to the same name', t => { t.equal(vm.runtime.targets[0].sprite.name, 'this name'); t.end(); }); + +test('renameSound sets the sound name', t => { + const vm = new VirtualMachine(); + vm.editingTarget = { + sprite: { + sounds: [{name: 'first'}, {name: 'second'}] + } + }; + vm.renameSound(0, 'hello'); + t.equal(vm.editingTarget.sprite.sounds[0].name, 'hello'); + t.equal(vm.editingTarget.sprite.sounds[1].name, 'second'); + // Make sure renaming to same name doesn't increment + vm.renameSound(0, 'hello'); + t.equal(vm.editingTarget.sprite.sounds[0].name, 'hello'); + // But renaming to used name does increment + vm.renameSound(1, 'hello'); + t.equal(vm.editingTarget.sprite.sounds[1].name, 'hello2'); + t.end(); +}); + +test('renameCostume sets the costume name', t => { + const vm = new VirtualMachine(); + vm.editingTarget = { + sprite: { + costumes: [{name: 'first'}, {name: 'second'}] + } + }; + vm.renameCostume(0, 'hello'); + t.equal(vm.editingTarget.sprite.costumes[0].name, 'hello'); + t.equal(vm.editingTarget.sprite.costumes[1].name, 'second'); + // Make sure renaming to same name doesn't increment + vm.renameCostume(0, 'hello'); + t.equal(vm.editingTarget.sprite.costumes[0].name, 'hello'); + // But renaming to used name does increment + vm.renameCostume(1, 'hello'); + t.equal(vm.editingTarget.sprite.costumes[1].name, 'hello2'); + t.end(); +});