blob: 41673e50afd9858ec035877b8001ad5c80af6f7d [file] [log] [blame]
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001" Tests for expand()
2
3let s:sfile = expand('<sfile>')
4let s:slnum = str2nr(expand('<slnum>'))
5let s:sflnum = str2nr(expand('<sflnum>'))
6
7func s:expand_sfile()
8 return expand('<sfile>')
9endfunc
10
11func s:expand_slnum()
12 return str2nr(expand('<slnum>'))
13endfunc
14
15func s:expand_sflnum()
16 return str2nr(expand('<sflnum>'))
17endfunc
18
Bram Moolenaara5d04232020-07-26 15:37:02 +020019" This test depends on the location in the test file, put it first.
20func Test_expand_sflnum()
21 call assert_equal(5, s:sflnum)
22 call assert_equal(22, str2nr(expand('<sflnum>')))
23
24 " Line-continuation
25 call assert_equal(
26 \ 25,
27 \ str2nr(expand('<sflnum>')))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020028
29 " Call in script-local function
Bram Moolenaara5d04232020-07-26 15:37:02 +020030 call assert_equal(16, s:expand_sflnum())
31
32 " Call in command
33 command Flnum echo expand('<sflnum>')
34 call assert_equal(34, str2nr(trim(execute('Flnum'))))
35 delcommand Flnum
36endfunc
37
38func Test_expand_sfile_and_stack()
39 call assert_match('test_expand_func\.vim$', s:sfile)
40 let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$'
41 call assert_match(expected , expand('<sfile>'))
42 call assert_match(expected , expand('<stack>'))
43
44 " Call in script-local function
45 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 +020046
47 " Call in command
48 command Sfile echo expand('<sfile>')
Bram Moolenaara5d04232020-07-26 15:37:02 +020049 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 +020050 delcommand Sfile
Bram Moolenaara5d04232020-07-26 15:37:02 +020051
52 " Use <stack> from sourced script.
53 let lines =<< trim END
54 let g:stack_value = expand('<stack>')
55 END
56 call writefile(lines, 'Xstack')
57 source Xstack
58 call assert_match('\<Xstack$', g:stack_value)
59 call delete('Xstack')
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020060endfunc
61
62func Test_expand_slnum()
63 call assert_equal(4, s:slnum)
64 call assert_equal(2, str2nr(expand('<slnum>')))
65
66 " Line-continuation
67 call assert_equal(
68 \ 5,
69 \ str2nr(expand('<slnum>')))
70
71 " Call in script-local function
72 call assert_equal(1, s:expand_slnum())
73
74 " Call in command
75 command Slnum echo expand('<slnum>')
76 call assert_equal(14, str2nr(trim(execute('Slnum'))))
77 delcommand Slnum
78endfunc
79
Bram Moolenaar17aca702019-05-16 22:24:55 +020080func Test_expand()
81 new
82 call assert_equal("", expand('%:S'))
Bram Moolenaara4208962019-08-24 20:50:19 +020083 call assert_equal('3', '<slnum>'->expand())
Bram Moolenaar17aca702019-05-16 22:24:55 +020084 call assert_equal(['4'], expand('<slnum>', v:false, v:true))
85 " Don't add any line above this, otherwise <slnum> will change.
86 quit
87endfunc
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010088
89" Test for 'wildignore' with expand()
90func Test_expand_wildignore()
91 set wildignore=*.vim
92 call assert_equal('', expand('test_expand_func.vim'))
93 call assert_equal('', expand('test_expand_func.vim', 0))
94 call assert_equal([], expand('test_expand_func.vim', 0, 1))
95 call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1))
96 call assert_equal(['test_expand_func.vim'],
97 \ expand('test_expand_func.vim', 1, 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +010098 call assert_fails("call expand('*', [])", 'E745:')
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010099 set wildignore&
100endfunc
101
102" vim: shiftwidth=2 sts=2 expandtab