11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst Stream = require('stream'); 261cb0ef41Sopenharmony_ciconst requiredConsole = require('console'); 271cb0ef41Sopenharmony_ciconst Console = requiredConsole.Console; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst out = new Stream(); 301cb0ef41Sopenharmony_ciconst err = new Stream(); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci// Ensure the Console instance doesn't write to the 331cb0ef41Sopenharmony_ci// process' "stdout" or "stderr" streams. 341cb0ef41Sopenharmony_ciprocess.stdout.write = process.stderr.write = common.mustNotCall(); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci// Make sure that the "Console" function exists. 371cb0ef41Sopenharmony_ciassert.strictEqual(typeof Console, 'function'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciassert.strictEqual(requiredConsole, global.console); 401cb0ef41Sopenharmony_ci// Make sure the custom instanceof of Console works 411cb0ef41Sopenharmony_ciassert.ok(global.console instanceof Console); 421cb0ef41Sopenharmony_ciassert.ok(!({} instanceof Console)); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci// Make sure that the Console constructor throws 451cb0ef41Sopenharmony_ci// when not given a writable stream instance. 461cb0ef41Sopenharmony_ciassert.throws( 471cb0ef41Sopenharmony_ci () => { new Console(); }, 481cb0ef41Sopenharmony_ci { 491cb0ef41Sopenharmony_ci code: 'ERR_CONSOLE_WRITABLE_STREAM', 501cb0ef41Sopenharmony_ci name: 'TypeError', 511cb0ef41Sopenharmony_ci message: /stdout/ 521cb0ef41Sopenharmony_ci } 531cb0ef41Sopenharmony_ci); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci// Console constructor should throw if stderr exists but is not writable. 561cb0ef41Sopenharmony_ciassert.throws( 571cb0ef41Sopenharmony_ci () => { 581cb0ef41Sopenharmony_ci out.write = () => {}; 591cb0ef41Sopenharmony_ci err.write = undefined; 601cb0ef41Sopenharmony_ci new Console(out, err); 611cb0ef41Sopenharmony_ci }, 621cb0ef41Sopenharmony_ci { 631cb0ef41Sopenharmony_ci code: 'ERR_CONSOLE_WRITABLE_STREAM', 641cb0ef41Sopenharmony_ci name: 'TypeError', 651cb0ef41Sopenharmony_ci message: /stderr/ 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciout.write = err.write = (d) => {}; 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci{ 721cb0ef41Sopenharmony_ci const c = new Console(out, err); 731cb0ef41Sopenharmony_ci assert.ok(c instanceof Console); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci out.write = err.write = common.mustCall((d) => { 761cb0ef41Sopenharmony_ci assert.strictEqual(d, 'test\n'); 771cb0ef41Sopenharmony_ci }, 2); 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci c.log('test'); 801cb0ef41Sopenharmony_ci c.error('test'); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci out.write = common.mustCall((d) => { 831cb0ef41Sopenharmony_ci assert.strictEqual(d, '{ foo: 1 }\n'); 841cb0ef41Sopenharmony_ci }); 851cb0ef41Sopenharmony_ci 861cb0ef41Sopenharmony_ci c.dir({ foo: 1 }); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci // Ensure that the console functions are bound to the console instance. 891cb0ef41Sopenharmony_ci let called = 0; 901cb0ef41Sopenharmony_ci out.write = common.mustCall((d) => { 911cb0ef41Sopenharmony_ci called++; 921cb0ef41Sopenharmony_ci assert.strictEqual(d, `${called} ${called - 1} [ 1, 2, 3 ]\n`); 931cb0ef41Sopenharmony_ci }, 3); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci [1, 2, 3].forEach(c.log); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci// Test calling Console without the `new` keyword. 991cb0ef41Sopenharmony_ci{ 1001cb0ef41Sopenharmony_ci const withoutNew = Console(out, err); 1011cb0ef41Sopenharmony_ci assert.ok(withoutNew instanceof Console); 1021cb0ef41Sopenharmony_ci} 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ci// Test extending Console 1051cb0ef41Sopenharmony_ci{ 1061cb0ef41Sopenharmony_ci class MyConsole extends Console { 1071cb0ef41Sopenharmony_ci hello() {} 1081cb0ef41Sopenharmony_ci // See if the methods on Console.prototype are overridable. 1091cb0ef41Sopenharmony_ci log() { return 'overridden'; } 1101cb0ef41Sopenharmony_ci } 1111cb0ef41Sopenharmony_ci const myConsole = new MyConsole(process.stdout); 1121cb0ef41Sopenharmony_ci assert.strictEqual(typeof myConsole.hello, 'function'); 1131cb0ef41Sopenharmony_ci assert.ok(myConsole instanceof Console); 1141cb0ef41Sopenharmony_ci assert.strictEqual(myConsole.log(), 'overridden'); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci const log = myConsole.log; 1171cb0ef41Sopenharmony_ci assert.strictEqual(log(), 'overridden'); 1181cb0ef41Sopenharmony_ci} 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci// Instance that does not ignore the stream errors. 1211cb0ef41Sopenharmony_ci{ 1221cb0ef41Sopenharmony_ci const c2 = new Console(out, err, false); 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_ci out.write = () => { throw new Error('out'); }; 1251cb0ef41Sopenharmony_ci err.write = () => { throw new Error('err'); }; 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci assert.throws(() => c2.log('foo'), /^Error: out$/); 1281cb0ef41Sopenharmony_ci assert.throws(() => c2.warn('foo'), /^Error: err$/); 1291cb0ef41Sopenharmony_ci assert.throws(() => c2.dir('foo'), /^Error: out$/); 1301cb0ef41Sopenharmony_ci} 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ci// Console constructor throws if inspectOptions is not an object. 1331cb0ef41Sopenharmony_ci[null, true, false, 'foo', 5, Symbol()].forEach((inspectOptions) => { 1341cb0ef41Sopenharmony_ci assert.throws( 1351cb0ef41Sopenharmony_ci () => { 1361cb0ef41Sopenharmony_ci new Console({ 1371cb0ef41Sopenharmony_ci stdout: out, 1381cb0ef41Sopenharmony_ci stderr: err, 1391cb0ef41Sopenharmony_ci inspectOptions 1401cb0ef41Sopenharmony_ci }); 1411cb0ef41Sopenharmony_ci }, 1421cb0ef41Sopenharmony_ci { 1431cb0ef41Sopenharmony_ci message: 'The "options.inspectOptions" property must be of type object.' + 1441cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(inspectOptions), 1451cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 1461cb0ef41Sopenharmony_ci } 1471cb0ef41Sopenharmony_ci ); 1481cb0ef41Sopenharmony_ci}); 149