forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-repl-completion-on-getters-disabled.js
More file actions
188 lines (167 loc) Β· 6.52 KB
/
test-repl-completion-on-getters-disabled.js
File metadata and controls
188 lines (167 loc) Β· 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
'use strict';
const common = require('../common');
const assert = require('node:assert');
const { describe, test } = require('node:test');
const { startNewREPLServer } = require('../common/repl');
function runCompletionTests(replInit, tests) {
const { replServer: testRepl, input } = startNewREPLServer();
input.run([replInit]);
tests.forEach(([query, expectedCompletions]) => {
testRepl.complete(query, common.mustCall((error, data) => {
const actualCompletions = data[0];
if (expectedCompletions.length === 0) {
assert.deepStrictEqual(actualCompletions, []);
} else {
expectedCompletions.forEach((expectedCompletion) =>
assert(actualCompletions.includes(expectedCompletion), `completion '${expectedCompletion}' not found`)
);
}
}));
});
}
describe('REPL completion in relation of getters', () => {
describe('standard behavior without proxies/getters', () => {
test('completion of nested properties of an undeclared objects', () => {
runCompletionTests('', [
['nonExisting.', []],
['nonExisting.f', []],
['nonExisting.foo', []],
['nonExisting.foo.', []],
['nonExisting.foo.bar.b', []],
]);
});
test('completion of nested properties on plain objects', () => {
runCompletionTests('const plainObj = { foo: { bar: { baz: {} } } };', [
['plainObj.', ['plainObj.foo']],
['plainObj.f', ['plainObj.foo']],
['plainObj.foo', ['plainObj.foo']],
['plainObj.foo.', ['plainObj.foo.bar']],
['plainObj.foo.bar.b', ['plainObj.foo.bar.baz']],
['plainObj.fooBar.', []],
['plainObj.fooBar.baz', []],
]);
});
});
describe('completions on an object with getters', () => {
test(`completions are generated for properties that don't trigger getters`, () => {
runCompletionTests(
`
const fooKey = "foo";
const keys = {
"foo key": "foo",
};
const objWithGetters = {
foo: { bar: { baz: { buz: {} } }, get gBar() { return { baz: {} } } },
get gFoo() { return { bar: { baz: {} } }; }
};
`, [
['objWithGetters.', ['objWithGetters.foo']],
['objWithGetters.f', ['objWithGetters.foo']],
['objWithGetters.foo', ['objWithGetters.foo']],
['objWithGetters["foo"].b', ['objWithGetters["foo"].bar']],
['objWithGetters.foo.', ['objWithGetters.foo.bar']],
['objWithGetters.foo.bar.b', ['objWithGetters.foo.bar.baz']],
['objWithGetters.gFo', ['objWithGetters.gFoo']],
['objWithGetters.foo.gB', ['objWithGetters.foo.gBar']],
["objWithGetters.foo['bar'].b", ["objWithGetters.foo['bar'].baz"]],
["objWithGetters['foo']['bar'].b", ["objWithGetters['foo']['bar'].baz"]],
["objWithGetters['foo']['bar']['baz'].b", ["objWithGetters['foo']['bar']['baz'].buz"]],
["objWithGetters[keys['foo key']].b", ["objWithGetters[keys['foo key']].bar"]],
['objWithGetters[fooKey].b', ['objWithGetters[fooKey].bar']],
["objWithGetters['f' + 'oo'].b", ["objWithGetters['f' + 'oo'].bar"]],
]);
});
test('no completions are generated for properties that trigger getters', () => {
runCompletionTests(
`
function getGFooKey() {
return "g" + "Foo";
}
const gFooKey = "gFoo";
const keys = {
"g-foo key": "gFoo",
};
const objWithGetters = {
foo: { bar: { baz: {} }, get gBar() { return { baz: {}, get gBuz() { return 5; } } } },
get gFoo() { return { bar: { baz: {} } }; }
};
`,
[
['objWithGetters.gFoo.', []],
['objWithGetters.gFoo.b', []],
['objWithGetters["gFoo"].b', []],
['objWithGetters.gFoo.bar.b', []],
['objWithGetters.foo.gBar.', []],
['objWithGetters.foo.gBar.b', []],
["objWithGetters.foo['gBar'].b", []],
["objWithGetters['foo']['gBar'].b", []],
["objWithGetters['foo']['gBar']['gBuz'].", []],
["objWithGetters[keys['g-foo key']].b", []],
['objWithGetters[gFooKey].b', []],
["objWithGetters['g' + 'Foo'].b", []],
['objWithGetters[getGFooKey()].b', []],
]);
});
test('no side effects are triggered for getters during completion', async () => {
const { replServer } = startNewREPLServer();
await new Promise((resolve, reject) => {
replServer.eval('const foo = { get name() { globalThis.nameGetterRun = true; throw new Error(); } };',
replServer.context, '', (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
['foo.name.', 'foo["name"].'].forEach((test) => {
replServer.complete(
test,
common.mustCall((error, data) => {
// The context's nameGetterRun variable hasn't been set
assert.strictEqual(replServer.context.nameGetterRun, undefined);
// No errors has been thrown
assert.strictEqual(error, null);
})
);
});
});
});
describe('completions on proxies', () => {
test('no completions are generated for a proxy object', () => {
runCompletionTests(
`
function getFooKey() {
return "foo";
}
const fooKey = "foo";
const keys = {
"foo key": "foo",
};
const proxyObj = new Proxy({ foo: { bar: { baz: {} } } }, {});
`, [
['proxyObj.', []],
['proxyObj.f', []],
['proxyObj.foo', []],
['proxyObj.foo.', []],
['proxyObj.["foo"].', []],
['proxyObj.["f" + "oo"].', []],
['proxyObj.[fooKey].', []],
['proxyObj.[getFooKey()].', []],
['proxyObj.[keys["foo key"]].', []],
['proxyObj.foo.bar.b', []],
]);
});
test('no completions are generated for a proxy present in a standard object', () => {
runCompletionTests(
'const objWithProxy = { foo: { bar: new Proxy({ baz: {} }, {}) } };', [
['objWithProxy.', ['objWithProxy.foo']],
['objWithProxy.foo', ['objWithProxy.foo']],
['objWithProxy.foo.', ['objWithProxy.foo.bar']],
['objWithProxy.foo.b', ['objWithProxy.foo.bar']],
['objWithProxy.foo.bar.', []],
['objWithProxy.foo["b" + "ar"].', []],
]);
});
});
});