Skip to content

Commit

Permalink
test: fix test-repl-tab-complete.js
Browse files Browse the repository at this point in the history
test-repl-tab-complete.js contains numerous assertions that are
never run. Anything that results in a ReferenceError bails out,
and never calls the functions containing the assertions. This
commit adds checking for successful tab completions, as well as
ReferenceErrors.

PR-URL: #2052
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
cjihrig committed Jun 25, 2015
1 parent a198c68 commit 06721fe
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
var common = require('../common');
var assert = require('assert');
var util = require('util');

var repl = require('repl');
var referenceErrors = 0;
var completionCount = 0;

function doNotCall() {
assert(false);
}

process.on('exit', function() {
assert.strictEqual(referenceErrors, 6);
assert.strictEqual(completionCount, 12);
});

// A stream to push an array into a REPL
function ArrayStream() {
Expand All @@ -21,27 +31,37 @@ ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function() {};

var works = [['inner.one'], 'inner.o'];
var doesNotBreak = [[], 'inner.o'];

var putIn = new ArrayStream();
var testMe = repl.start('', putIn);

// Some errors are passed to the domain, but do not callback
testMe._domain.on('error', function(err) {
// Errors come from another context, so instanceof doesn't work
var str = err.toString();

if (/^ReferenceError:/.test(str))
referenceErrors++;
else
assert(false);
});

// Tab Complete will not break in an object literal
putIn.run(['.clear']);
putIn.run([
'var inner = {',
'one:1'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

testMe.complete('console.lo', function(error, data) {
completionCount++;
assert.deepEqual(data, [['console.log'], 'console.lo']);
});

// Tab Complete will return globaly scoped variables
putIn.run(['};']);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -53,9 +73,7 @@ putIn.run([
'?',
'{one: 1} : '
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

putIn.run(['.clear']);

Expand All @@ -65,15 +83,14 @@ putIn.run([
'var inner = {one:1};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

// When you close the function scope tab complete will not return the
// locally scoped variable
putIn.run(['};']);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

putIn.run(['.clear']);

Expand All @@ -85,6 +102,7 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -99,6 +117,7 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -114,12 +133,12 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

putIn.run(['.clear']);

// currently does not work, but should not break note the inner function
// def has the params and { on a separate line
putIn.run([
'var top = function() {',
Expand All @@ -129,9 +148,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

putIn.run(['.clear']);

Expand All @@ -144,9 +161,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

putIn.run(['.clear']);

Expand All @@ -160,9 +175,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

putIn.run(['.clear']);

Expand All @@ -171,6 +184,7 @@ putIn.run([
'var str = "test";'
]);
testMe.complete('str.len', function(error, data) {
completionCount++;
assert.deepEqual(data, [['str.length'], 'str.len']);
});

Expand All @@ -182,29 +196,40 @@ var spaceTimeout = setTimeout(function() {
}, 1000);

testMe.complete(' ', function(error, data) {
completionCount++;
assert.deepEqual(data, [[], undefined]);
clearTimeout(spaceTimeout);
});

// tab completion should pick up the global "toString" object, and
// any other properties up the "global" object's prototype chain
testMe.complete('toSt', function(error, data) {
completionCount++;
assert.deepEqual(data, [['toString'], 'toSt']);
});

// Tab complete provides built in libs for require()
putIn.run(['.clear']);

testMe.complete('require(\'', function(error, data) {
completionCount++;
assert.strictEqual(error, null);
repl._builtinLibs.forEach(function(lib) {
assert.notStrictEqual(data[0].indexOf(lib), -1, lib + ' not found');
});
});

testMe.complete('require(\'n', function(error, data) {
completionCount++;
assert.strictEqual(error, null);
assert.deepEqual(data, [['net'], 'n']);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], 'n');
assert.notStrictEqual(data[0].indexOf('net'), -1);
// It's possible to pick up non-core modules too
data[0].forEach(function(completion) {
if (completion)
assert(/^n/.test(completion));
});
});

// Make sure tab completion works on context properties
Expand All @@ -214,5 +239,6 @@ putIn.run([
'var custom = "test";'
]);
testMe.complete('cus', function(error, data) {
completionCount++;
assert.deepEqual(data, [['custom'], 'cus']);
});

0 comments on commit 06721fe

Please sign in to comment.