Tone.js/test/helper/APITest.js

127 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-05-18 16:07:20 +00:00
define(["helper/Test", "Tone/type/Type", "Tone/core/Transport", "Tone/type/Time",
"Tone/type/Frequency"], function(Test, Type, Transport, Time, Frequency){
2016-09-26 01:36:32 +00:00
//modified from http://stackoverflow.com/questions/15298912/javascript-generating-combinations-from-n-arrays-with-m-elements
function generatePermutations(args){
var results = [], max = args.length-1;
function helper(arr, i){
for (var j = 0; j < args[i].length; j++){
var a = arr.slice(0); // clone arr
a.push(args[i][j]);
if (i===max){
results.push(a);
} else {
helper(a, i+1);
}
}
2016-09-26 01:36:32 +00:00
}
helper([], 0);
return results;
}
2016-09-26 01:36:32 +00:00
function getVariations(param){
if (typeof param === "string" && param.indexOf("=") !== -1){
return [undefined].concat(getVariations(param.replace("=", "")));
} else if (param === "Time"){
return ["2n", Time("4n"), { "4n" : 1 }];
} else if (param === "Number"){
return [20, -0.5];
} else if (param === "NormalRange"){
return [0, 0.5];
} else if (param === "Frequency"){
return ["C#4", Frequency("A4")];
} else if (param === "Function"){
return [function(){}];
} else {
return param;
2017-12-30 16:26:29 +00:00
}
}
2016-09-26 01:36:32 +00:00
function generateArgs(args){
2016-09-26 01:36:32 +00:00
// turn the object into an array
var keys = [];
if (!Array.isArray(args)){
keys = Object.keys(args);
var tmp = [];
for (var p in args){
tmp.push(args[p]);
2017-12-30 16:26:29 +00:00
}
args = tmp;
}
for (var i = 0; i < args.length; i++){
args[i] = getVariations(args[i]);
}
//generate all the permutations
if (keys.length){
var permutations = generatePermutations(args);
permutations.map(function(permutation, index){
var obj = {};
permutation.map(function(val, index){
obj[keys[index]] = val;
2016-09-26 01:36:32 +00:00
});
permutations[index] = [obj];
});
return permutations;
} else {
return generatePermutations(args);
2016-09-26 01:36:32 +00:00
}
}
2016-09-26 01:36:32 +00:00
function silenceWarning(cb){
var warning = console.warn;
console.warn = function(){};
cb();
console.warn = warning;
}
2017-09-15 21:46:22 +00:00
return {
method : function(constructor, fn, args, consArgs){
2016-09-26 01:36:32 +00:00
it(fn+" ("+args.join(", ") + ")", function(){
silenceWarning(function(){
var permutations = generateArgs(args);
for (var i = 0; i < permutations.length; i++){
var instance = new constructor(consArgs);
instance[fn].apply(instance, permutations[i]);
instance.dispose();
}
2017-09-15 21:46:22 +00:00
});
});
},
member : function(constructor, member, param, consArgs){
it(member+" = "+param, function(){
silenceWarning(function(){
var permutations = generateArgs([param]);
for (var i = 0; i < permutations.length; i++){
var instance = new constructor(consArgs);
instance[member] = permutations[i];
instance.dispose();
}
2017-12-30 16:26:29 +00:00
});
});
},
constructor : function(constructor, args){
2016-09-26 01:36:32 +00:00
var argString;
if (Array.isArray(args)){
argString = args.join(", ");
} else {
argString = JSON.stringify(args);
}
2016-09-26 01:36:32 +00:00
it("constructor ( "+ argString + " )", function(){
silenceWarning(function(){
var permutations = generateArgs(args);
for (var i = 0; i < permutations.length; i++){
var Temp = function(){}; // temporary constructor
Temp.prototype = constructor.prototype;
var tmpInst = new Temp();
constructor.apply(tmpInst, permutations[i]);
tmpInst.dispose();
}
2017-09-15 21:46:22 +00:00
});
});
},
};
});