core tests

This commit is contained in:
Yotam Mann 2015-08-16 22:21:14 -04:00
parent 9e7066f4ce
commit 2da9badb96
9 changed files with 392 additions and 119 deletions

113
test/core/Buffer.js Normal file
View file

@ -0,0 +1,113 @@
define(["Test", "Tone/core/Buffer"], function (Test, Buffer) {
describe("Buffer", function(){
it ("can be created and disposed", function(){
var buff = new Buffer("./audio/kick.mp3");
buff.dispose();
Test.wasDisposed(buff);
});
it("loads a file from a url string", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(buff){
expect(buff).to.be.instanceof(Buffer);
buffer.dispose();
done();
});
});
it("has a duration", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(){
expect(buffer.duration).to.be.closeTo(0.23, 0.01);
buffer.dispose();
done();
});
});
it("the static onload method is invoked", function(done){
var buffer = new Buffer("./audio/hh.mp3");
Buffer.onload = function(){
buffer.dispose();
done();
//reset this method for the next one
Buffer.onload = function(){};
};
});
it("can be constructed with an options object", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"reverse" : true,
"onload" : function(){
buffer.dispose();
done();
}
});
});
it("takes an AudioBuffer in the constructor method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("takes Tone.Buffer in the set method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer("./audio/hh.mp3");
testOne.set(buffer);
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("takes AudioBuffer in the set method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer("./audio/hh.mp3");
testOne.set(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("the static onprogress method is invoked", function(done){
var progressWasInvoked = false;
var buffer = new Buffer("./audio/hh.mp3", function(){
buffer.dispose();
expect(progressWasInvoked).to.be.true;
done();
});
Buffer.onprogress = function(){
progressWasInvoked = true;
//reset this method for the next one
Buffer.onprogress = function(){};
};
});
it("can reverse a buffer", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(){
var buffArray = buffer.get();
var lastSample = buffArray[buffArray.length - 1];
buffer.reverse = true;
expect(buffer.get()[0]).to.equal(lastSample);
buffer.dispose();
done();
});
});
});
});

111
test/core/Schedulable.js Normal file
View file

@ -0,0 +1,111 @@
define(["Test", "Tone/core/Schedulable"], function (Test, Schedulable) {
describe("Schedulable", function(){
it ("can be created and disposed", function(){
var sched = new Schedulable();
sched.dispose();
Test.wasDisposed(sched);
});
it ("has a private timeline array", function(){
var sched = new Schedulable();
expect(sched).has.property("_timeline").that.is.an("array")
});
it ("accepts events into the timeline", function(){
var sched = new Schedulable();
sched._insertEvent({
"state" : "A",
"time" : 0
});
sched._insertEvent({
"state" : "B",
"time" : 1
});
sched._insertEvent({
"state" : "C",
"time" : 2
});
sched.dispose();
});
it ("can insert events in the timeline in the right order", function(){
var sched = new Schedulable();
sched._insertEvent({
"time" : 0
});
sched._insertEvent({
"time" : 2
});
sched._insertEvent({
"time" : 1
});
expect(sched._timeline[0].time).to.equal(0);
expect(sched._timeline[1].time).to.equal(1);
expect(sched._timeline[2].time).to.equal(2);
sched.dispose();
});
it ("can search for events in the timeline by time", function(){
var sched = new Schedulable();
sched._insertEvent({
"time" : 0
});
sched._insertEvent({
"time" : 2
});
sched._insertEvent({
"time" : 1
});
expect(sched._search(0)).to.equal(0);
expect(sched._search(0.01)).to.equal(0);
expect(sched._search(1)).to.equal(1);
expect(sched._search(1.01)).to.equal(1);
expect(sched._search(20000)).to.equal(2);
expect(sched._search(-1)).to.equal(-1);
sched.dispose();
});
it ("can get the scheduled event at the given time", function(){
var sched = new Schedulable();
sched._insertEvent({
"state" : "A",
"time" : 2
});
sched._insertEvent({
"state" : "C",
"time" : 9.4
});
sched._insertEvent({
"state" : "B",
"time" : 6
});
expect(sched._getEvent(0)).is.null
expect(sched._getEvent(2).state).is.equal("A");
expect(sched._getEvent(5.9).state).is.equal("A");
expect(sched._getEvent(6.1).state).is.equal("B");
expect(sched._getEvent(12).state).is.equal("C");
sched.dispose();
});
it ("puts the second scheduled event after if two events are scheduled at the same time", function(){
var sched = new Schedulable();
sched._insertEvent({
"name" : "A",
"time" : 0
});
sched._insertEvent({
"name" : "B",
"time" : 0
});
expect(sched._getEvent(0).name).is.equal("B");
sched._insertEvent({
"name" : "C",
"time" : 0
});
expect(sched._getEvent(0).name).is.equal("C");
sched.dispose();
});
});
});

View file

@ -0,0 +1,48 @@
define(["Test", "Tone/core/SchedulableState"], function (Test, SchedulableState) {
describe("SchedulableState", function(){
it ("can be created and disposed", function(){
var sched = new SchedulableState();
sched.dispose();
Test.wasDisposed(sched);
});
it ("can schedule a state at a given time", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 1);
sched.dispose();
});
it ("can get a state at a given time", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 2);
expect(sched.getStateAtTime(1)).to.equal("B");
expect(sched.getStateAtTime(0.999)).to.equal("A");
sched.dispose();
});
it ("returns undefined if it's before any scheduled states", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 2);
expect(sched.getStateAtTime(-11)).is.undefined;
sched.dispose();
});
it ("returns initial state if defined and query time is before any scheduled states", function(){
var sched = new SchedulableState("initial");
sched.setStateAtTime("A", 20);
sched.setStateAtTime("B", 21);
sched.setStateAtTime("C", 22);
expect(sched.getStateAtTime(0)).is.equal("initial");
sched.dispose();
});
});
});

View file

@ -30,24 +30,6 @@ define(["Test", "Tone/core/Tone", "PassAudio", "Tone/source/Oscillator", "Tone/i
});
describe("Tone.setContext", function(){
it ("can set a new context", function(){
var origCtx = Tone.context;
var ctx = new OfflineAudioContext(2, 44100, 44100);
Tone.setContext(ctx);
expect(Tone.context).to.equal(ctx);
expect(Tone.prototype.context).to.equal(ctx);
//then set it back
Tone.setContext(origCtx);
expect(Tone.context).to.equal(origCtx);
expect(Tone.prototype.context).to.equal(origCtx);
//and a saftey check
expect(ctx).to.not.equal(origCtx);
});
});
describe("Tone", function(){
var tone = new Tone();
@ -136,121 +118,140 @@ define(["Test", "Tone/core/Tone", "PassAudio", "Tone/source/Oscillator", "Tone/i
done();
});
});
});
describe("Tone.prototype.set / get", function(){
context("Tone.setContext", function(){
it("sets a value given an object", function(){
var osc = new Oscillator(0);
osc.set({
"frequency" : 30
it ("can set a new context", function(){
var origCtx = Tone.context;
var ctx = new OfflineAudioContext(2, 44100, 44100);
Tone.setContext(ctx);
expect(Tone.context).to.equal(ctx);
expect(Tone.prototype.context).to.equal(ctx);
//then set it back
Tone.setContext(origCtx);
expect(Tone.context).to.equal(origCtx);
expect(Tone.prototype.context).to.equal(origCtx);
//and a saftey check
expect(ctx).to.not.equal(origCtx);
});
expect(osc.frequency.value).to.be.closeTo(30, 0.001);
osc.dispose();
});
});
it("sets a value given a string and a value", function(){
var osc = new Oscillator(0);
osc.set("frequency", 2);
expect(osc.frequency.value).to.be.closeTo(2, 0.001);
osc.dispose();
});
context("Tone.prototype.set / get", function(){
it("ramps to a value given an object and ramp time", function(done){
var osc;
var setValue = 30;
var offline = new Offline(0.6);
offline.before(function(dest){
osc = new Oscillator(0);
osc.frequency.connect(dest);
it("sets a value given an object", function(){
var osc = new Oscillator(0);
osc.set({
"frequency" : setValue
}, 0.5);
expect(osc.frequency.value).to.not.be.closeTo(setValue, 0.001);
});
offline.test(function(sample, time){
if (time > 0.5){
expect(sample).to.closeTo(setValue, 0.01);
}
});
offline.after(function(){
"frequency" : 30
});
expect(osc.frequency.value).to.be.closeTo(30, 0.001);
osc.dispose();
done();
});
offline.run();
});
});
it("ramps to a value given a string and a value and a ramp time", function(done){
var osc;
var setValue = 30;
var offline = new Offline(0.6);
offline.before(function(dest){
osc = new Oscillator(0);
osc.frequency.connect(dest);
osc.set("frequency", setValue, 0.5);
expect(osc.frequency.value).to.not.be.closeTo(setValue, 0.001);
});
offline.test(function(sample, time){
if (time > 0.5){
expect(sample).to.closeTo(setValue, 0.01);
}
});
offline.after(function(){
it("sets a value given a string and a value", function(){
var osc = new Oscillator(0);
osc.set("frequency", 2);
expect(osc.frequency.value).to.be.closeTo(2, 0.001);
osc.dispose();
done();
});
offline.run();
});
});
it("gets all defaults of the object with no arguments", function(){
var osc = new Oscillator(0);
expect(osc.get()).to.contain.keys(Object.keys(Oscillator.defaults));
osc.dispose();
});
it("ramps to a value given an object and ramp time", function(done){
var osc;
var setValue = 30;
var offline = new Offline(0.6);
offline.before(function(dest){
osc = new Oscillator(0);
osc.frequency.connect(dest);
osc.set({
"frequency" : setValue
}, 0.5);
expect(osc.frequency.value).to.not.be.closeTo(setValue, 0.001);
});
offline.test(function(sample, time){
if (time > 0.5){
expect(sample).to.closeTo(setValue, 0.01);
}
});
offline.after(function(){
osc.dispose();
done();
});
offline.run();
});
it("can 'get' only the given keys", function(){
var osc = new Oscillator(0);
var keys = ["frequency", "type"];
expect(Object.keys(osc.get(keys))).to.deep.equal(keys);
osc.dispose();
});
it("ramps to a value given a string and a value and a ramp time", function(done){
var osc;
var setValue = 30;
var offline = new Offline(0.6);
offline.before(function(dest){
osc = new Oscillator(0);
osc.frequency.connect(dest);
osc.set("frequency", setValue, 0.5);
expect(osc.frequency.value).to.not.be.closeTo(setValue, 0.001);
});
offline.test(function(sample, time){
if (time > 0.5){
expect(sample).to.closeTo(setValue, 0.01);
}
});
offline.after(function(){
osc.dispose();
done();
});
offline.run();
});
it("can 'set' a nested object", function(){
var synth = new SimpleSynth();
synth.set({
"oscillator" : {
"type" : "square2"
}
});
expect(synth.oscillator.type).to.equal("square2");
synth.dispose();
});
it("gets all defaults of the object with no arguments", function(){
var osc = new Oscillator(0);
expect(osc.get()).to.contain.keys(Object.keys(Oscillator.defaults));
osc.dispose();
});
it("can 'set' a value with dot notation", function(){
var synth = new SimpleSynth();
synth.set("oscillator.type", "triangle");
expect(synth.oscillator.type).to.equal("triangle");
synth.dispose();
});
it("can 'get' only the given keys", function(){
var osc = new Oscillator(0);
var keys = ["frequency", "type"];
expect(Object.keys(osc.get(keys))).to.deep.equal(keys);
osc.dispose();
});
it("can 'get' a value with dot notation", function(){
var synth = new SimpleSynth();
synth.set({
"oscillator" : {
"type" : "sine10",
"phase" : 20,
}
});
expect(synth.get("oscillator.type").oscillator.type).to.equal("sine10");
//get multiple values
expect(synth.get(["oscillator.type", "oscillator.phase"])).to.deep.equal({
"oscillator" : {
"type" : "sine10",
"phase" : 20,
}
});
synth.dispose();
});
it("can 'set' a nested object", function(){
var synth = new SimpleSynth();
synth.set({
"oscillator" : {
"type" : "square2"
}
});
expect(synth.oscillator.type).to.equal("square2");
synth.dispose();
});
it("can 'set' a value with dot notation", function(){
var synth = new SimpleSynth();
synth.set("oscillator.type", "triangle");
expect(synth.oscillator.type).to.equal("triangle");
synth.dispose();
});
it("can 'get' a value with dot notation", function(){
var synth = new SimpleSynth();
synth.set({
"oscillator" : {
"type" : "sine10",
"phase" : 20,
}
});
expect(synth.get("oscillator.type").oscillator.type).to.equal("sine10");
//get multiple values
expect(synth.get(["oscillator.type", "oscillator.phase"])).to.deep.equal({
"oscillator" : {
"type" : "sine10",
"phase" : 20,
}
});
synth.dispose();
});
});
});
});