Skip to content

Commit

Permalink
Merge pull request #251 from romario333/threshold3
Browse files Browse the repository at this point in the history
Fix issue #166 - configurable threshold in objDisplay.
  • Loading branch information
logicalparadox committed Mar 18, 2014
2 parents 07b34c7 + 9e5f9ff commit b638a36
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 44 deletions.
1 change: 1 addition & 0 deletions component.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"index.js"
, "lib/chai.js"
, "lib/chai/assertion.js"
, "lib/chai/config.js"
, "lib/chai/core/assertions.js"
, "lib/chai/interface/assert.js"
, "lib/chai/interface/expect.js"
Expand Down
7 changes: 7 additions & 0 deletions lib/chai.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ exports.use = function (fn) {
return this;
};

/*!
* Configuration
*/

var config = require('./chai/config');
exports.config = config;

/*!
* Primary `Assertion` prototype
*/
Expand Down
52 changes: 24 additions & 28 deletions lib/chai/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* MIT Licensed
*/

var config = require('./config');

module.exports = function (_chai, util) {
/*!
* Module dependencies.
Expand Down Expand Up @@ -33,33 +35,27 @@ module.exports = function (_chai, util) {
flag(this, 'message', msg);
}

/*!
* ### Assertion.includeStack
*
* User configurable property, influences whether stack trace
* is included in Assertion error message. Default of false
* suppresses stack trace in the error message
*
* Assertion.includeStack = true; // enable stack on error
*
* @api public
*/

Assertion.includeStack = false;

/*!
* ### Assertion.showDiff
*
* User configurable property, influences whether or not
* the `showDiff` flag should be included in the thrown
* AssertionErrors. `false` will always be `false`; `true`
* will be true when the assertion has requested a diff
* be shown.
*
* @api public
*/
Object.defineProperty(Assertion, 'includeStack', {
get: function() {
console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
return config.includeStack;
},
set: function(value) {
console.warn('Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
config.includeStack = value;
}
});

Assertion.showDiff = true;
Object.defineProperty(Assertion, 'showDiff', {
get: function() {
console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
return config.showDiff;
},
set: function(value) {
console.warn('Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
config.showDiff = value;
}
});

Assertion.addProperty = function (name, fn) {
util.addProperty(this.prototype, name, fn);
Expand Down Expand Up @@ -102,7 +98,7 @@ module.exports = function (_chai, util) {
Assertion.prototype.assert = function (expr, msg, negateMsg, expected, _actual, showDiff) {
var ok = util.test(this, arguments);
if (true !== showDiff) showDiff = false;
if (true !== Assertion.showDiff) showDiff = false;
if (true !== config.showDiff) showDiff = false;

if (!ok) {
var msg = util.getMessage(this, arguments)
Expand All @@ -111,7 +107,7 @@ module.exports = function (_chai, util) {
actual: actual
, expected: expected
, showDiff: showDiff
}, (Assertion.includeStack) ? this.assert : flag(this, 'ssfi'));
}, (config.includeStack) ? this.assert : flag(this, 'ssfi'));
}
};

Expand Down
44 changes: 44 additions & 0 deletions lib/chai/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {

/*!
* ### config.includeStack
*
* User configurable property, influences whether stack trace
* is included in Assertion error message. Default of false
* suppresses stack trace in the error message
*
* chai.config.includeStack = true; // enable stack on error
*
* @api public
*/
includeStack: false,

/*!
* ### config.showDiff
*
* User configurable property, influences whether or not
* the `showDiff` flag should be included in the thrown
* AssertionErrors. `false` will always be `false`; `true`
* will be true when the assertion has requested a diff
* be shown.
*
* @api public
*/
showDiff: true,

/*!
* ### config.truncateThreshold
*
* User configurable property, sets length threshold for actual and
* expected values in assertion errors. If this threshold is exceeded,
* the value is truncated.
*
* Set it to zero if you want to disable truncating altogether.
*
* chai.config.truncateThreshold = 0; // disable truncating
*
* @api public
*/
truncateThreshold: 40

};
3 changes: 2 additions & 1 deletion lib/chai/utils/addChainableMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

var transferFlags = require('./transferFlags');
var flag = require('./flag');
var config = require('../config');

/*!
* Module variables
Expand Down Expand Up @@ -77,7 +78,7 @@ module.exports = function (ctx, name, method, chainingBehavior) {

var assert = function assert() {
var old_ssfi = flag(this, 'ssfi');
if (old_ssfi && ctx.constructor.includeStack === false)
if (old_ssfi && config.includeStack === false)
flag(this, 'ssfi', assert);
var result = chainableBehavior.method.apply(this, arguments);
return result === undefined ? this : result;
Expand Down
4 changes: 3 additions & 1 deletion lib/chai/utils/addMethod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* MIT Licensed
*/

var config = require('../config');

/**
* ### .addMethod (ctx, name, method)
*
Expand Down Expand Up @@ -33,7 +35,7 @@ var flag = require('./flag');
module.exports = function (ctx, name, method) {
ctx[name] = function () {
var old_ssfi = flag(this, 'ssfi');
if (old_ssfi && ctx.constructor.includeStack === false)
if (old_ssfi && config.includeStack === false)
flag(this, 'ssfi', ctx[name]);
var result = method.apply(this, arguments);
return result === undefined ? this : result;
Expand Down
3 changes: 2 additions & 1 deletion lib/chai/utils/objDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

var inspect = require('./inspect');
var config = require('../config');

/**
* ### .objDisplay (object)
Expand All @@ -26,7 +27,7 @@ module.exports = function (obj) {
var str = inspect(obj)
, type = Object.prototype.toString.call(obj);

if (str.length >= 40) {
if (config.truncateThreshold && str.length >= config.truncateThreshold) {
if (type === '[object Function]') {
return !obj.name || obj.name === ''
? '[Function]'
Expand Down
106 changes: 97 additions & 9 deletions test/configuration.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
describe('configuration', function () {
var assert = chai.assert;

var origConfig;

beforeEach(function() {
// backup current config
function clone(o) {
return JSON.parse(JSON.stringify(o));
}
origConfig = clone(chai.config);
});

afterEach(function() {
// restore config
Object.keys(origConfig).forEach(function(key) {
chai.config[key] = origConfig[key];
});
});

function fooThrows () {
chai.expect('foo').to.be.equal('bar');
}

it('Assertion.includeStack is true', function () {
var orig = chai.Assertion.includeStack;
chai.Assertion.includeStack = true;
it('includeStack is true', function () {
chai.config.includeStack = true;

try {
fooThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
chai.Assertion.includeStack = orig;
// not all browsers support err.stack
if ('undefined' !== typeof err.stack) {
assert.include(err.stack, 'assertEqual', 'should have internal stack trace in error message');
Expand All @@ -23,16 +38,13 @@ describe('configuration', function () {

});

it('Assertion.includeStack is false', function () {
var orig = chai.Assertion.includeStack;
chai.Assertion.includeStack = false;
it('includeStack is false', function () {
chai.config.includeStack = false;

try {
fooThrows();
assert.ok(false, 'should not get here because error thrown');
} catch (err) {
chai.Assertion.includeStack = orig;

// IE 10 supports err.stack in Chrome format, but without
// `Error.captureStackTrace` support that allows tuning of the error
// message.
Expand All @@ -42,4 +54,80 @@ describe('configuration', function () {
}
}
});

describe('truncateThreshold', function() {
it('is 20', function() {
chai.config.truncateThreshold = 20;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { Object (v) } to deeply equal { v: 'x' }");
});

it('is 0', function() {
chai.config.truncateThreshold = 0;

err(function() {
assert.deepEqual({v: 'something longer than 20'}, {v: 'x'});
}, "expected { v: 'something longer than 20' } to deeply equal { v: 'x' }");
});
});

describe('deprecated properties', function() {
var origWarnFn;
var warnings;

beforeEach(function() {
origWarnFn = console.warn;
warnings = [];
console.warn = function(message) {
warnings.push(message);
};
});

afterEach(function() {
console.warn = origWarnFn;
});

it('Assertion.includeStack warns that it is deprecated', function() {
chai.Assertion.includeStack;

assert.equal(warnings.length, 1);
assert.equal(warnings[0], 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.');

chai.Assertion.includeStack = true;

assert.equal(warnings.length, 2);
assert.equal(warnings[1], 'Assertion.includeStack is deprecated, use chai.config.includeStack instead.');
});

it('Assertion.includeStack is kept in sync with config.includeStack', function() {
assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
chai.Assertion.includeStack = !chai.Assertion.includeStack;
assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
chai.config.includeStack = !chai.config.includeStack;
assert.equal(chai.Assertion.includeStack, chai.config.includeStack);
});

it('Assertion.showDiff warns that it is deprecated', function() {
chai.Assertion.showDiff;

assert.equal(warnings.length, 1);
assert.equal(warnings[0], 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.');

chai.Assertion.showDiff = true;

assert.equal(warnings.length, 2);
assert.equal(warnings[1], 'Assertion.showDiff is deprecated, use chai.config.showDiff instead.');
});

it('Assertion.showDiff is kept in sync with config.showDiff', function() {
assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
chai.Assertion.showDiff = !chai.Assertion.showDiff;
assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
chai.config.showDiff = !chai.config.showDiff;
assert.equal(chai.Assertion.showDiff, chai.config.showDiff);
});

});
});
2 changes: 1 addition & 1 deletion test/display/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if (!chai)

var expect = chai.expect;

chai.Assertion.includeStack = true;
chai.config.includeStack = true;

suite('error display', function () {

Expand Down
6 changes: 3 additions & 3 deletions test/display/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var deepObj2 = {
]
};

chai.Assertion.includeStack = true;
chai.config.includeStack = true;

suite('object display', function () {

Expand All @@ -32,9 +32,9 @@ suite('object display', function () {
});

test('deep equal no diff', function () {
chai.Assertion.showDiff = false;
chai.config.showDiff = false;
deepObj.should.deep.equal(deepObj2);
chai.Assertion.showDiff = true;
chai.config.showDiff = true;
});

});

0 comments on commit b638a36

Please sign in to comment.