lots of work so far-- mostly tidying

This commit is contained in:
2013-01-05 15:07:07 -05:00
parent 3726488bcc
commit 29873db3cf
968 changed files with 307391 additions and 0 deletions

4
node_modules/formidable/node-gently/Makefile generated vendored Normal file
View File

@@ -0,0 +1,4 @@
test:
@find test/simple/test-*.js | xargs -n 1 -t node
.PHONY: test

167
node_modules/formidable/node-gently/Readme.md generated vendored Normal file
View File

@@ -0,0 +1,167 @@
# Gently
## Purpose
A node.js module that helps with stubbing and behavior verification. It allows you to test the most remote and nested corners of your code while keeping being fully unobtrusive.
## Features
* Overwrite and stub individual object functions
* Verify that all expected calls have been made in the expected order
* Restore stubbed functions to their original behavior
* Detect object / class names from obj.constructor.name and obj.toString()
* Hijack any required module function or class constructor
## Installation
Via [npm](http://github.com/isaacs/npm):
npm install gently@latest
## Example
Make sure your dog is working properly:
function Dog() {}
Dog.prototype.seeCat = function() {
this.bark('whuf, whuf');
this.run();
}
Dog.prototype.bark = function(bark) {
require('sys').puts(bark);
}
var gently = new (require('gently'))
, assert = require('assert')
, dog = new Dog();
gently.expect(dog, 'bark', function(bark) {
assert.equal(bark, 'whuf, whuf');
});
gently.expect(dog, 'run');
dog.seeCat();
You can also easily test event emitters with this, for example a simple sequence of 2 events emitted by `fs.WriteStream`:
var gently = new (require('gently'))
, stream = new (require('fs').WriteStream)('my_file.txt');
gently.expect(stream, 'emit', function(event) {
assert.equal(event, 'open');
});
gently.expect(stream, 'emit', function(event) {
assert.equal(event, 'drain');
});
For a full read world example, check out this test case: [test-incoming-form.js](http://github.com/felixge/node-formidable/blob/master/test/simple/test-incoming-form.js) (in [node-formdiable](http://github.com/felixge/node-formidable)).
## API
### Gently
#### new Gently()
Creates a new gently instance. It listens to the process `'exit'` event to make sure all expectations have been verified.
#### gently.expect(obj, method, [[count], stubFn])
Creates an expectation for an objects method to be called. You can optionally specify the call `count` you are expecting, as well as `stubFn` function that will run instead of the original function.
Returns a reference to the function that is getting overwritten.
#### gently.expect([count], stubFn)
Returns a function that is supposed to be executed `count` times, delegating any calls to the provided `stubFn` function. Naming your stubFn closure will help to properly diagnose errors that are being thrown:
childProcess.exec('ls', gently.expect(function lsCallback(code) {
assert.equal(0, code);
}));
#### gently.restore(obj, method)
Restores an object method that has been previously overwritten using `gently.expect()`.
#### gently.hijack(realRequire)
Returns a new require functions that catches a reference to all required modules into `gently.hijacked`.
To use this function, include a line like this in your `'my-module.js'`.
if (global.GENTLY) require = GENTLY.hijack(require);
var sys = require('sys');
exports.hello = function() {
sys.log('world');
};
Now you can write a test for the module above:
var gently = global.GENTLY = new (require('gently'))
, myModule = require('./my-module');
gently.expect(gently.hijacked.sys, 'log', function(str) {
assert.equal(str, 'world');
});
myModule.hello();
#### gently.stub(location, [exportsName])
Returns a stub class that will be used instead of the real class from the module at `location` with the given `exportsName`.
This allows to test an OOP version of the previous example, where `'my-module.js'`.
if (global.GENTLY) require = GENTLY.hijack(require);
var World = require('./world');
exports.hello = function() {
var world = new World();
world.hello();
}
And `world.js` looks like this:
var sys = require('sys');
function World() {
}
module.exports = World;
World.prototype.hello = function() {
sys.log('world');
};
Testing `'my-module.js'` can now easily be accomplished:
var gently = global.GENTLY = new (require('gently'))
, WorldStub = gently.stub('./world')
, myModule = require('./my-module')
, WORLD;
gently.expect(WorldStub, 'new', function() {
WORLD = this;
});
gently.expect(WORLD, 'hello');
myModule.hello();
#### gently.hijacked
An object that holds the references to all hijacked modules.
#### gently.verify([msg])
Verifies that all expectations of this gently instance have been satisfied. If not called manually, this method is called when the process `'exit'` event is fired.
If `msg` is given, it will appear in any error that might be thrown.
## License
Gently is licensed under the MIT license.

22
node_modules/formidable/node-gently/example/dog.js generated vendored Normal file
View File

@@ -0,0 +1,22 @@
require('../test/common');
function Dog() {}
Dog.prototype.seeCat = function() {
this.bark('whuf, whuf');
this.run();
}
Dog.prototype.bark = function(bark) {
require('sys').puts(bark);
}
var gently = new (require('gently'))
, assert = require('assert')
, dog = new Dog();
gently.expect(dog, 'bark', function(bark) {
assert.equal(bark, 'whuf, whuf');
});
gently.expect(dog, 'run');
dog.seeCat();

View File

@@ -0,0 +1,11 @@
require('../test/common');
var gently = new (require('gently'))
, stream = new (require('fs').WriteStream)('my_file.txt');
gently.expect(stream, 'emit', function(event) {
assert.equal(event, 'open');
});
gently.expect(stream, 'emit', function(event) {
assert.equal(event, 'drain');
});

1
node_modules/formidable/node-gently/index.js generated vendored Normal file
View File

@@ -0,0 +1 @@
module.exports = require('./lib/gently');

View File

@@ -0,0 +1,184 @@
var path = require('path');
function Gently() {
this.expectations = [];
this.hijacked = {};
var self = this;
process.addListener('exit', function() {
self.verify('process exit');
});
};
module.exports = Gently;
Gently.prototype.stub = function(location, exportsName) {
function Stub() {
return Stub['new'].apply(this, arguments);
};
Stub['new'] = function () {};
var stubName = 'require('+JSON.stringify(location)+')';
if (exportsName) {
stubName += '.'+exportsName;
}
Stub.prototype.toString = Stub.toString = function() {
return stubName;
};
var exports = this.hijacked[location] || {};
if (exportsName) {
exports[exportsName] = Stub;
} else {
exports = Stub;
}
this.hijacked[location] = exports;
return Stub;
};
Gently.prototype.hijack = function(realRequire) {
var self = this;
return function(location) {
return self.hijacked[location] = (self.hijacked[location])
? self.hijacked[location]
: realRequire(location);
};
};
Gently.prototype.expect = function(obj, method, count, stubFn) {
if (typeof obj != 'function' && typeof obj != 'object' && typeof obj != 'number') {
throw new Error
( 'Bad 1st argument for gently.expect(), '
+ 'object, function, or number expected, got: '+(typeof obj)
);
} else if (typeof obj == 'function' && (typeof method != 'string')) {
// expect(stubFn) interface
stubFn = obj;
obj = null;
method = null;
count = 1;
} else if (typeof method == 'function') {
// expect(count, stubFn) interface
count = obj;
stubFn = method;
obj = null;
method = null;
} else if (typeof count == 'function') {
// expect(obj, method, stubFn) interface
stubFn = count;
count = 1;
} else if (count === undefined) {
// expect(obj, method) interface
count = 1;
}
var name = this._name(obj, method, stubFn);
this.expectations.push({obj: obj, method: method, stubFn: stubFn, name: name, count: count});
var self = this;
function delegate() {
return self._stubFn(this, obj, method, name, Array.prototype.slice.call(arguments));
}
if (!obj) {
return delegate;
}
var original = (obj[method])
? obj[method]._original || obj[method]
: undefined;
obj[method] = delegate;
return obj[method]._original = original;
};
Gently.prototype.restore = function(obj, method) {
if (!obj[method] || !obj[method]._original) {
throw new Error(this._name(obj, method)+' is not gently stubbed');
}
obj[method] = obj[method]._original;
};
Gently.prototype.verify = function(msg) {
if (!this.expectations.length) {
return;
}
var validExpectations = [];
for (var i = 0, l = this.expectations.length; i < l; i++) {
var expectation = this.expectations[i];
if (expectation.count > 0) {
validExpectations.push(expectation);
}
}
this.expectations = []; // reset so that no duplicate verification attempts are made
if (!validExpectations.length) {
return;
}
var expectation = validExpectations[0];
throw new Error
( 'Expected call to '+expectation.name+' did not happen'
+ ( (msg)
? ' ('+msg+')'
: ''
)
);
};
Gently.prototype._stubFn = function(self, obj, method, name, args) {
var expectation = this.expectations[0], obj, method;
if (!expectation) {
throw new Error('Unexpected call to '+name+', no call was expected');
}
if (expectation.obj !== obj || expectation.method !== method) {
throw new Error('Unexpected call to '+name+', expected call to '+ expectation.name);
}
expectation.count -= 1;
if (expectation.count === 0) {
this.expectations.shift();
// autorestore original if its not a closure
// and no more expectations on that object
var has_more_expectations = this.expectations.reduce(function (memo, expectation) {
return memo || (expectation.obj === obj && expectation.method === method);
}, false);
if (obj !== null && method !== null && !has_more_expectations) {
if (typeof obj[method]._original !== 'undefined') {
obj[method] = obj[method]._original;
delete obj[method]._original;
} else {
delete obj[method];
}
}
}
if (expectation.stubFn) {
return expectation.stubFn.apply(self, args);
}
};
Gently.prototype._name = function(obj, method, stubFn) {
if (obj) {
var objectName = obj.toString();
if (objectName == '[object Object]' && obj.constructor.name) {
objectName = '['+obj.constructor.name+']';
}
return (objectName)+'.'+method+'()';
}
if (stubFn.name) {
return stubFn.name+'()';
}
return '>> '+stubFn.toString()+' <<';
};

View File

@@ -0,0 +1 @@
module.exports = require('./gently');

14
node_modules/formidable/node-gently/package.json generated vendored Normal file
View File

@@ -0,0 +1,14 @@
{
"name": "gently",
"version": "0.9.2",
"directories": {
"lib": "./lib/gently"
},
"main": "./lib/gently/index",
"dependencies": {},
"devDependencies": {},
"engines": {
"node": "*"
},
"optionalDependencies": {}
}

8
node_modules/formidable/node-gently/test/common.js generated vendored Normal file
View File

@@ -0,0 +1,8 @@
var path = require('path')
, sys = require('sys');
require.paths.unshift(path.dirname(__dirname)+'/lib');
global.puts = sys.puts;
global.p = function() {sys.error(sys.inspect.apply(null, arguments))};;
global.assert = require('assert');

View File

@@ -0,0 +1,348 @@
require('../common');
var Gently = require('gently')
, gently;
function test(test) {
process.removeAllListeners('exit');
gently = new Gently();
test();
}
test(function constructor() {
assert.deepEqual(gently.expectations, []);
assert.deepEqual(gently.hijacked, {});
assert.equal(gently.constructor.name, 'Gently');
});
test(function expectBadArgs() {
var BAD_ARG = 'oh no';
try {
gently.expect(BAD_ARG);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Bad 1st argument for gently.expect(), object, function, or number expected, got: '+(typeof BAD_ARG));
}
});
test(function expectObjMethod() {
var OBJ = {}, NAME = 'foobar';
OBJ.foo = function(x) {
return x;
};
gently._name = function() {
return NAME;
};
var original = OBJ.foo
, stubFn = function() {};
(function testAddOne() {
assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
assert.equal(gently.expectations.length, 1);
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, OBJ);
assert.strictEqual(expectation.method, 'foo');
assert.strictEqual(expectation.stubFn, stubFn);
assert.strictEqual(expectation.name, NAME);
assert.strictEqual(OBJ.foo._original, original);
})();
(function testAddTwo() {
gently.expect(OBJ, 'foo', 2, stubFn);
assert.equal(gently.expectations.length, 2);
assert.strictEqual(OBJ.foo._original, original);
})();
(function testAddOneWithoutMock() {
gently.expect(OBJ, 'foo');
assert.equal(gently.expectations.length, 3);
})();
var stubFnCalled = 0, SELF = {};
gently._stubFn = function(self, obj, method, name, args) {
stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, OBJ);
assert.strictEqual(method, 'foo');
assert.strictEqual(name, NAME);
assert.deepEqual(args, [1, 2]);
return 23;
};
assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
assert.equal(stubFnCalled, 1);
});
test(function expectClosure() {
var NAME = 'MY CLOSURE';
function closureFn() {};
gently._name = function() {
return NAME;
};
var fn = gently.expect(closureFn);
assert.equal(gently.expectations.length, 1);
var expectation = gently.expectations[0];
assert.strictEqual(expectation.obj, null);
assert.strictEqual(expectation.method, null);
assert.strictEqual(expectation.stubFn, closureFn);
assert.strictEqual(expectation.name, NAME);
var stubFnCalled = 0, SELF = {};
gently._stubFn = function(self, obj, method, name, args) {
stubFnCalled++;
assert.strictEqual(self, SELF);
assert.strictEqual(obj, null);
assert.strictEqual(method, null);
assert.strictEqual(name, NAME);
assert.deepEqual(args, [1, 2]);
return 23;
};
assert.equal(fn.apply(SELF, [1, 2]), 23);
assert.equal(stubFnCalled, 1);
});
test(function expectClosureCount() {
var stubFnCalled = 0;
function closureFn() {stubFnCalled++};
var fn = gently.expect(2, closureFn);
assert.equal(gently.expectations.length, 1);
fn();
assert.equal(gently.expectations.length, 1);
fn();
assert.equal(stubFnCalled, 2);
});
test(function restore() {
var OBJ = {}, NAME = '[my object].myFn()';
OBJ.foo = function(x) {
return x;
};
gently._name = function() {
return NAME;
};
var original = OBJ.foo;
gently.expect(OBJ, 'foo');
gently.restore(OBJ, 'foo');
assert.strictEqual(OBJ.foo, original);
(function testError() {
try {
gently.restore(OBJ, 'foo');
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, NAME+' is not gently stubbed');
}
})();
});
test(function _stubFn() {
var OBJ1 = {toString: function() {return '[OBJ 1]'}}
, OBJ2 = {toString: function() {return '[OBJ 2]'}, foo: function () {return 'bar';}}
, SELF = {};
gently.expect(OBJ1, 'foo', function(x) {
assert.strictEqual(this, SELF);
return x * 2;
});
assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
(function testAutorestore() {
assert.equal(OBJ2.foo(), 'bar');
gently.expect(OBJ2, 'foo', function() {
return 'stubbed foo';
});
gently.expect(OBJ2, 'foo', function() {
return "didn't restore yet";
});
assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
assert.equal(OBJ2.foo(), 'bar');
assert.deepEqual(gently.expectations, []);
})();
(function testNoMoreCallExpected() {
try {
gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
}
})();
(function testDifferentCallExpected() {
gently.expect(OBJ2, 'bar');
try {
gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));
}
assert.equal(gently.expectations.length, 1);
})();
(function testNoMockCallback() {
OBJ2.bar();
assert.equal(gently.expectations.length, 0);
})();
});
test(function stub() {
var LOCATION = './my_class';
(function testRegular() {
var Stub = gently.stub(LOCATION);
assert.ok(Stub instanceof Function);
assert.strictEqual(gently.hijacked[LOCATION], Stub);
assert.ok(Stub['new'] instanceof Function);
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
(function testConstructor() {
var newCalled = 0
, STUB
, ARGS = ['foo', 'bar'];
Stub['new'] = function(a, b) {
assert.equal(a, ARGS[0]);
assert.equal(b, ARGS[1]);
newCalled++;
STUB = this;
};
var stub = new Stub(ARGS[0], ARGS[1]);
assert.strictEqual(stub, STUB);
assert.equal(newCalled, 1);
assert.equal(stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
})();
(function testUseReturnValueAsInstance() {
var R = {};
Stub['new'] = function() {
return R;
};
var stub = new Stub();
assert.strictEqual(stub, R);
})();
})();
var EXPORTS_NAME = 'MyClass';
test(function testExportsName() {
var Stub = gently.stub(LOCATION, EXPORTS_NAME);
assert.strictEqual(gently.hijacked[LOCATION][EXPORTS_NAME], Stub);
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
(function testConstructor() {
var stub = new Stub();
assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
})();
});
});
test(function hijack() {
var LOCATION = './foo'
, REQUIRE_CALLS = 0
, EXPORTS = {}
, REQUIRE = function() {
REQUIRE_CALLS++;
return EXPORTS;
};
var hijackedRequire = gently.hijack(REQUIRE);
hijackedRequire(LOCATION);
assert.strictEqual(gently.hijacked[LOCATION], EXPORTS);
assert.equal(REQUIRE_CALLS, 1);
// make sure we are caching the hijacked module
hijackedRequire(LOCATION);
assert.equal(REQUIRE_CALLS, 1);
});
test(function verify() {
var OBJ = {toString: function() {return '[OBJ]'}};
gently.verify();
gently.expect(OBJ, 'foo');
try {
gently.verify();
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen');
}
try {
gently.verify('foo');
assert.ok(false, 'throw needs to happen');
} catch (e) {
assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)');
}
});
test(function processExit() {
var verifyCalled = 0;
gently.verify = function(msg) {
verifyCalled++;
assert.equal(msg, 'process exit');
};
process.emit('exit');
assert.equal(verifyCalled, 1);
});
test(function _name() {
(function testNamedClass() {
function Foo() {};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), '[Foo].bar()');
})();
(function testToStringPreference() {
function Foo() {};
Foo.prototype.toString = function() {
return '[Superman 123]';
};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), '[Superman 123].bar()');
})();
(function testUnamedClass() {
var Foo = function() {};
var foo = new Foo();
assert.equal(gently._name(foo, 'bar'), foo.toString()+'.bar()');
})();
(function testNamedClosure() {
function myClosure() {};
assert.equal(gently._name(null, null, myClosure), myClosure.name+'()');
})();
(function testUnamedClosure() {
var myClosure = function() {2+2 == 5};
assert.equal(gently._name(null, null, myClosure), '>> '+myClosure.toString()+' <<');
})();
});
test(function verifyExpectNone() {
var OBJ = {toString: function() {return '[OBJ]'}};
gently.verify();
gently.expect(OBJ, 'foo', 0);
try {
gently.verify();
} catch (e) {
assert.fail('Exception should not have been thrown');
}
});