Skip to content

Commit

Permalink
Adding tests and support for comparing generators.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 15, 2014
1 parent f5d7190 commit fb03635
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var ObjectPrototype = Object.prototype;
var toString = ObjectPrototype.toString;
var has = ObjectPrototype.hasOwnProperty;
var isGenerator = require('is-generator-function');

var getPrototypeOf = Object.getPrototypeOf;
if (!getPrototypeOf) {
Expand Down Expand Up @@ -69,6 +70,10 @@ module.exports = function isEqual(value, other) {
if (!isEqual(value.name, other.name)) { return false; }
if (!isEqual(value.length, other.length)) { return false; }

var valueIsGen = isGenerator(value);
var otherIsGen = isGenerator(other);
if (valueIsGen !== otherIsGen) { return false; }

return isEqual(String(value), String(other));
}

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
"comparison",
"equality"
],
"dependencies": {},
"dependencies": {
"is-generator-function": "~1.0.0"
},
"devDependencies": {
"tape": "~3.0.3",
"covert": "1.0.0",
"jscs": "~1.8.1",
"foreach": "~2.0.5"
"foreach": "~2.0.5",
"make-generator-function": "~1.0.0"
},
"testling": {
"files": "test.js",
Expand Down
18 changes: 18 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var test = require('tape');
var isEqual = require('./');
var hasGeneratorSupport = typeof require('make-generator-function') === 'function';

var forEach = require('foreach');
var copyFunction = function (fn) {
Expand Down Expand Up @@ -122,6 +123,9 @@ test('functions', function (t) {
var g = function g() { /* SOME STUFF */ return 1; };
var anon1 = function () { /* ANONYMOUS! */ return 'anon'; };
var anon2 = function () { /* ANONYMOUS! */ return 'anon'; };
/* jscs: disable */
var fnNoSpace = function(){};
/* jscs: enable */

/* for code coverage */
f1();
Expand All @@ -143,6 +147,20 @@ test('functions', function (t) {
t.notOk(isEqual(f1, f3), 'functions with same names but different implementations are not equal');
t.ok(isEqual(anon1, anon2), 'anon functions with same implementations are equal');

t.test('generators', { skip: !hasGeneratorSupport }, function (st) {
var genFnStar = Function('return function* () {};')();
var genFnSpaceStar = Function('return function *() {};')();
var genNoSpaces = Function('return function*(){};')();
st.notOk(isEqual(fnNoSpace, genNoSpaces), 'generator and fn that are otherwise identical are not equal');

var generators = [genFnStar, genFnSpaceStar, genNoSpaces];
forEach(generators, function (generator) {
st.ok(isEqual(generator, generator), generator + ' is equal to itself');
st.ok(isEqual(generator, copyFunction(generator)), generator + ' is equal to copyFunction(generator)');
});
st.end();
});

t.end();
});

Expand Down

0 comments on commit fb03635

Please sign in to comment.