Skip to content

Commit

Permalink
feat(empower-core): modifyMessageBeforeAssert option for compatibility
Browse files Browse the repository at this point in the history
for testing frameworks that does not throw Error on assertion failure such as QUnit and buster
  • Loading branch information
twada committed Nov 29, 2015
1 parent ca80923 commit c1ade36
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Decorator.prototype.concreteAssert = function (invocation, context) {
var thisObj = invocation.thisObj;
var args = invocation.values;
var message = invocation.message;
if (context && typeof this.config.modifyMessageBeforeAssert === 'function') {
message = this.config.modifyMessageBeforeAssert({originalMessage: message, powerAssertContext: context});
}
args = args.concat(message);
var ret;
try {
Expand Down
177 changes: 177 additions & 0 deletions test/buster_assertions_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['empower-core', 'espower', 'acorn', 'escodegen', 'assert', 'buster-assertions'], factory);
} else if (typeof exports === 'object') {
factory(require('..'), require('espower'), require('acorn'), require('escodegen'), require('assert'), require('buster-assertions'));
} else {
factory(root.empowerCore, root.espower, root.acorn, root.escodegen, root.assert, root.buster);
}
}(this, function (
empowerCore,
espower,
acorn,
escodegen,
baseAssert,
busterAssertions
) {

var weave = function (line) {
var filepath = '/absolute/path/to/project/test/some_test.js';
var espowerOptions = {
source: line,
path: filepath,
sourceRoot: '/absolute/path/to/project/',
destructive: true,
patterns: [
'assert(actual, [message])',
'assert.isNull(object, [message])',
'assert.same(actual, expected, [message])',
'assert.near(actual, expected, delta, [message])'
]
};
var jsAST = acorn.parse(line, {ecmaVersion: 6, locations: true, sourceType: 'module', sourceFile: filepath});
var espoweredAST = espower(jsAST, espowerOptions);
return escodegen.generate(espoweredAST, {format: {compact: true}});
},
fakeFormatter = function (context) {
var events = context.args.reduce(function (accum, arg) {
return accum.concat(arg.events);
}, []);
return [
context.source.filepath,
context.source.content,
JSON.stringify(events)
].join('\n');
};

var assert = empowerCore(busterAssertions.assert, {
modifyMessageBeforeAssert: function (ev) {
var message = ev.originalMessage;
var powerAssertText = fakeFormatter(ev.powerAssertContext);
return message ? message + ' ' + powerAssertText : powerAssertText;
},
patterns: [
'assert(actual, [message])',
'assert.isNull(object, [message])',
'assert.same(actual, expected, [message])',
'assert.near(actual, expected, delta, [message])'
]
});


test('buster assertion is also an assert function', function () {
var falsy = 0;
try {
eval(weave('assert(falsy);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'test/some_test.js',
'assert(falsy)',
'[{"value":0,"espath":"arguments/0"}]'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});


suite('buster assertion with one argument', function () {
test('isNull method', function () {
var falsy = 0;
try {
eval(weave('assert.isNull(falsy);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.isNull] test/some_test.js',
'assert.isNull(falsy)',
'[{"value":0,"espath":"arguments/0"}]: Expected 0 to be null'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});
});


suite('buster assertion method with two arguments', function () {
test('both Identifier', function () {
var foo = 'foo', bar = 'bar';
try {
eval(weave('assert.same(foo, bar);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.same] test/some_test.js',
'assert.same(foo, bar)',
'[{"value":"foo","espath":"arguments/0"},{"value":"bar","espath":"arguments/1"}]: foo expected to be the same object as bar'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});

test('first argument is Literal', function () {
var bar = 'bar';
try {
eval(weave('assert.same("foo", bar);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.same] test/some_test.js',
'assert.same("foo", bar)',
'[{"value":"bar","espath":"arguments/1"}]: foo expected to be the same object as bar'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});

test('second argument is Literal', function () {
var foo = 'foo';
try {
eval(weave('assert.same(foo, "bar");'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.same] test/some_test.js',
'assert.same(foo, "bar")',
'[{"value":"foo","espath":"arguments/0"}]: foo expected to be the same object as bar'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});
});


suite('buster assertion method with three arguments', function () {
test('when every argument is Identifier', function () {
var actualVal = 10.6, expectedVal = 10, delta = 0.5;
try {
eval(weave('assert.near(actualVal, expectedVal, delta);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.near] test/some_test.js',
'assert.near(actualVal, expectedVal, delta)',
'[{"value":10.6,"espath":"arguments/0"},{"value":10,"espath":"arguments/1"},{"value":0.5,"espath":"arguments/2"}]: Expected 10.6 to be equal to 10 +/- 0.5'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});

test('optional fourth argument', function () {
var actualVal = 10.6, expectedVal = 10, delta = 0.5, messageStr = 'not in delta';
try {
eval(weave('assert.near(actualVal, expectedVal, delta, messageStr);'));
baseAssert.ok(false, 'AssertionError should be thrown');
} catch (e) {
baseAssert.equal(e.message, [
'[assert.near] not in delta test/some_test.js',
'assert.near(actualVal, expectedVal, delta, messageStr)',
'[{"value":10.6,"espath":"arguments/0"},{"value":10,"espath":"arguments/1"},{"value":0.5,"espath":"arguments/2"}]: Expected 10.6 to be equal to 10 +/- 0.5'
].join('\n'));
baseAssert.equal(e.name, 'AssertionError');
}
});
});

}));
3 changes: 2 additions & 1 deletion test/test-amd.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
mocha.setup('tdd');
require([
"./empower_option_test",
"./empower_test"
"./empower_test",
"./buster_assertions_test"
], function() {
mocha.checkLeaks();
if (window.mochaPhantomJS) {
Expand Down
1 change: 1 addition & 0 deletions test/test-browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</script>
<script type="text/javascript" src="./empower_option_test.js"></script>
<script type="text/javascript" src="./empower_test.js"></script>
<script type="text/javascript" src="./buster_assertions_test.js"></script>
<script type="text/javascript">
if (typeof initMochaPhantomJS === 'function') {
initMochaPhantomJS()
Expand Down

0 comments on commit c1ade36

Please sign in to comment.