Make reorder functions return if a change occurred.

Gotta get that testing coverage percent up :)
This commit is contained in:
Paul Kaplan 2018-06-05 12:12:43 -04:00
parent e6ebc33e9e
commit 837703d4ac
2 changed files with 21 additions and 10 deletions

View file

@ -644,6 +644,7 @@ class RenderedTarget extends Target {
* Reorder costume list by moving costume at costumeIndex to newIndex.
* @param {!number} costumeIndex Index of the costume to move.
* @param {!number} newIndex New index for that costume.
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
*/
reorderCostume (costumeIndex, newIndex) {
const clampedNewIndex =
@ -651,7 +652,8 @@ class RenderedTarget extends Target {
const clampedCostumeIndex =
Math.max(0, Math.min(this.sprite.costumes.length - 1, costumeIndex));
if (clampedNewIndex === clampedCostumeIndex) return;
if (clampedNewIndex === clampedCostumeIndex) return false;
const currentCostume = this.getCurrentCostume();
const costume = this.sprite.costumes[clampedCostumeIndex];
@ -660,12 +662,14 @@ class RenderedTarget extends Target {
this.addCostume(costume, clampedNewIndex);
this.setCostume(this.getCostumeIndexByName(currentCostume.name));
return true;
}
/**
* Reorder sound list by moving sound at soundIndex to newIndex.
* @param {!number} soundIndex Index of the sound to move.
* @param {!number} newIndex New index for that sound.
* @returns {boolean} If a change occurred (i.e. if the indices do not match)
*/
reorderSound (soundIndex, newIndex) {
const clampedNewIndex =
@ -673,11 +677,12 @@ class RenderedTarget extends Target {
const clampedSoundIndex =
Math.max(0, Math.min(this.sprite.sounds.length - 1, soundIndex));
if (clampedNewIndex === clampedSoundIndex) return;
if (clampedNewIndex === clampedSoundIndex) return false;
const sound = this.sprite.sounds[clampedSoundIndex];
this.deleteSound(clampedSoundIndex);
this.addSound(sound, clampedNewIndex);
return true;
}
/**

View file

@ -451,26 +451,29 @@ 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);
// Make sure reordering up and down works and current costume follows
resetCostumes();
a.reorderCostume(0, 3);
t.equal(a.reorderCostume(0, 3), true);
t.deepEquals(costumeIds(), [1, 2, 3, 0, 4]);
t.equals(a.currentCostume, 3); // Index of id=0
resetCostumes();
a.setCostume(1);
a.reorderCostume(3, 1);
t.equal(a.reorderCostume(3, 1), true);
t.deepEquals(costumeIds(), [0, 3, 1, 2, 4]);
t.equals(a.currentCostume, 2); // Index of id=1
// Out of bounds indices get clamped
resetCostumes();
a.reorderCostume(10, 0);
t.equal(a.reorderCostume(10, 0), true);
t.deepEquals(costumeIds(), [4, 0, 1, 2, 3]);
t.equals(a.currentCostume, 1); // Index of id=0
resetCostumes();
a.reorderCostume(2, -1000);
t.equal(a.reorderCostume(2, -1000), true);
t.deepEquals(costumeIds(), [2, 0, 1, 3, 4]);
t.equals(a.currentCostume, 1); // Index of id=0
@ -498,22 +501,25 @@ test('#reorderSound', t => {
resetSounds();
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);
// Make sure reordering up and down works and current sound follows
resetSounds();
a.reorderSound(0, 3);
t.equal(a.reorderSound(0, 3), true);
t.deepEquals(soundIds(), [1, 2, 3, 0, 4]);
resetSounds();
a.reorderSound(3, 1);
t.equal(a.reorderSound(3, 1), true);
t.deepEquals(soundIds(), [0, 3, 1, 2, 4]);
// Out of bounds indices get clamped
resetSounds();
a.reorderSound(10, 0);
t.equal(a.reorderSound(10, 0), true);
t.deepEquals(soundIds(), [4, 0, 1, 2, 3]);
resetSounds();
a.reorderSound(2, -1000);
t.equal(a.reorderSound(2, -1000), true);
t.deepEquals(soundIds(), [2, 0, 1, 3, 4]);
t.end();