Skip to content

Commit

Permalink
Better integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
blopker committed Feb 21, 2025
1 parent c5c8571 commit 14636a7
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 67 deletions.
123 changes: 110 additions & 13 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion examples/example.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
I'm bd at Splellin
nothing
157 changes: 104 additions & 53 deletions integration_tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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<void>((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<void>((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<void>((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);
}
});
});
54 changes: 54 additions & 0 deletions integration_tests/utils.ts
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 14636a7

Please sign in to comment.