Skip to content

Commit

Permalink
Merge 21d1772 into ee8dbce
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed Dec 22, 2013
2 parents ee8dbce + 21d1772 commit c90af99
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 1,310 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Power Assert feature enhancer for assert function/object.

DESCRIPTION
---------------------------------------
`empower` is a core module of [power-assert](http://github.com/twada/power-assert) family. `empower` enhances standard `assert` function or any assert-like object to work with power-assert feature added code instrumented by [espower](http://github.com/twada/espower). `empower` module also provides a default formatter for power-assert output.
`empower` is a core module of [power-assert](http://github.com/twada/power-assert) family. `empower` enhances standard `assert` function or any assert-like object to work with power-assert feature added code instrumented by [espower](http://github.com/twada/espower).


`empower` works with standard `assert` function (best fit with [Mocha](http://visionmedia.github.io/mocha/)), and also supports assert-like objects provided by various testing frameworks such as [QUnit](http://qunitjs.com/) or [nodeunit](https://github.com/caolan/nodeunit).
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "empower",
"version": "0.2.1",
"version": "0.3.0-dev",
"main": [
"lib/empower.js",
"lib/power-assert-formatter.js"
Expand Down
42 changes: 21 additions & 21 deletions lib/empower.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,19 @@

// using returnExports UMD pattern
if (typeof define === 'function' && define.amd) {
define(['./power-assert-formatter'], factory);
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('./power-assert-formatter'));
module.exports = factory();
} else {
root.empower = factory(root.powerAssertFormatter);
root.empower = factory();
}
}(this, function (defaultFormatter) {
}(this, function () {
'use strict';


function defaultOptions () {
return {
destructive: false,
formatter: defaultFormatter,
lineSeparator: '\n',
targetMethods: {
oneArg: [
Expand All @@ -52,10 +51,11 @@
/**
* Enhance Power Assert feature to assert function/object.
* @param assert target assert function or object to enhance
* @param formatter power assert formatter object
* @param options enhancement options
* @return enhanced assert function/object
*/
function empower (assert, options) {
function empower (assert, formatter, options) {
var typeOfAssert = (typeof assert),
config;
if ((typeOfAssert !== 'object' && typeOfAssert !== 'function') || assert === null) {
Expand All @@ -67,9 +67,9 @@
config = extend(defaultOptions(), (options || {}));
switch (typeOfAssert) {
case 'function':
return empowerAssertFunction(assert, config);
return empowerAssertFunction(assert, formatter, config);
case 'object':
return empowerAssertObject(assert, config);
return empowerAssertObject(assert, formatter, config);
default:
throw new Error('Cannot be here');
}
Expand All @@ -81,21 +81,21 @@
}


function empowerAssertObject (assertObject, config) {
function empowerAssertObject (assertObject, formatter, config) {
if (typeof assertObject.ok !== 'function') {
throw new TypeError('empower target object should be respond to \'ok\' method.');
}
var enhancement = enhance(assertObject, config),
var enhancement = enhance(assertObject, formatter, config),
target = config.destructive ? assertObject : Object.create(assertObject);
return extend(target, enhancement);
}


function empowerAssertFunction (assertFunction, config) {
function empowerAssertFunction (assertFunction, formatter, config) {
if (config.destructive) {
throw new Error('cannot use destructive:true to function.');
}
var enhancement = enhance(assertFunction, config),
var enhancement = enhance(assertFunction, formatter, config),
powerAssert = function powerAssert (context, message) {
enhancement.ok(context, message);
};
Expand All @@ -104,7 +104,7 @@
}


function enhance (target, config) {
function enhance (target, formatter, config) {
var events = [],
enhancement = {};

Expand All @@ -121,12 +121,12 @@

config.targetMethods.oneArg.forEach(function (methodName) {
if (typeof target[methodName] === 'function') {
enhancement[methodName] = decorateOneArg(bind(target[methodName], target), config);
enhancement[methodName] = decorateOneArg(bind(target[methodName], target), formatter, config);
}
});
config.targetMethods.twoArgs.forEach(function (methodName) {
if (typeof target[methodName] === 'function') {
enhancement[methodName] = decorateTwoArgs(bind(target[methodName], target), config);
enhancement[methodName] = decorateTwoArgs(bind(target[methodName], target), formatter, config);
}
});

Expand All @@ -141,19 +141,19 @@
}


function decorateOneArg (baseAssert, config) {
function decorateOneArg (baseAssert, formatter, config) {
return function (value, message) {
var context;
if (! isEspoweredValue(value)) {
return baseAssert(value, message);
}
context = value.powerAssertContext;
return baseAssert(context.value, buildPowerAssertText(message, context, config));
return baseAssert(context.value, buildPowerAssertText(message, context, formatter, config));
};
}


function decorateTwoArgs (baseAssert, config) {
function decorateTwoArgs (baseAssert, formatter, config) {
return function (arg1, arg2, message) {
var context, val1, val2;
if (!(isEspoweredValue(arg1) || isEspoweredValue(arg2))) {
Expand All @@ -178,13 +178,13 @@
val2 = arg2;
}

return baseAssert(val1, val2, buildPowerAssertText(message, context, config));
return baseAssert(val1, val2, buildPowerAssertText(message, context, formatter, config));
};
}


function buildPowerAssertText (message, context, config) {
var powerAssertText = config.formatter.format(context).join(config.lineSeparator);
function buildPowerAssertText (message, context, formatter, config) {
var powerAssertText = formatter.format(context).join(config.lineSeparator);
return message ? message + ' ' + powerAssertText : powerAssertText;
}

Expand Down
160 changes: 0 additions & 160 deletions lib/power-assert-formatter.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "empower",
"description": "Power Assert feature enhancer for assert function/object",
"version": "0.2.1",
"version": "0.3.0-dev",
"keywords": [
"test",
"assert",
Expand Down
Loading

0 comments on commit c90af99

Please sign in to comment.