Permalink
Browse files

Hack up an "exportAssertion" method.

  • Loading branch information...
1 parent 77409ac commit 205cf3dea7fc5be3011d215adf64510cc8ce7a90 @papandreou papandreou committed Feb 16, 2017
Showing with 56 additions and 6 deletions.
  1. +17 −6 lib/Unexpected.js
  2. +39 −0 test/api/exportAssertion.spec.js
View
@@ -556,8 +556,8 @@ function calculateAssertionSpecificity(assertion) {
// expect.addAssertion(pattern, handler)
// expect.addAssertion([pattern, ...], handler)
-Unexpected.prototype.addAssertion = function (patternOrPatterns, handler) {
- if (arguments.length > 2 || typeof handler !== 'function' || (typeof patternOrPatterns !== 'string' && !Array.isArray(patternOrPatterns))) {
+Unexpected.prototype.addAssertion = function (patternOrPatterns, handler, options) {
+ if (arguments.length > 3 || typeof handler !== 'function' || (typeof patternOrPatterns !== 'string' && !Array.isArray(patternOrPatterns))) {
var errorMessage = "Syntax: expect.addAssertion(<string|array[string]>, function (expect, subject, ...) { ... });";
if ((typeof handler === 'string' || Array.isArray(handler)) && typeof arguments[2] === 'function') {
errorMessage +=
@@ -599,7 +599,8 @@ Unexpected.prototype.addAssertion = function (patternOrPatterns, handler) {
subject: assertionDeclaration.subject,
args: assertionDeclaration.args,
testDescriptionString: expandedAssertion.text,
- declaration: pattern
+ declaration: pattern,
+ unexpected: options && options.unexpected
});
});
});
@@ -1068,6 +1069,7 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
function executeExpect(subject, testDescriptionString, args) {
var assertionRule = that.lookupAssertionRule(subject, testDescriptionString, args);
+ // var assertionRule = that.lookupAssertionRule(subject, testDescriptionString, args);
if (!assertionRule) {
var tokens = testDescriptionString.split(' ');
@@ -1086,6 +1088,9 @@ Unexpected.prototype.expect = function expect(subject, testDescriptionString) {
that.throwAssertionNotFoundError(subject, testDescriptionString, args);
}
}
+ if (assertionRule && assertionRule.unexpected) {
+ return assertionRule.unexpected.expect.apply(assertionRule.unexpected, [subject, testDescriptionString].concat(args));
+ }
var flags = extend({}, assertionRule.flags);
var wrappedExpect = function (subject, testDescriptionString) {
@@ -1295,16 +1300,22 @@ Unexpected.prototype.clone = function () {
};
Unexpected.prototype.child = function () {
- var unexpected = new Unexpected({
+ var childUnexpected = new Unexpected({
assertions: {},
types: [].concat(this.types),
typeByName: extend({}, this.typeByName),
output: this.output.clone(),
format: this.outputFormat(),
installedPlugins: []
});
- unexpected.parent = this;
- return makeExpectFunction(unexpected);
+ childUnexpected.parent = this;
+ var childExpect = makeExpectFunction(childUnexpected);
+ childExpect.exportAssertion = function (testDescription, handler, options) {
+ childExpect.addAssertion(testDescription, handler, options);
+ childUnexpected.parent.addAssertion(testDescription.replace(/<[^>+*?]+/g, '<any'), handler, utils.extend({}, options, {unexpected: childUnexpected}));
+ return this;
+ };
+ return childExpect;
};
Unexpected.prototype.outputFormat = function (format) {
@@ -0,0 +1,39 @@
+/*global expect*/
+describe('exportAssertion', function () {
+ var parentExpect;
+ var childExpect;
+ beforeEach(function () {
+ parentExpect = expect.clone();
+ childExpect = parentExpect.child();
+ });
+
+ it('is chainable', function () {
+ childExpect.exportAssertion('foo', function () {})
+ .exportAssertion('bar', function () {});
+
+ expect(childExpect.assertions, 'to have keys',
+ 'foo',
+ 'bar');
+ });
+
+ it('makes the assertion available to the parent expect', function () {
+ childExpect.exportAssertion('<string> to foo', function (expect, subject) {
+ expect(subject, 'to equal', 'foo');
+ });
+
+ parentExpect('foo', 'to foo');
+ });
+
+ it('binds the assertion to the child expect', function () {
+ childExpect.addType({
+ name: 'yadda',
+ identify: function (obj) {
+ return /^yadda/.test(obj);
+ }
+ });
+ childExpect.exportAssertion('<yadda> to foo', function (expect, subject) {
+ expect(subject, 'to contain', 'foo');
+ });
+ parentExpect('yaddafoo', 'to foo');
+ });
+});

0 comments on commit 205cf3d

Please sign in to comment.