Skip to content

Commit

Permalink
feat(empower): use escallmatch module to describe target patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
twada committed Aug 6, 2014
1 parent 10edf49 commit 533a21a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
50 changes: 42 additions & 8 deletions lib/empower.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'use strict';

var extend = require('node.extend'),
escallmatch = require('escallmatch'),
isPhantom = typeof window !== 'undefined' && typeof window.callPhantom === 'function';

function defaultOptions () {
Expand All @@ -29,7 +30,17 @@ function defaultOptions () {
'deepEqual',
'notDeepEqual'
]
}
},
patterns: [
'assert(value, [message])',
'assert.ok(value, [message])',
'assert.equal(actual, expected, [message])',
'assert.notEqual(actual, expected, [message])',
'assert.strictEqual(actual, expected, [message])',
'assert.notStrictEqual(actual, expected, [message])',
'assert.deepEqual(actual, expected, [message])',
'assert.notDeepEqual(actual, expected, [message])'
]
};
}

Expand Down Expand Up @@ -146,14 +157,29 @@ function enhance (target, formatter, config) {
return { powerAssertContext: {value: value, events: captured}, source: {content: args.content, filepath: args.filepath, line: args.line} };
}

config.targetMethods.oneArg.forEach(function (methodName) {
if (typeof target[methodName] === 'function') {
enhancement[methodName] = decorateOneArg(target, target[methodName], doPowerAssert);
var matchers = config.patterns.map(function (pt) { return escallmatch(pt); });
matchers.forEach(function (matcher) {
var args = matcher.argumentSignitures();
var len = args.length,
lastArg;
if (0 < len) {
lastArg = args[len - 1];
if (lastArg.name === 'message' && lastArg.kind === 'optional') {
len -= 1;
}
}
});
config.targetMethods.twoArgs.forEach(function (methodName) {
if (typeof target[methodName] === 'function') {
enhancement[methodName] = decorateTwoArgs(target, target[methodName], doPowerAssert);
var methodName = detectMethodName(matcher.calleeAst());
if (methodName && typeof target[methodName] === 'function') {
switch (len) {
case 1:
enhancement[methodName] = decorateOneArg(target, target[methodName], doPowerAssert);
break;
case 2:
enhancement[methodName] = decorateTwoArgs(target, target[methodName], doPowerAssert);
break;
default:
throw new Error('not supported');
}
}
});

Expand All @@ -163,6 +189,14 @@ function enhance (target, formatter, config) {
}


function detectMethodName (node) {
if (node.type === 'MemberExpression') {
return node.property.name;
}
return null;
}


function isEspoweredValue (value) {
return (typeof value === 'object') && (value !== null) && (typeof value.powerAssertContext !== 'undefined');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"url": "https://github.com/twada/empower.git"
},
"dependencies": {
"escallmatch": "~0.2.0",
"node.extend": "~1.0.10"
},
"devDependencies": {
Expand Down
18 changes: 12 additions & 6 deletions test/buster_assertions_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
targetMethods: {
oneArg: ['isNull'],
twoArgs: ['same']
}
},
patterns: [
'assert(actual, [message])',
'assert.same(actual, expected, [message])',
'assert.isNull(object, [message])'
]
};
return espowerSource(line, '/path/to/some_test.js', options);
},
Expand All @@ -38,10 +43,11 @@
};

var assert = empower(busterAssertions.assert, fakeFormatter, {
targetMethods: {
oneArg: ['isNull'],
twoArgs: ['same']
}
patterns: [
'assert(actual, [message])',
'assert.same(actual, expected, [message])',
'assert.isNull(object, [message])'
]
});


Expand Down Expand Up @@ -79,7 +85,7 @@
});


suite('assertion method with two arguments', function () {
suite('buster assertion method with two arguments', function () {
test('both Identifier', function () {
var foo = 'foo', bar = 'bar';
try {
Expand Down

0 comments on commit 533a21a

Please sign in to comment.