* Copyright (c) 2025-2026 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs';
import { getLsp, getRealPath, getLspWithUi } from '../utils';
import path from 'path';
describe('getReferencesAtPositionTest', () => {
const moduleName: string = 'getReferencesAtPosition';
const REFERENCES_001 = [
{ fileName: 'getReferencesAtPosition2.ets', start: 635, length: 1 },
{ fileName: 'getReferencesAtPosition2.ets', start: 665, length: 1 },
{ fileName: 'getReferencesAtPosition3.ets', start: 617, length: 1 },
{ fileName: 'getReferencesAtPosition3.ets', start: 667, length: 1 }
];
const REFERENCES_002 = [
{ fileName: 'getReferencesAtPosition5.ets', start: 617, length: 1 },
{ fileName: 'getReferencesAtPosition5.ets', start: 655, length: 1 }
];
const REFERENCES_003 = [
{ fileName: 'getReferencesAtPosition6.ets', start: 695, length: 4 },
{ fileName: 'getReferencesAtPosition6.ets', start: 708, length: 4 }
];
function expectReferences(references: any, expected: { fileName: string; start: number; length: number }) {
references.fileName = path.basename(references.fileName);
expect(references).toMatchObject(expected);
}
describe('No UI Plugins', () => {
const lsp = getLsp(moduleName);
lsp.modifyFilesMap(getRealPath(moduleName, 'getReferencesAtPosition2.ets'), { newDoc: fs.readFileSync(getRealPath(moduleName, 'getReferencesAtPosition2.ets'), 'utf8') });
lsp.modifyFilesMap(getRealPath(moduleName, 'getReferencesAtPosition3.ets'), { newDoc: fs.readFileSync(getRealPath(moduleName, 'getReferencesAtPosition3.ets'), 'utf8') });
lsp.modifyFilesMap(getRealPath(moduleName, 'getReferencesAtPosition4.ets'), { newDoc: fs.readFileSync(getRealPath(moduleName, 'getReferencesAtPosition4.ets'), 'utf8') });
lsp.modifyFilesMap(getRealPath(moduleName, 'getReferencesAtPosition5.ets'), { newDoc: fs.readFileSync(getRealPath(moduleName, 'getReferencesAtPosition5.ets'), 'utf8') });
lsp.modifyFilesMap(getRealPath(moduleName, 'getReferencesAtPosition6.ets'), { newDoc: fs.readFileSync(getRealPath(moduleName, 'getReferencesAtPosition6.ets'), 'utf8') });
test('getReferencesAtPosition_000', () => {
const res = lsp.getReferencesAtPosition(getRealPath(moduleName, 'getReferencesAtPosition1.ets'), 613);
expect(res?.length).toBe(0);
});
test('getReferencesAtPosition_001', () => {
const res = lsp.getReferencesAtPosition(getRealPath(moduleName, 'getReferencesAtPosition2.ets'), 635);
expect(res?.length).toBe(4);
const length = res ? res.length : 0;
for (let i = 0; i < length; i++) {
expectReferences(res ? res[i] : undefined, REFERENCES_001[i]);
}
});
test('getReferencesAtPosition_002', () => {
const res = lsp.getReferencesAtPosition(getRealPath(moduleName, 'getReferencesAtPosition4.ets'), 625);
expect(res?.length).toBe(2);
const length = res ? res.length : 0;
for (let i = 0; i < length; i++) {
expectReferences(res ? res[i] : undefined, REFERENCES_002[i]);
}
});
});
describe('With UI Plugins', () => {
const getUiLsp = (): ReturnType<typeof getLspWithUi> => getLspWithUi(moduleName);
(process.env.SKIP_UI_PLUGINS ? test.skip : test)('getReferencesAtPosition_003', () => {
const res = getUiLsp().getReferencesAtPosition(getRealPath(moduleName, 'getReferencesAtPosition6.ets'), 697);
expect(res?.length).toBe(2);
const length = res ? res.length : 0;
for (let i = 0; i < length; i++) {
expectReferences(res ? res[i] : undefined, REFERENCES_003[i]);
}
});
});
});