blob: 42aa8e01bcd540e5a2b48cee3e9293a16994f595 [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)
42 let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$'
43 call assert_match(expected , expand('<sfile>'))
44 call assert_match(expected , expand('<stack>'))
45
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
56 let g:stack_value = expand('<stack>')
57 END
58 call writefile(lines, 'Xstack')
59 source Xstack
60 call assert_match('\<Xstack$', g:stack_value)
61 call delete('Xstack')
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020062endfunc
63
64func Test_expand_slnum()
Bram Moolenaar90944302020-08-01 20:45:11 +020065 call assert_equal(6, s:slnum)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020066 call assert_equal(2, str2nr(expand('<slnum>')))
67
68 " Line-continuation
69 call assert_equal(
70 \ 5,
71 \ str2nr(expand('<slnum>')))
72
73 " Call in script-local function
74 call assert_equal(1, s:expand_slnum())
75
76 " Call in command
77 command Slnum echo expand('<slnum>')
78 call assert_equal(14, str2nr(trim(execute('Slnum'))))
79 delcommand Slnum
80endfunc
81
Bram Moolenaar17aca702019-05-16 22:24:55 +020082func Test_expand()
83 new
84 call assert_equal("", expand('%:S'))
Bram Moolenaara4208962019-08-24 20:50:19 +020085 call assert_equal('3', '<slnum>'->expand())
Bram Moolenaar17aca702019-05-16 22:24:55 +020086 call assert_equal(['4'], expand('<slnum>', v:false, v:true))
87 " Don't add any line above this, otherwise <slnum> will change.
88 quit
89endfunc
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010090
Bram Moolenaar90944302020-08-01 20:45:11 +020091func s:sid_test()
92 return 'works'
93endfunc
94
95func Test_expand_SID()
96 let sid = expand('<SID>')
97 execute 'let g:sid_result = ' .. sid .. 'sid_test()'
98 call assert_equal('works', g:sid_result)
99endfunc
100
101
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100102" Test for 'wildignore' with expand()
103func Test_expand_wildignore()
104 set wildignore=*.vim
105 call assert_equal('', expand('test_expand_func.vim'))
106 call assert_equal('', expand('test_expand_func.vim', 0))
107 call assert_equal([], expand('test_expand_func.vim', 0, 1))
108 call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1))
109 call assert_equal(['test_expand_func.vim'],
110 \ expand('test_expand_func.vim', 1, 1))
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100111 call assert_fails("call expand('*', [])", 'E745:')
Bram Moolenaarbd7206e2020-03-06 20:36:04 +0100112 set wildignore&
113endfunc
114
115" vim: shiftwidth=2 sts=2 expandtab