From 1e37efb08bbd1ebd82437b417329b6f8f1639fd0 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 23 Sep 2011 00:03:46 +0700 Subject: [PATCH] initial tests * Don't buffer command, before it's execution (repl) * `quit` command, custom streams for .start, stubbed out test, disable history repeation for non-tty (debugger) --- lib/_debugger.js | 9 ++- lib/repl.js | 8 +- test/simple/test-debugger-repl.js | 155 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 test/simple/test-debugger-repl.js diff --git a/lib/_debugger.js b/lib/_debugger.js index 26d2248..ff34341 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -880,7 +880,7 @@ Interface.prototype.requireConnection = function() { Interface.prototype.controlEval = function(code, context, filename, callback) { try { // Repeat last command if empty line are going to be evaluated - if (this.repl.rli.history.length > 0) { + if (this.repl.rli.history && this.repl.rli.history.length > 0) { if (code === '(undefined\n)') { code = '(' + this.repl.rli.history[0] + '\n)'; } @@ -1362,6 +1362,13 @@ Interface.prototype.exitRepl = function() { }; +// Quit +Interface.prototype.quit = function() { + this.killChild(); + process.exit(0); +}; + + // Kills child process Interface.prototype.killChild = function() { if (this.child) { diff --git a/lib/repl.js b/lib/repl.js index f50c14b..fc1eb54 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -159,7 +159,8 @@ function REPLServer(prompt, stream, eval) { } if (!skipCatchall) { - self.bufferedCommand += cmd + '\n'; + var evalCmd = self.bufferedCommand + cmd + '\n'; + // This try is for determining if the command is complete, or should // continue onto the next line. // We try to evaluate both expressions e.g. @@ -169,7 +170,7 @@ function REPLServer(prompt, stream, eval) { // First we attempt to eval as expression with parens. // This catches '{a : 1}' properly. - self.eval('(' + self.bufferedCommand + ')', + self.eval('(' + evalCmd + ')', self.context, 'repl', function(e, ret) { @@ -177,7 +178,7 @@ function REPLServer(prompt, stream, eval) { if (typeof ret === 'function' || e) { // Now as statement without parens. - self.eval(self.bufferedCommand, self.context, 'repl', finish); + self.eval(evalCmd, self.context, 'repl', finish); } else { finish(null, ret); } @@ -203,6 +204,7 @@ function REPLServer(prompt, stream, eval) { // { // ... x: 1 // ... } + self.bufferedCommand += cmd + '\n'; self.displayPrompt(); return; } else if (e) { diff --git a/test/simple/test-debugger-repl.js b/test/simple/test-debugger-repl.js new file mode 100644 index 0000000..1897f28 --- /dev/null +++ b/test/simple/test-debugger-repl.js @@ -0,0 +1,155 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + + +var common = require('../common'); +var assert = require('assert'); +var spawn = require('child_process').spawn; +var debug = require('_debugger'); + +var code = [ + '', + 'debugger;', + 'debugger;', + 'function a(x) {', + ' var i = 10;', + ' while (--i != 0);', + ' debugger;', + ' return i;', + '}', + 'function b() {', + ' return ["hello", "world"].join(" ");', + '}', + 'a()', + 'a(1)', + 'b()', + 'b()' +].join('\n'); + +var child = spawn(process.execPath, ['debug', '-e', code]); + +var buffer = ''; +child.stdout.on('data', function(data) { + data = (buffer + data.toString()).split(/\n/g); + buffer = data.pop(); + data.forEach(function(line) { + child.emit('line', line); + }); +}); +child.stderr.pipe(process.stdout); + +var expected = []; + +child.on('line', function(line) { + assert.ok(expected.length > 0, 'Got unexpected line: ' + line); + + console.log(JSON.stringify(line) + ','); + + var expectedLine = expected[0].lines.shift(); + assert.equal(line, expectedLine); + + if (expected[0].lines.length === 0) { + var callback = expected[0].callback; + expected.shift(); + callback && callback(); + } +}); + +function addTest(input, output) { + function next() { + if (expected.length > 0) { + child.stdin.write(expected[0].input + '\n'); + console.log('---'); + console.log('>>', expected[0].input); + } else { + finish(); + } + }; + expected.push({input: input, lines: output, callback: next}); +}; + +// Initial lines +addTest(null, [ + "debug> < debugger listening on port 5858", + "debug> connecting... ok", + "debug> break in [unnamed]:3", + " 1 ", + " 2 debugger;", + " 3 debugger;", + " 4 function a(x) {", + " 5 var i = 10;" +]); + +// Next +addTest('n', [ + "debug> debug> debug> break in [unnamed]:13", + " 11 return [\"hello\", \"world\"].join(\" \");", + " 12 }", + " 13 a()", + " 14 a(1)", + " 15 b()" +]); + +// Continue +addTest('c', [ + "debug> debug> debug> break in [unnamed]:7", + " 5 var i = 10;", + " 6 while (--i != 0);", + " 7 debugger;", + " 8 return i;", + " 9 }" +]); + + +// Step out +addTest('o', [ + "debug> debug> debug> break in [unnamed]:14", + " 12 }", + " 13 a()", + " 14 a(1)", + " 15 b()", + " 16 b()" +]); + + +function finish() { + console.log('passed'); + process.exit(0); +}; + +function quit() { + if (quit.called) return; + quit.called = true; + child.stdin.write('quit'); +}; + +setTimeout(function() { + throw new Error('timeout!'); +}, 5000); + +process.once('uncaughtException', function(e) { + quit(); + throw e; +}); + +process.on('exit', function() { + quit(); +}); -- 2.7.4