Skip to content

Commit

Permalink
Merge pull request #16 from power-assert-js/dontenum
Browse files Browse the repository at this point in the history
make `_capt` and `_expr` methods not enumerable
  • Loading branch information
twada committed Oct 21, 2015
2 parents e2a6f4c + 1b885b6 commit 6af11b4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
19 changes: 13 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
*/
var defaultOptions = require('./lib/default-options');
var Decorator = require('./lib/decorator');
var capturable = require('./lib/capturable');
var create = require('object-create');
var slice = Array.prototype.slice;
var extend = require('xtend/mutable');
var define = require('define-properties');

/**
* Enhance Power Assert feature to assert function/object.
Expand All @@ -22,31 +24,36 @@ var extend = require('xtend/mutable');
*/
function empower (assert, formatter, options) {
var typeOfAssert = (typeof assert);
var config;
var enhancedAssert;
if ((typeOfAssert !== 'object' && typeOfAssert !== 'function') || assert === null) {
throw new TypeError('empower argument should be a function or object.');
}
if (isEmpowered(assert)) {
return assert;
}
config = extend(defaultOptions(), options);
switch (typeOfAssert) {
case 'function':
return empowerAssertFunction(assert, formatter, config);
enhancedAssert = empowerAssertFunction(assert, formatter, options);
break;
case 'object':
return empowerAssertObject(assert, formatter, config);
enhancedAssert = empowerAssertObject(assert, formatter, options);
break;
default:
throw new Error('Cannot be here');
}
define(enhancedAssert, capturable());
return enhancedAssert;
}

function empowerAssertObject (assertObject, formatter, config) {
function empowerAssertObject (assertObject, formatter, options) {
var config = extend(defaultOptions(), options);
var target = config.destructive ? assertObject : create(assertObject);
var decorator = new Decorator(target, formatter, config);
return extend(target, decorator.enhancement());
}

function empowerAssertFunction (assertFunction, formatter, config) {
function empowerAssertFunction (assertFunction, formatter, options) {
var config = extend(defaultOptions(), options);
if (config.destructive) {
throw new Error('cannot use destructive:true to function.');
}
Expand Down
2 changes: 0 additions & 2 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var extend = require('xtend/mutable');
var forEach = require('array-foreach');
var map = require('array-map');
var filter = require('array-filter');
var capturable = require('./capturable');
var decorate = require('./decorate');


Expand All @@ -31,7 +30,6 @@ Decorator.prototype.enhancement = function () {
container[methodName] = decorate(callSpec, that);
}
});
extend(container, capturable());
return container;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"array-foreach": "^1.0.1",
"array-map": "0.0.0",
"array-some": "^1.0.0",
"define-properties": "^1.1.2",
"escallmatch": "^1.4.1",
"object-create": "^0.1.0",
"xtend": "^4.0.0"
Expand Down
14 changes: 14 additions & 0 deletions test/empower_option_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,23 @@ function sharedTestsForEmpowerFunctionReturnValue () {
test('has _capt method', function () {
assert.equal(typeof this.empoweredAssert._capt, 'function');
});
test('_capt method should not be enumerable', function () {
var key, enumerated = {};
for (key in this.empoweredAssert) {
enumerated[key] = this.empoweredAssert[key];
}
assert.equal(typeof enumerated['_capt'], 'undefined');
});
test('has _expr method', function () {
assert.equal(typeof this.empoweredAssert._expr, 'function');
});
test('_expr method should not be enumerable', function () {
var key, enumerated = {};
for (key in this.empoweredAssert) {
enumerated[key] = this.empoweredAssert[key];
}
assert.equal(typeof enumerated['_expr'], 'undefined');
});
test('has equal method', function () {
assert.equal(typeof this.empoweredAssert.equal, 'function');
});
Expand Down

0 comments on commit 6af11b4

Please sign in to comment.