|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +var assert = require('assert'); |
| 23 | +var util = require('util'); |
| 24 | + |
| 25 | +var repl = require('repl'); |
| 26 | + |
| 27 | +// A stream to push an array into a REPL |
| 28 | +function ArrayStream() { |
| 29 | + this.run = function (data) { |
| 30 | + var self = this; |
| 31 | + data.forEach(function (line) { |
| 32 | + self.emit('data', line); |
| 33 | + }); |
| 34 | + } |
| 35 | +} |
| 36 | +util.inherits(ArrayStream, require('stream').Stream); |
| 37 | +ArrayStream.prototype.readable = true; |
| 38 | +ArrayStream.prototype.writable = true; |
| 39 | +ArrayStream.prototype.resume = function () {}; |
| 40 | +ArrayStream.prototype.write = function () {}; |
| 41 | + |
| 42 | +var works = [ [ 'inner.one' ], 'inner.o' ]; |
| 43 | +var doesNotBreak = [ [], 'inner.o' ]; |
| 44 | + |
| 45 | +var putIn = new ArrayStream(); |
| 46 | +var testMe = repl.start('', putIn); |
| 47 | + |
| 48 | +// Tab Complete will not break in an object literal |
| 49 | +putIn.run(['.clear']); |
| 50 | +putIn.run([ |
| 51 | + 'var inner = {', |
| 52 | + 'one:1']); |
| 53 | +testMe.complete('inner.o', function (error, data) { |
| 54 | + assert.deepEqual(data, doesNotBreak); |
| 55 | +}); |
| 56 | + |
| 57 | +// Tab Complete will return globaly scoped variables |
| 58 | +putIn.run(['};']); |
| 59 | +testMe.complete('inner.o', function (error, data) { |
| 60 | + assert.deepEqual(data, works); |
| 61 | +}); |
| 62 | + |
| 63 | +putIn.run(['.clear']); |
| 64 | + |
| 65 | +// Tab Complete will not break in an ternary operator with () |
| 66 | +putIn.run([ |
| 67 | + 'var inner = ( true ' , |
| 68 | + '?', |
| 69 | + '{one: 1} : ']); |
| 70 | +testMe.complete('inner.o', function (error, data) { |
| 71 | + assert.deepEqual(data, doesNotBreak); |
| 72 | +}); |
| 73 | + |
| 74 | +putIn.run(['.clear']); |
| 75 | + |
| 76 | +// Tab Complete will not return localy scoped variables |
| 77 | +putIn.run([ |
| 78 | + 'var top = function () {', |
| 79 | + 'var inner = {one:1};']); |
| 80 | +testMe.complete('inner.o', function (error, data) { |
| 81 | + assert.deepEqual(data, doesNotBreak); |
| 82 | +}); |
| 83 | + |
| 84 | +// When you close the function scope tab complete will not return the |
| 85 | +// localy scoped variable |
| 86 | +putIn.run(['};']); |
| 87 | +testMe.complete('inner.o', function (error, data) { |
| 88 | + assert.deepEqual(data, doesNotBreak); |
| 89 | +}); |
| 90 | + |
| 91 | +putIn.run(['.clear']); |
| 92 | + |
0 commit comments