Skip to content

Commit 180fd39

Browse files
thefourtheyecjihrig
authored andcommitted
test: refactor test-repl-tab-complete
The original test uses a variable to explicitly count how many times the callback is invoked. This patch uses common.mustCall() to track if the callback is called or not. This makes the test more robust, as we don't explicitly hardcode the number of times to be called. PR-URL: #2122 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 84b3915 commit 180fd39

File tree

1 file changed

+37
-47
lines changed

1 file changed

+37
-47
lines changed

test/parallel/test-repl-tab-complete.js

+37-47
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ var assert = require('assert');
44
var util = require('util');
55
var repl = require('repl');
66
var referenceErrors = 0;
7-
var completionCount = 0;
7+
var expectedReferenceErrors = 0;
88

9-
function doNotCall() {
10-
assert(false);
9+
function getDoNotCallFunction() {
10+
expectedReferenceErrors += 1;
11+
return function() {
12+
assert(false);
13+
};
1114
}
1215

1316
process.on('exit', function() {
14-
assert.strictEqual(referenceErrors, 6);
15-
assert.strictEqual(completionCount, 12);
17+
assert.strictEqual(referenceErrors, expectedReferenceErrors);
1618
});
1719

1820
// A stream to push an array into a REPL
@@ -51,19 +53,17 @@ putIn.run([
5153
'var inner = {',
5254
'one:1'
5355
]);
54-
testMe.complete('inner.o', doNotCall);
56+
testMe.complete('inner.o', getDoNotCallFunction());
5557

56-
testMe.complete('console.lo', function(error, data) {
57-
completionCount++;
58+
testMe.complete('console.lo', common.mustCall(function(error, data) {
5859
assert.deepEqual(data, [['console.log'], 'console.lo']);
59-
});
60+
}));
6061

6162
// Tab Complete will return globaly scoped variables
6263
putIn.run(['};']);
63-
testMe.complete('inner.o', function(error, data) {
64-
completionCount++;
64+
testMe.complete('inner.o', common.mustCall(function(error, data) {
6565
assert.deepEqual(data, works);
66-
});
66+
}));
6767

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

@@ -73,7 +73,7 @@ putIn.run([
7373
'?',
7474
'{one: 1} : '
7575
]);
76-
testMe.complete('inner.o', doNotCall);
76+
testMe.complete('inner.o', getDoNotCallFunction());
7777

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

@@ -82,15 +82,14 @@ putIn.run([
8282
'var top = function() {',
8383
'var inner = {one:1};'
8484
]);
85-
testMe.complete('inner.o', function(error, data) {
86-
completionCount++;
85+
testMe.complete('inner.o', common.mustCall(function(error, data) {
8786
assert.deepEqual(data, works);
88-
});
87+
}));
8988

9089
// When you close the function scope tab complete will not return the
9190
// locally scoped variable
9291
putIn.run(['};']);
93-
testMe.complete('inner.o', doNotCall);
92+
testMe.complete('inner.o', getDoNotCallFunction());
9493

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

@@ -101,10 +100,9 @@ putIn.run([
101100
' one:1',
102101
'};'
103102
]);
104-
testMe.complete('inner.o', function(error, data) {
105-
completionCount++;
103+
testMe.complete('inner.o', common.mustCall(function(error, data) {
106104
assert.deepEqual(data, works);
107-
});
105+
}));
108106

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

@@ -116,10 +114,9 @@ putIn.run([
116114
' one:1',
117115
'};'
118116
]);
119-
testMe.complete('inner.o', function(error, data) {
120-
completionCount++;
117+
testMe.complete('inner.o', common.mustCall(function(error, data) {
121118
assert.deepEqual(data, works);
122-
});
119+
}));
123120

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

@@ -132,10 +129,9 @@ putIn.run([
132129
' one:1',
133130
'};'
134131
]);
135-
testMe.complete('inner.o', function(error, data) {
136-
completionCount++;
132+
testMe.complete('inner.o', common.mustCall(function(error, data) {
137133
assert.deepEqual(data, works);
138-
});
134+
}));
139135

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

@@ -148,7 +144,7 @@ putIn.run([
148144
' one:1',
149145
'};'
150146
]);
151-
testMe.complete('inner.o', doNotCall);
147+
testMe.complete('inner.o', getDoNotCallFunction());
152148

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

@@ -161,7 +157,7 @@ putIn.run([
161157
' one:1',
162158
'};'
163159
]);
164-
testMe.complete('inner.o', doNotCall);
160+
testMe.complete('inner.o', getDoNotCallFunction());
165161

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

@@ -175,18 +171,17 @@ putIn.run([
175171
' one:1',
176172
'};'
177173
]);
178-
testMe.complete('inner.o', doNotCall);
174+
testMe.complete('inner.o', getDoNotCallFunction());
179175

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

182178
// make sure tab completion works on non-Objects
183179
putIn.run([
184180
'var str = "test";'
185181
]);
186-
testMe.complete('str.len', function(error, data) {
187-
completionCount++;
182+
testMe.complete('str.len', common.mustCall(function(error, data) {
188183
assert.deepEqual(data, [['str.length'], 'str.len']);
189-
});
184+
}));
190185

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

@@ -195,32 +190,28 @@ var spaceTimeout = setTimeout(function() {
195190
throw new Error('timeout');
196191
}, 1000);
197192

198-
testMe.complete(' ', function(error, data) {
199-
completionCount++;
193+
testMe.complete(' ', common.mustCall(function(error, data) {
200194
assert.deepEqual(data, [[], undefined]);
201195
clearTimeout(spaceTimeout);
202-
});
196+
}));
203197

204198
// tab completion should pick up the global "toString" object, and
205199
// any other properties up the "global" object's prototype chain
206-
testMe.complete('toSt', function(error, data) {
207-
completionCount++;
200+
testMe.complete('toSt', common.mustCall(function(error, data) {
208201
assert.deepEqual(data, [['toString'], 'toSt']);
209-
});
202+
}));
210203

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

214-
testMe.complete('require(\'', function(error, data) {
215-
completionCount++;
207+
testMe.complete('require(\'', common.mustCall(function(error, data) {
216208
assert.strictEqual(error, null);
217209
repl._builtinLibs.forEach(function(lib) {
218210
assert.notStrictEqual(data[0].indexOf(lib), -1, lib + ' not found');
219211
});
220-
});
212+
}));
221213

222-
testMe.complete('require(\'n', function(error, data) {
223-
completionCount++;
214+
testMe.complete('require(\'n', common.mustCall(function(error, data) {
224215
assert.strictEqual(error, null);
225216
assert.strictEqual(data.length, 2);
226217
assert.strictEqual(data[1], 'n');
@@ -230,15 +221,14 @@ testMe.complete('require(\'n', function(error, data) {
230221
if (completion)
231222
assert(/^n/.test(completion));
232223
});
233-
});
224+
}));
234225

235226
// Make sure tab completion works on context properties
236227
putIn.run(['.clear']);
237228

238229
putIn.run([
239230
'var custom = "test";'
240231
]);
241-
testMe.complete('cus', function(error, data) {
242-
completionCount++;
232+
testMe.complete('cus', common.mustCall(function(error, data) {
243233
assert.deepEqual(data, [['custom'], 'cus']);
244-
});
234+
}));

0 commit comments

Comments
 (0)