Skip to content

Commit

Permalink
feat(power-assert-formatter): outputOffset option to configure number…
Browse files Browse the repository at this point in the history
… of spaces inserted at the left
  • Loading branch information
twada committed Oct 23, 2014
1 parent 635bb96 commit d646aa8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = function defaultOptions () {
return {
lineDiffThreshold: 5,
maxDepth: 1,
outputOffset: 0,
anonymous: 'Object',
circular: '#@Circular#',
lineSeparator: '\n',
Expand Down
12 changes: 11 additions & 1 deletion lib/string-writer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
'use strict';

function spacerStr (len) {
var str = '';
for(var i = 0; i < len; i += 1) {
str += ' ';
}
return str;
}

function StringWriter (config) {
this.lines = [];
this.lineSeparator = config.lineSeparator;
this.regex = new RegExp(this.lineSeparator, 'g');
this.spacer = spacerStr(config.outputOffset);
}

StringWriter.prototype.write = function (str) {
this.lines.push(str);
this.lines.push(this.spacer + str.replace(this.regex, this.lineSeparator + this.spacer));
};

StringWriter.prototype.flush = function () {
Expand Down
53 changes: 53 additions & 0 deletions test/options_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,59 @@ suite('lineSeparator option', function () {
lineSeparatorTest('CRLF', {lineSeparator: '\r\n'}, '\r\n');
});


suite('outputOffset option', function () {
function outputOffsetCustomizationTest (option, expectedLines) {
var assert = empower(baseAssert, createFormatter(option));
test(JSON.stringify(option), function () {
var hoge = 'foo';
var fuga = 'bar';
try {
eval(weave('assert.ok(hoge === fuga, "comment");'));
} catch (e) {
baseAssert.equal(e.name, 'AssertionError');
var actual = e.message.split(createFormatter.defaultOptions().lineSeparator);
baseAssert.deepEqual(actual, expectedLines);
}
});
}
outputOffsetCustomizationTest({outputOffset: 1}, [
'comment # /path/to/some_test.js:1',
' ',
' assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);
outputOffsetCustomizationTest({outputOffset: 3}, [
'comment # /path/to/some_test.js:1',
' ',
' assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);
});


suite('renderers customization', function () {
function rendererCustomizationTest (name, option, expectedLines) {
var assert = empower(baseAssert, createFormatter(option));
Expand Down

0 comments on commit d646aa8

Please sign in to comment.