Comparing version 0.3.5 to 0.4.0
{ | ||
"name": "empower", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"main": "lib/empower.js", | ||
@@ -13,7 +13,4 @@ "ignore": [ | ||
], | ||
"dependencies": { | ||
"es5-shim": "~2.3.0" | ||
}, | ||
"devDependencies": { | ||
"espower": "~0.3.1", | ||
"espower": "~0.4.0", | ||
"esprima": "~1.0.4", | ||
@@ -20,0 +17,0 @@ "escodegen": "~1.3.0", |
@@ -55,8 +55,2 @@ module.exports = function(grunt) { | ||
}, | ||
withoutBind: { | ||
src: ['test/test-without-bind.html'], | ||
options: { | ||
run: true | ||
} | ||
}, | ||
amd: { | ||
@@ -97,4 +91,4 @@ src: ['test/test-amd.html'], | ||
grunt.registerTask('unit', ['jshint', 'mochaTest:unit']); | ||
grunt.registerTask('test', ['jshint', 'mochaTest:unit', 'mocha:browser', 'mocha:amd', 'mocha:withoutBind']); | ||
grunt.registerTask('test', ['jshint', 'mochaTest:unit', 'mocha:browser', 'mocha:amd']); | ||
grunt.registerTask('coverage', ['mochaTest:coverage']); | ||
}; |
@@ -33,2 +33,4 @@ /** | ||
destructive: false, | ||
modifyMessageOnFail: false, | ||
saveContextOnFail: false, | ||
targetMethods: { | ||
@@ -64,5 +66,2 @@ oneArg: [ | ||
} | ||
if (typeof (function(){}.bind) !== 'function') { | ||
throw new Error('empower module depends on method Function#bind that your browser does not support. Please add es5-shim.js to your dependencies.'); | ||
} | ||
if (isEmpowered(assert)) { | ||
@@ -109,5 +108,35 @@ return assert; | ||
function enhance (target, formatter, config) { | ||
var events = [], | ||
enhancement = (typeof target === 'function') ? decorateOneArg(target, formatter) : {}; | ||
var eagerEvaluation = !(config.modifyMessageOnFail || config.saveContextOnFail), | ||
doPowerAssert = function (baseAssert, args, message, context) { | ||
if (eagerEvaluation) { | ||
args.push(buildPowerAssertText(message, context)); | ||
return baseAssert.apply(target, args); | ||
} | ||
try { | ||
args.push(message); | ||
return baseAssert.apply(target, args); | ||
} catch (e) { | ||
if (e.name !== 'AssertionError') { | ||
throw e; | ||
} | ||
if (config.modifyMessageOnFail) { | ||
e.message = buildPowerAssertText(message, context); | ||
if (typeof e.generatedMessage !== 'undefined') { | ||
e.generatedMessage = false; | ||
} | ||
} | ||
if (config.saveContextOnFail) { | ||
e.powerAssertContext = context; | ||
} | ||
throw e; | ||
} | ||
}, | ||
enhancement = (typeof target === 'function') ? decorateOneArg(target, target, doPowerAssert) : {}, | ||
events = []; | ||
function buildPowerAssertText (message, context) { | ||
var powerAssertText = formatter(context); | ||
return message ? message + ' ' + powerAssertText : powerAssertText; | ||
} | ||
function _capt (value, kind, location) { | ||
@@ -126,3 +155,3 @@ events.push({value: value, kind: kind, location: location}); | ||
if (typeof target[methodName] === 'function') { | ||
enhancement[methodName] = decorateOneArg(target[methodName].bind(target), formatter); | ||
enhancement[methodName] = decorateOneArg(target, target[methodName], doPowerAssert); | ||
} | ||
@@ -132,3 +161,3 @@ }); | ||
if (typeof target[methodName] === 'function') { | ||
enhancement[methodName] = decorateTwoArgs(target[methodName].bind(target), formatter); | ||
enhancement[methodName] = decorateTwoArgs(target, target[methodName], doPowerAssert); | ||
} | ||
@@ -148,10 +177,10 @@ }); | ||
function decorateOneArg (baseAssert, formatter) { | ||
function decorateOneArg (target, baseAssert, doPowerAssert) { | ||
return function (value, message) { | ||
var context; | ||
if (! isEspoweredValue(value)) { | ||
return baseAssert(value, message); | ||
return baseAssert.apply(target, [value, message]); | ||
} | ||
context = value.powerAssertContext; | ||
return baseAssert(context.value, buildPowerAssertText(message, context, formatter)); | ||
return doPowerAssert(baseAssert, [context.value], message, context); | ||
}; | ||
@@ -161,7 +190,7 @@ } | ||
function decorateTwoArgs (baseAssert, formatter) { | ||
function decorateTwoArgs (target, baseAssert, doPowerAssert) { | ||
return function (arg1, arg2, message) { | ||
var context, val1, val2; | ||
if (!(isEspoweredValue(arg1) || isEspoweredValue(arg2))) { | ||
return baseAssert(arg1, arg2, message); | ||
return baseAssert.apply(target, [arg1, arg2, message]); | ||
} | ||
@@ -187,3 +216,3 @@ | ||
return baseAssert(val1, val2, buildPowerAssertText(message, context, formatter)); | ||
return doPowerAssert(baseAssert, [val1, val2], message, context); | ||
}; | ||
@@ -193,8 +222,2 @@ } | ||
function buildPowerAssertText (message, context, formatter) { | ||
var powerAssertText = formatter(context); | ||
return message ? message + ' ' + powerAssertText : powerAssertText; | ||
} | ||
// borrowed from qunit.js | ||
@@ -201,0 +224,0 @@ function extend (a, b) { |
{ | ||
"name": "empower", | ||
"description": "Power Assert feature enhancer for assert function/object", | ||
"version": "0.3.5", | ||
"version": "0.4.0", | ||
"keywords": [ | ||
@@ -30,3 +30,3 @@ "test", | ||
"devDependencies": { | ||
"espower": "~0.3.1", | ||
"espower": "~0.4.0", | ||
"esprima": "~1.0.4", | ||
@@ -33,0 +33,0 @@ "escodegen": "~1.3.0", |
@@ -39,15 +39,2 @@ (function (root, factory) { | ||
var weave = function () { | ||
function extractBodyFrom (source) { | ||
var tree = esprima.parse(source, {tolerant: true, loc: true, range: true}); | ||
return tree.body[0]; | ||
} | ||
function extractBodyOfAssertionAsCode (node) { | ||
var expression; | ||
if (node.type === 'ExpressionStatement') { | ||
expression = node.expression; | ||
} | ||
return escodegen.generate(expression.arguments[0], {format: {compact: true}}); | ||
} | ||
function applyEspower (line, options) { | ||
@@ -64,3 +51,3 @@ options = options || { | ||
}; | ||
var tree = extractBodyFrom(line); | ||
var tree = esprima.parse(line, {tolerant: true, loc: true, range: true, tokens: true}); | ||
return espower(tree, options); | ||
@@ -67,0 +54,0 @@ } |
@@ -36,2 +36,8 @@ (function (root, factory) { | ||
}); | ||
test('modifyMessageOnFail: false', function () { | ||
assert.equal(this.options.modifyMessageOnFail, false); | ||
}); | ||
test('saveContextOnFail: false', function () { | ||
assert.equal(this.options.saveContextOnFail, false); | ||
}); | ||
test('formatter: undefined', function () { | ||
@@ -38,0 +44,0 @@ assert.deepEqual(typeof this.options.formatter, 'undefined'); |
@@ -35,18 +35,5 @@ (function (root, factory) { | ||
var weave = function () { | ||
function extractBodyFrom (source) { | ||
var tree = esprima.parse(source, {tolerant: true, loc: true, range: true}); | ||
return tree.body[0]; | ||
} | ||
function extractBodyOfAssertionAsCode (node) { | ||
var expression; | ||
if (node.type === 'ExpressionStatement') { | ||
expression = node.expression; | ||
} | ||
return escodegen.generate(expression.arguments[0], {format: {compact: true}}); | ||
} | ||
function applyEspower (line, options) { | ||
options = options || {destructive: false, source: line, path: '/path/to/some_test.js', powerAssertVariableName: 'assert'}; | ||
var tree = extractBodyFrom(line); | ||
var tree = esprima.parse(line, {tolerant: true, loc: true, range: true, tokens: true}); | ||
return espower(tree, options); | ||
@@ -65,6 +52,9 @@ } | ||
].join('\n'); | ||
}, | ||
assert = empower(baseAssert, fakeFormatter); | ||
}; | ||
function testWithOption (option) { | ||
var assert = empower(baseAssert, fakeFormatter, option); | ||
test('empowered function also acts like an assert function', function () { | ||
@@ -77,7 +67,17 @@ var falsy = 0; | ||
baseAssert.equal(e.name, 'AssertionError'); | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert(falsy);', | ||
'[{"value":0,"kind":"ident","location":{"start":{"line":1,"column":7}}}]' | ||
].join('\n')); | ||
if (option.modifyMessageOnFail) { | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert(falsy);', | ||
'[{"value":0,"kind":"ident","location":{"start":{"line":1,"column":7}}}]' | ||
].join('\n')); | ||
} | ||
if (option.saveContextOnFail) { | ||
baseAssert.deepEqual(e.powerAssertContext, { | ||
"value":0, | ||
"location":{"start":{"line":1,"column":7},"path":"/path/to/some_test.js"}, | ||
"content":"assert(falsy);", | ||
"events": [{"value":0,"kind":"ident","location":{"start":{"line":1,"column":7}}}] | ||
}); | ||
} | ||
} | ||
@@ -95,7 +95,17 @@ }); | ||
baseAssert.equal(e.name, 'AssertionError'); | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.ok(falsy);', | ||
'[{"value":0,"kind":"ident","location":{"start":{"line":1,"column":10}}}]' | ||
].join('\n')); | ||
if (option.modifyMessageOnFail) { | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.ok(falsy);', | ||
'[{"value":0,"kind":"ident","location":{"start":{"line":1,"column":10}}}]' | ||
].join('\n')); | ||
} | ||
if (option.saveContextOnFail) { | ||
baseAssert.deepEqual(e.powerAssertContext, { | ||
"value":0, | ||
"location":{"start":{"line":1,"column":10},"path":"/path/to/some_test.js"}, | ||
"content":"assert.ok(falsy);", | ||
"events": [{"value":0,"kind":"ident","location":{"start":{"line":1,"column":10}}}] | ||
}); | ||
} | ||
} | ||
@@ -114,7 +124,17 @@ }); | ||
baseAssert.equal(e.name, 'AssertionError'); | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal(foo, bar);', | ||
'[{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}},{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":18}}}]' | ||
].join('\n')); | ||
if (option.modifyMessageOnFail) { | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal(foo, bar);', | ||
'[{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}},{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":18}}}]' | ||
].join('\n')); | ||
} | ||
if (option.saveContextOnFail) { | ||
baseAssert.deepEqual(e.powerAssertContext, { | ||
"value":"foo", | ||
"location":{"start":{"line":1,"column":13},"path":"/path/to/some_test.js"}, | ||
"content":"assert.equal(foo, bar);", | ||
"events": [{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}},{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":18}}}] | ||
}); | ||
} | ||
} | ||
@@ -130,7 +150,17 @@ }); | ||
baseAssert.equal(e.name, 'AssertionError'); | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal("foo", bar);', | ||
'[{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":20}}}]' | ||
].join('\n')); | ||
if (option.modifyMessageOnFail) { | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal("foo", bar);', | ||
'[{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":20}}}]' | ||
].join('\n')); | ||
} | ||
if (option.saveContextOnFail) { | ||
baseAssert.deepEqual(e.powerAssertContext, { | ||
"value":"bar", | ||
"location":{"start":{"line":1,"column":20},"path":"/path/to/some_test.js"}, | ||
"content":"assert.equal(\"foo\", bar);", | ||
"events": [{"value":"bar","kind":"ident","location":{"start":{"line":1,"column":20}}}] | ||
}); | ||
} | ||
} | ||
@@ -146,7 +176,17 @@ }); | ||
baseAssert.equal(e.name, 'AssertionError'); | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal(foo, "bar");', | ||
'[{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}}]' | ||
].join('\n')); | ||
if (option.modifyMessageOnFail) { | ||
baseAssert.equal(e.message, [ | ||
'/path/to/some_test.js', | ||
'assert.equal(foo, "bar");', | ||
'[{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}}]' | ||
].join('\n')); | ||
} | ||
if (option.saveContextOnFail) { | ||
baseAssert.deepEqual(e.powerAssertContext, { | ||
"value":"foo", | ||
"location":{"start":{"line":1,"column":13},"path":"/path/to/some_test.js"}, | ||
"content":"assert.equal(foo, \"bar\");", | ||
"events": [{"value":"foo","kind":"ident","location":{"start":{"line":1,"column":13}}}] | ||
}); | ||
} | ||
} | ||
@@ -156,2 +196,24 @@ }); | ||
} | ||
testWithOption({ | ||
modifyMessageOnFail: false, | ||
saveContextOnFail: false | ||
}); | ||
testWithOption({ | ||
modifyMessageOnFail: true, | ||
saveContextOnFail: false | ||
}); | ||
testWithOption({ | ||
modifyMessageOnFail: false, | ||
saveContextOnFail: true | ||
}); | ||
testWithOption({ | ||
modifyMessageOnFail: true, | ||
saveContextOnFail: true | ||
}); | ||
})); |
var require = { | ||
paths: { | ||
assert: "../bower_components/assert/assert", | ||
"es5-shim": "../bower_components/es5-shim/es5-shim", | ||
escodegen: "../bower_components/escodegen/escodegen.browser", | ||
@@ -6,0 +5,0 @@ esprima: "../bower_components/esprima/esprima", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
939
3
43048
14