Skip to content

Commit

Permalink
Also support the new options in the array-like diff, added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jan 20, 2016
1 parent 31de255 commit 8e1e096
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/types.js
Expand Up @@ -514,7 +514,15 @@ module.exports = function (expect) {
return this.baseType.diff(actual, expected, output);
}

output.append(this.prefix(output.clone(), actual)).nl().indentLines();
output.append(this.prefix(output.clone(), actual));

if (this.leadingNewline) {
output.nl();
}

if (this.indent) {
output.indentLines();
}

var type = this;
var changes = arrayChanges(actual, expected, equal, function (a, b) {
Expand All @@ -525,7 +533,7 @@ module.exports = function (expect) {
}, -1);
changes.forEach(function (diffItem, index) {
var delimiterOutput = type.delimiter(output.clone(), index, indexOfLastNonInsert + 1);
output.i().block(function () {
output.nl(index > 0 ? 1 : 0).i().block(function () {
var type = diffItem.type;
if (type === 'insert') {
this.annotationBlock(function () {
Expand All @@ -549,10 +557,18 @@ module.exports = function (expect) {
});
}
}
}).nl();
});
});

output.outdentLines().append(this.suffix(output.clone(), actual));
if (this.indent) {
output.outdentLines();
}

if (this.trailingNewline) {
output.nl();
}

output.append(this.suffix(output.clone(), actual));

return result;
}
Expand Down
113 changes: 113 additions & 0 deletions test/types/array-like-type.spec.js
@@ -0,0 +1,113 @@
/*global expect*/
describe('array-like type', function () {
describe('with a subtype that disables indentation', function () {
var clonedExpect = expect.clone();

clonedExpect.addType({
base: 'array-like',
name: 'bogusarray',
identify: Array.isArray,
indent: false
});

it('should not render the indentation when an instance is inspected in a multi-line context', function () {
expect(
clonedExpect.inspect([
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
]).toString(),
'to equal',
"[\n" +
"'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n" +
"'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'\n" +
"]"
);
});

it('should not render the indentation when an instance is diffed', function () {
expect(
clonedExpect.diff(['a', 'b'], ['aa', 'bb']).diff.toString(),
'to equal',
"[\n" +
"'a', // should equal 'aa'\n" +
" // -a\n" +
" // +aa\n" +
"'b' // should equal 'bb'\n" +
" // -b\n" +
" // +bb\n" +
"]"
);
});

it('should not render the indentation when an instance participates in a "to satisfy" diff', function () {
expect(function () {
clonedExpect(['aaa', 'bbb'], 'to satisfy', { 0: 'foo' });
}, 'to throw',
"expected [ 'aaa', 'bbb' ] to satisfy { 0: 'foo' }\n" +
"\n" +
"[\n" +
"'aaa', // should equal 'foo'\n" +
" // -aaa\n" +
" // +foo\n" +
"'bbb'\n" +
"]"
);
});
});

describe('with a subtype that disables prefix, suffix, leading and trailing newline', function () {
var clonedExpect = expect.clone();

clonedExpect.addType({
base: 'array-like',
name: 'bogusarray',
identify: Array.isArray,
prefix: function (output) {
return output;
},
suffix: function (output) {
return output;
},
leadingNewline: false,
trailingNewline: false
});

it('should not render the newlines when an instance is inspected in a multi-line context', function () {
expect(
clonedExpect.inspect([
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
]).toString(),
'to equal',
" 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',\n" +
" 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'"
);
});

it('should not render the newlines when an instance is diffed', function () {
expect(
clonedExpect.diff(['a', 'b'], ['aa', 'bb']).diff.toString(),
'to equal',
" 'a', // should equal 'aa'\n" +
" // -a\n" +
" // +aa\n" +
" 'b' // should equal 'bb'\n" +
" // -b\n" +
" // +bb"
);
});

it('should not render the indentation when an instance participates in a "to satisfy" diff', function () {
expect(function () {
clonedExpect(['aaa', 'bbb'], 'to satisfy', { 0: 'foo' });
}, 'to throw',
"expected 'aaa', 'bbb' to satisfy { 0: 'foo' }\n" +
"\n" +
" 'aaa', // should equal 'foo'\n" +
" // -aaa\n" +
" // +foo\n" +
" 'bbb'"
);
});
});
});

0 comments on commit 8e1e096

Please sign in to comment.