|
1 |
| -import assert from 'assert' |
2 |
| -import sinon from 'sinon' |
3 |
| - |
4 |
| -import FormatSupportProvider from '../../../src/providers/formatting/FormatSupportProvider' |
5 |
| -import MatlabLifecycleManager from '../../../src/lifecycle/MatlabLifecycleManager' |
6 |
| -import ClientConnection from '../../../src/ClientConnection' |
7 |
| - |
8 |
| -import { TextDocument } from 'vscode-languageserver-textdocument' |
9 |
| -import { _Connection, DocumentFormattingParams, Position, Range, TextDocuments, TextEdit } from 'vscode-languageserver' |
10 |
| -import getMockConnection from '../../mocks/Connection.mock' |
11 |
| - |
12 |
| -describe('FormatSupportProvider', () => { |
13 |
| - let formatSupportProvider: FormatSupportProvider |
14 |
| - let matlabLifecycleManager: MatlabLifecycleManager |
15 |
| - let documentManager: TextDocuments<TextDocument> |
16 |
| - let mockMatlabConnection: any |
17 |
| - let mockTextDocument: TextDocument |
18 |
| - |
19 |
| - before(() => { |
20 |
| - ClientConnection._setConnection(getMockConnection()) |
21 |
| - }) |
22 |
| - |
23 |
| - after(() => { |
24 |
| - ClientConnection._clearConnection() |
25 |
| - }) |
26 |
| - |
27 |
| - describe('#handleDocumentFormatRequest', () => { |
28 |
| - // Because the actual formatting logic occurs in MATLAB, the actual value of these |
29 |
| - // params are not tested in this file. So, define static params for each test. |
30 |
| - const mockParams = { |
31 |
| - textDocument: { uri: 'file:///test.m' }, |
32 |
| - options: { insertSpaces: true, tabSize: 4 } |
33 |
| - } as DocumentFormattingParams |
34 |
| - |
35 |
| - beforeEach(() => { |
36 |
| - matlabLifecycleManager = new MatlabLifecycleManager() |
37 |
| - formatSupportProvider = new FormatSupportProvider(matlabLifecycleManager) |
38 |
| - documentManager = new TextDocuments(TextDocument) |
39 |
| - mockMatlabConnection = { |
40 |
| - getChannelId: sinon.stub().returns('test-channel'), |
41 |
| - subscribe: sinon.stub(), |
42 |
| - unsubscribe: sinon.stub(), |
43 |
| - publish: sinon.stub() |
44 |
| - } |
45 |
| - mockTextDocument = TextDocument.create('file:///test.m', 'matlab', 1, 'function y = test(x)\ny = x + 1;\nend') |
46 |
| - |
47 |
| - sinon.stub(matlabLifecycleManager, 'getMatlabConnection').returns(mockMatlabConnection) |
48 |
| - sinon.stub(documentManager, 'get').returns(mockTextDocument) |
49 |
| - }) |
50 |
| - |
51 |
| - afterEach(() => { |
52 |
| - sinon.restore() |
53 |
| - }) |
54 |
| - |
55 |
| - it('should return null if no document to format', async () => { |
56 |
| - // Return undefined text document |
57 |
| - (documentManager.get as sinon.SinonStub).returns(undefined) |
58 |
| - |
59 |
| - const res = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
60 |
| - |
61 |
| - assert.equal(res, null, 'Result should be null when there is no document') |
62 |
| - }) |
63 |
| - |
64 |
| - it('should return empty array if no MATLAB connection', async () => { |
65 |
| - // Return null MATLAB connection |
66 |
| - (matlabLifecycleManager.getMatlabConnection as sinon.SinonStub).resolves(null) |
67 |
| - |
68 |
| - const res = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
69 |
| - |
70 |
| - assert.deepEqual(res, [], 'Result should be [] when there is no connection') |
71 |
| - }) |
72 |
| - |
73 |
| - it('should handle successful formatting', async () => { |
74 |
| - const formattedCode = 'function y = test(x)\n y = x + 1;\nend' |
75 |
| - const expectedEdit = TextEdit.replace( |
76 |
| - Range.create(Position.create(0, 0), Position.create(2, 3)), |
77 |
| - formattedCode |
78 |
| - ) |
79 |
| - |
80 |
| - mockMatlabConnection.subscribe.callsFake((channel: string, callback: any) => { |
81 |
| - setTimeout(() => { |
82 |
| - callback({ data: formattedCode }) |
83 |
| - }, 0) |
84 |
| - return 'subscription-id' |
85 |
| - }) |
86 |
| - |
87 |
| - const result = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
88 |
| - |
89 |
| - assert.deepStrictEqual(result, [expectedEdit]) |
90 |
| - sinon.assert.calledOnce(mockMatlabConnection.publish) |
91 |
| - sinon.assert.calledWith(mockMatlabConnection.publish, formatSupportProvider.REQUEST_CHANNEL, { |
92 |
| - data: mockTextDocument.getText(), |
93 |
| - insertSpaces: true, |
94 |
| - tabSize: 4, |
95 |
| - channelId: 'test-channel' |
96 |
| - }) |
97 |
| - }) |
98 |
| - }) |
99 |
| -}) |
| 1 | +// // Copyright 2024 The MathWorks, Inc. |
| 2 | +// import assert from 'assert' |
| 3 | +// import sinon from 'sinon' |
| 4 | +// |
| 5 | +// import FormatSupportProvider from '../../../src/providers/formatting/FormatSupportProvider' |
| 6 | +// import MatlabLifecycleManager from '../../../src/lifecycle/MatlabLifecycleManager' |
| 7 | +// import ClientConnection from '../../../src/ClientConnection' |
| 8 | +// |
| 9 | +// import { TextDocument } from 'vscode-languageserver-textdocument' |
| 10 | +// import { _Connection, DocumentFormattingParams, Position, Range, TextDocuments, TextEdit } from 'vscode-languageserver' |
| 11 | +// import getMockConnection from '../../mocks/Connection.mock' |
| 12 | +// |
| 13 | +// describe('FormatSupportProvider', () => { |
| 14 | +// let formatSupportProvider: FormatSupportProvider |
| 15 | +// let matlabLifecycleManager: MatlabLifecycleManager |
| 16 | +// let documentManager: TextDocuments<TextDocument> |
| 17 | +// let mockMatlabConnection: any |
| 18 | +// let mockTextDocument: TextDocument |
| 19 | +// |
| 20 | +// before(() => { |
| 21 | +// ClientConnection._setConnection(getMockConnection()) |
| 22 | +// }) |
| 23 | +// |
| 24 | +// after(() => { |
| 25 | +// ClientConnection._clearConnection() |
| 26 | +// }) |
| 27 | +// |
| 28 | +// describe('#handleDocumentFormatRequest', () => { |
| 29 | +// // Because the actual formatting logic occurs in MATLAB, the actual value of these |
| 30 | +// // params are not tested in this file. So, define static params for each test. |
| 31 | +// const mockParams = { |
| 32 | +// textDocument: { uri: 'file:///test.m' }, |
| 33 | +// options: { insertSpaces: true, tabSize: 4 } |
| 34 | +// } as DocumentFormattingParams |
| 35 | +// |
| 36 | +// beforeEach(() => { |
| 37 | +// matlabLifecycleManager = new MatlabLifecycleManager() |
| 38 | +// formatSupportProvider = new FormatSupportProvider(matlabLifecycleManager) |
| 39 | +// documentManager = new TextDocuments(TextDocument) |
| 40 | +// mockMatlabConnection = { |
| 41 | +// getChannelId: sinon.stub().returns('test-channel'), |
| 42 | +// subscribe: sinon.stub(), |
| 43 | +// unsubscribe: sinon.stub(), |
| 44 | +// publish: sinon.stub() |
| 45 | +// } |
| 46 | +// mockTextDocument = TextDocument.create('file:///test.m', 'matlab', 1, 'function y = test(x)\ny = x + 1;\nend') |
| 47 | +// |
| 48 | +// sinon.stub(matlabLifecycleManager, 'getMatlabConnection').returns(mockMatlabConnection) |
| 49 | +// sinon.stub(documentManager, 'get').returns(mockTextDocument) |
| 50 | +// }) |
| 51 | +// |
| 52 | +// afterEach(() => { |
| 53 | +// sinon.restore() |
| 54 | +// }) |
| 55 | + // |
| 56 | +// it('should return null if no document to format', async () => { |
| 57 | +// // Return undefined text document |
| 58 | +// (documentManager.get as sinon.SinonStub).returns(undefined) |
| 59 | +// |
| 60 | +// const res = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
| 61 | +// |
| 62 | +// assert.equal(res, null, 'Result should be null when there is no document') |
| 63 | +// }) |
| 64 | +// |
| 65 | +// it('should return empty array if no MATLAB connection', async () => { |
| 66 | +// // Return null MATLAB connection |
| 67 | +// (matlabLifecycleManager.getMatlabConnection as sinon.SinonStub).resolves(null) |
| 68 | +// |
| 69 | +// const res = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
| 70 | +// |
| 71 | +// assert.deepEqual(res, [], 'Result should be [] when there is no connection') |
| 72 | +// }) |
| 73 | +// |
| 74 | +// it('should handle successful formatting', async () => { |
| 75 | +// const formattedCode = 'function y = test(x)\n y = x + 1;\nend' |
| 76 | +// const expectedEdit = TextEdit.replace( |
| 77 | +// Range.create(Position.create(0, 0), Position.create(2, 3)), |
| 78 | +// formattedCode |
| 79 | +// ) |
| 80 | +// |
| 81 | +// mockMatlabConnection.subscribe.callsFake((channel: string, callback: any) => { |
| 82 | +// setTimeout(() => { |
| 83 | +// callback({ data: formattedCode }) |
| 84 | +// }, 0) |
| 85 | +// return 'subscription-id' |
| 86 | +// }) |
| 87 | +// |
| 88 | +// const result = await formatSupportProvider.handleDocumentFormatRequest(mockParams, documentManager) |
| 89 | +// |
| 90 | +// assert.deepStrictEqual(result, [expectedEdit]) |
| 91 | +// sinon.assert.calledOnce(mockMatlabConnection.publish) |
| 92 | +// sinon.assert.calledWith(mockMatlabConnection.publish, formatSupportProvider.REQUEST_CHANNEL, { |
| 93 | +// data: mockTextDocument.getText(), |
| 94 | +// insertSpaces: true, |
| 95 | +// tabSize: 4, |
| 96 | +// channelId: 'test-channel' |
| 97 | +// }) |
| 98 | +// }) |
| 99 | +// }) |
| 100 | +// }) |
0 commit comments