Skip to content

Commit

Permalink
Merge b21766e into a734ef7
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Aug 2, 2015
2 parents a734ef7 + b21766e commit a8cedcf
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 12 deletions.
6 changes: 1 addition & 5 deletions lib/Unexpected.js
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,7 @@ Unexpected.prototype.createOutput = function (format) {
var that = this;
var output = this.output.clone(format || 'text');
output.addStyle('appendInspected', function (value, depth) {
if (value && value.isMagicPen) {
this.append(value);
} else {
this.append(that.inspect(value, depth, this.clone()));
}
this.append(that.inspect(value, depth, this.clone()));
});
return output;
};
Expand Down
109 changes: 109 additions & 0 deletions lib/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ module.exports = function (expect) {
}
});

expect.addStyle('commentBlock', function () {
var pen = this.getContentFromArguments(arguments);
var height = pen.size().height;

if (height > 0) {
this.block(function () {
for (var i = 0; i < height; i += 1) {
if (0 < i) {
this.nl();
}
this.jsComment('//');
}
});
this.sp().block(pen);
}
});

expect.addStyle('removedHighlight', function (content) {
this.alt({
text: function () {
Expand Down Expand Up @@ -180,4 +197,96 @@ module.exports = function (expect) {
that.appendInspected(item);
});
});

expect.addStyle('magicPenLine', function (line, pen) {
line.forEach(function (lineEntry, j) {
if (j > 0) {
this.nl();
}
if (lineEntry.style === 'text') {
var styles = lineEntry.args.styles;
if (pen && styles.length === 1 && typeof pen[styles[0]] === 'function') {
// Text with a single style also available as a method on the pen being inspected:
this
.text('.')
.jsFunctionName(styles[0])
.text('(')
.singleQuotedString(lineEntry.args.content)
.text(')');
} else {
this
.text('.')
.jsFunctionName('text')
.text('(')
.singleQuotedString(lineEntry.args.content);
if (styles.length > 0) {
this
.text(', ')
.appendInspected(styles.length === 1 && Array.isArray(styles[0]) ? styles[0] : styles);
}
this.text(')');
}
} else {
// lineEntry.style === 'block'
this
.text('.')
.jsFunctionName('block').text('(').jsKeyword('function').text(' () {');
if (lineEntry.args) {
this
.nl()
.indentLines()
.i()
.magicPen(pen, lineEntry.args)
.outdentLines()
.nl();
}
this.text('})');
}
}, this);
});

expect.addStyle('magicPen', function (pen, lines) {
var isTopLevel = !lines;
lines = lines || pen.output;
this.block(function () {
if (isTopLevel) {
this.jsFunctionName('magicpen').text('(');
if (pen.format) {
this.singleQuotedString(pen.format);
}
this.text(')');
} else {
this.jsKeyword('this');
}
if (!pen.isEmpty()) {
var inspectOnMultipleLines = lines.length > 1 || lines[0].length > 1;
if (inspectOnMultipleLines) {
this
.nl()
.indentLines()
.i();
}
this.block(function () {
lines.forEach(function (line, i) {
if (i > 0) {
this.text('.').jsFunctionName('nl').text('()').nl();
}
this.magicPenLine(line, pen);
}, this);
if (!isTopLevel) {
this.text(';');
}
});
if (inspectOnMultipleLines) {
this.outdentLines();
}
}
});

// If we're at the top level of a non-empty pen compatible with the current output,
// render the output of the pen in a comment:
if (isTopLevel && !pen.isEmpty() && (pen.format === this.format || !pen.format)) {
this.sp().commentBlock(pen);
}
});
};
2 changes: 1 addition & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ module.exports = function (expect) {
return obj && obj.isMagicPen;
},
inspect: function (pen, depth, output) {
return output.append(pen);
output.magicPen(pen);
},
equal: function (a, b) {
if (a.format !== b.format) {
Expand Down
140 changes: 134 additions & 6 deletions test/unexpected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,63 @@ describe('unexpected', function () {
});
});

describe('magicpen type', function () {
describe('#inspect', function () {
describe('with a pen in text format', function () {
var pen = expect.createOutput('text').green('abc').nl().text('def').block(function () {
this.text('foo').nl().text('bar');
});

it('should inspect correctly', function () {
expect(pen, 'to inspect as',
"magicpen('text') // abc\n" +
" .green('abc').nl() // deffoo\n" +
" .text('def') // bar\n" +
" .block(function () {\n" +
" this\n" +
" .text('foo').nl()\n" +
" .text('bar');\n" +
" })"
);
});
});

describe('with a pen in ansi format', function () {
var pen = expect.createOutput('ansi').green('abc').text('def').block(function () {
this.text('foo');
});

it('should inspect correctly', function () {
expect(pen, 'to inspect as',
"magicpen('ansi')\n" +
" .green('abc')\n" +
" .text('def')\n" +
" .block(function () {\n" +
" this.text('foo');\n" +
" })"
);
});
});

describe('with a pen in ansi format', function () {
var pen = expect.createOutput('html').green('abc').text('def').block(function () {
this.text('foo');
});

it('should inspect correctly', function () {
expect(pen, 'to inspect as',
"magicpen('html')\n" +
" .green('abc')\n" +
" .text('def')\n" +
" .block(function () {\n" +
" this.text('foo');\n" +
" })"
);
});
});
});
});

describe('ok/truthy/falsy assertion', function () {
it('assert that the value is truthy or not', function () {
expect(1, 'to be ok');
Expand Down Expand Up @@ -1142,13 +1199,84 @@ describe('unexpected', function () {
));
});

it('should support building the expected output via a function', function () {
it('should fail to get the diff from an Unexpected error that does not have one', function () {
expect(function () {
expect('abc', 'to equal', 'def');
}, 'to throw', expect.it('to have ansi diff', function () {
this.red('-').text('abc', ['bgRed', 'black']).nl()
.green('+').text('def', ['bgGreen', 'black']);
}));
expect(function () {
expect(123, 'to equal', 456);
}, 'to throw', expect.it('to have ansi diff', function () {}));
}, 'to throw',
"expected\n" +
"function () {\n" +
" expect(123, 'to equal', 456);\n" +
"}\n" +
"to throw expect.it('to have ansi diff', function () {})\n" +
" expected UnexpectedError(expected 123 to equal 456) to have ansi diff function () {}\n" +
" The UnexpectedError instance does not have a diff"
);
});

describe('when building the expected output via a function', function () {
it('should succeed', function () {
expect(function () {
expect('abc', 'to equal', 'def');
}, 'to throw', expect.it('to have ansi diff', function () {
this.red('-').text('abc', ['bgRed', 'black']).nl()
.green('+').text('def', ['bgGreen', 'black']);
}));
});

it('should fail with a diff', function () {
expect(function () {
expect(function () {
expect('abc', 'to equal', 'def');
}, 'to throw', expect.it('to have ansi diff', function () {
this.red('-').text('abc').nl()
.green('+').text('def', ['bgGreen', 'black']);
}));
}, 'to throw',
"expected\n" +
"function () {\n" +
" expect('abc', 'to equal', 'def');\n" +
"}\n" +
"to throw\n" +
"expect.it('to have ansi diff', function () {\n" +
" this.red('-').text('abc').nl()\n" +
" .green('+').text('def', ['bgGreen', 'black']);\n" +
"})\n" +
" expected\n" +
" UnexpectedError(\n" +
" expected 'abc' to equal 'def'\n" +
"\n" +
" -abc\n" +
" +def\n" +
" )\n" +
" to have ansi diff\n" +
" function () {\n" +
" this.red('-').text('abc').nl()\n" +
" .green('+').text('def', ['bgGreen', 'black']);\n" +
" }\n" +
" expected\n" +
" magicpen('ansi')\n" +
" .block(function () {\n" +
" this.diffRemovedLine('-');\n" +
" })\n" +
" .block(function () {\n" +
" this.diffRemovedHighlight('abc');\n" +
" }).nl()\n" +
" .block(function () {\n" +
" this.diffAddedLine('+');\n" +
" })\n" +
" .block(function () {\n" +
" this.diffAddedHighlight('def');\n" +
" })\n" +
" to equal\n" +
" magicpen('ansi')\n" +
" .red('-')\n" +
" .text('abc').nl()\n" +
" .green('+')\n" +
" .text('def', [ 'bgGreen', 'black' ])"
);
});
});

it('should assume that a function that does not produce any output has run assertions on the stringified diff/message, and thus should not fail', function () {
Expand Down

0 comments on commit a8cedcf

Please sign in to comment.