From 14636a7a8208dc3213bd197a5461c54c5b1799a2 Mon Sep 17 00:00:00 2001 From: Bo Lopker Date: Fri, 21 Feb 2025 14:33:37 -0800 Subject: [PATCH] Better integration test --- examples/example.py | 123 ++++++++++++++++++++++--- examples/example.txt | 2 +- integration_tests/index.test.ts | 157 +++++++++++++++++++++----------- integration_tests/utils.ts | 54 +++++++++++ 4 files changed, 269 insertions(+), 67 deletions(-) create mode 100644 integration_tests/utils.ts diff --git a/examples/example.py b/examples/example.py index 2ce035c..58d1f5d 100644 --- a/examples/example.py +++ b/examples/example.py @@ -1,20 +1,117 @@ -# Example Pthon fie -def main(): - print("Hello, Wolrd!") +# Impurt statements with misspellings +# Globul constants +MAXSIZ = 100 +NUMBR = 42 -class JSONLD: - def jsonld(self): - return self.bad_spelin() - def bad_spelin(self): - return "Spelling is hard" +# Klass definition with misspellings +class UserAccaunt: + def __init__(selff, usrrname, ballance, intrest_rate): + selff.usrrname = usrrname + selff.ballance = ballance + selff.intrest_rate = intrest_rate + def calculayt_intrest(selff): + return selff.ballance * selff.intrest_rate -multi_line_string = """This is a multi-line string -that spans multiple linest -and is enclosed in triple quotes""" +# Enumm-like dictionary +Colrs = {"REDD": 1, "BLUU": 2, "GREAN": 3, "YELOW": 4} -if __name__ == "__main__": - main() +# Globul variables +globalCountr = 0 +mesage = "Helllo Wolrd!" + + +# Funktion definitions +def memry_allocaton(siz): + try: + return [None] * siz + except MemoryError: + return None + + +def calculatr(numbr1: int, numbr2, operashun): + resalt = 0 + + if operashun == "+": + resalt = numbr1 + numbr2 + elif operashun == "-": + resalt = numbr1 - numbr2 + elif operashun == "*": + resalt = numbr1 * numbr2 + elif operashun == "/": + if numbr2 != 0: + resalt = numbr1 / numbr2 + else: + print("Cannott divid by ziro!") + return -1 + + return resalt + + +# Dekorator with misspellings +def debugg_dekorator(funkshun): + def wrappr(*args, **kwargs): + print(f"Callin {funkshun.__name__}") + return funkshun(*args, **kwargs) + + return wrappr + + +# Main funktion +@debugg_dekorator +def mainee(): + # Listt comprehension with misspellings + numbrs = [x for x in range(MAXSIZ)] + + # Dictionry comprehension + squres = {x: x * x for x in range(10)} + + # Structur-like usage + usrr1 = UserAccaunt(usrrname="JohnDoee", ballance=1000, intrest_rate=2.5) + + # Condishunals and loops + resalt = calculatr(10, 5, "+") + if resalt == 15: + print("Currect anser!\n youu best") + else: + print("Rong anser!") + + # Whiel loop with misspellings + countr = 0 + while countr < 5: + print(f"Iterashun {countr}") + countr += 1 + + # Multi-line string + multiline_txt = """This is a verry long string + that continuez on multiple linez + with lots of speling misstakes""" + + # Generatr expression + evenn_numbrs = (x for x in range(10) if x % 2 == 0) + + # Exeption handling + try: + raise ValueError("Somthing went rong!") + except ValueError as errr: + print(f"Caught an errr: {errr}") + finally: + print("Cleening upp") + + +# Lambda funkshun +quikMath = lambda x: x * NUMBR + + +# Claas inheritance +class AdvancedAccaunt(UserAccaunt): + def __init__(selff, usrrname, ballance, intrest_rate, creditt_limit): + super().__init__(usrrname, ballance, intrest_rate) + selff.creditt_limit = creditt_limit + + +if __name__ == "__maine__": + mainee() diff --git a/examples/example.txt b/examples/example.txt index 038eeb9..9dafe9b 100644 --- a/examples/example.txt +++ b/examples/example.txt @@ -1 +1 @@ -I'm bd at Splellin +nothing diff --git a/integration_tests/index.test.ts b/integration_tests/index.test.ts index f0577f8..efd497d 100644 --- a/integration_tests/index.test.ts +++ b/integration_tests/index.test.ts @@ -1,5 +1,8 @@ import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"; +import fs from "node:fs"; +import path from "node:path"; import { LSPTestClient } from "./client"; +import { getLanguageFromFileName } from "./utils"; // async function makeClient() { // const client = new LSPTestClient("../target/debug/codebook-lsp"); @@ -26,65 +29,113 @@ afterEach(async () => { languageClient.removeAllListeners(); }); -test("should provide diagnostics for text", async (done) => { - // const languageClient = await makeClient(); - languageClient.on("textDocument/publishDiagnostics", (params) => { - console.log("Received diagnostics:", params); - expect(params).toBeDefined(); - expect(false); - done(); - // assert.ok(Array.isArray(completions)); - }); - languageClient.sendNotification("textDocument/didOpen", { - textDocument: { - uri: "file:///test.txt", - languageId: "plaintext", - version: 1, - text: "Hello, Wolrd!", - }, +test("should provide diagnostics for text", async () => { + await new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + reject(new Error("Timeout waiting for diagnostics for text")); + }, 5000); + + try { + languageClient.once("textDocument/publishDiagnostics", (params) => { + try { + console.log("Received diagnostics:", params); + expect(params).toBeDefined(); + expect(params.diagnostics.length).toBeGreaterThan(0); + clearTimeout(timeoutId); + resolve(); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } + }); + + languageClient.sendNotification("textDocument/didOpen", { + textDocument: { + uri: "file:///test.txt", + languageId: "plaintext", + version: 1, + text: "Hello, Wolrd!", + }, + }); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } }); }); -test("should provide diagnostics for code", async (done) => { - // const languageClient = await makeClient(); - languageClient.on("textDocument/publishDiagnostics", (params) => { - console.log("Received diagnostics:", params); - expect(params).toBeDefined(); - expect(false); - done(); - // assert.ok(Array.isArray(completions)); - }); - languageClient.sendNotification("textDocument/didOpen", { - textDocument: { - uri: "file:///test.rs", - languageId: "rust", - version: 1, - text: 'fn main() { println!("Hello, Wolrd!"); }', - }, +test("should provide diagnostics for code", async () => { + await new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + reject(new Error("Timeout waiting for diagnostics for code")); + }, 5000); + + try { + languageClient.once("textDocument/publishDiagnostics", (params) => { + try { + console.log("Received diagnostics:", params); + expect(params).toBeDefined(); + expect(params.diagnostics.length).toBeGreaterThan(0); + clearTimeout(timeoutId); + resolve(); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } + }); + + languageClient.sendNotification("textDocument/didOpen", { + textDocument: { + uri: "file:///test.rs", + languageId: "rust", + version: 1, + text: 'fn main() { println!("Hello, Wolrd!"); }', + }, + }); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } }); }); -test("should only highlight word in code", async (done) => { - // const languageClient = await makeClient(); - languageClient.on("textDocument/publishDiagnostics", (params) => { - console.log("Received diagnostics:", params); - expect(params).toBeDefined(); - expect(false); - done(); - // assert.ok(Array.isArray(completions)); - }); - languageClient.sendNotification("textDocument/didOpen", { - textDocument: { - uri: "file:///example.py", - languageId: "python", - version: 1, - text: `# Example Pthon fie - def main(): - print("Hello, Wolrd!") +test("should only highlight word in code", async () => { + await new Promise((resolve, reject) => { + const timeoutId = setTimeout(() => { + reject(new Error("Timeout waiting for diagnostics for python")); + }, 5000); + + try { + languageClient.once("textDocument/publishDiagnostics", (params) => { + try { + console.log("Received diagnostics:", params); + expect(params).toBeDefined(); + expect(params.diagnostics.length).toBeGreaterThan(0); + clearTimeout(timeoutId); + resolve(); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } + }); + + languageClient.sendNotification("textDocument/didOpen", { + textDocument: { + uri: "file:///example.py", + languageId: "python", + version: 1, + text: `# Example Pthon fie + def main(): + print("Hello, Wolrd!") - if __name__ == "__main__": - main() - `, - }, + if __name__ == "__main__": + main() + `, + }, + }); + } catch (error) { + clearTimeout(timeoutId); + reject(error); + } }); }); diff --git a/integration_tests/utils.ts b/integration_tests/utils.ts new file mode 100644 index 0000000..69a7ece --- /dev/null +++ b/integration_tests/utils.ts @@ -0,0 +1,54 @@ +function getLanguageFromFileName(fileName: string): string { + // Extract the extension from the filename + const extension = fileName.split('.').pop()?.toLowerCase() || ''; + + // Map of file extensions to language IDs + const extensionToLanguage: { [key: string]: string } = { + // Programming Languages + 'ts': 'typescript', + 'js': 'javascript', + 'py': 'python', + 'java': 'java', + 'cpp': 'cpp', + 'c': 'c', + 'cs': 'csharp', + 'rb': 'ruby', + 'php': 'php', + 'go': 'go', + 'rs': 'rust', + 'swift': 'swift', + 'kt': 'kotlin', + + // Web Technologies + 'html': 'html', + 'htm': 'html', + 'css': 'css', + 'scss': 'scss', + 'sass': 'sass', + 'jsx': 'javascriptreact', + 'tsx': 'typescriptreact', + 'vue': 'vue', + + // Data Formats + 'json': 'json', + 'xml': 'xml', + 'yaml': 'yaml', + 'yml': 'yaml', + 'md': 'markdown', + + // Shell Scripts + 'sh': 'shell', + 'bash': 'shell', + 'ps1': 'powershell', + + // Other + 'sql': 'sql', + 'dockerfile': 'dockerfile', + 'txt': 'plaintext' + }; + + // Return the language ID if found, otherwise return 'plaintext' + return extensionToLanguage[extension] || 'plaintext'; +} + +export {getLanguageFromFileName}