let_spec.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local clear = n.clear
  5. local command = n.command
  6. local eval = n.eval
  7. local api = n.api
  8. local exec = n.exec
  9. local exec_capture = n.exec_capture
  10. local expect_exit = n.expect_exit
  11. local source = n.source
  12. local testprg = n.testprg
  13. before_each(clear)
  14. describe(':let', function()
  15. it('correctly lists variables with curly-braces', function()
  16. api.nvim_set_var('v', { 0 })
  17. eq('v [0]', exec_capture('let {"v"}'))
  18. end)
  19. it('correctly lists variables with subscript', function()
  20. api.nvim_set_var('v', { 0 })
  21. eq('v[0] #0', exec_capture('let v[0]'))
  22. eq('g:["v"][0] #0', exec_capture('let g:["v"][0]'))
  23. eq('{"g:"}["v"][0] #0', exec_capture('let {"g:"}["v"][0]'))
  24. end)
  25. it(':unlet self-referencing node in a List graph #6070', function()
  26. -- :unlet-ing a self-referencing List must not allow GC on indirectly
  27. -- referenced in-scope Lists. Before #6070 this caused use-after-free.
  28. expect_exit(
  29. 1000,
  30. source,
  31. [=[
  32. let [l1, l2] = [[], []]
  33. echo 'l1:' . id(l1)
  34. echo 'l2:' . id(l2)
  35. echo ''
  36. let [l3, l4] = [[], []]
  37. call add(l4, l4)
  38. call add(l4, l3)
  39. call add(l3, 1)
  40. call add(l2, l2)
  41. call add(l2, l1)
  42. call add(l1, 1)
  43. unlet l2
  44. unlet l4
  45. call garbagecollect(1)
  46. call feedkeys(":\e:echo l1 l3\n:echo 42\n:cq\n", "t")
  47. ]=]
  48. )
  49. end)
  50. it('multibyte env var #8398 #9267', function()
  51. command("let $NVIM_TEST_LET = 'AìaB'")
  52. eq('AìaB', eval('$NVIM_TEST_LET'))
  53. command("let $NVIM_TEST_LET = 'AaあB'")
  54. eq('AaあB', eval('$NVIM_TEST_LET'))
  55. local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
  56. .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
  57. .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
  58. command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
  59. eq(mbyte, eval('$NVIM_TEST_LET'))
  60. end)
  61. it('multibyte env var to child process #8398 #9267', function()
  62. local cmd_get_child_env = ("let g:env_from_child = system(['%s', 'NVIM_TEST_LET'])"):format(
  63. testprg('printenv-test')
  64. )
  65. command("let $NVIM_TEST_LET = 'AìaB'")
  66. command(cmd_get_child_env)
  67. eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
  68. command("let $NVIM_TEST_LET = 'AaあB'")
  69. command(cmd_get_child_env)
  70. eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
  71. local mbyte = [[\p* .ม .ม .ม .ม่ .ม่ .ม่ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ ֹֻ
  72. .ֹֻ .ֹֻ .ֹֻ ֹֻ ֹֻ ֹֻ .ֹֻ .ֹֻ .ֹֻ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹ ֹ ֹ .ֹ .ֹ .ֹ ֹֻ ֹֻ
  73. .ֹֻ .ֹֻ .ֹֻ a a a ca ca ca à à à]]
  74. command("let $NVIM_TEST_LET = '" .. mbyte .. "'")
  75. command(cmd_get_child_env)
  76. eq(eval('$NVIM_TEST_LET'), eval('g:env_from_child'))
  77. end)
  78. it('release of list assigned to l: variable does not trigger assertion #12387, #12430', function()
  79. source([[
  80. func! s:f()
  81. let l:x = [1]
  82. let g:x = l:
  83. endfunc
  84. for _ in range(2)
  85. call s:f()
  86. endfor
  87. call garbagecollect()
  88. call feedkeys('i', 't')
  89. ]])
  90. eq(1, eval('1'))
  91. end)
  92. it('can apply operator to boolean option', function()
  93. eq(true, api.nvim_get_option_value('equalalways', {}))
  94. command('let &equalalways -= 1')
  95. eq(false, api.nvim_get_option_value('equalalways', {}))
  96. command('let &equalalways += 1')
  97. eq(true, api.nvim_get_option_value('equalalways', {}))
  98. command('let &equalalways *= 1')
  99. eq(true, api.nvim_get_option_value('equalalways', {}))
  100. command('let &equalalways /= 1')
  101. eq(true, api.nvim_get_option_value('equalalways', {}))
  102. command('let &equalalways %= 1')
  103. eq(false, api.nvim_get_option_value('equalalways', {}))
  104. end)
  105. end)
  106. describe(':let and :const', function()
  107. it('have the same output when called without arguments', function()
  108. eq(exec_capture('let'), exec_capture('const'))
  109. end)
  110. it('can be used in sandbox', function()
  111. exec([[
  112. func Func()
  113. let l:foo = 'foo'
  114. const l:bar = 'bar'
  115. endfunc
  116. sandbox call Func()
  117. ]])
  118. end)
  119. end)