Skip to content
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

Truncate Typed Arrays (#441) #576

Merged
merged 7 commits into from
Jan 5, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 39 additions & 2 deletions lib/chai/utils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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)));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it is open to abuse, but you could just use:

/\w+Array]$/.test(objectToString(ar))


function isArray(ar) {
return Array.isArray(ar) ||
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
Expand Down
59 changes: 59 additions & 0 deletions test/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down