test_method.vim 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. " Tests for ->method()
  2. source check.vim
  3. func Test_list_method()
  4. let l = [1, 2, 3]
  5. call assert_equal([1, 2, 3, 4], [1, 2, 3]->add(4))
  6. eval l->assert_equal(l)
  7. eval l->assert_equal(l, 'wrong')
  8. eval l->assert_notequal([3, 2, 1])
  9. eval l->assert_notequal([3, 2, 1], 'wrong')
  10. call assert_equal(l, l->copy())
  11. call assert_equal(l, l->deepcopy())
  12. call assert_equal(1, l->count(2))
  13. call assert_false(l->empty())
  14. call assert_true([]->empty())
  15. call assert_equal(579, ['123', '+', '456']->join()->eval())
  16. call assert_equal([1, 2, 3, 4, 5], [1, 2, 3]->extend([4, 5]))
  17. call assert_equal([1, 3], [1, 2, 3]->filter('v:val != 2'))
  18. call assert_equal(2, l->get(1))
  19. call assert_equal(1, l->index(2))
  20. call assert_equal([0, 1, 2, 3], [1, 2, 3]->insert(0))
  21. call assert_equal('1 2 3', l->join())
  22. call assert_fails('eval l->keys()', 'E1206:')
  23. call assert_equal(3, l->len())
  24. call assert_equal([2, 3, 4], [1, 2, 3]->map('v:val + 1'))
  25. call assert_equal(3, l->max())
  26. call assert_equal(1, l->min())
  27. call assert_equal(2, [1, 2, 3]->remove(1))
  28. call assert_equal([1, 2, 3, 1, 2, 3], l->repeat(2))
  29. call assert_equal([3, 2, 1], [1, 2, 3]->reverse())
  30. call assert_equal([1, 2, 3, 4], [4, 2, 3, 1]->sort())
  31. call assert_equal('[1, 2, 3]', l->string())
  32. call assert_equal(v:t_list, l->type())
  33. call assert_equal([1, 2, 3], [1, 1, 2, 3, 3]->uniq())
  34. call assert_fails('eval l->values()', 'E1206:')
  35. call assert_fails('echo []->len', 'E107:')
  36. endfunc
  37. func Test_dict_method()
  38. let d = #{one: 1, two: 2, three: 3}
  39. call assert_equal(d, d->copy())
  40. call assert_equal(d, d->deepcopy())
  41. call assert_equal(1, d->count(2))
  42. call assert_false(d->empty())
  43. call assert_true({}->empty())
  44. call assert_equal(#{one: 1, two: 2, three: 3, four: 4}, d->extend(#{four: 4}))
  45. call assert_equal(#{one: 1, two: 2, three: 3}, d->filter('v:val != 4'))
  46. call assert_equal(2, d->get('two'))
  47. call assert_fails("let x = d->index(2)", 'E897:')
  48. call assert_fails("let x = d->insert(0)", 'E899:')
  49. call assert_true(d->has_key('two'))
  50. call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items())
  51. call assert_fails("let x = d->join()", 'E714:')
  52. call assert_equal(['one', 'two', 'three'], d->keys())
  53. call assert_equal(3, d->len())
  54. call assert_equal(#{one: 2, two: 3, three: 4}, d->map('v:val + 1'))
  55. call assert_equal(#{one: 1, two: 2, three: 3}, d->map('v:val - 1'))
  56. call assert_equal(3, d->max())
  57. call assert_equal(1, d->min())
  58. call assert_equal(2, d->remove("two"))
  59. let d.two = 2
  60. call assert_fails('let x = d->repeat(2)', 'E731:')
  61. call assert_fails('let x = d->reverse()', 'E1252:')
  62. call assert_fails('let x = d->sort()', 'E686:')
  63. call assert_equal("{'one': 1, 'two': 2, 'three': 3}", d->string())
  64. call assert_equal(v:t_dict, d->type())
  65. call assert_fails('let x = d->uniq()', 'E686:')
  66. call assert_equal([1, 2, 3], d->values())
  67. endfunc
  68. func Test_string_method()
  69. eval '1 2 3'->split()->assert_equal(['1', '2', '3'])
  70. eval '1 2 3'->split()->map({i, v -> str2nr(v)})->assert_equal([1, 2, 3])
  71. eval 'ABC'->str2list()->assert_equal([65, 66, 67])
  72. eval 'ABC'->strlen()->assert_equal(3)
  73. eval "a\rb\ec"->strtrans()->assert_equal('a^Mb^[c')
  74. eval "aあb"->strwidth()->assert_equal(4)
  75. eval 'abc'->substitute('b', 'x', '')->assert_equal('axc')
  76. call assert_fails('eval 123->items()', 'E1225:')
  77. eval 'abc'->printf('the %s arg')->assert_equal('the abc arg')
  78. endfunc
  79. func Test_method_append()
  80. new
  81. eval ['one', 'two', 'three']->append(1)
  82. call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
  83. %del
  84. let bnr = bufnr('')
  85. wincmd w
  86. eval ['one', 'two', 'three']->appendbufline(bnr, 1)
  87. call assert_equal(['', 'one', 'two', 'three'], getbufline(bnr, 1, '$'))
  88. exe 'bwipe! ' .. bnr
  89. endfunc
  90. func Test_method_funcref()
  91. func Concat(one, two, three)
  92. return a:one .. a:two .. a:three
  93. endfunc
  94. let FuncRef = function('Concat')
  95. eval 'foo'->FuncRef('bar', 'tail')->assert_equal('foobartail')
  96. " not enough arguments
  97. call assert_fails("eval 'foo'->FuncRef('bar')", 'E119:')
  98. " too many arguments
  99. call assert_fails("eval 'foo'->FuncRef('bar', 'tail', 'four')", 'E118:')
  100. let Partial = function('Concat', ['two'])
  101. eval 'one'->Partial('three')->assert_equal('onetwothree')
  102. " not enough arguments
  103. call assert_fails("eval 'one'->Partial()", 'E119:')
  104. " too many arguments
  105. call assert_fails("eval 'one'->Partial('three', 'four')", 'E118:')
  106. delfunc Concat
  107. endfunc
  108. func Test_method_float()
  109. CheckFeature float
  110. eval 1.234->string()->assert_equal('1.234')
  111. eval -1.234->string()->assert_equal('-1.234')
  112. endfunc
  113. func Test_method_syntax()
  114. eval [1, 2, 3] ->sort( )
  115. eval [1, 2, 3]
  116. \ ->sort(
  117. \ )
  118. call assert_fails('eval [1, 2, 3]-> sort()', 'E15:')
  119. call assert_fails('eval [1, 2, 3]->sort ()', 'E274:')
  120. call assert_fails('eval [1, 2, 3]-> sort ()', 'E15:')
  121. " Test for using a method name containing a curly brace name
  122. let s = 'len'
  123. call assert_equal(4, "xxxx"->str{s}())
  124. " Test for using a method in an interpolated string
  125. call assert_equal('4', $'{"xxxx"->strlen()}')
  126. endfunc
  127. func Test_method_lambda()
  128. eval "text"->{x -> x .. " extended"}()->assert_equal('text extended')
  129. eval "text"->{x, y -> x .. " extended " .. y}('more')->assert_equal('text extended more')
  130. call assert_fails('eval "text"->{x -> x .. " extended"} ()', 'E274:')
  131. " todo: lambda accepts more arguments than it consumes
  132. " call assert_fails('eval "text"->{x -> x .. " extended"}("more")', 'E99:')
  133. " Nvim doesn't include test_refcount().
  134. " let l = [1, 2, 3]
  135. " eval l->{x -> x}()
  136. " call assert_equal(1, test_refcount(l))
  137. endfunc
  138. func Test_method_not_supported()
  139. call assert_fails('eval 123->changenr()', 'E276:')
  140. call assert_fails('echo "abc"->invalidfunc()', 'E117:')
  141. " Test for too many or too few arguments to a method
  142. call assert_fails('let n="abc"->len(2)', 'E118:')
  143. call assert_fails('let n=10->setwinvar()', 'E119:')
  144. endfunc
  145. " Test for passing optional arguments to methods
  146. func Test_method_args()
  147. let v:errors = []
  148. let n = 10->assert_inrange(1, 5, "Test_assert_inrange")
  149. if v:errors[0] !~ 'Test_assert_inrange'
  150. call assert_report(v:errors[0])
  151. else
  152. " Test passed
  153. let v:errors = []
  154. endif
  155. endfunc
  156. " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker