Tone.js/test/helper/APITest.js
2016-09-25 21:36:32 -04:00

103 lines
No EOL
2.7 KiB
JavaScript

define(["Test", "Tone/type/Type", "Tone/core/Transport"],
function (Test, Type, Transport) {
//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);
}
}
helper([], 0);
return results;
}
function getVariations(param){
if (typeof param === "string" && param.indexOf("=") !== -1){
return [undefined].concat(getVariations(param.replace("=", "")));
} else if (param === "Time"){
return ["2n", 1, "0.5"];
} else if (param === "Number"){
return [20, 0, -0.5];
} else if (param === "NormalRange"){
return [0, 0.5, 1];
} else if (param === "Frequency"){
return ["2n", "C#4", 220];
} else if (param === "Function"){
return [function(){}];
} else {
return param;
}
}
function generateArgs(args){
// 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]);
}
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;
});
permutations[index] = [obj];
});
return permutations;
} else {
return generatePermutations(args);
}
}
return {
method : function(constructor, fn, args){
it (fn+" ("+args.join(", ") + ")", function(){
var permutations = generateArgs(args);
for (var i = 0; i < permutations.length; i++){
var instance = new constructor();
instance[fn].apply(instance, permutations[i]);
instance.dispose();
}
});
},
constructor : function(constructor, args){
var argString;
if (Array.isArray(args)){
argString = args.join(", ");
} else {
argString = JSON.stringify(args);
}
it ("constructor ( "+ argString + " )", 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();
}
});
},
};
});