Skip to content

Commit e13df1d

Browse files
Added tests (#1813)
* feat: added tests * Update src/lib/commands.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update src/lib/keyBindings.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update src/test/editor.tests.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * format * fix: bugs * fix: write arg * feat: add to command palatte * feat: use a saperate editor instance for every test * format * Update src/test/editor.tests.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update src/test/editor.tests.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update src/test/editor.tests.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * feat:change keybind * revert: ai chnages * format
1 parent 0284b2c commit e13df1d

File tree

6 files changed

+533
-0
lines changed

6 files changed

+533
-0
lines changed

src/ace/commands.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ const commands = [
357357
},
358358
readOnly: true,
359359
},
360+
{
361+
name: "run-tests",
362+
description: "Run Tests",
363+
exec() {
364+
acode.exec("run-tests");
365+
},
366+
readOnly: true,
367+
},
360368
];
361369

362370
export function setCommands(editor) {

src/lib/commands.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import findFile from "palettes/findFile";
2020
import browser from "plugins/browser";
2121
import help from "settings/helpSettings";
2222
import mainSettings from "settings/mainSettings";
23+
import { runAllTests } from "test/tester";
2324
import { getColorRange } from "utils/color/regex";
2425
import helpers from "utils/helpers";
2526
import Url from "utils/Url";
@@ -34,6 +35,9 @@ import appSettings from "./settings";
3435
import showFileInfo from "./showFileInfo";
3536

3637
export default {
38+
async "run-tests"() {
39+
await runAllTests();
40+
},
3741
async "close-all-tabs"() {
3842
let save = false;
3943
const unsavedFiles = editorManager.files.filter(

src/lib/keyBindings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,10 @@ export default {
697697
readOnly: true,
698698
action: "new-terminal",
699699
},
700+
"run-tests": {
701+
description: "Run Tests",
702+
key: "Ctrl-Shift-T",
703+
readOnly: true,
704+
action: "run-tests",
705+
},
700706
};

src/test/editor.tests.js

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import { TestRunner } from "./tester";
2+
3+
export async function runAceEditorTests(writeOutput) {
4+
const runner = new TestRunner("Ace Editor API Tests");
5+
6+
function createEditor() {
7+
const container = document.createElement("div");
8+
container.style.width = "500px";
9+
container.style.height = "300px";
10+
container.style.backgroundColor = "#a02f2f";
11+
document.body.appendChild(container);
12+
13+
const editor = ace.edit(container);
14+
return { editor, container };
15+
}
16+
17+
async function withEditor(test, fn) {
18+
let editor, container;
19+
20+
try {
21+
({ editor, container } = createEditor());
22+
test.assert(editor != null, "Editor instance should be created");
23+
await new Promise((resolve) => setTimeout(resolve, 100));
24+
await fn(editor);
25+
await new Promise((resolve) => setTimeout(resolve, 200));
26+
} finally {
27+
if (editor) editor.destroy();
28+
if (container) container.remove();
29+
}
30+
}
31+
32+
// Test 1: Ace is available
33+
runner.test("Ace is loaded", async (test) => {
34+
test.assert(typeof ace !== "undefined", "Ace should be available globally");
35+
test.assert(
36+
typeof ace.edit === "function",
37+
"ace.edit should be a function",
38+
);
39+
});
40+
41+
// Test 2: Editor creation
42+
runner.test("Editor creation", async (test) => {
43+
const { editor, container } = createEditor();
44+
test.assert(editor != null, "Editor instance should be created");
45+
test.assert(
46+
typeof editor.getSession === "function",
47+
"Editor should expose getSession",
48+
);
49+
editor.destroy();
50+
container.remove();
51+
});
52+
53+
// Test 3: Session access
54+
runner.test("Session access", async (test) => {
55+
await withEditor(test, async (editor) => {
56+
const session = editor.getSession();
57+
test.assert(session != null, "Editor session should exist");
58+
test.assert(
59+
typeof session.getValue === "function",
60+
"Session should expose getValue",
61+
);
62+
});
63+
});
64+
65+
// Test 4: Set and get value
66+
runner.test("Set and get value", async (test) => {
67+
await withEditor(test, async (editor) => {
68+
const text = "Hello Ace Editor";
69+
editor.setValue(text, -1);
70+
test.assertEqual(editor.getValue(), text);
71+
});
72+
});
73+
74+
// Test 5: Cursor movement
75+
runner.test("Cursor movement", async (test) => {
76+
await withEditor(test, async (editor) => {
77+
editor.setValue("line1\nline2\nline3", -1);
78+
editor.moveCursorTo(1, 2);
79+
80+
const pos = editor.getCursorPosition();
81+
test.assertEqual(pos.row, 1);
82+
test.assertEqual(pos.column, 2);
83+
});
84+
});
85+
86+
// Test 6: Selection API
87+
runner.test("Selection handling", async (test) => {
88+
await withEditor(test, async (editor) => {
89+
editor.setValue("abc\ndef", -1);
90+
editor.selectAll();
91+
test.assert(editor.getSelectedText().length > 0);
92+
});
93+
});
94+
95+
// Test 7: Undo manager
96+
runner.test("Undo manager works", async (test) => {
97+
await withEditor(test, async (editor) => {
98+
const session = editor.getSession();
99+
const undoManager = session.getUndoManager();
100+
101+
session.setValue("one");
102+
undoManager.reset();
103+
104+
editor.insert("\ntwo");
105+
editor.undo();
106+
107+
test.assertEqual(editor.getValue(), "one");
108+
});
109+
});
110+
111+
// Test 8: Mode setting
112+
runner.test("Mode setting", async (test) => {
113+
await withEditor(test, async (editor) => {
114+
const session = editor.getSession();
115+
session.setMode("ace/mode/javascript");
116+
117+
const mode = session.getMode();
118+
test.assert(mode && mode.$id === "ace/mode/javascript");
119+
});
120+
});
121+
122+
// Test 9: Theme setting
123+
runner.test("Theme setting", async (test) => {
124+
await withEditor(test, async (editor) => {
125+
editor.setTheme("ace/theme/monokai");
126+
test.assert(editor.getTheme().includes("monokai"));
127+
});
128+
});
129+
130+
// Test 11: Line count
131+
runner.test("Line count", async (test) => {
132+
await withEditor(test, async (editor) => {
133+
editor.setValue("a\nb\nc\nd", -1);
134+
test.assertEqual(editor.session.getLength(), 4);
135+
});
136+
});
137+
138+
// Test 12: Replace text
139+
runner.test("Replace text", async (test) => {
140+
await withEditor(test, async (editor) => {
141+
editor.setValue("hello world", -1);
142+
editor.find("world");
143+
editor.replace("ace");
144+
145+
test.assertEqual(editor.getValue(), "hello ace");
146+
});
147+
});
148+
149+
// Test 13: Search API
150+
runner.test("Search API", async (test) => {
151+
await withEditor(test, async (editor) => {
152+
editor.setValue("foo bar foo", -1);
153+
editor.find("foo");
154+
155+
const range = editor.getSelectionRange();
156+
test.assert(range.start.column === 0);
157+
});
158+
});
159+
160+
// Test 14: Renderer availability
161+
runner.test("Renderer exists", async (test) => {
162+
await withEditor(test, async (editor) => {
163+
const renderer = editor.renderer;
164+
test.assert(renderer != null);
165+
test.assert(typeof renderer.updateFull === "function");
166+
});
167+
});
168+
169+
// Test 15: Editor options
170+
runner.test("Editor options", async (test) => {
171+
await withEditor(test, async (editor) => {
172+
editor.setOption("showPrintMargin", false);
173+
test.assertEqual(editor.getOption("showPrintMargin"), false);
174+
});
175+
});
176+
177+
// Test 16: Scroll API
178+
runner.test("Scroll API", async (test) => {
179+
await withEditor(test, async (editor) => {
180+
editor.setValue(Array(100).fill("line").join("\n"), -1);
181+
editor.scrollToLine(50, true, true, () => {});
182+
183+
const firstVisibleRow = editor.renderer.getFirstVisibleRow();
184+
test.assert(firstVisibleRow >= 0);
185+
});
186+
});
187+
188+
// Test 17: Redo manager
189+
runner.test("Redo manager works", async (test) => {
190+
await withEditor(test, async (editor) => {
191+
const session = editor.getSession();
192+
const undoManager = session.getUndoManager();
193+
194+
session.setValue("one");
195+
undoManager.reset();
196+
197+
session.insert({ row: 0, column: 3 }, "\ntwo");
198+
editor.undo();
199+
editor.redo();
200+
201+
test.assertEqual(editor.getValue(), "one\ntwo");
202+
});
203+
});
204+
205+
// Test 18: Focus and blur
206+
runner.test("Focus and blur", async (test) => {
207+
await withEditor(test, async (editor) => {
208+
editor.focus();
209+
test.assert(editor.isFocused());
210+
211+
editor.blur();
212+
test.assert(!editor.isFocused());
213+
});
214+
});
215+
216+
return await runner.run(writeOutput);
217+
}

src/test/sanity.tests.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { TestRunner } from "./tester";
2+
3+
export async function runSanityTests(writeOutput) {
4+
const runner = new TestRunner("JS (WebView) Sanity Tests");
5+
6+
// Test 1: String operations
7+
runner.test("String concatenation", (test) => {
8+
const result = "Hello" + " " + "World";
9+
test.assertEqual(result, "Hello World", "String concatenation should work");
10+
});
11+
12+
// Test 2: Number operations
13+
runner.test("Basic arithmetic", (test) => {
14+
const sum = 5 + 3;
15+
test.assertEqual(sum, 8, "Addition should work correctly");
16+
});
17+
18+
// Test 3: Array operations
19+
runner.test("Array operations", (test) => {
20+
const arr = [1, 2, 3];
21+
test.assertEqual(arr.length, 3, "Array length should be correct");
22+
test.assert(arr.includes(2), "Array should include 2");
23+
});
24+
25+
// Test 4: Object operations
26+
runner.test("Object operations", (test) => {
27+
const obj = { name: "Test", value: 42 };
28+
test.assertEqual(obj.name, "Test", "Object property should be accessible");
29+
test.assertEqual(obj.value, 42, "Object value should be correct");
30+
});
31+
32+
// Test 5: Function execution
33+
runner.test("Function execution", (test) => {
34+
const add = (a, b) => a + b;
35+
const result = add(10, 20);
36+
test.assertEqual(result, 30, "Function should return correct value");
37+
});
38+
39+
// Test 6: Async function
40+
runner.test("Async function handling", async (test) => {
41+
const asyncFunc = async () => {
42+
return new Promise((resolve) => {
43+
setTimeout(() => resolve("done"), 10);
44+
});
45+
};
46+
47+
const result = await asyncFunc();
48+
test.assertEqual(result, "done", "Async function should work correctly");
49+
});
50+
51+
// Test 7: Error handling
52+
runner.test("Error handling", (test) => {
53+
try {
54+
throw new Error("Test error");
55+
} catch (e) {
56+
test.assert(e instanceof Error, "Should catch Error instances");
57+
}
58+
});
59+
60+
// Test 8: Conditional logic
61+
runner.test("Conditional logic", (test) => {
62+
const value = 10;
63+
test.assert(value > 5, "Condition should be true");
64+
test.assert(!(value < 5), "Negation should work");
65+
});
66+
67+
// Run all tests
68+
return await runner.run(writeOutput);
69+
}

0 commit comments

Comments
 (0)