fnamemodify_spec.lua (5252B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local clear = n.clear 5 local eq = t.eq 6 local fnamemodify = n.fn.fnamemodify 7 local getcwd = n.fn.getcwd 8 local command = n.command 9 local write_file = t.write_file 10 local is_os = t.is_os 11 12 local function eq_slashconvert(expected, got) 13 eq(t.fix_slashes(expected), t.fix_slashes(got)) 14 end 15 16 describe('fnamemodify()', function() 17 setup(function() 18 write_file('Xtest-fnamemodify.txt', [[foobar]]) 19 end) 20 21 before_each(clear) 22 23 teardown(function() 24 os.remove('Xtest-fnamemodify.txt') 25 end) 26 27 it('handles the root path', function() 28 local root = n.pathroot() 29 eq(root, fnamemodify([[/]], ':p:h')) 30 eq(root, fnamemodify([[/]], ':p')) 31 if is_os('win') then 32 eq(root, fnamemodify([[\]], ':p:h')) 33 eq(root, fnamemodify([[\]], ':p')) 34 command('set shellslash') 35 root = string.sub(root, 1, -2) .. '/' 36 eq(root, fnamemodify([[\]], ':p:h')) 37 eq(root, fnamemodify([[\]], ':p')) 38 eq(root, fnamemodify([[/]], ':p:h')) 39 eq(root, fnamemodify([[/]], ':p')) 40 end 41 end) 42 43 it(':8 works', function() 44 eq('Xtest-fnamemodify.txt', fnamemodify([[Xtest-fnamemodify.txt]], ':8')) 45 end) 46 47 it('handles examples from ":help filename-modifiers"', function() 48 -- src/ cannot be a symlink in this test. 49 n.api.nvim_set_current_dir(t.paths.test_source_path) 50 51 local filename = 'src/version.c' 52 local cwd = getcwd() 53 54 eq_slashconvert(cwd .. '/src/version.c', fnamemodify(filename, ':p')) 55 56 eq_slashconvert('src/version.c', fnamemodify(filename, ':p:.')) 57 eq_slashconvert(cwd .. '/src', fnamemodify(filename, ':p:h')) 58 eq_slashconvert(cwd .. '', fnamemodify(filename, ':p:h:h')) 59 eq('version.c', fnamemodify(filename, ':p:t')) 60 eq_slashconvert(cwd .. '/src/version', fnamemodify(filename, ':p:r')) 61 62 eq_slashconvert(cwd .. '/src/main.c', fnamemodify(filename, ':s?version?main?:p')) 63 64 local converted_cwd = cwd:gsub('/', '\\') 65 eq(converted_cwd .. '\\src\\version.c', fnamemodify(filename, ':p:gs?/?\\\\?')) 66 67 eq('src', fnamemodify(filename, ':h')) 68 eq('version.c', fnamemodify(filename, ':t')) 69 eq_slashconvert('src/version', fnamemodify(filename, ':r')) 70 eq('version', fnamemodify(filename, ':t:r')) 71 eq('c', fnamemodify(filename, ':e')) 72 73 eq_slashconvert('src/main.c', fnamemodify(filename, ':s?version?main?')) 74 end) 75 76 it('handles advanced examples from ":help filename-modifiers"', function() 77 local filename = 'src/version.c.gz' 78 79 eq('gz', fnamemodify(filename, ':e')) 80 eq('c.gz', fnamemodify(filename, ':e:e')) 81 eq('c.gz', fnamemodify(filename, ':e:e:e')) 82 83 eq('c', fnamemodify(filename, ':e:e:r')) 84 85 eq_slashconvert('src/version.c', fnamemodify(filename, ':r')) 86 eq('c', fnamemodify(filename, ':r:e')) 87 88 eq_slashconvert('src/version', fnamemodify(filename, ':r:r')) 89 eq_slashconvert('src/version', fnamemodify(filename, ':r:r:r')) 90 end) 91 92 it('handles :h', function() 93 eq('.', fnamemodify('hello.txt', ':h')) 94 95 eq_slashconvert('path/to', fnamemodify('path/to/hello.txt', ':h')) 96 end) 97 98 it('handles :t', function() 99 eq('hello.txt', fnamemodify('hello.txt', ':t')) 100 eq_slashconvert('hello.txt', fnamemodify('path/to/hello.txt', ':t')) 101 end) 102 103 it('handles :r', function() 104 eq('hello', fnamemodify('hello.txt', ':r')) 105 eq_slashconvert('path/to/hello', fnamemodify('path/to/hello.txt', ':r')) 106 end) 107 108 it('handles :e', function() 109 eq('txt', fnamemodify('hello.txt', ':e')) 110 eq_slashconvert('txt', fnamemodify('path/to/hello.txt', ':e')) 111 end) 112 113 it('handles regex replacements', function() 114 eq('content-there-here.txt', fnamemodify('content-here-here.txt', ':s/here/there/')) 115 eq('content-there-there.txt', fnamemodify('content-here-here.txt', ':gs/here/there/')) 116 end) 117 118 it('handles shell escape', function() 119 local expected 120 121 if is_os('win') then 122 -- we expand with double-quotes on Windows 123 expected = [["hello there! quote ' newline]] .. '\n' .. [["]] 124 else 125 expected = [['hello there! quote '\'' newline]] .. '\n' .. [[']] 126 end 127 128 eq(expected, fnamemodify("hello there! quote ' newline\n", ':S')) 129 end) 130 131 it('can combine :e and :r', function() 132 -- simple, single extension filename 133 eq('c', fnamemodify('a.c', ':e')) 134 eq('c', fnamemodify('a.c', ':e:e')) 135 eq('c', fnamemodify('a.c', ':e:e:r')) 136 eq('c', fnamemodify('a.c', ':e:e:r:r')) 137 138 -- multi extension filename 139 eq('rb', fnamemodify('a.spec.rb', ':e:r')) 140 eq('rb', fnamemodify('a.spec.rb', ':e:r:r')) 141 142 eq('spec', fnamemodify('a.spec.rb', ':e:e:r')) 143 eq('spec', fnamemodify('a.spec.rb', ':e:e:r:r')) 144 145 eq('spec', fnamemodify('a.b.spec.rb', ':e:e:r')) 146 eq('b.spec', fnamemodify('a.b.spec.rb', ':e:e:e:r')) 147 eq('b', fnamemodify('a.b.spec.rb', ':e:e:e:r:r')) 148 149 eq('spec', fnamemodify('a.b.spec.rb', ':r:e')) 150 eq('b', fnamemodify('a.b.spec.rb', ':r:r:e')) 151 152 -- extraneous :e expansions 153 eq('c', fnamemodify('a.b.c.d.e', ':r:r:e')) 154 eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e')) 155 156 -- :e never includes the whole filename, so "a.b":e:e:e --> "b" 157 eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e')) 158 eq('b.c', fnamemodify('a.b.c.d.e', ':r:r:e:e:e:e')) 159 end) 160 end)