|
8 | 8 | "strings" |
9 | 9 | "testing" |
10 | 10 |
|
| 11 | + "github.com/itchyny/bed/buffer" |
11 | 12 | "github.com/itchyny/bed/event" |
12 | 13 | "github.com/itchyny/bed/layout" |
13 | 14 | "github.com/itchyny/bed/mode" |
@@ -204,3 +205,109 @@ func TestManagerWincmd(t *testing.T) { |
204 | 205 |
|
205 | 206 | wm.Close() |
206 | 207 | } |
| 208 | + |
| 209 | +func TestManagerCopyCutPaste(t *testing.T) { |
| 210 | + wm := NewManager() |
| 211 | + eventCh, redrawCh, waitCh := make(chan event.Event), make(chan struct{}), make(chan struct{}) |
| 212 | + wm.Init(eventCh, redrawCh) |
| 213 | + f, err := ioutil.TempFile("", "bed-test-manager-copy-cut-paste") |
| 214 | + str := "Hello, world!" |
| 215 | + _, err = f.WriteString(str) |
| 216 | + if err != nil { |
| 217 | + t.Errorf("err should be nil but got %v", err) |
| 218 | + } |
| 219 | + if err := f.Close(); err != nil { |
| 220 | + t.Errorf("err should be nil but got: %v", err) |
| 221 | + } |
| 222 | + defer os.Remove(f.Name()) |
| 223 | + wm.SetSize(110, 20) |
| 224 | + if err := wm.Open(f.Name()); err != nil { |
| 225 | + t.Errorf("err should be nil but got: %v", err) |
| 226 | + } |
| 227 | + _, _, _, _ = wm.State() |
| 228 | + go func() { |
| 229 | + <-redrawCh |
| 230 | + <-redrawCh |
| 231 | + <-redrawCh |
| 232 | + waitCh <- struct{}{} |
| 233 | + ev := <-eventCh |
| 234 | + if ev.Type != event.Copied { |
| 235 | + t.Errorf("event type should be %d but got: %d", event.Copied, ev.Type) |
| 236 | + } |
| 237 | + if ev.Buffer == nil { |
| 238 | + t.Errorf("Buffer should not be nil but got: %#v", ev) |
| 239 | + } |
| 240 | + if ev.Arg != "yanked" { |
| 241 | + t.Errorf("Arg should be %q but got: %q", "yanked", ev.Arg) |
| 242 | + } |
| 243 | + p := make([]byte, 20) |
| 244 | + _, _ = ev.Buffer.ReadAt(p, 0) |
| 245 | + if !strings.HasPrefix(string(p), "lo, worl") { |
| 246 | + t.Errorf("buffer string should be %q but got: %q", "", string(p)) |
| 247 | + } |
| 248 | + waitCh <- struct{}{} |
| 249 | + <-redrawCh |
| 250 | + <-redrawCh |
| 251 | + waitCh <- struct{}{} |
| 252 | + ev = <-eventCh |
| 253 | + if ev.Type != event.Copied { |
| 254 | + t.Errorf("event type should be %d but got: %d", event.Copied, ev.Type) |
| 255 | + } |
| 256 | + if ev.Buffer == nil { |
| 257 | + t.Errorf("Buffer should not be nil but got: %#v", ev) |
| 258 | + } |
| 259 | + if ev.Arg != "deleted" { |
| 260 | + t.Errorf("Arg should be %q but got: %q", "deleted", ev.Arg) |
| 261 | + } |
| 262 | + p = make([]byte, 20) |
| 263 | + _, _ = ev.Buffer.ReadAt(p, 0) |
| 264 | + if !strings.HasPrefix(string(p), "lo, wo") { |
| 265 | + t.Errorf("buffer string should be %q but got: %q", "", string(p)) |
| 266 | + } |
| 267 | + windowStates, _, _, _ := wm.State() |
| 268 | + ws := windowStates[0] |
| 269 | + if ws.Length != int64(7) { |
| 270 | + t.Errorf("Length should be %d but got %d", int64(7), ws.Length) |
| 271 | + } |
| 272 | + expected := "Helrld!" |
| 273 | + if !strings.HasPrefix(string(ws.Bytes), expected) { |
| 274 | + t.Errorf("Bytes should start with %q but got %q", expected, string(ws.Bytes)) |
| 275 | + } |
| 276 | + waitCh <- struct{}{} |
| 277 | + <-redrawCh |
| 278 | + waitCh <- struct{}{} |
| 279 | + ev = <-eventCh |
| 280 | + if ev.Type != event.Pasted { |
| 281 | + t.Errorf("event type should be %d but got: %d", event.Pasted, ev.Type) |
| 282 | + } |
| 283 | + if ev.Count != 18 { |
| 284 | + t.Errorf("Count should be %d but got: %d", 18, ev.Count) |
| 285 | + } |
| 286 | + windowStates, _, _, _ = wm.State() |
| 287 | + ws = windowStates[0] |
| 288 | + if ws.Length != int64(25) { |
| 289 | + t.Errorf("Length should be %d but got %d", int64(25), ws.Length) |
| 290 | + } |
| 291 | + expected = "Hefoobarfoobarfoobarlrld!" |
| 292 | + if !strings.HasPrefix(string(ws.Bytes), expected) { |
| 293 | + t.Errorf("Bytes should start with %q but got %q", expected, string(ws.Bytes)) |
| 294 | + } |
| 295 | + close(waitCh) |
| 296 | + }() |
| 297 | + wm.Emit(event.Event{Type: event.CursorNext, Mode: mode.Normal, Count: 3}) |
| 298 | + wm.Emit(event.Event{Type: event.StartVisual}) |
| 299 | + wm.Emit(event.Event{Type: event.CursorNext, Mode: mode.Visual, Count: 7}) |
| 300 | + <-waitCh |
| 301 | + wm.Emit(event.Event{Type: event.Copy}) |
| 302 | + <-waitCh |
| 303 | + wm.Emit(event.Event{Type: event.StartVisual}) |
| 304 | + wm.Emit(event.Event{Type: event.CursorNext, Mode: mode.Visual, Count: 5}) |
| 305 | + <-waitCh |
| 306 | + wm.Emit(event.Event{Type: event.Cut}) |
| 307 | + <-waitCh |
| 308 | + wm.Emit(event.Event{Type: event.CursorPrev, Mode: mode.Normal, Count: 2}) |
| 309 | + <-waitCh |
| 310 | + wm.Emit(event.Event{Type: event.Paste, Buffer: buffer.NewBuffer(strings.NewReader("foobar")), Count: 3}) |
| 311 | + <-waitCh |
| 312 | + wm.Close() |
| 313 | +} |
0 commit comments