0
0
mirror of https://github.com/vim/vim.git synced 2025-10-04 05:25:06 -04:00

patch 8.1.1120: cannot easily get directory entry matches

Problem:    Cannot easily get directory entry matches.
Solution:   Add the readdir() function. (Yasuhiro Matsumoto, closes #2439)
This commit is contained in:
Bram Moolenaar
2019-04-05 22:50:40 +02:00
parent 577fadfc10
commit 543c9b1921
7 changed files with 256 additions and 3 deletions

View File

@@ -1401,3 +1401,33 @@ func Test_platform_name()
call assert_equal(uname =~? 'CYGWIN\|MSYS', has('win32unix'))
endif
endfunc
func Test_readdir()
call mkdir('Xdir')
call writefile([], 'Xdir/foo.txt')
call writefile([], 'Xdir/bar.txt')
call mkdir('Xdir/dir')
" All results
let files = readdir('Xdir')
call assert_equal(['bar.txt', 'dir', 'foo.txt'], sort(files))
" Only results containing "f"
let files = readdir('Xdir', { x -> stridx(x, 'f') !=- 1 })
call assert_equal(['foo.txt'], sort(files))
" Only .txt files
let files = readdir('Xdir', { x -> x =~ '.txt$' })
call assert_equal(['bar.txt', 'foo.txt'], sort(files))
" Only .txt files with string
let files = readdir('Xdir', 'v:val =~ ".txt$"')
call assert_equal(['bar.txt', 'foo.txt'], sort(files))
" Limit to 1 result.
let l = []
let files = readdir('Xdir', {x -> len(add(l, x)) == 2 ? -1 : 1})
call assert_equal(1, len(files))
call delete('Xdir', 'rf')
endfunc