Skip to content

Commit

Permalink
refactor: Turned off verbose when skipping objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mradionov committed Jan 29, 2017
1 parent dc270a1 commit 4b107dc
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/format/diff/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = function diffFull(
) {
return {
expected: expectedValue.indent(options) +
highlighter.expected(expectedValue.out()),
highlighter.expected(expectedValue.out(options)),
actual: actualValue.indent(options) +
highlighter.actual(actualValue.out())
highlighter.actual(actualValue.out(options))
};
};
5 changes: 4 additions & 1 deletion src/format/diff/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ module.exports = function diffPrimitives(
actual: actualValue.indent(options)
};

var diff = jsDiff.diffWordsWithSpace(expectedValue.out(), actualValue.out());
var diff = jsDiff.diffWordsWithSpace(
expectedValue.out(options),
actualValue.out(options)
);

diff.forEach(function (part) {
var value = part.value;
Expand Down
8 changes: 4 additions & 4 deletions src/format/diff/traversers/array-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ module.exports = {
if (value.parent.containing) {
if (oppositeParent.includes(value)) {
skipPath(value.getPath());
return indent + value.out();
return indent + value.out(options);
}

skipPath(value.getPath());
return indent + highlightValue(value.out());
return indent + highlightValue(value.out(options));
}

if (oppositeParent && oppositeParent.containing) {
skipPath(value.getPath());
return indent + value.out();
return indent + value.out(options);
}

if (!oppositeValue) {
skipPath(value.getPath());
return indent + highlightValue(value.out());
return indent + highlightValue(value.out(options));
}

return indent;
Expand Down
4 changes: 2 additions & 2 deletions src/format/diff/traversers/object-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module.exports = {
if (!oppositeValue) {
if (oppositeParent && oppositeParent.containing) {
skipPath(value.getPath());
return indent + key + value.out();
return indent + key + value.out(options);
}

skipPath(value.getPath());
return indent + highlightValue(key + value.out());
return indent + highlightValue(key + value.out(options));
}

return indent + key;
Expand Down
4 changes: 2 additions & 2 deletions src/format/diff/traversers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ module.exports = {
// Different types are not comparable
if (value.type !== oppositeValue.type) {
skipPath(value.getPath());
return highlightValue(value.out());
return highlightValue(value.out(options));
}

if (value.any || oppositeValue.any) {
skipPath(value.getPath());
return value.out();
return value.out(options);
}

if (value.instance !== oppositeValue.instance) {
Expand Down
16 changes: 8 additions & 8 deletions src/format/diff/traversers/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ var Value = require('../../../value');
module.exports = {

enter: function (
value, oppositeRootValue, highlightValue, highlighter
value, oppositeRootValue, highlightValue, highlighter, options
) {
var oppositeValue = oppositeRootValue.byPath(value.getPath());

if (value.type === Value.ANYTHING || oppositeValue.type === Value.ANYTHING) {
return value.out();
return value.out(options);
}

// Different types are not comparable
if (value.type !== oppositeValue.type) {
return highlightValue(value.out());
return highlightValue(value.out(options));
}

// Don't highlight "any" for same types
if (value.any || oppositeValue.any) {
return value.out();
return value.out(options);
}

if (value.isComplex()) {
if (value.canNest()) {
// Should not land here
// If it nests, then it should be handled higher before particular formatter
} else {
return highlighter.warning(value.out());
return highlighter.warning(value.out(options));
}
}

if (value.out() !== oppositeValue.out()) {
return highlightValue(value.out());
if (value.out(options) !== oppositeValue.out(options)) {
return highlightValue(value.out(options));
}

return value.out();
return value.out(options);
},

leave: function () {
Expand Down
4 changes: 2 additions & 2 deletions src/format/diff/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = function diffWarning(
) {
return {
expected: expectedValue.indent(options) +
highlighter.warning(expectedValue.out()),
highlighter.warning(expectedValue.out(options)),
actual: actualValue.indent(options) +
highlighter.warning(actualValue.out())
highlighter.warning(actualValue.out(options))
};
};
7 changes: 6 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ function createArray(valueStr, valueOptions, parseOptions) {

function createObject(valueStr, valueOptions) {
var objectValues = extractObjectValues(valueStr);

var objectContentStr = valueStr.substr(8, valueStr.length - 3 - 8);
var objectWrappedStr = '{ ' + objectContentStr.trim() + ' }';

var children = [];
for (var i = 0; i < objectValues.length; i++) {
var objectKey = objectValues[i].key;
Expand All @@ -227,7 +231,8 @@ function createObject(valueStr, valueOptions) {
key: objectKey
}));
}
return new Value(Value.OBJECT, valueStr, Object.assign({

return new Value(Value.OBJECT, objectWrappedStr, Object.assign({
children: children
}, valueOptions));
}
Expand Down
3 changes: 3 additions & 0 deletions src/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function traverse(value, options, skippedPaths) {
enter(value, skipPath);

if (isSkipped(value.getPath(), skippedPaths)) {
if (value.parent) {
leaveProp(value);
}
return;
}

Expand Down
20 changes: 18 additions & 2 deletions src/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,27 @@ Value.prototype.byPath = function (path) {
return result;
};

Value.prototype.out = function () {
Value.prototype.out = function (options) {
options = options || {};
options.verbose = options.verbose || {};

if (this.any) {
return '<jasmine.any(' + this.text + ')>';
}
return this.text;

var out = '';

if (this.type === Value.OBJECT && options.verbose.object) {
out += 'Object(';
}

out += this.text;

if (this.type === Value.OBJECT && options.verbose.object) {
out += ')';
}

return out;
};

Value.prototype.indent = function (options) {
Expand Down
20 changes: 20 additions & 0 deletions test/verbose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,23 @@ test('whole turned off',

{ format: { verbose: false } }
);

test('object props facing different type prop',

'Expected [ 1, 2, 3 ] to equal [ 1, Object({ foo: 42 }), 3 ].',

'Expected [ 1, <a>2</a>, 3 ] ' +
'to equal [ 1, <e>{ foo: 42 }</e>, 3 ].',

{ format: { verbose: false } }
);

test('instance props facing different type prop',

'Expected [ 1, 2, 3 ] to equal [ 1, Foo({ foo: 42 }), 3 ].',

'Expected [ 1, <a>2</a>, 3 ] ' +
'to equal [ 1, <e>Foo({ foo: 42 })</e>, 3 ].',

{ format: { verbose: false } }
);

0 comments on commit 4b107dc

Please sign in to comment.