import { assertEquals } from "@std/assert"; import { TextInput } from "$/client/gui/widgets.ts"; // the text with the selection in [brackets], or a | at the caret when nothing is selected function show(input: TextInput) { const [start, end] = input.selection(); if (start === end) { return input.value.slice(0, start) + "|" + input.value.slice(start); } return input.value.slice(0, start) + "[" + input.value.slice(start, end) + "]" + input.value.slice(end); } Deno.test("typing and pasting replace the selection", () => { const input = new TextInput("hello world"); input.move_caret(6); input.move_caret(11, true); assertEquals(show(input), "hello [world]"); assertEquals(input.selected_text(), "world"); input.insert("there"); assertEquals(show(input), "hello there|"); input.select_all(); assertEquals(show(input), "[hello there]"); input.insert("two\r\nlines\tand a tab"); assertEquals(show(input), "two lines and a tab|"); }); Deno.test("pasting keeps what fits and what's allowed", () => { const input = new TextInput("ab", 5, (char) => /[a-z]/.test(char)); input.insert("c-d-e-f"); assertEquals(show(input), "abcde|"); // nothing fits, so the selection isn't deleted either input.move_caret(0, true); input.insert("123"); assertEquals(show(input), "[abcde]"); }); Deno.test("deleting takes the selection, or back to where it's asked", () => { const input = new TextInput("say hello there"); input.move_caret(4); input.move_caret(10, true); input.delete_to(input.caret - 1); assertEquals(show(input), "say |there"); input.move_caret(input.value.length); input.delete_to(input.word_start(input.caret)); assertEquals(show(input), "say |"); input.delete_to(input.caret - 1); assertEquals(show(input), "say|"); }); Deno.test("word jumps skip spaces and stop at the next word", () => { const input = new TextInput("/give bworld:carrot 5"); assertEquals(input.word_start(22), 21); assertEquals(input.word_start(21), 7); assertEquals(input.word_start(7), 0); assertEquals(input.word_end(0), 7); assertEquals(input.word_end(7), 21); assertEquals(input.word_end(21), 22); });