special_vars_spec.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local exc_exec = n.exc_exec
  4. local command = n.command
  5. local fn = n.fn
  6. local clear = n.clear
  7. local eval = n.eval
  8. local eq = t.eq
  9. local api = n.api
  10. local NIL = vim.NIL
  11. describe('Special values', function()
  12. before_each(clear)
  13. it('do not cause error when freed', function()
  14. command([[
  15. function Test()
  16. try
  17. return v:true
  18. finally
  19. return 'something else'
  20. endtry
  21. endfunction
  22. ]])
  23. eq(0, exc_exec('call Test()'))
  24. end)
  25. it('work with empty()', function()
  26. eq(0, fn.empty(true))
  27. eq(1, fn.empty(false))
  28. eq(1, fn.empty(NIL))
  29. end)
  30. it('can be stringified and eval’ed back', function()
  31. eq(true, fn.eval(fn.string(true)))
  32. eq(false, fn.eval(fn.string(false)))
  33. eq(NIL, fn.eval(fn.string(NIL)))
  34. end)
  35. it('work with is/isnot properly', function()
  36. eq(1, eval('v:null is v:null'))
  37. eq(0, eval('v:null is v:true'))
  38. eq(0, eval('v:null is v:false'))
  39. eq(1, eval('v:true is v:true'))
  40. eq(0, eval('v:true is v:false'))
  41. eq(1, eval('v:false is v:false'))
  42. eq(0, eval('v:null is 0'))
  43. eq(0, eval('v:true is 0'))
  44. eq(0, eval('v:false is 0'))
  45. eq(0, eval('v:null is 1'))
  46. eq(0, eval('v:true is 1'))
  47. eq(0, eval('v:false is 1'))
  48. eq(0, eval('v:null is ""'))
  49. eq(0, eval('v:true is ""'))
  50. eq(0, eval('v:false is ""'))
  51. eq(0, eval('v:null is "null"'))
  52. eq(0, eval('v:true is "true"'))
  53. eq(0, eval('v:false is "false"'))
  54. eq(0, eval('v:null is []'))
  55. eq(0, eval('v:true is []'))
  56. eq(0, eval('v:false is []'))
  57. eq(0, eval('v:null isnot v:null'))
  58. eq(1, eval('v:null isnot v:true'))
  59. eq(1, eval('v:null isnot v:false'))
  60. eq(0, eval('v:true isnot v:true'))
  61. eq(1, eval('v:true isnot v:false'))
  62. eq(0, eval('v:false isnot v:false'))
  63. eq(1, eval('v:null isnot 0'))
  64. eq(1, eval('v:true isnot 0'))
  65. eq(1, eval('v:false isnot 0'))
  66. eq(1, eval('v:null isnot 1'))
  67. eq(1, eval('v:true isnot 1'))
  68. eq(1, eval('v:false isnot 1'))
  69. eq(1, eval('v:null isnot ""'))
  70. eq(1, eval('v:true isnot ""'))
  71. eq(1, eval('v:false isnot ""'))
  72. eq(1, eval('v:null isnot "null"'))
  73. eq(1, eval('v:true isnot "true"'))
  74. eq(1, eval('v:false isnot "false"'))
  75. eq(1, eval('v:null isnot []'))
  76. eq(1, eval('v:true isnot []'))
  77. eq(1, eval('v:false isnot []'))
  78. end)
  79. it('work with +/-/* properly', function()
  80. eq(1, eval('0 + v:true'))
  81. eq(0, eval('0 + v:null'))
  82. eq(0, eval('0 + v:false'))
  83. eq(-1, eval('0 - v:true'))
  84. eq(0, eval('0 - v:null'))
  85. eq(0, eval('0 - v:false'))
  86. eq(1, eval('1 * v:true'))
  87. eq(0, eval('1 * v:null'))
  88. eq(0, eval('1 * v:false'))
  89. end)
  90. it('does not work with +=/-=/.=', function()
  91. api.nvim_set_var('true', true)
  92. api.nvim_set_var('false', false)
  93. command('let null = v:null')
  94. eq('Vim(let):E734: Wrong variable type for +=', exc_exec('let true += 1'))
  95. eq('Vim(let):E734: Wrong variable type for +=', exc_exec('let false += 1'))
  96. eq('Vim(let):E734: Wrong variable type for +=', exc_exec('let null += 1'))
  97. eq('Vim(let):E734: Wrong variable type for -=', exc_exec('let true -= 1'))
  98. eq('Vim(let):E734: Wrong variable type for -=', exc_exec('let false -= 1'))
  99. eq('Vim(let):E734: Wrong variable type for -=', exc_exec('let null -= 1'))
  100. eq('Vim(let):E734: Wrong variable type for .=', exc_exec('let true .= 1'))
  101. eq('Vim(let):E734: Wrong variable type for .=', exc_exec('let false .= 1'))
  102. eq('Vim(let):E734: Wrong variable type for .=', exc_exec('let null .= 1'))
  103. end)
  104. it('work with . (concat) properly', function()
  105. eq('v:true', eval('"" . v:true'))
  106. eq('v:null', eval('"" . v:null'))
  107. eq('v:false', eval('"" . v:false'))
  108. end)
  109. it('work with ?? (falsy operator)', function()
  110. eq(true, eval('v:true ?? 42'))
  111. eq(42, eval('v:false ?? 42'))
  112. eq(42, eval('v:null ?? 42'))
  113. end)
  114. it('work with type()', function()
  115. eq(6, fn.type(true))
  116. eq(6, fn.type(false))
  117. eq(7, fn.type(NIL))
  118. end)
  119. it('work with copy() and deepcopy()', function()
  120. eq(true, fn.deepcopy(true))
  121. eq(false, fn.deepcopy(false))
  122. eq(NIL, fn.deepcopy(NIL))
  123. eq(true, fn.copy(true))
  124. eq(false, fn.copy(false))
  125. eq(NIL, fn.copy(NIL))
  126. end)
  127. it('fails in index', function()
  128. eq('Vim(echo):E909: Cannot index a special variable', exc_exec('echo v:true[0]'))
  129. eq('Vim(echo):E909: Cannot index a special variable', exc_exec('echo v:false[0]'))
  130. eq('Vim(echo):E909: Cannot index a special variable', exc_exec('echo v:null[0]'))
  131. end)
  132. it('is accepted by assert_true and assert_false', function()
  133. fn.assert_false(false)
  134. fn.assert_false(true)
  135. fn.assert_false(NIL)
  136. fn.assert_true(false)
  137. fn.assert_true(true)
  138. fn.assert_true(NIL)
  139. eq({
  140. 'Expected False but got v:true',
  141. 'Expected False but got v:null',
  142. 'Expected True but got v:false',
  143. 'Expected True but got v:null',
  144. }, api.nvim_get_vvar('errors'))
  145. end)
  146. describe('compat', function()
  147. it('v:count is distinct from count', function()
  148. command('let count = []') -- v:count is readonly
  149. eq(1, eval('count is# g:["count"]'))
  150. end)
  151. it('v:errmsg is distinct from errmsg', function()
  152. command('let errmsg = 1')
  153. eq(1, eval('errmsg is# g:["errmsg"]'))
  154. end)
  155. it('v:shell_error is distinct from shell_error', function()
  156. command('let shell_error = []') -- v:shell_error is readonly
  157. eq(1, eval('shell_error is# g:["shell_error"]'))
  158. end)
  159. it('v:this_session is distinct from this_session', function()
  160. command('let this_session = []')
  161. eq(1, eval('this_session is# g:["this_session"]'))
  162. end)
  163. end)
  164. end)