| 🛟 Updated. 28d5985 k33g 18h ago | 1 | package buffer |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestNoSelectionByDefault(t *testing.T) { |
| 6 | b := NewFromString("abc") |
| 7 | |
| 8 | if _, ok := b.Selection(); ok { |
| 9 | t.Error("a fresh buffer must have no selection") |
| 10 | } |
| 11 | if got := b.SelectedText(); got != "" { |
| 12 | t.Errorf("SelectedText() = %q, want empty", got) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestSelectionExtendsFromTheAnchor(t *testing.T) { |
| 17 | b := NewFromString("hello world") |
| 18 | b.SetCursor(Position{Line: 0, Col: 6}) |
| 19 | b.StartSelection() |
| 20 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 11}) |
| 21 | |
| 22 | r, ok := b.Selection() |
| 23 | if !ok { |
| 24 | t.Fatal("Selection() reported none after extending from an anchor") |
| 25 | } |
| 26 | if r.Start != (Position{Line: 0, Col: 6}) || r.End != (Position{Line: 0, Col: 11}) { |
| 27 | t.Errorf("Selection() = %+v, want columns 6 to 11", r) |
| 28 | } |
| 29 | if got := b.SelectedText(); got != "world" { |
| 30 | t.Errorf("SelectedText() = %q, want %q", got, "world") |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestSelectionIsOrderedWhenTheCursorGoesBackwards(t *testing.T) { |
| 35 | b := NewFromString("hello world") |
| 36 | b.SetCursor(Position{Line: 0, Col: 5}) |
| 37 | b.StartSelection() |
| 38 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 0}) |
| 39 | |
| 40 | if got := b.SelectedText(); got != "hello" { |
| 41 | t.Errorf("SelectedText() = %q, want %q even though the cursor moved left", got, "hello") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestSelectionSpanningLinesJoinsWithLineFeeds(t *testing.T) { |
| 46 | b := NewFromString("one\r\ntwo\r\nthree\r\n") |
| 47 | b.SetCursor(Position{Line: 0, Col: 1}) |
| 48 | b.StartSelection() |
| 49 | b.SetCursorKeepingSelection(Position{Line: 2, Col: 2}) |
| 50 | |
| 51 | if got := b.SelectedText(); got != "ne\ntwo\nth" { |
| 52 | t.Errorf("SelectedText() = %q, want line feeds regardless of the file's CRLF endings", got) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestSelectionCollapsedOntoItsAnchorCountsAsAbsent(t *testing.T) { |
| 57 | b := NewFromString("abc") |
| 58 | b.SetCursor(Position{Line: 0, Col: 1}) |
| 59 | b.StartSelection() |
| 60 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 2}) |
| 61 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 1}) |
| 62 | |
| 63 | if _, ok := b.Selection(); ok { |
| 64 | t.Error("a selection reduced to nothing must report as absent") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestClearSelectionDropsTheAnchor(t *testing.T) { |
| 69 | b := NewFromString("abc") |
| 70 | b.StartSelection() |
| 71 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 3}) |
| 72 | |
| 73 | b.ClearSelection() |
| 74 | |
| 75 | if _, ok := b.Selection(); ok { |
| 76 | t.Error("ClearSelection left a selection behind") |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestSetCursorDropsTheSelectionButSetCursorKeepingSelectionDoesNot(t *testing.T) { |
| 81 | b := NewFromString("abcdef") |
| 82 | b.StartSelection() |
| 83 | |
| 84 | b.SetCursorKeepingSelection(Position{Line: 0, Col: 3}) |
| 85 | if _, ok := b.Selection(); !ok { |
| 86 | t.Fatal("SetCursorKeepingSelection must keep the anchor") |
| 87 | } |
| 88 | |
| 89 | b.SetCursor(Position{Line: 0, Col: 5}) |
| 90 | if _, ok := b.Selection(); ok { |
| 91 | t.Error("SetCursor must drop the anchor") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func TestSelectAllCoversTheWholeBuffer(t *testing.T) { |
| 96 | b := NewFromString("one\ntwo") |
| 97 | |
| 98 | b.SelectAll() |
| 99 | |
| 100 | if got := b.SelectedText(); got != "one\ntwo" { |
| 101 | t.Errorf("SelectedText() = %q, want the whole text", got) |
| 102 | } |
| 103 | if got := b.Cursor(); got != (Position{Line: 1, Col: 3}) { |
| 104 | t.Errorf("Cursor() = %+v, want the end of the buffer", got) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestDeleteSelectionRemovesItAndReportsSo(t *testing.T) { |
| 109 | b := NewFromString("one\ntwo\nthree") |
| 110 | b.SetCursor(Position{Line: 0, Col: 1}) |
| 111 | b.StartSelection() |
| 112 | b.SetCursorKeepingSelection(Position{Line: 2, Col: 2}) |
| 113 | |
| 114 | if !b.DeleteSelection() { |
| 115 | t.Fatal("DeleteSelection() = false, want true when something is selected") |
| 116 | } |
| 117 | if got := b.Text(); got != "oree" { |
| 118 | t.Errorf("Text() = %q, want %q", got, "oree") |
| 119 | } |
| 120 | if got := b.Cursor(); got != (Position{Line: 0, Col: 1}) { |
| 121 | t.Errorf("Cursor() = %+v, want the start of the deleted span", got) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestDeleteSelectionWithoutOneChangesNothing(t *testing.T) { |
| 126 | b := NewFromString("abc") |
| 127 | |
| 128 | if b.DeleteSelection() { |
| 129 | t.Error("DeleteSelection() = true, want false when nothing is selected") |
| 130 | } |
| 131 | if got := b.Text(); got != "abc" { |
| 132 | t.Errorf("Text() = %q, want it unchanged", got) |
| 133 | } |
| 134 | if b.Modified() { |
| 135 | t.Error("DeleteSelection with no selection must not mark the buffer modified") |
| 136 | } |
| 137 | } |