|
| 1 | +import { assert } from "console"; |
| 2 | +import { promises as fs } from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import { DocumentUri, TextDocumentItem } from "vscode-languageserver-types"; |
| 5 | +import { URI } from "vscode-uri"; |
| 6 | +import * as LanguageServer from "./../src/LanguageServer"; |
| 7 | + |
| 8 | +describe("ocamllsp/switchImplIntf", () => { |
| 9 | + let languageServer: LanguageServer.LanguageServer = null; |
| 10 | + |
| 11 | + async function openDocument(documentUri: DocumentUri) { |
| 12 | + languageServer.sendNotification("textDocument/didOpen", { |
| 13 | + textDocument: TextDocumentItem.create(documentUri, "ocaml", 0, ""), |
| 14 | + }); |
| 15 | + } |
| 16 | + |
| 17 | + /* sends request "ocamllsp/switchImplIntf" */ |
| 18 | + async function ocamllspSwitchImplIntf( |
| 19 | + documentUri: DocumentUri, |
| 20 | + ): Promise<Array<DocumentUri>> { |
| 21 | + return languageServer.sendRequest("ocamllsp/switchImplIntf", documentUri); |
| 22 | + } |
| 23 | + |
| 24 | + let testWorkspacePath = path.join(__dirname, "..", "test_files/"); |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + languageServer = await LanguageServer.startAndInitialize(); |
| 28 | + await fs.rmdir(testWorkspacePath, { recursive: true }); |
| 29 | + fs.mkdir(testWorkspacePath); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + await fs.rmdir(testWorkspacePath, { recursive: true }); |
| 34 | + await LanguageServer.exit(languageServer); |
| 35 | + languageServer = null; |
| 36 | + }); |
| 37 | + |
| 38 | + let createPathForFile = (filename: string) => |
| 39 | + path.join(testWorkspacePath, filename); |
| 40 | + |
| 41 | + let createFileAtPath = async (path: string) => |
| 42 | + fs.writeFile(path, "", { flag: "a+" }); |
| 43 | + |
| 44 | + let pathToDocumentUri = (path: string): DocumentUri => |
| 45 | + URI.file(path).toString(); |
| 46 | + |
| 47 | + let [mli, ml, mll, mly, rei, re] = ["mli", "ml", "mll", "mly", "rei", "re"]; |
| 48 | + |
| 49 | + let testRequest = async ( |
| 50 | + requestParam: DocumentUri, |
| 51 | + expectedResponse: DocumentUri[], |
| 52 | + ) => { |
| 53 | + let response = await ocamllspSwitchImplIntf(requestParam); |
| 54 | + expect(response).toEqual(expectedResponse); |
| 55 | + }; |
| 56 | + |
| 57 | + /** |
| 58 | + * For testing 'ocamllsp/switchImplIntf' |
| 59 | + * |
| 60 | + * @param extsForCreation file name extension for files to be created in |
| 61 | + * (test) workspace folder. The first file created (even if only one file |
| 62 | + * is created) is treated as the file a user wants to switch from. |
| 63 | + * @param extExpected file name extensions that are expected to be returned as |
| 64 | + * a reponse to 'ocamllsp/switchImplIntf' |
| 65 | + */ |
| 66 | + let testingPipeline = async ( |
| 67 | + extsForCreation: string[], |
| 68 | + extExpected: string[], |
| 69 | + ) => { |
| 70 | + assert( |
| 71 | + extsForCreation.length > 0, |
| 72 | + "extensions for creation should not be empty", |
| 73 | + ); |
| 74 | + assert( |
| 75 | + extExpected.length > 0, |
| 76 | + "expected response extensions should not be empty", |
| 77 | + ); |
| 78 | + |
| 79 | + let filePathsForCreation = extsForCreation.map((ext) => { |
| 80 | + let filename = "test.".concat(ext); |
| 81 | + return createPathForFile(filename); |
| 82 | + }); |
| 83 | + |
| 84 | + await Promise.all(filePathsForCreation.map(createFileAtPath)); |
| 85 | + |
| 86 | + let filePathToSwitchFrom = filePathsForCreation[0]; |
| 87 | + let fileURIToSwitchFrom = pathToDocumentUri(filePathToSwitchFrom); |
| 88 | + await openDocument(fileURIToSwitchFrom); |
| 89 | + |
| 90 | + let expectedFileURIs = extExpected.map((ext) => { |
| 91 | + let filename = "test.".concat(ext); |
| 92 | + let filePath = createPathForFile(filename); |
| 93 | + return pathToDocumentUri(filePath); |
| 94 | + }); |
| 95 | + |
| 96 | + testRequest(fileURIToSwitchFrom, expectedFileURIs); |
| 97 | + }; |
| 98 | + |
| 99 | + test.each([ |
| 100 | + [[mli], [ml]], |
| 101 | + [[mli, ml], [ml]], |
| 102 | + [[ml], [mli]], |
| 103 | + [[ml, mli], [mli]], |
| 104 | + [ |
| 105 | + [mli, ml, mll], |
| 106 | + [ml, mll], |
| 107 | + ], |
| 108 | + ])("test switches (%s => %s)", testingPipeline); |
| 109 | +}); |
0 commit comments