diff options
author | Yegappan Lakshmanan <yegappan@yahoo.com> | 2022-08-28 18:52:10 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2022-08-28 18:52:10 +0100 |
commit | 2f892d8663498c21296ad6661dac1bb8372cfd10 (patch) | |
tree | 96b4bed89d8d4237fc46370fa227f086944e514f /src/testdir | |
parent | 75e9a6693e8d0126b5fff42203bfba9428cd19ae (diff) |
patch 9.0.0303: it is not easy to get information about a scriptv9.0.0303
Problem: It is not easy to get information about a script.
Solution: Make getscriptinf() return the version. When selecting a specific
script return functions and variables. (Yegappan Lakshmanan,
closes #10991)
Diffstat (limited to 'src/testdir')
-rw-r--r-- | src/testdir/test_scriptnames.vim | 39 | ||||
-rw-r--r-- | src/testdir/test_vim9_builtin.vim | 40 | ||||
-rw-r--r-- | src/testdir/test_vim9_import.vim | 1 |
3 files changed, 78 insertions, 2 deletions
diff --git a/src/testdir/test_scriptnames.vim b/src/testdir/test_scriptnames.vim index 2d8ee61867..b558af5b4d 100644 --- a/src/testdir/test_scriptnames.vim +++ b/src/testdir/test_scriptnames.vim @@ -32,20 +32,53 @@ endfunc " Test for the getscriptinfo() function func Test_getscriptinfo() let lines =<< trim END + scriptversion 3 let g:loaded_script_id = expand("<SID>") let s:XscriptVar = [1, #{v: 2}] - func s:XscriptFunc() + func s:XgetScriptVar() + return s:XscriptVar endfunc + func s:Xscript_legacy_func1() + endfunc + def s:Xscript_def_func1() + enddef + func Xscript_legacy_func2() + endfunc + def Xscript_def_func2() + enddef END call writefile(lines, 'X22script91') source X22script91 let l = getscriptinfo() call assert_match('X22script91$', l[-1].name) call assert_equal(g:loaded_script_id, $"<SNR>{l[-1].sid}_") + call assert_equal(3, l[-1].version) + call assert_equal(0, has_key(l[-1], 'variables')) + call assert_equal(0, has_key(l[-1], 'functions')) - let l = getscriptinfo({'name': '22script91'}) + " Get script information using script name + let l = getscriptinfo(#{name: '22script91'}) call assert_equal(1, len(l)) call assert_match('22script91$', l[0].name) + let sid = l[0].sid + + " Get script information using script-ID + let l = getscriptinfo({'sid': sid}) + call assert_equal(#{XscriptVar: [1, {'v': 2}]}, l[0].variables) + let funcs = ['Xscript_legacy_func2', + \ $"<SNR>{sid}_Xscript_legacy_func1", + \ $"<SNR>{sid}_Xscript_def_func1", + \ 'Xscript_def_func2', + \ $"<SNR>{sid}_XgetScriptVar"] + for f in funcs + call assert_true(index(l[0].functions, f) != -1) + endfor + + " Verify that a script-local variable cannot be modified using the dict + " returned by getscriptinfo() + let l[0].variables.XscriptVar = ['n'] + let funcname = $"<SNR>{sid}_XgetScriptVar" + call assert_equal([1, {'v': 2}], call(funcname, [])) let l = getscriptinfo({'name': 'foobar'}) call assert_equal(0, len(l)) @@ -58,6 +91,8 @@ func Test_getscriptinfo() call assert_true(len(l) > 1) call assert_fails("echo getscriptinfo('foobar')", 'E1206:') + call assert_fails("echo getscriptinfo({'sid': []})", 'E745:') + call delete('X22script91') endfunc diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim index e3ebc3e587..d68ddce7aa 100644 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -1898,6 +1898,46 @@ enddef def Test_getscriptinfo() v9.CheckDefAndScriptFailure(['getscriptinfo("x")'], ['E1013: Argument 1: type mismatch, expected dict<any> but got string', 'E1206: Dictionary required for argument 1']) + + var lines1 =<< trim END + vim9script + g:loaded_script_id = expand("<SID>") + var XscriptVar = [1, {v: 2}] + func XgetScriptVar() + return XscriptVar + endfunc + func Xscript_legacy_func1() + endfunc + def Xscript_def_func1() + enddef + func g:Xscript_legacy_func2() + endfunc + def g:Xscript_def_func2() + enddef + END + writefile(lines1, 'X22script92') + + var lines2 =<< trim END + source X22script92 + var sid = matchstr(g:loaded_script_id, '<SNR>\zs\d\+\ze_')->str2nr() + + var l = getscriptinfo({sid: sid, name: 'ignored'}) + assert_match('X22script92$', l[0].name) + assert_equal(g:loaded_script_id, $"<SNR>{l[0].sid}_") + assert_equal(999999, l[0].version) + assert_equal(0, l[0].sourced) + assert_equal({XscriptVar: [1, {v: 2}]}, l[0].variables) + var funcs = ['Xscript_legacy_func2', + $"<SNR>{sid}_Xscript_legacy_func1", + $"<SNR>{sid}_Xscript_def_func1", + 'Xscript_def_func2', + $"<SNR>{sid}_XgetScriptVar"] + for f in funcs + assert_true(index(l[0].functions, f) != -1) + endfor + END + v9.CheckDefAndScriptSuccess(lines2) + delete('X22script92') enddef def Test_gettabinfo() diff --git a/src/testdir/test_vim9_import.vim b/src/testdir/test_vim9_import.vim index 832fad8d74..ed4e9d2ffd 100644 --- a/src/testdir/test_vim9_import.vim +++ b/src/testdir/test_vim9_import.vim @@ -741,6 +741,7 @@ def Test_use_relative_autoload_import_in_mapping() assert_true(len(l) == 1) assert_match('XrelautoloadExport.vim$', l[0].name) assert_false(l[0].autoload) + assert_equal(999999, l[0].version) unlet g:result delete('XrelautoloadExport.vim') |