github.com/elves/elvish@v0.15.0/pkg/cli/addons/navigation/navigation_test.go (about) 1 package navigation 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/elves/elvish/pkg/cli" 8 . "github.com/elves/elvish/pkg/cli/clitest" 9 "github.com/elves/elvish/pkg/cli/lscolors" 10 "github.com/elves/elvish/pkg/cli/term" 11 "github.com/elves/elvish/pkg/testutil" 12 "github.com/elves/elvish/pkg/ui" 13 ) 14 15 var testDir = testutil.Dir{ 16 "a": "", 17 "d": testutil.Dir{ 18 "d1": "content\td1\nline 2", 19 "d2": testutil.Dir{ 20 "d21": "content d21", 21 "d22": "content d22", 22 "d23.png": "", 23 }, 24 "d3": testutil.Dir{}, 25 ".dh": "hidden", 26 }, 27 "f": "", 28 } 29 30 func TestErrorInAscend(t *testing.T) { 31 f := Setup() 32 defer f.Stop() 33 34 c := getTestCursor() 35 c.ascendErr = errors.New("cannot ascend") 36 Start(f.App, Config{Cursor: c}) 37 38 f.TTY.Inject(term.K(ui.Left)) 39 f.TestTTYNotes(t, "cannot ascend") 40 } 41 42 func TestErrorInDescend(t *testing.T) { 43 f := Setup() 44 defer f.Stop() 45 46 c := getTestCursor() 47 c.descendErr = errors.New("cannot descend") 48 Start(f.App, Config{Cursor: c}) 49 50 f.TTY.Inject(term.K(ui.Down)) 51 f.TTY.Inject(term.K(ui.Right)) 52 f.TestTTYNotes(t, "cannot descend") 53 } 54 55 func TestErrorInCurrent(t *testing.T) { 56 f, cleanup := setup() 57 defer cleanup() 58 defer f.Stop() 59 60 c := getTestCursor() 61 c.currentErr = errors.New("ERR") 62 Start(f.App, Config{Cursor: c}) 63 64 buf := f.MakeBuffer( 65 "", term.DotHere, "\n", 66 " NAVIGATING \n", Styles, 67 "************ ", 68 " a ERR \n", Styles, 69 " !!!", 70 " d \n", Styles, 71 "////", 72 " f ", 73 ) 74 75 f.TTY.TestBuffer(t, buf) 76 77 // Test that Right does nothing. 78 f.TTY.Inject(term.K(ui.Right)) 79 f.TTY.TestBuffer(t, buf) 80 } 81 82 func TestErrorInParent(t *testing.T) { 83 f, cleanup := setup() 84 defer cleanup() 85 defer f.Stop() 86 87 c := getTestCursor() 88 c.parentErr = errors.New("ERR") 89 Start(f.App, Config{Cursor: c}) 90 91 f.TestTTY(t, 92 "", term.DotHere, "\n", 93 " NAVIGATING \n", Styles, 94 "************ ", 95 "ERR d1 content d1\n", Styles, 96 "!!! ++++++++++++++", 97 " d2 line 2\n", Styles, 98 " //////////////", 99 " d3 ", Styles, 100 " //////////////", 101 ) 102 } 103 104 func TestWidthRatio(t *testing.T) { 105 f, cleanup := setup() 106 defer cleanup() 107 defer f.Stop() 108 109 c := getTestCursor() 110 Start(f.App, Config{ 111 Cursor: c, 112 WidthRatio: func() [3]int { return [3]int{1, 1, 1} }, 113 }) 114 115 f.TestTTY(t, 116 "", term.DotHere, "\n", 117 " NAVIGATING \n", Styles, 118 "************ ", 119 " a d1 content d1\n", Styles, 120 " +++++++++++++", 121 " d d2 line 2\n", Styles, 122 "############ /////////////", 123 " f d3 ", Styles, 124 " /////////////", 125 ) 126 } 127 128 func TestGetSelectedName(t *testing.T) { 129 f := Setup() 130 defer f.Stop() 131 132 wantName := "" 133 if name := SelectedName(f.App); name != wantName { 134 t.Errorf("Got name %q, want %q", name, wantName) 135 } 136 137 Start(f.App, Config{Cursor: getTestCursor()}) 138 139 wantName = "d1" 140 if name := SelectedName(f.App); name != wantName { 141 t.Errorf("Got name %q, want %q", name, wantName) 142 } 143 } 144 145 func TestNavigation_FakeFS(t *testing.T) { 146 cursor := getTestCursor() 147 testNavigation(t, cursor) 148 } 149 150 func TestNavigation_RealFS(t *testing.T) { 151 _, cleanupFs := testutil.InTestDir() 152 defer cleanupFs() 153 testutil.ApplyDir(testDir) 154 155 testutil.MustChdir("d") 156 testNavigation(t, nil) 157 } 158 159 func testNavigation(t *testing.T, c Cursor) { 160 f, cleanup := setup() 161 defer cleanup() 162 defer f.Stop() 163 164 Start(f.App, Config{Cursor: c}) 165 166 // Test initial UI and file preview. 167 // NOTE: Buffers are named after the file that is now being selected. 168 d1Buf := f.MakeBuffer( 169 "", term.DotHere, "\n", 170 " NAVIGATING \n", Styles, 171 "************ ", 172 " a d1 content d1\n", Styles, 173 " ++++++++++++++", 174 " d d2 line 2\n", Styles, 175 "#### //////////////", 176 " f d3 ", Styles, 177 " //////////////", 178 ) 179 f.TTY.TestBuffer(t, d1Buf) 180 181 // Test scrolling of preview. 182 ScrollPreview(f.App, 1) 183 d1Buf2 := f.MakeBuffer( 184 "", term.DotHere, "\n", 185 " NAVIGATING \n", Styles, 186 "************ ", 187 " a d1 line 2 │\n", Styles, 188 " ++++++++++++++ -", 189 " d d2 │\n", Styles, 190 "#### ////////////// -", 191 " f d3 \n", Styles, 192 " ////////////// X", 193 " ", Styles, 194 " X", 195 ) 196 f.TTY.TestBuffer(t, d1Buf2) 197 198 // Test handling of selection change and directory preview. Also test 199 // LS_COLORS. 200 Select(f.App, cli.Next) 201 d2Buf := f.MakeBuffer( 202 "", term.DotHere, "\n", 203 " NAVIGATING \n", Styles, 204 "************ ", 205 " a d1 d21 \n", Styles, 206 " ++++++++++++++++++++", 207 " d d2 d22 \n", Styles, 208 "#### ##############", 209 " f d3 d23.png ", Styles, 210 " ////////////// !!!!!!!!!!!!!!!!!!!!", 211 ) 212 f.TTY.TestBuffer(t, d2Buf) 213 214 // Test handling of Descend. 215 Descend(f.App) 216 d21Buf := f.MakeBuffer( 217 "", term.DotHere, "\n", 218 " NAVIGATING \n", Styles, 219 "************ ", 220 " d1 d21 content d21\n", Styles, 221 " ++++++++++++++", 222 " d2 d22 \n", Styles, 223 "####", 224 " d3 d23.png ", Styles, 225 "//// !!!!!!!!!!!!!!", 226 ) 227 f.TTY.TestBuffer(t, d21Buf) 228 229 // Test handling of Ascend, and that the current column selects the 230 // directory we just ascended from, thus reverting to wantBuf1. 231 Ascend(f.App) 232 f.TTY.TestBuffer(t, d2Buf) 233 234 // Test handling of Descend on a regular file, i.e. do nothing. First move 235 // the cursor to d1, which is a regular file. 236 Select(f.App, cli.Prev) 237 f.TTY.TestBuffer(t, d1Buf) 238 // Now descend, and verify that the buffer has not changed. 239 Descend(f.App) 240 f.TTY.TestBuffer(t, d1Buf) 241 242 // Test showing hidden. 243 MutateShowHidden(f.App, func(bool) bool { return true }) 244 f.TestTTY(t, 245 "", term.DotHere, "\n", 246 " NAVIGATING (show hidden) \n", Styles, 247 "************************** ", 248 " a .dh content d1\n", 249 " d d1 line 2\n", Styles, 250 "#### ++++++++++++++", 251 " f d2 \n", Styles, 252 " //////////////", 253 " d3 ", Styles, 254 " //////////////", 255 ) 256 MutateShowHidden(f.App, func(bool) bool { return false }) 257 258 // Test filtering; current column shows d1, d2, d3 before filtering. 259 MutateFiltering(f.App, func(bool) bool { return true }) 260 f.TTY.Inject(term.K('3')) 261 f.TestTTY(t, 262 "\n", 263 " NAVIGATING 3", Styles, 264 "************ ", term.DotHere, "\n", 265 " a d3 \n", Styles, 266 " ##############", 267 " d \n", Styles, 268 "####", 269 " f ", 270 ) 271 MutateFiltering(f.App, func(bool) bool { return false }) 272 273 // Now move into d3, an empty directory. Test that the filter has been 274 // cleared. 275 Select(f.App, cli.Next) 276 Select(f.App, cli.Next) 277 Descend(f.App) 278 d3NoneBuf := f.MakeBuffer( 279 "", term.DotHere, "\n", 280 " NAVIGATING \n", Styles, 281 "************ ", 282 " d1 \n", 283 " d2 \n", Styles, 284 "////", 285 " d3 ", Styles, 286 "####", 287 ) 288 f.TTY.TestBuffer(t, d3NoneBuf) 289 // Test that selecting the previous does nothing in an empty directory. 290 Select(f.App, cli.Prev) 291 f.TTY.TestBuffer(t, d3NoneBuf) 292 // Test that selecting the next does nothing in an empty directory. 293 Select(f.App, cli.Next) 294 f.TTY.TestBuffer(t, d3NoneBuf) 295 // Test that Descend does nothing in an empty directory. 296 Descend(f.App) 297 f.TTY.TestBuffer(t, d3NoneBuf) 298 } 299 300 func setup() (*Fixture, func()) { 301 restore := lscolors.WithTestLsColors() 302 return Setup(WithTTY(func(tty TTYCtrl) { tty.SetSize(6, 40) })), restore 303 } 304 305 func getTestCursor() *testCursor { 306 return &testCursor{root: testDir, pwd: []string{"d"}} 307 }