github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/edit/navigation_test.go (about) 1 package edit 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "src.elv.sh/pkg/cli/lscolors" 8 "src.elv.sh/pkg/testutil" 9 10 "src.elv.sh/pkg/cli/term" 11 "src.elv.sh/pkg/ui" 12 ) 13 14 func TestNavigation(t *testing.T) { 15 f, cleanup := setupNav() 16 defer cleanup() 17 18 feedInput(f.TTYCtrl, "put") 19 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) 20 f.TestTTY(t, 21 filepath.Join("~", "d"), "> ", 22 "put", Styles, 23 "vvv", term.DotHere, "\n", 24 " NAVIGATING \n", Styles, 25 "************ ", 26 " d a \n", Styles, 27 "###### ++++++++++++++++++ ", 28 " e ", Styles, 29 " //////////////////", 30 ) 31 32 // Test $edit:selected-file. 33 evals(f.Evaler, `file = $edit:selected-file`) 34 wantFile := "a" 35 if file, _ := f.Evaler.Global().Index("file"); file != wantFile { 36 t.Errorf("Got $edit:selected-file %q, want %q", file, wantFile) 37 } 38 39 // Test Alt-Enter: inserts filename without quitting. 40 f.TTYCtrl.Inject(term.K(ui.Enter, ui.Alt)) 41 f.TestTTY(t, 42 filepath.Join("~", "d"), "> ", 43 "put a", Styles, 44 "vvv ", term.DotHere, "\n", 45 " NAVIGATING \n", Styles, 46 "************ ", 47 " d a \n", Styles, 48 "###### ++++++++++++++++++ ", 49 " e ", Styles, 50 " //////////////////", 51 ) 52 53 // Test Enter: inserts filename and quits. 54 f.TTYCtrl.Inject(term.K(ui.Enter)) 55 f.TestTTY(t, 56 filepath.Join("~", "d"), "> ", 57 "put a a", Styles, 58 "vvv ", term.DotHere, 59 ) 60 } 61 62 func TestNavigation_WidthRatio(t *testing.T) { 63 f, cleanup := setupNav() 64 defer cleanup() 65 66 evals(f.Evaler, `@edit:navigation:width-ratio = 1 1 1`) 67 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) 68 f.TestTTY(t, 69 filepath.Join("~", "d"), "> ", term.DotHere, "\n", 70 " NAVIGATING \n", Styles, 71 "************ ", 72 " d a \n", Styles, 73 "################ ++++++++++++++++ ", 74 " e ", Styles, 75 " ////////////////", 76 ) 77 } 78 79 // Test corner case: Inserting a selection when the CLI cursor is not at the 80 // start of the edit buffer, but the preceding char is a space, does not 81 // insert another space. 82 func TestNavigation_EnterDoesNotAddSpaceAfterSpace(t *testing.T) { 83 f, cleanup := setupNav() 84 defer cleanup() 85 86 feedInput(f.TTYCtrl, "put ") 87 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) // begin navigation mode 88 f.TTYCtrl.Inject(term.K(ui.Down)) // select "e" 89 f.TTYCtrl.Inject(term.K(ui.Enter)) // insert the "e" file name 90 f.TestTTY(t, 91 filepath.Join("~", "d"), "> ", 92 "put e", Styles, 93 "vvv", term.DotHere, 94 ) 95 } 96 97 // Test corner case: Inserting a selection when the CLI cursor is at the start 98 // of the edit buffer omits the space char prefix. 99 func TestNavigation_EnterDoesNotAddSpaceAtStartOfBuffer(t *testing.T) { 100 f, cleanup := setupNav() 101 defer cleanup() 102 103 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) // begin navigation mode 104 f.TTYCtrl.Inject(term.K(ui.Enter)) // insert the "a" file name 105 f.TestTTY(t, 106 filepath.Join("~", "d"), "> ", 107 "a", Styles, 108 "!", term.DotHere, 109 ) 110 } 111 112 // Test corner case: Inserting a selection when the CLI cursor is at the start 113 // of a line buffer omits the space char prefix. 114 func TestNavigation_EnterDoesNotAddSpaceAtStartOfLine(t *testing.T) { 115 f, cleanup := setupNav() 116 defer cleanup() 117 feedInput(f.TTYCtrl, "put [\n") 118 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) // begin navigation mode 119 f.TTYCtrl.Inject(term.K(ui.Enter)) // insert the "a" file name 120 f.TestTTY(t, 121 filepath.Join("~", "d"), "> ", 122 "put [", Styles, 123 "vvv b", "\n", 124 " a", term.DotHere, 125 ) 126 } 127 128 // Test corner case: Inserting the "selection" in an empty directory inserts 129 // nothing. Regression test for https://b.elv.sh/1169. 130 func TestNavigation_EnterDoesNothingInEmptyDir(t *testing.T) { 131 f, cleanup := setupNav() 132 defer cleanup() 133 134 feedInput(f.TTYCtrl, "pu") 135 f.TTYCtrl.Inject(term.K('N', ui.Ctrl)) // begin navigation mode 136 f.TTYCtrl.Inject(term.K(ui.Down)) // select empty directory "e" 137 f.TTYCtrl.Inject(term.K(ui.Right)) // move into "e" directory 138 f.TTYCtrl.Inject(term.K(ui.Enter, ui.Alt)) // insert nothing since the dir is empty 139 f.TTYCtrl.Inject(term.K('t')) // user presses 'a' 140 f.TestTTY(t, 141 filepath.Join("~", "d", "e"), "> ", 142 "put", Styles, 143 "vvv", term.DotHere, "\n", 144 " NAVIGATING \n", Styles, 145 "************ ", 146 " a \n", Styles, 147 " ", 148 " e ", Styles, 149 "######", 150 ) 151 } 152 153 var testDir = testutil.Dir{ 154 "d": testutil.Dir{ 155 "a": "", 156 "e": testutil.Dir{}, 157 }, 158 } 159 160 func setupNav() (*fixture, func()) { 161 f := setup() 162 restoreLsColors := lscolors.WithTestLsColors() 163 164 testutil.ApplyDir(testDir) 165 testutil.MustChdir("d") 166 167 return f, func() { 168 restoreLsColors() 169 f.Cleanup() 170 } 171 }