summaryrefslogtreecommitdiff
path: root/test/parallel/test-console-group.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2017-08-17 22:41:14 -0700
committerRich Trott <rtrott@gmail.com>2017-08-23 18:46:14 -0700
commitc40229a9b80736f1fdb31fac169b70a1d6af8669 (patch)
tree5c19099000c3bffc33468f77c2919d0c253f810d /test/parallel/test-console-group.js
parentc6da5c8cdfae9e80d63f090c4289fd146c3175df (diff)
downloadandroid-node-v8-c40229a9b80736f1fdb31fac169b70a1d6af8669.tar.gz
android-node-v8-c40229a9b80736f1fdb31fac169b70a1d6af8669.tar.bz2
android-node-v8-c40229a9b80736f1fdb31fac169b70a1d6af8669.zip
console: implement minimal `console.group()`
Node.js exposes `console.group()` and `console.groupEnd()` via the inspector. These functions have no apparent effect when called from Node.js without the inspector. We cannot easily hide them when Node.js is started without the inspector because we support opening the inspector during runtime via `inspector.port()`. Implement a minimal `console.group()`/`console.groupEnd()`. More sophisticated implementations are possible, but they can be done in userland and/or features can be added to this at a later time. `console.groupCollapsed()` is implemented as an alias for `console.group()`. PR-URL: https://github.com/nodejs/node/pull/14910 Fixes: https://github.com/nodejs/node/issues/1716 Ref: https://github.com/nodejs/node/issues/12675 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/parallel/test-console-group.js')
-rw-r--r--test/parallel/test-console-group.js111
1 files changed, 111 insertions, 0 deletions
diff --git a/test/parallel/test-console-group.js b/test/parallel/test-console-group.js
new file mode 100644
index 0000000000..8f7d84f2f1
--- /dev/null
+++ b/test/parallel/test-console-group.js
@@ -0,0 +1,111 @@
+'use strict';
+const common = require('../common');
+
+const assert = require('assert');
+const Console = require('console').Console;
+
+let c, stdout, stderr;
+
+function setup() {
+ stdout = '';
+ common.hijackStdout(function(data) {
+ stdout += data;
+ });
+
+ stderr = '';
+ common.hijackStderr(function(data) {
+ stderr += data;
+ });
+
+ c = new Console(process.stdout, process.stderr);
+}
+
+function teardown() {
+ common.restoreStdout();
+ common.restoreStderr();
+}
+
+// Basic group() functionality
+{
+ setup();
+ const expectedOut = 'This is the outer level\n' +
+ ' Level 2\n' +
+ ' Level 3\n' +
+ ' Back to level 2\n' +
+ 'Back to the outer level\n' +
+ 'Still at the outer level\n';
+
+
+ const expectedErr = ' More of level 3\n';
+
+ c.log('This is the outer level');
+ c.group();
+ c.log('Level 2');
+ c.group();
+ c.log('Level 3');
+ c.warn('More of level 3');
+ c.groupEnd();
+ c.log('Back to level 2');
+ c.groupEnd();
+ c.log('Back to the outer level');
+ c.groupEnd();
+ c.log('Still at the outer level');
+
+ assert.strictEqual(stdout, expectedOut);
+ assert.strictEqual(stderr, expectedErr);
+ teardown();
+}
+
+// Group indentation is tracked per Console instance.
+{
+ setup();
+ const expectedOut = 'No indentation\n' +
+ 'None here either\n' +
+ ' Now the first console is indenting\n' +
+ 'But the second one does not\n';
+ const expectedErr = '';
+
+ const c2 = new Console(process.stdout, process.stderr);
+ c.log('No indentation');
+ c2.log('None here either');
+ c.group();
+ c.log('Now the first console is indenting');
+ c2.log('But the second one does not');
+
+ assert.strictEqual(stdout, expectedOut);
+ assert.strictEqual(stderr, expectedErr);
+ teardown();
+}
+
+// Make sure labels work.
+{
+ setup();
+ const expectedOut = 'This is a label\n' +
+ ' And this is the data for that label\n';
+ const expectedErr = '';
+
+ c.group('This is a label');
+ c.log('And this is the data for that label');
+
+ assert.strictEqual(stdout, expectedOut);
+ assert.strictEqual(stderr, expectedErr);
+ teardown();
+}
+
+// Check that console.groupCollapsed() is an alias of console.group()
+{
+ setup();
+ const expectedOut = 'Label\n' +
+ ' Level 2\n' +
+ ' Level 3\n';
+ const expectedErr = '';
+
+ c.groupCollapsed('Label');
+ c.log('Level 2');
+ c.groupCollapsed();
+ c.log('Level 3');
+
+ assert.strictEqual(stdout, expectedOut);
+ assert.strictEqual(stderr, expectedErr);
+ teardown();
+}