vim9.vim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. " Use a different file name for each run.
  2. let s:sequence = 1
  3. func CheckDefSuccess(lines)
  4. return
  5. endfunc
  6. func CheckDefFailure(lines, error, lnum = -3)
  7. return
  8. endfunc
  9. func CheckDefExecFailure(lines, error, lnum = -3)
  10. return
  11. endfunc
  12. func CheckScriptFailure(lines, error, lnum = -3)
  13. if get(a:lines, 0, '') ==# 'vim9script'
  14. return
  15. endif
  16. let cwd = getcwd()
  17. let fname = 'XScriptFailure' .. s:sequence
  18. let s:sequence += 1
  19. call writefile(a:lines, fname)
  20. try
  21. call assert_fails('so ' .. fname, a:error, a:lines, a:lnum)
  22. finally
  23. call chdir(cwd)
  24. call delete(fname)
  25. endtry
  26. endfunc
  27. func CheckScriptSuccess(lines)
  28. if get(a:lines, 0, '') ==# 'vim9script'
  29. return
  30. endif
  31. let cwd = getcwd()
  32. let fname = 'XScriptSuccess' .. s:sequence
  33. let s:sequence += 1
  34. call writefile(a:lines, fname)
  35. try
  36. exe 'so ' .. fname
  37. finally
  38. call chdir(cwd)
  39. call delete(fname)
  40. endtry
  41. endfunc
  42. func CheckDefAndScriptSuccess(lines)
  43. return
  44. endfunc
  45. func CheckDefAndScriptFailure(lines, error, lnum = -3)
  46. return
  47. endfunc
  48. func CheckDefExecAndScriptFailure(lines, error, lnum = -3)
  49. return
  50. endfunc
  51. " Check that "lines" inside a legacy function has no error.
  52. func CheckLegacySuccess(lines)
  53. let cwd = getcwd()
  54. let fname = 'XlegacySuccess' .. s:sequence
  55. let s:sequence += 1
  56. call writefile(['func Func()'] + a:lines + ['endfunc'], fname)
  57. try
  58. exe 'so ' .. fname
  59. call Func()
  60. finally
  61. delfunc! Func
  62. call chdir(cwd)
  63. call delete(fname)
  64. endtry
  65. endfunc
  66. " Check that "lines" inside a legacy function results in the expected error
  67. func CheckLegacyFailure(lines, error)
  68. let cwd = getcwd()
  69. let fname = 'XlegacyFails' .. s:sequence
  70. let s:sequence += 1
  71. call writefile(['func Func()'] + a:lines + ['endfunc', 'call Func()'], fname)
  72. try
  73. call assert_fails('so ' .. fname, a:error)
  74. finally
  75. delfunc! Func
  76. call chdir(cwd)
  77. call delete(fname)
  78. endtry
  79. endfunc
  80. " Execute "lines" in a legacy function, translated as in
  81. " CheckLegacyAndVim9Success()
  82. func CheckTransLegacySuccess(lines)
  83. let legacylines = a:lines->mapnew({_, v ->
  84. \ v->substitute('\<VAR\>', 'let', 'g')
  85. \ ->substitute('\<LET\>', 'let', 'g')
  86. \ ->substitute('\<LSTART\>', '{', 'g')
  87. \ ->substitute('\<LMIDDLE\>', '->', 'g')
  88. \ ->substitute('\<LEND\>', '}', 'g')
  89. \ ->substitute('\<TRUE\>', '1', 'g')
  90. \ ->substitute('\<FALSE\>', '0', 'g')
  91. \ ->substitute('#"', ' "', 'g')
  92. \ })
  93. call CheckLegacySuccess(legacylines)
  94. endfunc
  95. func CheckTransDefSuccess(lines)
  96. return
  97. endfunc
  98. func CheckTransVim9Success(lines)
  99. return
  100. endfunc
  101. " Execute "lines" in a legacy function
  102. " Use 'VAR' for a declaration.
  103. " Use 'LET' for an assignment
  104. " Use ' #"' for a comment
  105. " Use LSTART arg LMIDDLE expr LEND for lambda
  106. " Use 'TRUE' for 1
  107. " Use 'FALSE' for 0
  108. func CheckLegacyAndVim9Success(lines)
  109. call CheckTransLegacySuccess(a:lines)
  110. endfunc
  111. " Execute "lines" in a legacy function
  112. " Use 'VAR' for a declaration.
  113. " Use 'LET' for an assignment
  114. " Use ' #"' for a comment
  115. func CheckLegacyAndVim9Failure(lines, error)
  116. if type(a:error) == type('string')
  117. let legacyError = a:error
  118. else
  119. let legacyError = a:error[0]
  120. endif
  121. let legacylines = a:lines->mapnew({_, v ->
  122. \ v->substitute('\<VAR\>', 'let', 'g')
  123. \ ->substitute('\<LET\>', 'let', 'g')
  124. \ ->substitute('#"', ' "', 'g')
  125. \ })
  126. call CheckLegacyFailure(legacylines, legacyError)
  127. endfunc
  128. " :source a list of "lines" and check whether it fails with "error"
  129. func CheckSourceScriptFailure(lines, error, lnum = -3)
  130. if get(a:lines, 0, '') ==# 'vim9script'
  131. return
  132. endif
  133. let cwd = getcwd()
  134. new
  135. call setline(1, a:lines)
  136. let bnr = bufnr()
  137. try
  138. call assert_fails('source', a:error, a:lines, a:lnum)
  139. finally
  140. call chdir(cwd)
  141. exe $':bw! {bnr}'
  142. endtry
  143. endfunc
  144. " :source a list of "lines" and check whether it fails with the list of
  145. " "errors"
  146. func CheckSourceScriptFailureList(lines, errors, lnum = -3)
  147. if get(a:lines, 0, '') ==# 'vim9script'
  148. return
  149. endif
  150. let cwd = getcwd()
  151. new
  152. let bnr = bufnr()
  153. call setline(1, a:lines)
  154. try
  155. call assert_fails('source', a:errors, a:lines, a:lnum)
  156. finally
  157. call chdir(cwd)
  158. exe $':bw! {bnr}'
  159. endtry
  160. endfunc
  161. " :source a list of "lines" and check whether it succeeds
  162. func CheckSourceScriptSuccess(lines)
  163. if get(a:lines, 0, '') ==# 'vim9script'
  164. return
  165. endif
  166. let cwd = getcwd()
  167. new
  168. let bnr = bufnr()
  169. call setline(1, a:lines)
  170. try
  171. :source
  172. finally
  173. call chdir(cwd)
  174. exe $':bw! {bnr}'
  175. endtry
  176. endfunc
  177. func CheckSourceSuccess(lines)
  178. call CheckSourceScriptSuccess(a:lines)
  179. endfunc
  180. func CheckSourceFailure(lines, error, lnum = -3)
  181. call CheckSourceScriptFailure(a:lines, a:error, a:lnum)
  182. endfunc
  183. func CheckSourceFailureList(lines, errors, lnum = -3)
  184. call CheckSourceScriptFailureList(a:lines, a:errors, a:lnum)
  185. endfunc
  186. func CheckSourceDefSuccess(lines)
  187. return
  188. endfunc
  189. func CheckSourceDefAndScriptSuccess(lines)
  190. return
  191. endfunc
  192. func CheckSourceDefCompileSuccess(lines)
  193. return
  194. endfunc
  195. func CheckSourceDefFailure(lines, error, lnum = -3)
  196. return
  197. endfunc
  198. func CheckSourceDefExecFailure(lines, error, lnum = -3)
  199. return
  200. endfunc
  201. func CheckSourceDefAndScriptFailure(lines, error, lnum = -3)
  202. return
  203. endfunc
  204. func CheckSourceDefExecAndScriptFailure(lines, error, lnum = -3)
  205. return
  206. endfunc