diff --git a/lib/chai/utils/inspect.js b/lib/chai/utils/inspect.js index 41f1821a6..8fd1cde80 100644 --- a/lib/chai/utils/inspect.js +++ b/lib/chai/utils/inspect.js @@ -4,11 +4,12 @@ var getName = require('./getName'); var getProperties = require('./getProperties'); var getEnumerableProperties = require('./getEnumerableProperties'); +var config = require('../config'); module.exports = inspect; /** - * Echos the value of a value. Trys to print the value out + * Echos the value of a value. Tries to print the value out * in the best way possible given the different types. * * @param {Object} obj The object to print out. @@ -120,7 +121,15 @@ function formatValue(ctx, value, recurseTimes) { } } - var base = '', array = false, braces = ['{', '}']; + var base = '' + , array = false + , typedArray = false + , braces = ['{', '}']; + + if (isTypedArray(value)) { + typedArray = true; + braces = ['[', ']']; + } // Make Array say that they are Array if (isArray(value)) { @@ -167,6 +176,8 @@ function formatValue(ctx, value, recurseTimes) { var output; if (array) { output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else if (typedArray) { + return formatTypedArray(value); } else { output = keys.map(function(key) { return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); @@ -221,6 +232,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { output.push(''); } } + keys.forEach(function(key) { if (!key.match(/^\d+$/)) { output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, @@ -230,6 +242,25 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { return output; } +function formatTypedArray(value) { + var str = '[ '; + + for (var i = 0; i < value.length; ++i) { + if (str.length >= config.truncateThreshold - 7) { + str += '...'; + break; + } + str += value[i] + ', '; + } + str += ' ]'; + + // Removing trailing `, ` if the array was not truncated + if (str.indexOf(', ]') !== -1) { + str = str.replace(', ]', ' ]'); + } + + return str; +} function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { var name, str; @@ -311,6 +342,12 @@ function reduceToSingleString(output, base, braces) { return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; } +function isTypedArray(ar) { + // Unfortunately there's no way to check if an object is a TypedArray + // We have to check if it's one of these types + return (typeof ar === 'object' && /\w+Array]$/.test(objectToString(ar))); +} + function isArray(ar) { return Array.isArray(ar) || (typeof ar === 'object' && objectToString(ar) === '[object Array]'); diff --git a/test/utilities.js b/test/utilities.js index 255fa13ea..e74ac0779 100644 --- a/test/utilities.js +++ b/test/utilities.js @@ -391,6 +391,65 @@ describe('utilities', function () { }); }); + it('inspect every kind of available TypedArray', function () { + chai.use(function (_chai, _) { + var arr = [1, 2, 3] + , exp = '[ 1, 2, 3 ]' + , isNode = true; + + if (typeof window !== 'undefined') { + isNode = false; + } + + // Checks if engine supports common TypedArrays + if ((!isNode && 'Int8Array' in window) || + isNode && typeof 'Int8Array' !== undefined) { + // Typed array inspections should work as array inspections do + expect(_.inspect(new Int8Array(arr))).to.equal(exp); + expect(_.inspect(new Uint8Array(arr))).to.equal(exp); + expect(_.inspect(new Int16Array(arr))).to.equal(exp); + expect(_.inspect(new Uint16Array(arr))).to.equal(exp); + expect(_.inspect(new Int32Array(arr))).to.equal(exp); + expect(_.inspect(new Uint32Array(arr))).to.equal(exp); + expect(_.inspect(new Float32Array(arr))).to.equal(exp); + } + + // These ones may not be available alongside the others above + if ((!isNode && 'Uint8ClampedArray' in window) || + isNode && typeof 'Uint8ClampedArray' !== undefined) { + expect(_.inspect(new Uint8ClampedArray(arr))).to.equal(exp); + } + + if ((!isNode && 'Float64Array' in window) || + isNode && typeof 'Float64Array' !== undefined) { + expect(_.inspect(new Float64Array(arr))).to.equal(exp); + } + }); + }); + + it('truncate long TypedArray', function () { + chai.use(function (_chai, _) { + + var arr = [] + , exp = '[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... ]' + , isNode = true; + + // Filling arr with lots of elements + for (var i = 1; i <= 1000; i++) { + arr.push(i); + } + + if (typeof window !== 'undefined') { + isNode = false; + } + + if ((!isNode && 'Int8Array' in window) || + isNode && typeof 'Int8Array' !== undefined) { + expect(_.inspect(new Int8Array(arr))).to.equal(exp); + } + }); + }); + it('addChainableMethod', function () { chai.use(function (_chai, _) { _chai.Assertion.addChainableMethod('x',