blob: c9e802a1e0b142006e243ef3319e8aef58917a83 [file] [log] [blame]
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001" Tests for expand()
2
Bram Moolenaar90944302020-08-01 20:45:11 +02003source shared.vim
4
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005let s:sfile = expand('<sfile>')
6let s:slnum = str2nr(expand('<slnum>'))
7let s:sflnum = str2nr(expand('<sflnum>'))
8
9func s:expand_sfile()
10 return expand('<sfile>')
11endfunc
12
13func s:expand_slnum()
14 return str2nr(expand('<slnum>'))
15endfunc
16
17func s:expand_sflnum()
18 return str2nr(expand('<sflnum>'))
19endfunc
20
Bram Moolenaara5d04232020-07-26 15:37:02 +020021" This test depends on the location in the test file, put it first.
22func Test_expand_sflnum()
Bram Moolenaar90944302020-08-01 20:45:11 +020023 call assert_equal(7, s:sflnum)
24 call assert_equal(24, str2nr(expand('<sflnum>')))
Bram Moolenaara5d04232020-07-26 15:37:02 +020025
26 " Line-continuation
27 call assert_equal(
Bram Moolenaar90944302020-08-01 20:45:11 +020028 \ 27,
Bram Moolenaara5d04232020-07-26 15:37:02 +020029 \ str2nr(expand('<sflnum>')))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020030
31 " Call in script-local function
Bram Moolenaar90944302020-08-01 20:45:11 +020032 call assert_equal(18, s:expand_sflnum())
Bram Moolenaara5d04232020-07-26 15:37:02 +020033
34 " Call in command
35 command Flnum echo expand('<sflnum>')
Bram Moolenaar90944302020-08-01 20:45:11 +020036 call assert_equal(36, str2nr(trim(execute('Flnum'))))
Bram Moolenaara5d04232020-07-26 15:37:02 +020037 delcommand Flnum
38endfunc
39
40func Test_expand_sfile_and_stack()
41 call assert_match('test_expand_func\.vim$', s:sfile)
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +020042 let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack'
43 call assert_match(expected .. '$', expand('<sfile>'))
zeertzjq5a4fff42022-08-15 17:53:55 +010044 call assert_match(expected .. '\[4\]$' , expand('<stack>'))
Bram Moolenaara5d04232020-07-26 15:37:02 +020045
46 " Call in script-local function
47 call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack\[7\]\.\.<SNR>\d\+_expand_sfile$', s:expand_sfile())
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020048
49 " Call in command
50 command Sfile echo expand('<sfile>')
Bram Moolenaara5d04232020-07-26 15:37:02 +020051 call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$', trim(execute('Sfile')))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020052 delcommand Sfile
Bram Moolenaara5d04232020-07-26 15:37:02 +020053
54 " Use <stack> from sourced script.
55 let lines =<< trim END
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +020056 " comment here
Bram Moolenaara5d04232020-07-26 15:37:02 +020057 let g:stack_value = expand('<stack>')
58 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +010059 call writefile(lines, 'Xstack', 'D')
Bram Moolenaara5d04232020-07-26 15:37:02 +020060 source Xstack
Bram Moolenaara810db32020-09-11 17:59:23 +020061 call assert_match('\<Xstack\[2\]$', g:stack_value)
zeertzjq5a4fff42022-08-15 17:53:55 +010062 unlet g:stack_value
zeertzjq5a4fff42022-08-15 17:53:55 +010063
64 if exists('+shellslash')
Bram Moolenaar5c645a22022-09-21 22:00:03 +010065 call mkdir('Xshellslash', 'R')
zeertzjq5a4fff42022-08-15 17:53:55 +010066 let lines =<< trim END
67 let g:stack1 = expand('<stack>')
68 set noshellslash
69 let g:stack2 = expand('<stack>')
70 set shellslash
71 let g:stack3 = expand('<stack>')
72 END
73 call writefile(lines, 'Xshellslash/Xstack')
74 " Test that changing 'shellslash' always affects the result of expand()
75 " when sourcing a script multiple times.
76 for i in range(2)
77 source Xshellslash/Xstack
78 call assert_match('\<Xshellslash/Xstack\[1\]$', g:stack1)
79 call assert_match('\<Xshellslash\\Xstack\[3\]$', g:stack2)
80 call assert_match('\<Xshellslash/Xstack\[5\]$', g:stack3)
81 unlet g:stack1
82 unlet g:stack2
83 unlet g:stack3
84 endfor
zeertzjq5a4fff42022-08-15 17:53:55 +010085 endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020086endfunc
87
88func Test_expand_slnum()
Bram Moolenaar90944302020-08-01 20:45:11 +020089 call assert_equal(6, s:slnum)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020090 call assert_equal(2, str2nr(expand('<slnum>')))
91
92 " Line-continuation
93 call assert_equal(
94 \ 5,
95 \ str2nr(expand('<slnum>')))
96
97 " Call in script-local function
98 call assert_equal(1, s:expand_slnum())
99
100 " Call in command
101 command Slnum echo expand('<slnum>')
102 call assert_equal(14, str2nr(trim(execute('Slnum'))))
103 delcommand Slnum
104endfunc
105
Bram Moolenaar17aca702019-05-16 22:24:55 +0200106func Test_expand()
107 new
Bram Moolenaar211a5bb2022-04-28 19:09:03 +0100108 call assert_equal("", expand('%:S'))
Bram Moolenaara4208962019-08-24 20:50:19 +0200109 call assert_equal('3', '<slnum>'->expand())
Bram Moolenaar17aca702019-05-16 22:24:55 +0200110 call assert_equal(['4'], expand('<slnum>', v:false, v:true))
111 " Don't add any line above this, otherwise <slnum> will change.
Bram Moolenaara96edb72022-04-28 17:52:24 +0100112 call assert_equal("", expand('%'))
113 set verbose=1
114 call assert_equal("", expand('%'))
115 set verbose=0
Bram Moolenaar211a5bb2022-04-28 19:09:03 +0100116 call assert_equal("", expand('%:p'))
Bram Moolenaar17aca702019-05-16 22:24:55 +0200117 quit
118endfunc
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100119
Bram Moolenaar90944302020-08-01 20:45:11 +0200120func s:sid_test()
121 return 'works'
122endfunc
123
124func Test_expand_SID()
125 let sid = expand('<SID>')
126 execute 'let g:sid_result = ' .. sid .. 'sid_test()'
127 call assert_equal('works', g:sid_result)
128endfunc
129
130
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100131" Test for 'wildignore' with expand()
132func Test_expand_wildignore()
133 set wildignore=*.vim
134 call assert_equal('', expand('test_expand_func.vim'))
135 call assert_equal('', expand('test_expand_func.vim', 0))
136 call assert_equal([], expand('test_expand_func.vim', 0, 1))
137 call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1))
138 call assert_equal(['test_expand_func.vim'],
139 \ expand('test_expand_func.vim', 1, 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100140 call assert_fails("call expand('*', [])", 'E745:')
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100141 set wildignore&
142endfunc
143
144" vim: shiftwidth=2 sts=2 expandtab