|
| 1 | +import {Model} from '../../../../json-crdt/model'; |
| 2 | +import {Peritext} from '../../Peritext'; |
| 3 | +import {Point} from '../../rga/Point'; |
| 4 | +import {Editor} from '../Editor'; |
| 5 | + |
| 6 | +const setup = (insert = (editor: Editor) => editor.insert('Hello world!'), sid?: number) => { |
| 7 | + const model = Model.withLogicalClock(sid); |
| 8 | + model.api.root({ |
| 9 | + text: '', |
| 10 | + slices: [], |
| 11 | + }); |
| 12 | + const peritext = new Peritext(model, model.api.str(['text']).node, model.api.arr(['slices']).node); |
| 13 | + const editor = peritext.editor; |
| 14 | + insert(editor); |
| 15 | + return {model, peritext, editor}; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('.fwdSkipWord()', () => { |
| 19 | + test('can go to the end of a word', () => { |
| 20 | + const {editor} = setup((editor) => editor.insert('Hello world!')); |
| 21 | + editor.cursor.setAt(0); |
| 22 | + const point = editor.fwdSkipWord(editor.cursor.end); |
| 23 | + editor.cursor.end.set(point!); |
| 24 | + expect(editor.cursor.text()).toBe('Hello'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('can skip whitespace between words', () => { |
| 28 | + const {editor} = setup((editor) => editor.insert('Hello world!')); |
| 29 | + editor.cursor.setAt(5); |
| 30 | + const point = editor.fwdSkipWord(editor.cursor.end); |
| 31 | + editor.cursor.end.set(point!); |
| 32 | + expect(editor.cursor.text()).toBe(' world'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('skipping stops before exclamation mark', () => { |
| 36 | + const {editor} = setup((editor) => editor.insert('Hello world!')); |
| 37 | + editor.cursor.setAt(6); |
| 38 | + const point = editor.fwdSkipWord(editor.cursor.end); |
| 39 | + editor.cursor.end.set(point!); |
| 40 | + expect(editor.cursor.text()).toBe('world'); |
| 41 | + }); |
| 42 | + |
| 43 | + test('can skip to the end of string', () => { |
| 44 | + const {editor} = setup((editor) => editor.insert('Hello world!')); |
| 45 | + editor.cursor.setAt(11); |
| 46 | + const point = editor.fwdSkipWord(editor.cursor.end); |
| 47 | + expect(point instanceof Point).toBe(true); |
| 48 | + editor.cursor.end.set(point!); |
| 49 | + expect(editor.cursor.text()).toBe('!'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('can skip various character classes', () => { |
| 53 | + const {editor} = setup((editor) => |
| 54 | + editor.insert("const {editor} = setup(editor => editor.insert('Hello world!'));"), |
| 55 | + ); |
| 56 | + editor.cursor.setAt(0); |
| 57 | + const move = (): string => { |
| 58 | + const point = editor.fwdSkipWord(editor.cursor.end); |
| 59 | + if (point) editor.cursor.end.set(point); |
| 60 | + return editor.cursor.text(); |
| 61 | + }; |
| 62 | + expect(move()).toBe('const'); |
| 63 | + expect(move()).toBe('const {editor'); |
| 64 | + expect(move()).toBe('const {editor} = setup'); |
| 65 | + expect(move()).toBe('const {editor} = setup(editor'); |
| 66 | + expect(move()).toBe('const {editor} = setup(editor => editor'); |
| 67 | + expect(move()).toBe('const {editor} = setup(editor => editor.insert'); |
| 68 | + expect(move()).toBe("const {editor} = setup(editor => editor.insert('Hello"); |
| 69 | + expect(move()).toBe("const {editor} = setup(editor => editor.insert('Hello world"); |
| 70 | + expect(move()).toBe("const {editor} = setup(editor => editor.insert('Hello world!'));"); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('.bwdSkipWord()', () => { |
| 75 | + test('can skip over simple text.', () => { |
| 76 | + const {editor} = setup((editor) => editor.insert('Hello world!\nfoo bar baz')); |
| 77 | + editor.cursor.setAt(editor.txt.str.length()); |
| 78 | + const move = (): string => { |
| 79 | + const point = editor.bwdSkipWord(editor.cursor.start); |
| 80 | + if (point) editor.cursor.start.set(point); |
| 81 | + return editor.cursor.text(); |
| 82 | + }; |
| 83 | + expect(move()).toBe('baz'); |
| 84 | + expect(move()).toBe('bar baz'); |
| 85 | + expect(move()).toBe('foo bar baz'); |
| 86 | + expect(move()).toBe('world!\nfoo bar baz'); |
| 87 | + expect(move()).toBe('Hello world!\nfoo bar baz'); |
| 88 | + }); |
| 89 | + |
| 90 | + test('can skip various character classes', () => { |
| 91 | + const {editor} = setup((editor) => |
| 92 | + editor.insert("const {editor} = setup(editor => editor.insert('Hello world!'));"), |
| 93 | + ); |
| 94 | + editor.cursor.setAt(editor.txt.str.length()); |
| 95 | + const move = (): string => { |
| 96 | + const point = editor.bwdSkipWord(editor.cursor.start); |
| 97 | + if (point) editor.cursor.start.set(point); |
| 98 | + return editor.cursor.text(); |
| 99 | + }; |
| 100 | + expect(move()).toBe("world!'));"); |
| 101 | + expect(move()).toBe("Hello world!'));"); |
| 102 | + expect(move()).toBe("insert('Hello world!'));"); |
| 103 | + expect(move()).toBe("editor.insert('Hello world!'));"); |
| 104 | + expect(move()).toBe("editor => editor.insert('Hello world!'));"); |
| 105 | + expect(move()).toBe("setup(editor => editor.insert('Hello world!'));"); |
| 106 | + expect(move()).toBe("editor} = setup(editor => editor.insert('Hello world!'));"); |
| 107 | + expect(move()).toBe("const {editor} = setup(editor => editor.insert('Hello world!'));"); |
| 108 | + }); |
| 109 | +}); |
0 commit comments