test_scriptnames.vim 3.4 KB

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