|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('node:assert'); |
| 5 | +const { describe, test } = require('node:test'); |
| 6 | + |
| 7 | +const ArrayStream = require('../common/arraystream'); |
| 8 | + |
| 9 | +const repl = require('node:repl'); |
| 10 | + |
| 11 | +function runCompletionTests(replInit, tests) { |
| 12 | + const stream = new ArrayStream(); |
| 13 | + const testRepl = repl.start({ stream }); |
| 14 | + |
| 15 | + // Some errors are passed to the domain |
| 16 | + testRepl._domain.on('error', assert.ifError); |
| 17 | + |
| 18 | + testRepl.write(replInit); |
| 19 | + testRepl.write('\n'); |
| 20 | + |
| 21 | + tests.forEach(([query, expectedCompletions]) => { |
| 22 | + testRepl.complete(query, common.mustCall((error, data) => { |
| 23 | + const actualCompletions = data[0]; |
| 24 | + if (expectedCompletions.length === 0) { |
| 25 | + assert.deepStrictEqual(actualCompletions, []); |
| 26 | + } else { |
| 27 | + expectedCompletions.forEach((expectedCompletion) => |
| 28 | + assert(actualCompletions.includes(expectedCompletion), `completion '${expectedCompletion}' not found`) |
| 29 | + ); |
| 30 | + } |
| 31 | + })); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +describe('REPL completion in relation of getters', () => { |
| 36 | + describe('standard behavior without proxies/getters', () => { |
| 37 | + test('completion of nested properties of an undeclared objects', () => { |
| 38 | + runCompletionTests('', [ |
| 39 | + ['nonExisting.', []], |
| 40 | + ['nonExisting.f', []], |
| 41 | + ['nonExisting.foo', []], |
| 42 | + ['nonExisting.foo.', []], |
| 43 | + ['nonExisting.foo.bar.b', []], |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + test('completion of nested properties on plain objects', () => { |
| 48 | + runCompletionTests('const plainObj = { foo: { bar: { baz: {} } } };', [ |
| 49 | + ['plainObj.', ['plainObj.foo']], |
| 50 | + ['plainObj.f', ['plainObj.foo']], |
| 51 | + ['plainObj.foo', ['plainObj.foo']], |
| 52 | + ['plainObj.foo.', ['plainObj.foo.bar']], |
| 53 | + ['plainObj.foo.bar.b', ['plainObj.foo.bar.baz']], |
| 54 | + ['plainObj.fooBar.', []], |
| 55 | + ['plainObj.fooBar.baz', []], |
| 56 | + ]); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('completions on an object with getters', () => { |
| 61 | + test(`completions are generated for properties that don't trigger getters`, () => { |
| 62 | + runCompletionTests( |
| 63 | + ` |
| 64 | + const objWithGetters = { |
| 65 | + foo: { bar: { baz: {} }, get gBar() { return { baz: {} } } }, |
| 66 | + get gFoo() { return { bar: { baz: {} } }; } |
| 67 | + }; |
| 68 | + `, [ |
| 69 | + ['objWithGetters.', ['objWithGetters.foo']], |
| 70 | + ['objWithGetters.f', ['objWithGetters.foo']], |
| 71 | + ['objWithGetters.foo', ['objWithGetters.foo']], |
| 72 | + ['objWithGetters.foo.', ['objWithGetters.foo.bar']], |
| 73 | + ['objWithGetters.foo.bar.b', ['objWithGetters.foo.bar.baz']], |
| 74 | + ['objWithGetters.gFo', ['objWithGetters.gFoo']], |
| 75 | + ['objWithGetters.foo.gB', ['objWithGetters.foo.gBar']], |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + test('no completions are generated for properties that trigger getters', () => { |
| 80 | + runCompletionTests( |
| 81 | + ` |
| 82 | + const objWithGetters = { |
| 83 | + foo: { bar: { baz: {} }, get gBar() { return { baz: {} } } }, |
| 84 | + get gFoo() { return { bar: { baz: {} } }; } |
| 85 | + }; |
| 86 | + `, |
| 87 | + [ |
| 88 | + ['objWithGetters.gFoo.', []], |
| 89 | + ['objWithGetters.gFoo.b', []], |
| 90 | + ['objWithGetters.gFoo.bar.b', []], |
| 91 | + ['objWithGetters.foo.gBar.', []], |
| 92 | + ['objWithGetters.foo.gBar.b', []], |
| 93 | + ]); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('completions on proxies', () => { |
| 98 | + test('no completions are generated for a proxy object', () => { |
| 99 | + runCompletionTests('const proxyObj = new Proxy({ foo: { bar: { baz: {} } } }, {});', [ |
| 100 | + ['proxyObj.', []], |
| 101 | + ['proxyObj.f', []], |
| 102 | + ['proxyObj.foo', []], |
| 103 | + ['proxyObj.foo.', []], |
| 104 | + ['proxyObj.foo.bar.b', []], |
| 105 | + ]); |
| 106 | + }); |
| 107 | + |
| 108 | + test('no completions are generated for a proxy present in a standard object', () => { |
| 109 | + runCompletionTests( |
| 110 | + 'const objWithProxy = { foo: { bar: new Proxy({ baz: {} }, {}) } };', [ |
| 111 | + ['objWithProxy.', ['objWithProxy.foo']], |
| 112 | + ['objWithProxy.foo', ['objWithProxy.foo']], |
| 113 | + ['objWithProxy.foo.', ['objWithProxy.foo.bar']], |
| 114 | + ['objWithProxy.foo.b', ['objWithProxy.foo.bar']], |
| 115 | + ['objWithProxy.foo.bar.', []], |
| 116 | + ]); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments