080_substitute_spec.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -- Test for *sub-replace-special* and *sub-replace-expression* on substitute().
  2. -- Test for submatch() on substitute().
  3. -- Test for *:s%* on :substitute.
  4. local helpers = require('test.functional.helpers')(after_each)
  5. local feed, insert = helpers.feed, helpers.insert
  6. local clear, feed_command, expect = helpers.clear, helpers.feed_command, helpers.expect
  7. local eq, eval = helpers.eq, helpers.eval
  8. describe('substitue()', function()
  9. before_each(clear)
  10. -- The original test contained several TEST_X lines to delimit different
  11. -- parts. These where used to split the test into different it() blocks.
  12. -- The TEST_X strings are repeated in the description of the blocks to make
  13. -- it easier to incorporate upstream changes.
  14. local function test_1_and_2()
  15. eq('AA', eval("substitute('A', 'A', '&&', '')"))
  16. eq('&', eval([[substitute('B', 'B', '\&', '')]]))
  17. eq('C123456789987654321', eval([[substitute('C123456789', ]] ..
  18. [['C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] ..
  19. [['\0\9\8\7\6\5\4\3\2\1', '')]]))
  20. eq('d', eval("substitute('D', 'D', 'd', '')"))
  21. eq('~', eval("substitute('E', 'E', '~', '')"))
  22. eq('~', eval([[substitute('F', 'F', '\~', '')]]))
  23. eq('Gg', eval([[substitute('G', 'G', '\ugg', '')]]))
  24. eq('Hh', eval([[substitute('H', 'H', '\Uh\Eh', '')]]))
  25. eq('iI', eval([[substitute('I', 'I', '\lII', '')]]))
  26. eq('jJ', eval([[substitute('J', 'J', '\LJ\EJ', '')]]))
  27. eq('Kk', eval([[substitute('K', 'K', '\Uk\ek', '')]]))
  28. eq('l\rl', eval("substitute('lLl', 'L', '\r', '')"))
  29. eq('m\rm', eval([[substitute('mMm', 'M', '\r', '')]]))
  30. eq('n\rn', eval("substitute('nNn', 'N', '\\\r', '')"))
  31. eq('o\no', eval([[substitute('oOo', 'O', '\n', '')]]))
  32. eq('p\bp', eval([[substitute('pPp', 'P', '\b', '')]]))
  33. eq('q\tq', eval([[substitute('qQq', 'Q', '\t', '')]]))
  34. eq('r\\r', eval([[substitute('rRr', 'R', '\\', '')]]))
  35. eq('scs', eval([[substitute('sSs', 'S', '\c', '')]]))
  36. eq('t\rt', eval([[substitute('tTt', 'T', "\r", '')]]))
  37. eq('u\nu', eval([[substitute('uUu', 'U', "\n", '')]]))
  38. eq('v\bv', eval([[substitute('vVv', 'V', "\b", '')]]))
  39. eq('w\\w', eval([[substitute('wWw', 'W', "\\", '')]]))
  40. eq('XxxX', eval([[substitute('X', 'X', '\L\uxXx\l\EX', '')]]))
  41. eq('yYYy', eval([[substitute('Y', 'Y', '\U\lYyY\u\Ey', '')]]))
  42. end
  43. it('with "set magic" (TEST_1)', function()
  44. feed_command('set magic')
  45. test_1_and_2()
  46. end)
  47. it('with "set nomagic" (TEST_2)', function()
  48. feed_command('set nomagic')
  49. test_1_and_2()
  50. end)
  51. it('with sub-replace-expression (TEST_3)', function()
  52. feed_command('set magic&')
  53. eq('a\\a', eval([[substitute('aAa', 'A', '\="\\"', '')]]))
  54. eq('b\\\\b', eval([[substitute('bBb', 'B', '\="\\\\"', '')]]))
  55. eq('c\rc', eval([[substitute('cCc', 'C', '\="]]..'\r'..[["', '')]]))
  56. eq('d\\\rd', eval([[substitute('dDd', 'D', '\="\\]]..'\r'..[["', '')]]))
  57. eq('e\\\\\re',
  58. eval([[substitute('eEe', 'E', '\="\\\\]]..'\r'..[["', '')]]))
  59. eq('f\\rf', eval([[substitute('fFf', 'F', '\="\\r"', '')]]))
  60. eq('j\\nj', eval([[substitute('jJj', 'J', '\="\\n"', '')]]))
  61. eq('k\rk', eval([[substitute('kKk', 'K', '\="\r"', '')]]))
  62. eq('l\nl', eval([[substitute('lLl', 'L', '\="\n"', '')]]))
  63. end)
  64. it('with submatch() (TEST_4)', function()
  65. feed_command('set magic&')
  66. eq('a\\a', eval([[substitute('aAa', 'A', ]] ..
  67. [['\=substitute(submatch(0), ".", "\\", "")', '')]]))
  68. eq('b\\b', eval([[substitute('bBb', 'B', ]] ..
  69. [['\=substitute(submatch(0), ".", "\\\\", "")', '')]]))
  70. eq('c\rc', eval([[substitute('cCc', 'C', ]] ..
  71. [['\=substitute(submatch(0), ".", "]]..'\r'..[[", "")', '')]]))
  72. eq('d\rd', eval([[substitute('dDd', 'D', ]] ..
  73. [['\=substitute(submatch(0), ".", "\\]]..'\r'..[[", "")', '')]]))
  74. eq('e\\\re', eval([[substitute('eEe', 'E', ]] ..
  75. [['\=substitute(submatch(0), ".", "\\\\]]..'\r'..[[", "")', '')]]))
  76. eq('f\rf', eval([[substitute('fFf', 'F', ]] ..
  77. [['\=substitute(submatch(0), ".", "\\r", "")', '')]]))
  78. eq('j\nj', eval([[substitute('jJj', 'J', ]] ..
  79. [['\=substitute(submatch(0), ".", "\\n", "")', '')]]))
  80. eq('k\rk', eval([[substitute('kKk', 'K', ]] ..
  81. [['\=substitute(submatch(0), ".", "\r", "")', '')]]))
  82. eq('l\nl', eval([[substitute('lLl', 'L', ]] ..
  83. [['\=substitute(submatch(0), ".", "\n", "")', '')]]))
  84. end)
  85. it('with submatch() (TEST_5)', function()
  86. feed_command('set magic&')
  87. eq('A123456789987654321', eval([[substitute('A123456789', ]] ..
  88. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] ..
  89. [['\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . ]] ..
  90. [[submatch(6) . submatch(5) . submatch(4) . submatch(3) . ]] ..
  91. [[submatch(2) . submatch(1)', '')]]))
  92. eq("[['A123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], " ..
  93. "['2'], ['1']]", eval([[substitute('A123456789', ]] ..
  94. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] ..
  95. [['\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), ]] ..
  96. [[submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), ]] ..
  97. [[submatch(3, 1), submatch(2, 1), submatch(1, 1)])', '')]]))
  98. end)
  99. -- TEST_6 was about the 'cpoptions' flag / which was removed in pull request
  100. -- #2943.
  101. it('with submatch or \\ze (TEST_7)', function()
  102. feed_command('set magic&')
  103. eq('A\rA', eval("substitute('A\rA', 'A.', '\\=submatch(0)', '')"))
  104. eq('B\nB', eval([[substitute("B\nB", 'B.', '\=submatch(0)', '')]]))
  105. eq("['B\n']B",
  106. eval([[substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')]]))
  107. eq('-abab', eval([[substitute('-bb', '\zeb', 'a', 'g')]]))
  108. eq('c-cbcbc', eval([[substitute('-bb', '\ze', 'c', 'g')]]))
  109. end)
  110. it('with \\zs and \\ze (TEST_10)', function()
  111. feed_command('set magic&')
  112. eq('a1a2a3a', eval([[substitute('123', '\zs', 'a', 'g')]]))
  113. eq('aaa', eval([[substitute('123', '\zs.', 'a', 'g')]]))
  114. eq('1a2a3a', eval([[substitute('123', '.\zs', 'a', 'g')]]))
  115. eq('a1a2a3a', eval([[substitute('123', '\ze', 'a', 'g')]]))
  116. eq('a1a2a3', eval([[substitute('123', '\ze.', 'a', 'g')]]))
  117. eq('aaa', eval([[substitute('123', '.\ze', 'a', 'g')]]))
  118. eq('aa2a3a', eval([[substitute('123', '1\|\ze', 'a', 'g')]]))
  119. eq('1aaa', eval([[substitute('123', '1\zs\|[23]', 'a', 'g')]]))
  120. end)
  121. end)
  122. describe(':substitue', function()
  123. before_each(clear)
  124. it('with \\ze and \\zs and confirmation dialog (TEST_8)', function()
  125. insert([[
  126. ,,X
  127. ,,Y
  128. ,,Z]])
  129. feed_command('set magic&')
  130. feed_command([[1s/\(^\|,\)\ze\(,\|X\)/\1N/g]])
  131. feed_command([[2s/\(^\|,\)\ze\(,\|Y\)/\1N/gc]])
  132. feed('a') -- For the dialog of the previous :s command.
  133. feed_command([[3s/\(^\|,\)\ze\(,\|Z\)/\1N/gc]])
  134. feed('yy') -- For the dialog of the previous :s command.
  135. expect([[
  136. N,,NX
  137. N,,NY
  138. N,,NZ]])
  139. end)
  140. it('with confirmation dialog (TEST_9)', function()
  141. insert('xxx')
  142. feed_command('set magic&')
  143. feed_command('s/x/X/gc')
  144. feed('yyq') -- For the dialog of the previous :s command.
  145. expect('XXx')
  146. end)
  147. end)