Skip to content

Commit d646aa8

Browse files
committed
feat(power-assert-formatter): outputOffset option to configure number of spaces inserted at the left
1 parent 635bb96 commit d646aa8

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

lib/default-options.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = function defaultOptions () {
33
return {
44
lineDiffThreshold: 5,
55
maxDepth: 1,
6+
outputOffset: 0,
67
anonymous: 'Object',
78
circular: '#@Circular#',
89
lineSeparator: '\n',

lib/string-writer.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
'use strict';
22

3+
function spacerStr (len) {
4+
var str = '';
5+
for(var i = 0; i < len; i += 1) {
6+
str += ' ';
7+
}
8+
return str;
9+
}
10+
311
function StringWriter (config) {
412
this.lines = [];
513
this.lineSeparator = config.lineSeparator;
14+
this.regex = new RegExp(this.lineSeparator, 'g');
15+
this.spacer = spacerStr(config.outputOffset);
616
}
717

818
StringWriter.prototype.write = function (str) {
9-
this.lines.push(str);
19+
this.lines.push(this.spacer + str.replace(this.regex, this.lineSeparator + this.spacer));
1020
};
1121

1222
StringWriter.prototype.flush = function () {

test/options_test.js

+53
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,59 @@ suite('lineSeparator option', function () {
4343
lineSeparatorTest('CRLF', {lineSeparator: '\r\n'}, '\r\n');
4444
});
4545

46+
47+
suite('outputOffset option', function () {
48+
function outputOffsetCustomizationTest (option, expectedLines) {
49+
var assert = empower(baseAssert, createFormatter(option));
50+
test(JSON.stringify(option), function () {
51+
var hoge = 'foo';
52+
var fuga = 'bar';
53+
try {
54+
eval(weave('assert.ok(hoge === fuga, "comment");'));
55+
} catch (e) {
56+
baseAssert.equal(e.name, 'AssertionError');
57+
var actual = e.message.split(createFormatter.defaultOptions().lineSeparator);
58+
baseAssert.deepEqual(actual, expectedLines);
59+
}
60+
});
61+
}
62+
outputOffsetCustomizationTest({outputOffset: 1}, [
63+
'comment # /path/to/some_test.js:1',
64+
' ',
65+
' assert.ok(hoge === fuga, "comment")',
66+
' | | | ',
67+
' | | "bar" ',
68+
' | false ',
69+
' "foo" ',
70+
' ',
71+
' --- [string] fuga',
72+
' +++ [string] hoge',
73+
' @@ -1,3 +1,3 @@',
74+
' -bar',
75+
' +foo',
76+
' ',
77+
' '
78+
]);
79+
outputOffsetCustomizationTest({outputOffset: 3}, [
80+
'comment # /path/to/some_test.js:1',
81+
' ',
82+
' assert.ok(hoge === fuga, "comment")',
83+
' | | | ',
84+
' | | "bar" ',
85+
' | false ',
86+
' "foo" ',
87+
' ',
88+
' --- [string] fuga',
89+
' +++ [string] hoge',
90+
' @@ -1,3 +1,3 @@',
91+
' -bar',
92+
' +foo',
93+
' ',
94+
' '
95+
]);
96+
});
97+
98+
4699
suite('renderers customization', function () {
47100
function rendererCustomizationTest (name, option, expectedLines) {
48101
var assert = empower(baseAssert, createFormatter(option));

0 commit comments

Comments
 (0)