11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_cirequire('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst Stream = require('stream'); 51cb0ef41Sopenharmony_ciconst repl = require('repl'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst tests = [ 81cb0ef41Sopenharmony_ci testSloppyMode, 91cb0ef41Sopenharmony_ci testStrictMode, 101cb0ef41Sopenharmony_ci testAutoMode, 111cb0ef41Sopenharmony_ci testStrictModeTerminal, 121cb0ef41Sopenharmony_ci]; 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_citests.forEach(function(test) { 151cb0ef41Sopenharmony_ci test(); 161cb0ef41Sopenharmony_ci}); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_cifunction testSloppyMode() { 191cb0ef41Sopenharmony_ci const cli = initRepl(repl.REPL_MODE_SLOPPY); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci cli.input.emit('data', 'x = 3\n'); 221cb0ef41Sopenharmony_ci assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> '); 231cb0ef41Sopenharmony_ci cli.output.accumulator.length = 0; 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci cli.input.emit('data', 'let y = 3\n'); 261cb0ef41Sopenharmony_ci assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> '); 271cb0ef41Sopenharmony_ci} 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_cifunction testStrictMode() { 301cb0ef41Sopenharmony_ci const cli = initRepl(repl.REPL_MODE_STRICT); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci cli.input.emit('data', 'x = 3\n'); 331cb0ef41Sopenharmony_ci assert.match(cli.output.accumulator.join(''), 341cb0ef41Sopenharmony_ci /ReferenceError: x is not defined/); 351cb0ef41Sopenharmony_ci cli.output.accumulator.length = 0; 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci cli.input.emit('data', 'let y = 3\n'); 381cb0ef41Sopenharmony_ci assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> '); 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction testStrictModeTerminal() { 421cb0ef41Sopenharmony_ci if (!process.features.inspector) { 431cb0ef41Sopenharmony_ci console.warn('Test skipped: V8 inspector is disabled'); 441cb0ef41Sopenharmony_ci return; 451cb0ef41Sopenharmony_ci } 461cb0ef41Sopenharmony_ci // Verify that ReferenceErrors are reported in strict mode previews. 471cb0ef41Sopenharmony_ci const cli = initRepl(repl.REPL_MODE_STRICT, { 481cb0ef41Sopenharmony_ci terminal: true 491cb0ef41Sopenharmony_ci }); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci cli.input.emit('data', 'xyz '); 521cb0ef41Sopenharmony_ci assert.ok( 531cb0ef41Sopenharmony_ci cli.output.accumulator.includes('\n// ReferenceError: xyz is not defined') 541cb0ef41Sopenharmony_ci ); 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cifunction testAutoMode() { 581cb0ef41Sopenharmony_ci const cli = initRepl(repl.REPL_MODE_MAGIC); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci cli.input.emit('data', 'x = 3\n'); 611cb0ef41Sopenharmony_ci assert.strictEqual(cli.output.accumulator.join(''), '> 3\n> '); 621cb0ef41Sopenharmony_ci cli.output.accumulator.length = 0; 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci cli.input.emit('data', 'let y = 3\n'); 651cb0ef41Sopenharmony_ci assert.strictEqual(cli.output.accumulator.join(''), 'undefined\n> '); 661cb0ef41Sopenharmony_ci} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_cifunction initRepl(mode, options) { 691cb0ef41Sopenharmony_ci const input = new Stream(); 701cb0ef41Sopenharmony_ci input.write = input.pause = input.resume = () => {}; 711cb0ef41Sopenharmony_ci input.readable = true; 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci const output = new Stream(); 741cb0ef41Sopenharmony_ci output.write = output.pause = output.resume = function(buf) { 751cb0ef41Sopenharmony_ci output.accumulator.push(buf); 761cb0ef41Sopenharmony_ci }; 771cb0ef41Sopenharmony_ci output.accumulator = []; 781cb0ef41Sopenharmony_ci output.writable = true; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci return repl.start({ 811cb0ef41Sopenharmony_ci input: input, 821cb0ef41Sopenharmony_ci output: output, 831cb0ef41Sopenharmony_ci useColors: false, 841cb0ef41Sopenharmony_ci terminal: false, 851cb0ef41Sopenharmony_ci replMode: mode, 861cb0ef41Sopenharmony_ci ...options 871cb0ef41Sopenharmony_ci }); 881cb0ef41Sopenharmony_ci} 89