Propagate the return value of reorder functions, update comments

This commit is contained in:
Paul Kaplan 2018-06-06 09:35:19 -04:00
parent d6d72f6fa7
commit 0cd50fbb2c
3 changed files with 16 additions and 12 deletions

View file

@ -1038,29 +1038,27 @@ class VirtualMachine extends EventEmitter {
* @param {!string} targetId ID of the target which owns the costumes.
* @param {!number} costumeIndex index of the costume to move.
* @param {!number} newIndex index that the costume should be moved to.
* @returns {boolean} whether the target was found for reordering.
* @returns {boolean} Whether a costume was reordered.
*/
reorderCostume (targetId, costumeIndex, newIndex) {
const target = this.runtime.getTargetById(targetId);
if (target) {
target.reorderCostume(costumeIndex, newIndex);
return true;
return target.reorderCostume(costumeIndex, newIndex);
}
return false;
}
/**
* Reorder the sounds of a target if it exists. Return whether it succeeded.
* Reorder the sounds of a target if it exists. Return whether it occured.
* @param {!string} targetId ID of the target which owns the sounds.
* @param {!number} soundIndex index of the sound to move.
* @param {!number} newIndex index that the sound should be moved to.
* @returns {boolean} whether the target was found for reordering.
* @returns {boolean} Whether a sound was reordered.
*/
reorderSound (targetId, soundIndex, newIndex) {
const target = this.runtime.getTargetById(targetId);
if (target) {
target.reorderSound(soundIndex, newIndex);
return true;
return target.reorderSound(soundIndex, newIndex);
}
return false;
}

View file

@ -451,8 +451,10 @@ test('#reorderCostume', t => {
t.deepEquals(costumeIds(), [0, 1, 2, 3, 4]);
t.equals(a.currentCostume, 0);
// Returns false if the costumes are the same an no change occurred
t.equal(a.reorderCostume(0, 0), false);
// Returns false if the costumes are the same and no change occurred
t.equal(a.reorderCostume(3, 3), false);
t.equal(a.reorderCostume(999, 5000), false); // Clamped to the same values.
t.equal(a.reorderCostume(-999, -5000), false);
// Make sure reordering up and down works and current costume follows
resetCostumes();
@ -502,7 +504,9 @@ test('#reorderSound', t => {
t.deepEquals(soundIds(), [0, 1, 2, 3, 4]);
// Return false if indices are the same and no change occurred.
t.equal(a.reorderSound(100000, 99999), false);
t.equal(a.reorderSound(3, 3), false);
t.equal(a.reorderSound(100000, 99999), false); // Clamped to the same values
t.equal(a.reorderSound(-100000, -99999), false);
// Make sure reordering up and down works and current sound follows
resetSounds();

View file

@ -321,6 +321,7 @@ test('reorderCostume', t => {
target.reorderCostume = (_costumeIndex, _newIndex) => {
costumeIndex = _costumeIndex;
newIndex = _newIndex;
return true; // Do not need all the logic about if a reorder occurred.
};
vm.runtime.targets = [target];
@ -351,16 +352,17 @@ test('reorderSound', t => {
target.reorderSound = (_soundIndex, _newIndex) => {
soundIndex = _soundIndex;
newIndex = _newIndex;
return true; // Do not need all the logic about if a reorder occurred.
};
vm.runtime.targets = [target];
t.equal(vm.reorderSound('not-a-target', 0, 3), false);
t.equal(soundIndex, null); // Make sure reorder function was not called somehow
t.equal(soundIndex, null); // Make sure reorder function was not called somehow.
t.equal(newIndex, null);
t.equal(vm.reorderSound(target.id, 0, 3), true);
t.equal(soundIndex, 0); // Make sure reorder function was called correctly
t.equal(soundIndex, 0); // Make sure reorder function was called correctly.
t.equal(newIndex, 3);
t.end();