github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/cli/clitest/fake_tty_test.go (about) 1 package clitest 2 3 import ( 4 "os" 5 "reflect" 6 "testing" 7 8 "github.com/markusbkk/elvish/pkg/cli" 9 "github.com/markusbkk/elvish/pkg/cli/term" 10 ) 11 12 func TestFakeTTY_Setup(t *testing.T) { 13 tty, ttyCtrl := NewFakeTTY() 14 restoreCalled := 0 15 ttyCtrl.SetSetup(func() { restoreCalled++ }, nil) 16 17 restore, err := tty.Setup() 18 if err != nil { 19 t.Errorf("Setup -> error %v, want nil", err) 20 } 21 restore() 22 if restoreCalled != 1 { 23 t.Errorf("Setup did not return restore") 24 } 25 } 26 27 func TestFakeTTY_Size(t *testing.T) { 28 tty, ttyCtrl := NewFakeTTY() 29 ttyCtrl.SetSize(20, 30) 30 h, w := tty.Size() 31 if h != 20 || w != 30 { 32 t.Errorf("Size -> (%v, %v), want (20, 30)", h, w) 33 } 34 } 35 36 func TestFakeTTY_SetRawInput(t *testing.T) { 37 tty, ttyCtrl := NewFakeTTY() 38 tty.SetRawInput(2) 39 if raw := ttyCtrl.RawInput(); raw != 2 { 40 t.Errorf("RawInput() -> %v, want 2", raw) 41 } 42 } 43 44 func TestFakeTTY_Events(t *testing.T) { 45 tty, ttyCtrl := NewFakeTTY() 46 ttyCtrl.Inject(term.K('a'), term.K('b')) 47 if event, err := tty.ReadEvent(); event != term.K('a') || err != nil { 48 t.Errorf("Got (%v, %v), want (%v, nil)", event, err, term.K('a')) 49 } 50 if event := <-ttyCtrl.EventCh(); event != term.K('b') { 51 t.Errorf("Got event %v, want K('b')", event) 52 } 53 } 54 55 func TestFakeTTY_Signals(t *testing.T) { 56 tty, ttyCtrl := NewFakeTTY() 57 signals := tty.NotifySignals() 58 ttyCtrl.InjectSignal(os.Interrupt, os.Kill) 59 signal := <-signals 60 if signal != os.Interrupt { 61 t.Errorf("Got signal %v, want %v", signal, os.Interrupt) 62 } 63 signal = <-signals 64 if signal != os.Kill { 65 t.Errorf("Got signal %v, want %v", signal, os.Kill) 66 } 67 } 68 69 func TestFakeTTY_Buffer(t *testing.T) { 70 bufNotes1 := term.NewBufferBuilder(10).Write("notes 1").Buffer() 71 buf1 := term.NewBufferBuilder(10).Write("buf 1").Buffer() 72 bufNotes2 := term.NewBufferBuilder(10).Write("notes 2").Buffer() 73 buf2 := term.NewBufferBuilder(10).Write("buf 2").Buffer() 74 bufNotes3 := term.NewBufferBuilder(10).Write("notes 3").Buffer() 75 buf3 := term.NewBufferBuilder(10).Write("buf 3").Buffer() 76 77 tty, ttyCtrl := NewFakeTTY() 78 79 if ttyCtrl.LastNotesBuffer() != nil { 80 t.Errorf("LastNotesBuffer -> %v, want nil", ttyCtrl.LastNotesBuffer()) 81 } 82 if ttyCtrl.LastBuffer() != nil { 83 t.Errorf("LastBuffer -> %v, want nil", ttyCtrl.LastBuffer()) 84 } 85 86 tty.UpdateBuffer(bufNotes1, buf1, true) 87 if ttyCtrl.LastNotesBuffer() != bufNotes1 { 88 t.Errorf("LastBuffer -> %v, want %v", ttyCtrl.LastNotesBuffer(), bufNotes1) 89 } 90 if ttyCtrl.LastBuffer() != buf1 { 91 t.Errorf("LastBuffer -> %v, want %v", ttyCtrl.LastBuffer(), buf1) 92 } 93 ttyCtrl.TestBuffer(t, buf1) 94 ttyCtrl.TestNotesBuffer(t, bufNotes1) 95 96 tty.UpdateBuffer(bufNotes2, buf2, true) 97 if ttyCtrl.LastNotesBuffer() != bufNotes2 { 98 t.Errorf("LastBuffer -> %v, want %v", ttyCtrl.LastNotesBuffer(), bufNotes2) 99 } 100 if ttyCtrl.LastBuffer() != buf2 { 101 t.Errorf("LastBuffer -> %v, want %v", ttyCtrl.LastBuffer(), buf2) 102 } 103 ttyCtrl.TestBuffer(t, buf2) 104 ttyCtrl.TestNotesBuffer(t, bufNotes2) 105 106 // Test Test{,Notes}Buffer 107 tty.UpdateBuffer(bufNotes3, buf3, true) 108 ttyCtrl.TestBuffer(t, buf3) 109 ttyCtrl.TestNotesBuffer(t, bufNotes3) 110 // Cannot test the failure branch as that will fail the test 111 112 wantBufs := []*term.Buffer{buf1, buf2, buf3} 113 wantNotesBufs := []*term.Buffer{bufNotes1, bufNotes2, bufNotes3} 114 if !reflect.DeepEqual(ttyCtrl.BufferHistory(), wantBufs) { 115 t.Errorf("BufferHistory did not return {buf1, buf2}") 116 } 117 if !reflect.DeepEqual(ttyCtrl.NotesBufferHistory(), wantNotesBufs) { 118 t.Errorf("NotesBufferHistory did not return {bufNotes1, bufNotes2}") 119 } 120 } 121 122 func TestFakeTTY_ClearScreen(t *testing.T) { 123 fakeTTY, ttyCtrl := NewFakeTTY() 124 for i := 0; i < 5; i++ { 125 if cleared := ttyCtrl.ScreenCleared(); cleared != i { 126 t.Errorf("ScreenCleared -> %v, want %v", cleared, i) 127 } 128 fakeTTY.ClearScreen() 129 } 130 } 131 132 func TestGetTTYCtrl_FakeTTY(t *testing.T) { 133 fakeTTY, ttyCtrl := NewFakeTTY() 134 if got, ok := GetTTYCtrl(fakeTTY); got != ttyCtrl || !ok { 135 t.Errorf("-> %v, %v, want %v, %v", got, ok, ttyCtrl, true) 136 } 137 } 138 139 func TestGetTTYCtrl_RealTTY(t *testing.T) { 140 realTTY := cli.NewTTY(os.Stdin, os.Stderr) 141 if _, ok := GetTTYCtrl(realTTY); ok { 142 t.Errorf("-> _, true, want _, false") 143 } 144 }