Add tests and fixes for set instrument and play drum

This commit is contained in:
Paul Kaplan 2017-07-06 14:56:46 -04:00
parent 1b6baf8114
commit 2ddb6215fd
2 changed files with 42 additions and 5 deletions

View file

@ -183,7 +183,7 @@ class Scratch3SoundBlocks {
let drum = Cast.toNumber(args.DRUM);
drum -= 1; // drums are one-indexed
if (typeof this.runtime.audioEngine === 'undefined') return;
drum = MathUtil.wrapClamp(drum, 0, this.runtime.audioEngine.numDrums);
drum = MathUtil.wrapClamp(drum, 0, this.runtime.audioEngine.numDrums - 1);
let beats = Cast.toNumber(args.BEATS);
beats = this._clampBeats(beats);
if (util.target.audioPlayer === null) return;
@ -206,7 +206,7 @@ class Scratch3SoundBlocks {
let instNum = Cast.toNumber(args.INSTRUMENT);
instNum -= 1; // instruments are one-indexed
if (typeof this.runtime.audioEngine === 'undefined') return;
instNum = MathUtil.wrapClamp(instNum, 0, this.runtime.audioEngine.numInstruments);
instNum = MathUtil.wrapClamp(instNum, 0, this.runtime.audioEngine.numInstruments - 1);
soundState.currentInstrument = instNum;
return this.runtime.audioEngine.instrumentPlayer.loadInstrument(soundState.currentInstrument);
}

View file

@ -1,7 +1,16 @@
const test = require('tap').test;
const Sound = require('../../src/blocks/scratch3_sound');
const blocks = new Sound(null);
let playedSound = null;
let playedSound, playedDrum, playedInstrument;
const runtime = {
audioEngine: {
numDrums: 3,
numInstruments: 3,
instrumentPlayer: {
loadInstrument: instrument => (playedInstrument = instrument)
}
}
};
const blocks = new Sound(runtime);
const util = {
target: {
sprite: {
@ -13,7 +22,8 @@ const util = {
]
},
audioPlayer: {
playSound: md5 => (playedSound = md5)
playSound: md5 => (playedSound = md5),
playDrumForBeats: drum => (playedDrum = drum)
}
}
};
@ -70,3 +80,30 @@ test('playSound prioritizes sound name if given a string', t => {
t.strictEqual(playedSound, 'fourth md5');
t.end();
});
test('playDrum uses 1-indexing and wrap clamps', t => {
let args = {DRUM: 1};
blocks.playDrumForBeats(args, util);
t.strictEqual(playedDrum, 0);
args = {DRUM: runtime.audioEngine.numDrums + 1};
blocks.playDrumForBeats(args, util);
t.strictEqual(playedDrum, 0);
t.end();
});
test('setInstrument uses 1-indexing and wrap clamps', t => {
// Stub getSoundState
blocks._getSoundState = () => ({})
let args = {INSTRUMENT: 1};
blocks.setInstrument(args, util);
t.strictEqual(playedInstrument, 0);
args = {INSTRUMENT: runtime.audioEngine.numInstruments + 1};
blocks.setInstrument(args, util);
t.strictEqual(playedInstrument, 0);
t.end();
});