test_getvar.vim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. " Tests for getwinvar(), gettabvar(), gettabwinvar() and get().
  2. func Test_var()
  3. " Use strings to test for memory leaks. First, check that in an empty
  4. " window, gettabvar() returns the correct value
  5. let t:testvar='abcd'
  6. call assert_equal('abcd', gettabvar(1, 'testvar'))
  7. call assert_equal('abcd', gettabvar(1, 'testvar'))
  8. " test for getwinvar()
  9. let w:var_str = "Dance"
  10. let def_str = "Chance"
  11. call assert_equal('Dance', getwinvar(1, 'var_str'))
  12. call assert_equal('Dance', getwinvar(1, 'var_str', def_str))
  13. call assert_equal({'var_str': 'Dance'}, 1->getwinvar(''))
  14. call assert_equal({'var_str': 'Dance'}, 1->getwinvar('', def_str))
  15. unlet w:var_str
  16. call assert_equal('Chance', getwinvar(1, 'var_str', def_str))
  17. call assert_equal({}, getwinvar(1, ''))
  18. call assert_equal({}, getwinvar(1, '', def_str))
  19. call assert_equal('', getwinvar(9, ''))
  20. call assert_equal('Chance', getwinvar(9, '', def_str))
  21. call assert_equal(0, getwinvar(1, '&nu'))
  22. call assert_equal(0, getwinvar(1, '&nu', 1))
  23. call assert_match(v:t_dict, type(getwinvar(1, '&')))
  24. call assert_match(v:t_dict, type(getwinvar(1, '&', def_str)))
  25. call assert_equal('', getwinvar(9, '&'))
  26. call assert_equal('Chance', getwinvar(9, '&', def_str))
  27. call assert_equal('', getwinvar(1, '&nux'))
  28. call assert_equal('Chance', getwinvar(1, '&nux', def_str))
  29. unlet def_str
  30. " test for gettabvar()
  31. tabnew
  32. tabnew
  33. let t:var_list = [1, 2, 3]
  34. let t:other = 777
  35. let def_list = [4, 5, 6, 7]
  36. tabrewind
  37. call assert_equal([1, 2, 3], 3->gettabvar('var_list'))
  38. call assert_equal([1, 2, 3], gettabvar(3, 'var_list', def_list))
  39. call assert_equal({'var_list': [1, 2, 3], 'other': 777}, gettabvar(3, ''))
  40. call assert_equal({'var_list': [1, 2, 3], 'other': 777},
  41. \ gettabvar(3, '', def_list))
  42. tablast
  43. unlet t:var_list
  44. tabrewind
  45. call assert_equal([4, 5, 6, 7], gettabvar(3, 'var_list', def_list))
  46. call assert_equal('', gettabvar(9, ''))
  47. call assert_equal([4, 5, 6, 7], gettabvar(9, '', def_list))
  48. call assert_equal('', gettabvar(3, '&nu'))
  49. call assert_equal([4, 5, 6, 7], gettabvar(3, '&nu', def_list))
  50. unlet def_list
  51. tabonly
  52. " test for gettabwinvar()
  53. tabnew
  54. tabnew
  55. tabprev
  56. split
  57. split
  58. wincmd w
  59. vert split
  60. wincmd w
  61. let w:var_dict = {'dict': 'tabwin'}
  62. let def_dict = {'dict2': 'newval'}
  63. wincmd b
  64. tabrewind
  65. call assert_equal({'dict': 'tabwin'}, 2->gettabwinvar(3, 'var_dict'))
  66. call assert_equal({'dict': 'tabwin'},
  67. \ gettabwinvar(2, 3, 'var_dict', def_dict))
  68. call assert_equal({'var_dict': {'dict': 'tabwin'}}, gettabwinvar(2, 3, ''))
  69. call assert_equal({'var_dict': {'dict': 'tabwin'}},
  70. \ gettabwinvar(2, 3, '', def_dict))
  71. tabnext
  72. 3wincmd w
  73. unlet w:var_dict
  74. tabrewind
  75. call assert_equal({'dict2': 'newval'},
  76. \ gettabwinvar(2, 3, 'var_dict', def_dict))
  77. call assert_equal({}, gettabwinvar(2, 3, ''))
  78. call assert_equal({}, gettabwinvar(2, 3, '', def_dict))
  79. call assert_equal("", gettabwinvar(2, 9, ''))
  80. call assert_equal({'dict2': 'newval'}, gettabwinvar(2, 9, '', def_dict))
  81. call assert_equal('', gettabwinvar(9, 3, ''))
  82. call assert_equal({'dict2': 'newval'}, gettabwinvar(9, 3, '', def_dict))
  83. unlet def_dict
  84. call assert_match(v:t_dict, type(gettabwinvar(2, 3, '&')))
  85. call assert_match(v:t_dict, type(gettabwinvar(2, 3, '&', 1)))
  86. call assert_equal("", gettabwinvar(9, 2020, ''))
  87. call assert_equal(1, gettabwinvar(9, 2020, '', 1))
  88. call assert_equal('', gettabwinvar(9, 2020, '&'))
  89. call assert_equal(1, gettabwinvar(9, 2020, '&', 1))
  90. call assert_equal('', gettabwinvar(2, 3, '&nux'))
  91. call assert_equal(1, gettabwinvar(2, 3, '&nux', 1))
  92. tabonly
  93. endfunc
  94. " It was discovered that "gettabvar()" would fail if called from within the
  95. " tabline when the user closed a window. This test confirms the fix.
  96. func Test_gettabvar_in_tabline()
  97. let t:var_str = 'value'
  98. set tabline=%{assert_equal('value',gettabvar(1,'var_str'))}
  99. set showtabline=2
  100. " Simulate the user opening a split (which becomes window #1) and then
  101. " closing the split, which triggers the redrawing of the tabline.
  102. leftabove split
  103. redrawstatus!
  104. close
  105. redrawstatus!
  106. endfunc
  107. " Test get() function using default value.
  108. " get({dict}, {key} [, {default}])
  109. func Test_get_dict()
  110. let d = {'foo': 42}
  111. call assert_equal(42, get(d, 'foo', 99))
  112. call assert_equal(999, get(d, 'bar', 999))
  113. endfunc
  114. " get({list}, {idx} [, {default}])
  115. func Test_get_list()
  116. let l = [1,2,3]
  117. call assert_equal(1, get(l, 0, 999))
  118. call assert_equal(3, get(l, -1, 999))
  119. call assert_equal(999, get(l, 3, 999))
  120. endfunc
  121. " get({blob}, {idx} [, {default}]) - in test_blob.vim
  122. " get({lambda}, {what} [, {default}])
  123. func Test_get_lambda()
  124. let l:L = {-> 42}
  125. call assert_match('^<lambda>', get(l:L, 'name'))
  126. call assert_equal(l:L, get(l:L, 'func'))
  127. call assert_equal({'lambda has': 'no dict'}, get(l:L, 'dict', {'lambda has': 'no dict'}))
  128. call assert_equal(0, get(l:L, 'dict'))
  129. call assert_equal([], get(l:L, 'args'))
  130. endfunc
  131. func s:FooBar()
  132. endfunc
  133. " get({func}, {what} [, {default}])
  134. func Test_get_func()
  135. let l:F = function('tr')
  136. call assert_equal('tr', get(l:F, 'name'))
  137. call assert_equal(l:F, get(l:F, 'func'))
  138. call assert_equal({'required': 3, 'optional': 0, 'varargs': v:false},
  139. \ get(l:F, 'arity'))
  140. let Fb_func = function('s:FooBar')
  141. call assert_match('<SNR>\d\+_FooBar', get(Fb_func, 'name'))
  142. call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
  143. \ get(Fb_func, 'arity'))
  144. let Fb_ref = funcref('s:FooBar')
  145. call assert_match('<SNR>\d\+_FooBar', get(Fb_ref, 'name'))
  146. call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false},
  147. \ get(Fb_ref, 'arity'))
  148. call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'}))
  149. call assert_equal(0, get(l:F, 'dict'))
  150. call assert_equal([], get(l:F, 'args'))
  151. " Nvim doesn't have null functions
  152. " let NF = test_null_function()
  153. " call assert_equal('', get(NF, 'name'))
  154. " call assert_equal(NF, get(NF, 'func'))
  155. " call assert_equal(0, get(NF, 'dict'))
  156. " call assert_equal([], get(NF, 'args'))
  157. " call assert_equal({'required': 0, 'optional': 0, 'varargs': v:false}, get(NF, 'arity'))
  158. endfunc
  159. " get({partial}, {what} [, {default}]) - in test_partial.vim
  160. " vim: shiftwidth=2 sts=2 expandtab