Skip to content

outputOffset option to configure number of spaces inserted at the left #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ Threshold to show diff at character level or line level. If number of lines in t

Depth of object traversal. If object depth is greater than `maxDepth`, compound object (IOW, `Array` or `object`) will be pruned with `#` like `["foo",#Array#,#Object#]`.

#### options.outputOffset

| type | default value |
|:---------|:--------------|
| `number` | `2` |

Number of spaces inserted at the left in power-assert output.

#### options.anonymous

| type | default value |
Expand Down
13 changes: 12 additions & 1 deletion build/power-assert-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ module.exports = function defaultOptions () {
return {
lineDiffThreshold: 5,
maxDepth: 1,
outputOffset: 2,
anonymous: 'Object',
circular: '#@Circular#',
lineSeparator: '\n',
Expand Down Expand Up @@ -481,13 +482,23 @@ module.exports = function (str) {
},{"eastasianwidth":19}],11:[function(_dereq_,module,exports){
'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
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: 2,
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
209 changes: 131 additions & 78 deletions test/options_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ suite('lineSeparator option', function () {
} catch (e) {
baseAssert.equal(e.name, 'AssertionError');
baseAssert.equal(e.message, [
'# /path/to/some_test.js:1',
'',
'assert(falsyNum)',
' | ',
' 0 ',
''
' # /path/to/some_test.js:1',
' ',
' assert(falsyNum)',
' | ',
' 0 ',
' '
].join(expectedSeparator));
}
});
Expand All @@ -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 All @@ -59,21 +112,21 @@ suite('renderers customization', function () {
}

rendererCustomizationTest('default', null, [
'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',
'',
''
'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',
' ',
' '
]);

rendererCustomizationTest('without file renderer', {
Expand All @@ -83,20 +136,20 @@ suite('renderers customization', function () {
'./built-in/binary-expression'
]
}, [
'comment ',
'assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
'',
'--- [string] fuga',
'+++ [string] hoge',
'@@ -1,3 +1,3 @@',
'-bar',
'+foo',
'',
''
'comment ',
' assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);


Expand All @@ -107,19 +160,19 @@ suite('renderers customization', function () {
'./built-in/binary-expression'
]
}, [
'comment # /path/to/some_test.js:1',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
'',
'--- [string] fuga',
'+++ [string] hoge',
'@@ -1,3 +1,3 @@',
'-bar',
'+foo',
'',
''
'comment # /path/to/some_test.js:1',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);

rendererCustomizationTest('without diagram renderer', {
Expand All @@ -129,17 +182,17 @@ suite('renderers customization', function () {
'./built-in/binary-expression'
]
}, [
'comment # /path/to/some_test.js:1',
'',
'assert.ok(hoge === fuga, "comment")',
'',
'--- [string] fuga',
'+++ [string] hoge',
'@@ -1,3 +1,3 @@',
'-bar',
'+foo',
'',
''
'comment # /path/to/some_test.js:1',
' ',
' assert.ok(hoge === fuga, "comment")',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);

rendererCustomizationTest('without binary-expression renderer', {
Expand All @@ -149,14 +202,14 @@ suite('renderers customization', function () {
'./built-in/diagram'
]
}, [
'comment # /path/to/some_test.js:1',
'',
'assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
''
'comment # /path/to/some_test.js:1',
' ',
' assert.ok(hoge === fuga, "comment")',
' | | | ',
' | | "bar" ',
' | false ',
' "foo" ',
' '
]);


Expand All @@ -178,17 +231,17 @@ suite('renderers customization', function () {
'./built-in/binary-expression'
]
}, [
'comment # /path/to/some_test.js:1',
'',
'## assert.ok(hoge === fuga, "comment") ##',
'',
'--- [string] fuga',
'+++ [string] hoge',
'@@ -1,3 +1,3 @@',
'-bar',
'+foo',
'',
''
'comment # /path/to/some_test.js:1',
' ',
' ## assert.ok(hoge === fuga, "comment") ##',
' ',
' --- [string] fuga',
' +++ [string] hoge',
' @@ -1,3 +1,3 @@',
' -bar',
' +foo',
' ',
' '
]);
})();

Expand Down
Loading