2019-04-12 14:37:47 +00:00
|
|
|
import { expect } from "chai";
|
2019-07-23 17:12:41 +00:00
|
|
|
import "Tone/core/clock/Transport";
|
2019-07-23 17:51:22 +00:00
|
|
|
import "Tone/core/context/Destination";
|
2019-05-23 18:00:49 +00:00
|
|
|
import { OfflineContext } from "Tone/core/context/OfflineContext";
|
|
|
|
import { ToneWithContext } from "Tone/core/context/ToneWithContext";
|
|
|
|
import { Tone } from "Tone/core/Tone";
|
2019-08-04 14:07:19 +00:00
|
|
|
import { ConnectTest } from "./Connect";
|
2019-09-20 04:31:43 +00:00
|
|
|
import { setLogger } from "Tone/core/util/Debug";
|
2019-11-03 22:42:18 +00:00
|
|
|
import { ToneAudioNode } from "Tone/core/context/ToneAudioNode";
|
2019-11-12 17:06:37 +00:00
|
|
|
import { getContext } from "Tone/core/Global";
|
2019-11-14 17:30:17 +00:00
|
|
|
import * as Classes from "Tone/classes";
|
2021-10-13 17:24:41 +00:00
|
|
|
import { isFunction } from "Tone/core/util/TypeCheck";
|
|
|
|
import { noOp } from "Tone/core/util/Interface";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-05-23 18:00:49 +00:00
|
|
|
export const testAudioContext = new OfflineContext(1, 1, 11025);
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-05-23 18:00:49 +00:00
|
|
|
export function BasicTests(Constr, ...args: any[]): void {
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
context("Basic", () => {
|
|
|
|
|
2019-11-12 17:06:37 +00:00
|
|
|
before(() => {
|
|
|
|
return getContext().resume();
|
|
|
|
});
|
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
it("can be created and disposed", () => {
|
|
|
|
const instance = new Constr(...args);
|
|
|
|
instance.dispose();
|
2019-07-23 16:12:11 +00:00
|
|
|
// check that all of the attributes were disposed
|
|
|
|
expect(instance.disposed).to.equal(true);
|
|
|
|
// also check all of it's attributes to see if they also have the right context
|
|
|
|
for (const member in instance) {
|
|
|
|
if (instance[member] instanceof Tone && member !== "context") {
|
|
|
|
expect(instance[member].disposed, `member ${member}`).to.equal(true);
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 17:24:41 +00:00
|
|
|
// check that all callback functions are assigned to noOp
|
|
|
|
for (const member in instance) {
|
|
|
|
if (isFunction(instance[member]) && member.startsWith("on")) {
|
|
|
|
expect(instance[member]).to.equal(noOp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 14:37:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("extends Tone", () => {
|
|
|
|
const instance = new Constr(...args);
|
|
|
|
expect(instance).to.be.an.instanceof(Tone);
|
|
|
|
instance.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can specify the AudioContext", () => {
|
|
|
|
const instance = new Constr(Object.assign({
|
|
|
|
context: testAudioContext,
|
|
|
|
}, ...args));
|
2019-05-23 18:00:49 +00:00
|
|
|
if (instance instanceof ToneWithContext) {
|
2019-04-12 14:37:47 +00:00
|
|
|
expect(instance.context).to.equal(testAudioContext);
|
|
|
|
// also check all of it's attributes to see if they also have the right context
|
|
|
|
for (const member in instance) {
|
2019-05-23 18:00:49 +00:00
|
|
|
if (instance[member] instanceof ToneWithContext) {
|
2019-07-18 18:04:39 +00:00
|
|
|
expect(instance[member].context, `member: ${member}`).to.equal(testAudioContext);
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
instance.dispose();
|
|
|
|
});
|
2019-08-04 14:07:19 +00:00
|
|
|
|
2019-11-03 22:42:18 +00:00
|
|
|
it("can be serialized to JSON", () => {
|
|
|
|
const instance = new Constr(...args);
|
|
|
|
if (instance instanceof ToneAudioNode) {
|
|
|
|
const json = instance.get();
|
|
|
|
// this throws an error if the object is recursive
|
|
|
|
JSON.stringify(json);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-08-04 14:07:19 +00:00
|
|
|
ConnectTest(Constr, ...args);
|
2019-04-12 14:37:47 +00:00
|
|
|
});
|
2019-11-14 17:30:17 +00:00
|
|
|
|
|
|
|
it("exports its class name", () => {
|
|
|
|
// find the constructor
|
2021-10-13 17:24:41 +00:00
|
|
|
for (const className in Classes) {
|
2019-11-14 17:30:17 +00:00
|
|
|
if (Classes[className] === Constr) {
|
|
|
|
const instance = new Constr(...args);
|
|
|
|
expect(instance.toString()).to.equal(className);
|
2019-11-14 18:36:00 +00:00
|
|
|
instance.dispose();
|
2019-11-14 17:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
2019-09-09 23:27:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the function triggers a warning
|
|
|
|
*/
|
|
|
|
export async function warns(fn: (...args: any[]) => any): Promise<void> {
|
|
|
|
let wasInvoked = false;
|
2019-09-20 04:31:43 +00:00
|
|
|
setLogger({
|
|
|
|
log: () => {},
|
|
|
|
warn: () => wasInvoked = true,
|
|
|
|
});
|
2019-09-09 23:27:14 +00:00
|
|
|
const ret = fn();
|
|
|
|
if (ret instanceof Promise) {
|
|
|
|
await ret;
|
|
|
|
}
|
|
|
|
expect(wasInvoked).to.equal(true);
|
2019-09-20 04:31:43 +00:00
|
|
|
// return to the original logger
|
|
|
|
setLogger(console);
|
2019-09-09 23:27:14 +00:00
|
|
|
}
|