Skip to content

Commit

Permalink
For consideration: child => inherit
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Mar 8, 2017
1 parent 3c1208a commit c0a8c9b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 125 deletions.
62 changes: 31 additions & 31 deletions lib/Unexpected.js
Expand Up @@ -488,13 +488,13 @@ Unexpected.prototype.fail = function (arg) {
if (key === 'diff') {
if (typeof value === 'function' && this.parent) {
error.createDiff = function (output, diff, inspect, equal) {
var childOutput = expect.createOutput(output.format);
childOutput.inline = output.inline;
childOutput.output = output.output;
return value(childOutput, function diff(actual, expected) {
return expect.diff(actual, expected, childOutput.clone());
var inheritedOutput = expect.createOutput(output.format);
inheritedOutput.inline = output.inline;
inheritedOutput.output = output.output;
return value(inheritedOutput, function diff(actual, expected) {
return expect.diff(actual, expected, inheritedOutput.clone());
}, function inspect(v, depth) {
return childOutput.clone().appendInspected(v, (depth || defaultDepth) - 1);
return inheritedOutput.clone().appendInspected(v, (depth || defaultDepth) - 1);
}, function (actual, expected) {
return expect.equal(actual, expected);
});
Expand Down Expand Up @@ -563,9 +563,9 @@ function calculateAssertionSpecificity(assertion) {

// expect.addAssertion(pattern, handler)
// expect.addAssertion([pattern, ...], handler)
Unexpected.prototype.addAssertion = function (patternOrPatterns, handler, childUnexpected) {
Unexpected.prototype.addAssertion = function (patternOrPatterns, handler, inheritedUnexpected) {
var maxArguments;
if (typeof childUnexpected === 'object') {
if (typeof inheritedUnexpected === 'object') {
maxArguments = 3;
} else {
maxArguments = 2;
Expand Down Expand Up @@ -616,7 +616,7 @@ Unexpected.prototype.addAssertion = function (patternOrPatterns, handler, childU
args: assertionDeclaration.args,
testDescriptionString: expandedAssertion.text,
declaration: pattern,
unexpected: childUnexpected
unexpected: inheritedUnexpected
});
});
});
Expand Down Expand Up @@ -645,7 +645,7 @@ Unexpected.prototype.addAssertion = function (patternOrPatterns, handler, childU
return this.expect; // for chaining
};

Unexpected.prototype.addType = function (type, childUnexpected) {
Unexpected.prototype.addType = function (type, inheritedUnexpected) {
var that = this;
var baseType;
if (typeof type.name !== 'string' || !/^[a-z_](?:|[a-z0-9_.-]*[_a-z0-9])$/i.test(type.name)) {
Expand Down Expand Up @@ -706,22 +706,22 @@ Unexpected.prototype.addType = function (type, childUnexpected) {
extendedType.inspect = function (obj, depth, output, inspect) {
if (arguments.length < 2 || (!output || !output.isMagicPen)) {
return 'type: ' + type.name;
} else if (childUnexpected) {
var childOutput = childUnexpected.createOutput(output.format);
return originalInspect.call(this, obj, depth, childOutput, inspect) || childOutput;
} else if (inheritedUnexpected) {
var inheritedOutput = inheritedUnexpected.createOutput(output.format);
return originalInspect.call(this, obj, depth, inheritedOutput, inspect) || inheritedOutput;
} else {
return originalInspect.call(this, obj, depth, output, inspect) || output;
}
};

if (childUnexpected) {
extendedType.childUnexpected = childUnexpected;
if (inheritedUnexpected) {
extendedType.inheritedUnexpected = inheritedUnexpected;
var originalDiff = extendedType.diff;
extendedType.diff = function (actual, expected, output, inspect, diff, equal) {
var childOutput = childUnexpected.createOutput(output.format);
var inheritedOutput = inheritedUnexpected.createOutput(output.format);
// Make sure that already buffered up output is preserved:
childOutput.output = output.output;
return originalDiff.call(this, actual, expected, childOutput, inspect, diff, equal) || output;
inheritedOutput.output = output.output;
return originalDiff.call(this, actual, expected, inheritedOutput, inspect, diff, equal) || output;
};
}

Expand Down Expand Up @@ -870,7 +870,7 @@ function installExpectMethods(unexpected, expectFunction) {
expect.addType = unexpected.addType.bind(unexpected);
expect.getType = unexpected.getType;
expect.clone = unexpected.clone.bind(unexpected);
expect.child = unexpected.child.bind(unexpected);
expect.inherit = unexpected.inherit.bind(unexpected);
expect.toString = unexpected.toString.bind(unexpected);
expect.assertions = unexpected.assertions;
expect.use = expect.installPlugin = unexpected.use.bind(unexpected);
Expand Down Expand Up @@ -1335,33 +1335,33 @@ Unexpected.prototype.clone = function () {
return makeExpectFunction(unexpected);
};

Unexpected.prototype.child = function () {
var childUnexpected = new Unexpected({
Unexpected.prototype.inherit = function () {
var inheritedUnexpected = new Unexpected({
assertions: {},
types: [],
typeByName: {},
output: this.output.clone(),
format: this.outputFormat(),
installedPlugins: []
});
var parent = childUnexpected.parent = this;
var childExpect = makeExpectFunction(childUnexpected);
childExpect.exportAssertion = function (testDescription, handler) {
parent.addAssertion(testDescription, handler, childUnexpected);
var parent = inheritedUnexpected.parent = this;
var inheritedExpect = makeExpectFunction(inheritedUnexpected);
inheritedExpect.exportAssertion = function (testDescription, handler) {
parent.addAssertion(testDescription, handler, inheritedUnexpected);
return this;
};
childExpect.exportType = function (type) {
parent.addType(type, childUnexpected);
inheritedExpect.exportType = function (type) {
parent.addType(type, inheritedUnexpected);
return this;
};
childExpect.exportStyle = function (name, handler) {
inheritedExpect.exportStyle = function (name, handler) {
parent.addStyle(name, function () { // ...
var childOutput = childExpect.createOutput(this.format);
this.append(handler.apply(childOutput, arguments) || childOutput);
var inheritedOutput = inheritedExpect.createOutput(this.format);
this.append(handler.apply(inheritedOutput, arguments) || inheritedOutput);
});
return this;
};
return childExpect;
return inheritedExpect;
};

Unexpected.prototype.outputFormat = function (format) {
Expand Down
38 changes: 19 additions & 19 deletions test/api/exportAssertion.spec.js
@@ -1,14 +1,14 @@
/*global expect*/
describe('exportAssertion', function () {
var parentExpect;
var childExpect;
var inheritedExpect;
beforeEach(function () {
parentExpect = expect.clone();
childExpect = parentExpect.child();
inheritedExpect = parentExpect.inherit();
});

it('is chainable', function () {
childExpect.exportAssertion('foo', function () {})
inheritedExpect.exportAssertion('foo', function () {})
.exportAssertion('bar', function () {});

expect(parentExpect.assertions, 'to have keys',
Expand All @@ -17,15 +17,15 @@ describe('exportAssertion', function () {
});

it('makes the assertion available to the parent expect', function () {
childExpect.exportAssertion('<string> to foo', function (expect, subject) {
inheritedExpect.exportAssertion('<string> to foo', function (expect, subject) {
expect(subject, 'to equal', 'foo');
});

parentExpect('foo', 'to foo');
});

it('binds the assertion to the child expect so custom types are available', function () {
childExpect.addType({
it('binds the assertion to the inherited expect so custom types are available', function () {
inheritedExpect.addType({
name: 'yadda',
identify: function (obj) {
return /^yadda/.test(obj);
Expand All @@ -34,10 +34,10 @@ describe('exportAssertion', function () {
output.text('>>').text(value).text('<<');
}
});
childExpect.addAssertion('<yadda> to foo', function (expect, subject) {
inheritedExpect.addAssertion('<yadda> to foo', function (expect, subject) {
expect(subject, 'to contain', 'foo');
});
childExpect.exportAssertion('<string> to be silly', function (expect, subject) {
inheritedExpect.exportAssertion('<string> to be silly', function (expect, subject) {
expect(subject, 'to foo');
});
parentExpect('yaddafoo', 'to be silly');
Expand All @@ -53,7 +53,7 @@ describe('exportAssertion', function () {
});

it('picks up type definitions from the parent expect when setting expect.subjectType and expect.argTypes', function () {
childExpect.exportAssertion('<any> to foo <any>', function (expect, subject, value) {
inheritedExpect.exportAssertion('<any> to foo <any>', function (expect, subject, value) {
expect(expect.subjectType.name, 'to equal', 'number');
expect(expect.argTypes, 'to satisfy', [ { name: 'string' } ]);
});
Expand All @@ -62,10 +62,10 @@ describe('exportAssertion', function () {
});

it('should make custom styles available to the output generation code called by expect.fail', function () {
childExpect.addStyle('fancyQuotes', function (text) {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});
childExpect.exportAssertion('<any> to foo', function (expect, subject) {
inheritedExpect.exportAssertion('<any> to foo', function (expect, subject) {
if (subject !== 'foo') {
expect.fail({
diff: function (output, diff, inspect, equal) {
Expand Down Expand Up @@ -98,11 +98,11 @@ describe('exportAssertion', function () {
);
});

it('should make custom styles available when a parent expect appends an error associated with a child expect', function () {
childExpect.addStyle('fancyQuotes', function (text) {
it('should make custom styles available when a parent expect appends an error associated with a inherited expect', function () {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});
childExpect.exportAssertion('<any> to foo', function (expect, subject) {
inheritedExpect.exportAssertion('<any> to foo', function (expect, subject) {
if (subject !== 'foo') {
expect.fail({
diff: function (output, diff, inspect, equal) {
Expand Down Expand Up @@ -140,8 +140,8 @@ describe('exportAssertion', function () {
);
});

it('should support shifting from an exported middle-of-the-rocket assertion in the child to an assertion defined in the parent expect', function () {
childExpect.exportAssertion('<string> when prepended with foo <assertion?>', function (expect, subject) {
it('should support shifting from an exported middle-of-the-rocket assertion in an inherited expect to an assertion defined in the parent expect', function () {
inheritedExpect.exportAssertion('<string> when prepended with foo <assertion?>', function (expect, subject) {
return expect.shift('foo' + subject);
});

Expand All @@ -152,12 +152,12 @@ describe('exportAssertion', function () {
parentExpect('', 'when prepended with foo', 'to foo');
});

it('should support shifting from an exported middle-of-the-rocket assertion in a child to an assertion exported from another child', function () {
childExpect.exportAssertion('<string> when prepended with foo <assertion?>', function (expect, subject) {
it('should support shifting from an exported middle-of-the-rocket assertion in an inherited expect to an assertion exported from another inherit', function () {
inheritedExpect.exportAssertion('<string> when prepended with foo <assertion?>', function (expect, subject) {
return expect.shift('foo' + subject);
});

parentExpect.child().exportAssertion('<string> to foo', function (expect, subject) {
parentExpect.inherit().exportAssertion('<string> to foo', function (expect, subject) {
expect(subject, 'to equal', 'foo');
});

Expand Down
14 changes: 7 additions & 7 deletions test/api/exportStyle.spec.js
@@ -1,14 +1,14 @@
/*global expect*/
describe('styleType', function () {
var parentExpect;
var childExpect;
var inheritedExpect;
beforeEach(function () {
parentExpect = expect.clone();
childExpect = parentExpect.child();
inheritedExpect = parentExpect.inherit();
});

it('is chainable', function () {
childExpect
inheritedExpect
.exportStyle('firstStyle', function () {})
.exportStyle('secondStyle', function () {});

Expand All @@ -17,7 +17,7 @@ describe('styleType', function () {
});

it('makes the style available to the parent expect', function () {
childExpect.exportStyle('fancyQuotes', function (text) {
inheritedExpect.exportStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

Expand All @@ -28,12 +28,12 @@ describe('styleType', function () {
);
});

it('binds the style handler to a child expect output so custom types are available inside the exported style', function () {
childExpect.addStyle('fancyQuotes', function (text) {
it('binds the style handler to an inherited expect output so custom types are available inside the exported style', function () {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

childExpect.exportStyle('emphasize', function (text) {
inheritedExpect.exportStyle('emphasize', function (text) {
this.fancyQuotes(text);
});

Expand Down
24 changes: 12 additions & 12 deletions test/api/exportType.spec.js
@@ -1,22 +1,22 @@
/*global expect*/
describe('exportType', function () {
var parentExpect;
var childExpect;
var inheritedExpect;
beforeEach(function () {
parentExpect = expect.clone();
childExpect = parentExpect.child();
inheritedExpect = parentExpect.inherit();
});

it('is chainable', function () {
childExpect.exportType({name: 'abc', identify: false})
inheritedExpect.exportType({name: 'abc', identify: false})
.exportType({name: 'def', identify: false});

expect(parentExpect.getType('abc'), 'to satisfy', {name: 'abc'});
expect(parentExpect.getType('def'), 'to satisfy', {name: 'def'});
});

it('makes the type available to the parent expect', function () {
childExpect.exportType({
inheritedExpect.exportType({
name: 'fooString',
identify: function (obj) {
return typeof obj === 'string' && /^foo/.test(obj);
Expand All @@ -28,12 +28,12 @@ describe('exportType', function () {
});
});

it('binds the type to the child expect so custom types are available to the inspect function', function () {
childExpect.addStyle('fancyQuotes', function (text) {
it('binds the type to the inherit expect so custom types are available to the inspect function', function () {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

childExpect.exportType({
inheritedExpect.exportType({
name: 'yadda',
identify: function (obj) {
return /^yadda/.test(obj);
Expand All @@ -45,12 +45,12 @@ describe('exportType', function () {
expect(parentExpect.createOutput('text').appendInspected('yaddablah').toString(), 'to equal', '>>yaddablah<<');
});

it('binds the type to the child expect so custom types are available to the diff function', function () {
childExpect.addStyle('fancyQuotes', function (text) {
it('binds the type to the inherit expect so custom types are available to the diff function', function () {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

childExpect.exportType({
inheritedExpect.exportType({
name: 'yadda',
identify: function (obj) {
return /^yadda/.test(obj);
Expand All @@ -70,11 +70,11 @@ describe('exportType', function () {
});

it('should correctly append output generated by a inspect function that does not return the output', function () {
childExpect.addStyle('fancyQuotes', function (text) {
inheritedExpect.addStyle('fancyQuotes', function (text) {
this.text('>>').text(text).text('<<');
});

childExpect.exportType({
inheritedExpect.exportType({
name: 'yadda',
identify: function (obj) {
return /^yadda/.test(obj);
Expand Down

0 comments on commit c0a8c9b

Please sign in to comment.