Skip to content

Instantly share code, notes, and snippets.

@dikmax
Created February 23, 2013 14:38
Show Gist options
  • Save dikmax/5019995 to your computer and use it in GitHub Desktop.
Save dikmax/5019995 to your computer and use it in GitHub Desktop.
Object inheritance test generator
var fs = require('fs');
var generateBaseClass = function () {
var baseClass = 'var BaseClass=function(){};\n';
for (var i = 1; i <= 100; ++i) {
baseClass += 'BaseClass.prototype.method' + i + '=function(){return ' + i + '}\n';
}
return baseClass;
}
function generateInstantiations() {
var instantiations = '\nvar o=[]\n';
for (var i = 1; i <= 100; ++i) {
instantiations += 'for(i=0;i<100;++i){o.push(new ChildClass' + i + '())}\n';
}
return instantiations + 'console.log(o)\n';
}
/**
* Inside constructor
* @returns {string}
*/
var generateTest1 = function () {
var childClasses = '';
for (var i = 1; i <= 100; ++i) {
childClasses += '\nvar ChildClass' + i + '=function(){\n'
for (var j = 1; j <= 100; ++j) {
childClasses += '\tthis.method' + (i*100 + j) + '=function(){return ' + (i*100 + j) + '}\n';
}
childClasses += '}\n';
}
return childClasses;
};
/**
* Copy prototype with for
* @returns {string}
*/
var generateTest2 = function () {
var childClasses = '';
for (var i = 1; i <= 100; ++i) {
childClasses += '\nvar ChildClass' + i + '=function(){}\n' +
'for(var p in BaseClass.prototype){' +
'if(BaseClass.prototype.hasOwnProperty(p)){' +
'ChildClass' + i + '.prototype[p]=BaseClass.prototype[p]}}\n';
for (var j = 1; j <= 100; ++j) {
childClasses += 'ChildClass' + i + '.prototype.method' + (i*100 + j) + '=function(){return ' + (i*100 + j) + '}\n';
}
}
return childClasses;
};
/**
* With temp constructor
* @returns {string}
*/
var generateTest3 = function () {
var childClasses = '\nvar tmpCtor;';
for (var i = 1; i <= 100; ++i) {
childClasses += 'var ChildClass' + i + '=function(){}\n' +
'tmpCtor=function(){};tmpCtor.prototype=BaseClass.prototype;' +
'ChildClass' + i + '.prototype=new tmpCtor();\n'
for (var j = 1; j <= 100; ++j) {
childClasses += 'ChildClass' + i + '.prototype.method' + (i*100 + j) + '=function(){return ' + (i*100 + j) + '}\n';
}
}
return childClasses;
};
/**
* Object.create
* @returns {string}
*/
var generateTest4 = function () {
var childClasses = '';
for (var i = 1; i <= 100; ++i) {
childClasses += '\nvar ChildClass' + i + '=function(){}\n' +
'ChildClass' + i + '.prototype=Object.create(BaseClass.prototype);\n'
for (var j = 1; j <= 100; ++j) {
childClasses += 'ChildClass' + i + '.prototype.method' + (i*100 + j) + '=function(){return ' + (i*100 + j) + '}\n';
}
}
return childClasses;
};
/**
* __proto__
* @returns {string}
*/
var generateTest5 = function () {
var childClasses = '';
for (var i = 1; i <= 100; ++i) {
childClasses += '\nvar ChildClass' + i + '=function(){}\n' +
'ChildClass' + i + '.prototype.__proto__=BaseClass.prototype;\n'
for (var j = 1; j <= 100; ++j) {
childClasses += 'ChildClass' + i + '.prototype.method' + (i*100 + j) + '=function(){return ' + (i*100 + j) + '}\n';
}
}
return childClasses;
};
var generators = [generateTest1, generateTest2, generateTest3, generateTest4, generateTest5];
var baseClass = generateBaseClass();
var instantiations = generateInstantiations();
for (var i = 0; i < generators.length; ++i) {
var testCode = generators[i]();
fs.writeFile('./test' + (i+1) + '.html',
'<!DOCTYPE html><html><head><title>Test ' + (i+1) + '</title></head>' +
'<body><button onclick="test()">Start</button>' +
'<script type="text/javascript">\n' +
'function test(){var i,o;' +
baseClass +
testCode +
instantiations +
'alert("Done")}\n' +
'</script></body>' +
'</html>', 'utf8',
function (err) {
if (err) {
console.log(err);
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment