summaryrefslogtreecommitdiff
path: root/test/parallel/test-console-group.js
blob: c3c70bfa2487157117ccfc54f0db4dcae2a15826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
require('../common');
const {
  hijackStdout,
  hijackStderr,
  restoreStdout,
  restoreStderr
} = require('../common/hijackstdio');

const assert = require('assert');
const Console = require('console').Console;

let c, stdout, stderr;

function setup() {
  stdout = '';
  hijackStdout(function(data) {
    stdout += data;
  });

  stderr = '';
  hijackStderr(function(data) {
    stderr += data;
  });

  c = new Console(process.stdout, process.stderr);
}

function teardown() {
  restoreStdout();
  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();
}

// Check that multiline strings and object output are indented properly.
{
  setup();
  const expectedOut = 'not indented\n' +
                      '  indented\n' +
                      '  also indented\n' +
                      '  {\n' +
                      "    also: 'a',\n" +
                      "    multiline: 'object',\n" +
                      "    should: 'be',\n" +
                      "    indented: 'properly',\n" +
                      "    kthx: 'bai'\n" +
                      '  }\n';
  const expectedErr = '';

  c.log('not indented');
  c.group();
  c.log('indented\nalso indented');
  c.log({ also: 'a',
          multiline: 'object',
          should: 'be',
          indented: 'properly',
          kthx: 'bai' });

  assert.strictEqual(stdout, expectedOut);
  assert.strictEqual(stderr, expectedErr);
  teardown();
}

// Check that the kGroupIndent symbol property is not enumerable
{
  const keys = Reflect.ownKeys(console)
                      .filter((val) => console.propertyIsEnumerable(val))
                      .map((val) => val.toString());
  assert(!keys.includes('Symbol(groupIndent)'),
         'groupIndent should not be enumerable');
}