diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2018-05-25 12:05:39 +0200 |
---|---|---|
committer | Ruben Bridgewater <ruben@bridgewater.de> | 2018-07-18 17:21:14 +0200 |
commit | 81bc23fe61994784de270d5fc01bc5315dfa62f3 (patch) | |
tree | 9779397ab7a26019bd44844d5cf11e023a00ea32 /test/parallel/test-util-inspect.js | |
parent | caf2335a47db089a1e1b2c5a90d85cf644f6c355 (diff) | |
download | android-node-v8-81bc23fe61994784de270d5fc01bc5315dfa62f3.tar.gz android-node-v8-81bc23fe61994784de270d5fc01bc5315dfa62f3.tar.bz2 android-node-v8-81bc23fe61994784de270d5fc01bc5315dfa62f3.zip |
util: improve display of iterators and weak entries
This adds the number of not visible elements when inspecting iterators
while exceeding `maxArrayLength`.
It also fixes a edge case with `maxArrayLength` and the map.entries()
iterator. Now the whole entry will be visible instead of only the key
but not the value of the first entry.
Besides that it uses a slighly better algorithm that improves the
performance by skipping unnecessary steps.
PR-URL: https://github.com/nodejs/node/pull/20961
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/parallel/test-util-inspect.js')
-rw-r--r-- | test/parallel/test-util-inspect.js | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 0dc3f11bfe..c2b26fd5a4 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -447,13 +447,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324'); { const map = new Map(); map.set(1, 2); - const [ vals ] = previewEntries(map.entries()); - const valsOutput = []; - for (const o of vals) { - valsOutput.push(o); - } - - assert.strictEqual(util.inspect(valsOutput), '[ 1, 2 ]'); + // Passing only a single argument to indicate a set iterator. + const valsSetIterator = previewEntries(map.entries()); + // Passing through true to indicate a map iterator. + const valsMapIterEntries = previewEntries(map.entries(), true); + const valsMapIterKeys = previewEntries(map.keys(), true); + + assert.strictEqual(util.inspect(valsSetIterator), '[ 1, 2 ]'); + assert.strictEqual(util.inspect(valsMapIterEntries), '[ [ 1, 2 ], true ]'); + assert.strictEqual(util.inspect(valsMapIterKeys), '[ [ 1 ], false ]'); } // Test for other constructors in different context. @@ -913,18 +915,19 @@ if (typeof Symbol !== 'undefined') { // Test Map iterators. { const map = new Map([['foo', 'bar']]); - assert.strictEqual(util.inspect(map.keys()), "[Map Iterator] { 'foo' }"); - assert.strictEqual(util.inspect(map.values()), "[Map Iterator] { 'bar' }"); - assert.strictEqual(util.inspect(map.entries()), - "[Map Iterator] { [ 'foo', 'bar' ] }"); + assert.strictEqual(util.inspect(map.keys()), '[Map Iterator] { \'foo\' }'); + assert.strictEqual(util.inspect(map.values()), '[Map Iterator] { \'bar\' }'); + map.set('A', 'B!'); + assert.strictEqual(util.inspect(map.entries(), { maxArrayLength: 1 }), + "[Map Iterator] { [ 'foo', 'bar' ], ... 1 more item }"); // Make sure the iterator doesn't get consumed. const keys = map.keys(); - assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo' }"); - assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo' }"); + assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); + assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }"); keys.extra = true; assert.strictEqual( util.inspect(keys, { maxArrayLength: 0 }), - '[Map Iterator] { ... more items, extra: true }'); + '[Map Iterator] { ... 2 more items, extra: true }'); } // Test Set iterators. @@ -940,7 +943,7 @@ if (typeof Symbol !== 'undefined') { keys.extra = true; assert.strictEqual( util.inspect(keys, { maxArrayLength: 1 }), - '[Set Iterator] { 1, ... more items, extra: true }'); + '[Set Iterator] { 1, ... 1 more item, extra: true }'); } // Test alignment of items in container. @@ -1364,17 +1367,17 @@ util.inspect(process); assert.strictEqual(out, expect); out = util.inspect(weakMap, { maxArrayLength: 0, showHidden: true }); - expect = 'WeakMap { ... more items }'; + expect = 'WeakMap { ... 2 more items }'; assert.strictEqual(out, expect); weakMap.extra = true; out = util.inspect(weakMap, { maxArrayLength: 1, showHidden: true }); // It is not possible to determine the output reliable. - expect = 'WeakMap { [ [length]: 0 ] => {}, ... more items, extra: true }'; - const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... more items, ' + + expect = 'WeakMap { [ [length]: 0 ] => {}, ... 1 more item, extra: true }'; + const expectAlt = 'WeakMap { {} => [ [length]: 0 ], ... 1 more item, ' + 'extra: true }'; assert(out === expect || out === expectAlt, - `Found "${out}" rather than "${expect}" or "${expectAlt}"`); + `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); } { // Test WeakSet @@ -1388,17 +1391,17 @@ util.inspect(process); assert.strictEqual(out, expect); out = util.inspect(weakSet, { maxArrayLength: -2, showHidden: true }); - expect = 'WeakSet { ... more items }'; + expect = 'WeakSet { ... 2 more items }'; assert.strictEqual(out, expect); weakSet.extra = true; out = util.inspect(weakSet, { maxArrayLength: 1, showHidden: true }); // It is not possible to determine the output reliable. - expect = 'WeakSet { {}, ... more items, extra: true }'; - const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... more items, ' + + expect = 'WeakSet { {}, ... 1 more item, extra: true }'; + const expectAlt = 'WeakSet { [ 1, [length]: 1 ], ... 1 more item, ' + 'extra: true }'; assert(out === expect || out === expectAlt, - `Found "${out}" rather than "${expect}" or "${expectAlt}"`); + `Found: "${out}"\nrather than: "${expect}"\nor: "${expectAlt}"`); } { // Test argument objects. |