|
| 1 | +package editor |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_Editor_ProcessKeyDown(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + code string |
| 14 | + key string |
| 15 | + shift bool |
| 16 | + ctrl bool |
| 17 | + selectionStart int |
| 18 | + selectionEnd int |
| 19 | + |
| 20 | + wantCode string |
| 21 | + wantSelectionStart int |
| 22 | + wantSelectionEnd int |
| 23 | + wantPrefentDefault bool |
| 24 | + wantSaveCalls int |
| 25 | + wantEscapeCalls int |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "Tab key with caret", |
| 29 | + code: lines( |
| 30 | + `package main`, |
| 31 | + ``, |
| 32 | + `func main() {`, |
| 33 | + `}`), |
| 34 | + key: "Tab", |
| 35 | + selectionStart: 20, |
| 36 | + selectionEnd: 20, |
| 37 | + |
| 38 | + wantCode: lines( |
| 39 | + `package main`, |
| 40 | + ``, |
| 41 | + `func main() {`, |
| 42 | + ` `, |
| 43 | + `}`), |
| 44 | + wantSelectionStart: 21, |
| 45 | + wantSelectionEnd: 21, |
| 46 | + wantPrefentDefault: true, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + gotCode := tt.code |
| 53 | + cb := &fakeCodeBox{ |
| 54 | + initCode: tt.code, |
| 55 | + initSelectionStart: tt.selectionStart, |
| 56 | + initSelectionEnd: tt.selectionEnd, |
| 57 | + } |
| 58 | + gotPreventDefault := ProcessKeyDown(cb, tt.key, tt.shift, tt.ctrl) |
| 59 | + check(t, "code", gotCode, tt.wantCode) |
| 60 | + check(t, "selectedStart", cb.gotSelectionStart, tt.wantSelectionStart) |
| 61 | + check(t, "selectedEnd", cb.gotSelectionEnd, tt.wantSelectionEnd) |
| 62 | + check(t, "preventDefault", gotPreventDefault, tt.wantPrefentDefault) |
| 63 | + check(t, "saveCalls", cb.getSaveCalls, tt.wantSaveCalls) |
| 64 | + check(t, "escapeCalls", cb.gotEscapeCalls, tt.wantEscapeCalls) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +type fakeCodeBox struct { |
| 70 | + initCode string |
| 71 | + initSelectionStart int |
| 72 | + initSelectionEnd int |
| 73 | + |
| 74 | + gotCode string |
| 75 | + gotSelectionStart int |
| 76 | + gotSelectionEnd int |
| 77 | + getSaveCalls int |
| 78 | + gotEscapeCalls int |
| 79 | +} |
| 80 | + |
| 81 | +var _ CodeBoxWrapper = (*fakeCodeBox)(nil) |
| 82 | + |
| 83 | +func (cb *fakeCodeBox) Code() string { return cb.initCode } |
| 84 | +func (cb *fakeCodeBox) SetCode(code string) { cb.gotCode = code } |
| 85 | +func (cb *fakeCodeBox) EmitSave() { cb.getSaveCalls++ } |
| 86 | +func (cb *fakeCodeBox) EmitEscape() { cb.gotEscapeCalls++ } |
| 87 | + |
| 88 | +func (cb *fakeCodeBox) GetSelection() (int, int) { |
| 89 | + return cb.initSelectionStart, cb.initSelectionEnd |
| 90 | +} |
| 91 | + |
| 92 | +func (cb *fakeCodeBox) SetSelection(start, end int, _ string) { |
| 93 | + cb.gotSelectionStart = start |
| 94 | + cb.gotSelectionEnd = end |
| 95 | +} |
| 96 | + |
| 97 | +func lines(lines ...string) string { |
| 98 | + return strings.Join(lines, "\n") |
| 99 | +} |
| 100 | + |
| 101 | +func check[T comparable](t *testing.T, name string, got, want T) { |
| 102 | + t.Helper() |
| 103 | + |
| 104 | + switch any(want).(type) { |
| 105 | + case string: |
| 106 | + if diff := cmp.Diff(want, got); len(diff) > 0 { |
| 107 | + t.Errorf("%s was not as expected:\n"+ |
| 108 | + " want: %q\n"+ |
| 109 | + " got: %q\n"+ |
| 110 | + " diff:\n%s", name, any(want).(string), any(got).(string), |
| 111 | + strings.ReplaceAll(diff, "\n", "\n ")) |
| 112 | + } |
| 113 | + return |
| 114 | + } |
| 115 | + |
| 116 | + if got != want { |
| 117 | + t.Errorf("%s was not as expected:\n"+ |
| 118 | + " want: %v\n"+ |
| 119 | + " got: %v", name, want, got) |
| 120 | + } |
| 121 | +} |
0 commit comments