comparison src/testdir/test_vim9_script.vim @ 27057:8f6cab688901 v8.2.4057

patch 8.2.4057: Vim9: not fully implementing the autoload mechanism Commit: https://github.com/vim/vim/commit/160aa86a9d5f4b99437bf48ef16400de33bf2f50 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jan 10 21:29:57 2022 +0000 patch 8.2.4057: Vim9: not fully implementing the autoload mechanism Problem: Vim9: not fully implementing the autoload mechanism. Solution: Allow for exporting a legacy function. Improve test coverage.
author Bram Moolenaar <Bram@vim.org>
date Mon, 10 Jan 2022 22:45:03 +0100
parents 140102677c12
children 6fc63c6a7ee7
comparison
equal deleted inserted replaced
27056:55c2e1ddda32 27057:8f6cab688901
2992 # this was giving an error for setting w:quickfix_title 2992 # this was giving an error for setting w:quickfix_title
2993 copen 2993 copen
2994 quit 2994 quit
2995 enddef 2995 enddef
2996 2996
2997 " test using an auto-loaded function and variable
2998 def Test_vim9_autoload()
2999 var lines =<< trim END
3000 vim9script
3001 def some#gettest(): string
3002 return 'test'
3003 enddef
3004 g:some#name = 'name'
3005 g:some#dict = {key: 'value'}
3006
3007 def some#varargs(a1: string, ...l: list<string>): string
3008 return a1 .. l[0] .. l[1]
3009 enddef
3010 END
3011
3012 mkdir('Xdir/autoload', 'p')
3013 writefile(lines, 'Xdir/autoload/some.vim')
3014 var save_rtp = &rtp
3015 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3016
3017 assert_equal('test', g:some#gettest())
3018 assert_equal('name', g:some#name)
3019 assert_equal('value', g:some#dict.key)
3020 g:some#other = 'other'
3021 assert_equal('other', g:some#other)
3022
3023 assert_equal('abc', some#varargs('a', 'b', 'c'))
3024
3025 # upper case script name works
3026 lines =<< trim END
3027 vim9script
3028 def Other#getOther(): string
3029 return 'other'
3030 enddef
3031 END
3032 writefile(lines, 'Xdir/autoload/Other.vim')
3033 assert_equal('other', g:Other#getOther())
3034
3035 # using "vim9script autoload" prefix is not needed
3036 lines =<< trim END
3037 vim9script autoload
3038 g:prefixed_loaded = 'yes'
3039 export def Gettest(): string
3040 return 'test'
3041 enddef
3042 export var name = 'name'
3043 END
3044 writefile(lines, 'Xdir/autoload/prefixed.vim')
3045
3046 lines =<< trim END
3047 vim9script
3048 import autoload 'prefixed.vim'
3049 assert_false(exists('g:prefixed_loaded'))
3050 assert_equal('test', prefixed.Gettest())
3051 assert_equal('yes', g:prefixed_loaded)
3052 assert_equal('name', prefixed.name)
3053 END
3054 CheckScriptSuccess(lines)
3055
3056 # can also get the items by autoload name
3057 lines =<< trim END
3058 call assert_equal('test', prefixed#Gettest())
3059 call assert_equal('name', prefixed#name)
3060 END
3061 CheckScriptSuccess(lines)
3062
3063 delete('Xdir', 'rf')
3064 &rtp = save_rtp
3065 enddef
3066
3067 " test disassembling an auto-loaded function starting with "debug"
3068 def Test_vim9_autoload_disass()
3069 mkdir('Xdir/autoload', 'p')
3070 var save_rtp = &rtp
3071 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3072
3073 var lines =<< trim END
3074 vim9script
3075 def debugit#test(): string
3076 return 'debug'
3077 enddef
3078 END
3079 writefile(lines, 'Xdir/autoload/debugit.vim')
3080
3081 lines =<< trim END
3082 vim9script
3083 def profileit#test(): string
3084 return 'profile'
3085 enddef
3086 END
3087 writefile(lines, 'Xdir/autoload/profileit.vim')
3088
3089 lines =<< trim END
3090 vim9script
3091 assert_equal('debug', debugit#test())
3092 disass debugit#test
3093 assert_equal('profile', profileit#test())
3094 disass profileit#test
3095 END
3096 CheckScriptSuccess(lines)
3097
3098 delete('Xdir', 'rf')
3099 &rtp = save_rtp
3100 enddef
3101
3102 " test using a vim9script that is auto-loaded from an autocmd
3103 def Test_vim9_aucmd_autoload()
3104 var lines =<< trim END
3105 vim9script
3106 def foo#test()
3107 echomsg getreg('"')
3108 enddef
3109 END
3110
3111 mkdir('Xdir/autoload', 'p')
3112 writefile(lines, 'Xdir/autoload/foo.vim')
3113 var save_rtp = &rtp
3114 exe 'set rtp^=' .. getcwd() .. '/Xdir'
3115 augroup test
3116 autocmd TextYankPost * call foo#test()
3117 augroup END
3118
3119 normal Y
3120
3121 augroup test
3122 autocmd!
3123 augroup END
3124 delete('Xdir', 'rf')
3125 &rtp = save_rtp
3126 enddef
3127
3128 " This was causing a crash because suppress_errthrow wasn't reset.
3129 def Test_vim9_autoload_error()
3130 var lines =<< trim END
3131 vim9script
3132 def crash#func()
3133 try
3134 for x in List()
3135 endfor
3136 catch
3137 endtry
3138 g:ok = true
3139 enddef
3140 fu List()
3141 invalid
3142 endfu
3143 try
3144 alsoinvalid
3145 catch /wontmatch/
3146 endtry
3147 END
3148 call mkdir('Xruntime/autoload', 'p')
3149 call writefile(lines, 'Xruntime/autoload/crash.vim')
3150
3151 # run in a separate Vim to avoid the side effects of assert_fails()
3152 lines =<< trim END
3153 exe 'set rtp^=' .. getcwd() .. '/Xruntime'
3154 call crash#func()
3155 call writefile(['ok'], 'Xdidit')
3156 qall!
3157 END
3158 writefile(lines, 'Xscript')
3159 RunVim([], [], '-S Xscript')
3160 assert_equal(['ok'], readfile('Xdidit'))
3161
3162 delete('Xdidit')
3163 delete('Xscript')
3164 delete('Xruntime', 'rf')
3165
3166 lines =<< trim END
3167 vim9script
3168 var foo#bar = 'asdf'
3169 END
3170 CheckScriptFailure(lines, 'E461: Illegal variable name: foo#bar', 2)
3171 enddef
3172
3173 def Test_script_var_in_autocmd() 2997 def Test_script_var_in_autocmd()
3174 # using a script variable from an autocommand, defined in a :def function in a 2998 # using a script variable from an autocommand, defined in a :def function in a
3175 # legacy Vim script, cannot check the variable type. 2999 # legacy Vim script, cannot check the variable type.
3176 var lines =<< trim END 3000 var lines =<< trim END
3177 let s:counter = 1 3001 let s:counter = 1