robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8634ec3827
master
1 2" Test for the :scriptnames command 3func Test_scriptnames () 4call writefile ([ 'let did_load_script = 123' ], 'Xscripting' , 'D' ) 5source Xscripting 6call assert_equal ( 123 , g: did_load_script ) 7 8let scripts = split ( execute ( 'scriptnames' ), "\n" ) 9let last = scripts [ -1 ] 10call assert_match ( '\<Xscripting\>' , last ) 11let lastnr = substitute ( last , '\D*\(\d\+\):.*' , '\1' , '' ) 12exe 'script ' . lastnr 13call assert_equal ( 'Xscripting' , expand ( '%:t' )) 14 15call assert_fails ( 'script ' . ( lastnr + 1 ), 'E474:' ) 16call assert_fails ( 'script 0' , 'E939:' ) 17 18new 19call setline ( 1 , 'nothing' ) 20call assert_fails ( 'script ' . lastnr , 'E37:' ) 21exe 'script! ' . lastnr 22call assert_equal ( 'Xscripting' , expand ( '%:t' )) 23 24bwipe 25 26let msgs = execute ( 'messages' ) 27scriptnames 28call assert_equal ( msgs , execute ( 'messages' )) 29endfunc 30 31" Test for the getscriptinfo() function 32func Test_getscriptinfo () 33let lines =<< trim END 34" scriptversion 3 35let g:loaded_script_id = expand("<SID>") 36let s:XscriptVar = [1, #{v: 2}] 37func s:XgetScriptVar() 38return s:XscriptVar 39endfunc 40func s:Xscript_legacy_func1() 41endfunc 42" def s:Xscript_def_func1() 43" enddef 44func Xscript_legacy_func2() 45endfunc 46" def Xscript_def_func2() 47" enddef 48END 49call writefile ( lines , 'X22script91' , 'D' ) 50source X22script91 51let l = getscriptinfo () 52call assert_match ( 'X22script91$' , l [ -1 ]. name ) 53call assert_equal ( g: loaded_script_id , $ "<SNR>{l[-1].sid}_") 54" call assert_equal(3, l[-1].version) 55call assert_equal ( 1 , l [ -1 ]. version ) 56call assert_equal ( 0 , has_key ( l [ -1 ], 'variables' )) 57call assert_equal ( 0 , has_key ( l [ -1 ], 'functions' )) 58 59" Get script information using script name 60let l = getscriptinfo (#{ name : '22script91' }) 61call assert_equal ( 1 , len ( l )) 62call assert_match ( '22script91$' , l [ 0 ]. name ) 63let sid = l [ 0 ]. sid 64 65" Get script information using script-ID 66let l = getscriptinfo ({ 'sid' : sid }) 67call assert_equal (#{ XscriptVar : [ 1 , { 'v' : 2 }]}, l [ 0 ]. variables ) 68let funcs = [ 'Xscript_legacy_func2' , 69 \$ "<SNR>{sid}_Xscript_legacy_func1", 70"\ $"<SNR>{sid}_Xscript_def_func1", 71"\ 'Xscript_def_func2', 72 \$ "<SNR>{sid}_XgetScriptVar"] 73for f in funcs 74call assert_true ( index ( l [ 0 ]. functions , f ) != -1 ) 75endfor 76 77" Verify that a script-local variable cannot be modified using the dict 78" returned by getscriptinfo() 79let l [ 0 ]. variables . XscriptVar = [ 'n' ] 80let funcname = $ "<SNR>{sid}_XgetScriptVar" 81call assert_equal ([ 1 , { 'v' : 2 }], call ( funcname , [])) 82 83let l = getscriptinfo ({ 'name' : 'foobar' }) 84call assert_equal ( 0 , len ( l )) 85let l = getscriptinfo ({ 'name' : '' }) 86call assert_true ( len ( l ) > 1 ) 87 88call assert_fails ( "echo getscriptinfo({'name': []})" , 'E730:' ) 89call assert_fails ( "echo getscriptinfo({'name': '\\@'})" , 'E866:' ) 90let l = getscriptinfo ({ 'name' : v: _null_string }) 91call assert_true ( len ( l ) > 1 ) 92call assert_fails ( "echo getscriptinfo('foobar')" , 'E1206:' ) 93 94call assert_fails ( "echo getscriptinfo({'sid': []})" , 'E745:' ) 95call assert_fails ( "echo getscriptinfo({'sid': {}})" , 'E728:' ) 96call assert_fails ( "echo getscriptinfo({'sid': 0})" , 'E475:' ) 97call assert_fails ( "echo getscriptinfo({'sid': -1})" , 'E475:' ) 98call assert_fails ( "echo getscriptinfo({'sid': -999})" , 'E475:' ) 99 100echo getscriptinfo ({ 'sid' : '1' }) 101" call assert_fails("vim9cmd echo getscriptinfo({'sid': '1'})", 'E1030:') 102 103let max_sid = max ( map ( getscriptinfo (), { k , v ->v . sid })) 104call assert_equal ([], getscriptinfo ({ 'sid' : max_sid + 1 })) 105endfunc 106 107" vim: shiftwidth=2 sts=2 expandtab