Tone.js/test/helper/Basic.ts

116 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

2019-04-12 14:37:47 +00:00
import { expect } from "chai";
import "../../Tone/core/clock/Transport.js";
import "../../Tone/core/context/Destination.js";
import { OfflineContext } from "../../Tone/core/context/OfflineContext.js";
import { ToneWithContext } from "../../Tone/core/context/ToneWithContext.js";
import { Tone } from "../../Tone/core/Tone.js";
import { ConnectTest } from "./Connect.js";
import { setLogger } from "../../Tone/core/util/Debug.js";
import { ToneAudioNode } from "../../Tone/core/context/ToneAudioNode.js";
import { getContext } from "../../Tone/core/Global.js";
import * as Classes from "../../Tone/classes.js";
import { isFunction } from "../../Tone/core/util/TypeCheck.js";
import { noOp } from "../../Tone/core/util/Interface.js";
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();
// 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) {
expect(
instance[member].context,
`member: ${member}`
).to.equal(testAudioContext);
2019-04-12 14:37:47 +00:00
}
}
}
instance.dispose();
});
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);
}
});
ConnectTest(Constr, ...args);
2019-04-12 14:37:47 +00:00
});
it("exports its class name", () => {
// find the constructor
2021-10-13 17:24:41 +00:00
for (const className in Classes) {
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-04-12 14:37:47 +00:00
}
/**
* Assert that the function triggers a warning
*/
export async function warns(fn: (...args: any[]) => any): Promise<void> {
let wasInvoked = false;
setLogger({
log: () => {},
warn: () => (wasInvoked = true),
});
const ret = fn();
if (ret instanceof Promise) {
await ret;
}
expect(wasInvoked).to.equal(true);
// return to the original logger
setLogger(console);
}