robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
8.2 KiB230 linesraw
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
5local t = require('test.testutil')
6local n = require('test.functional.testnvim')()
7local Screen = require('test.functional.ui.screen')
8
9local describe, it, before_each = t.describe, t.it, t.before_each
10local feed, insert = n.feed, n.insert
11local exec = n.exec
12local clear, feed_command, expect = n.clear, n.feed_command, n.expect
13local eq, eval = t.eq, n.eval
14
15describe('substitute()', function()
16  before_each(clear)
17
18  -- The original test contained several TEST_X lines to delimit different
19  -- parts.  These where used to split the test into different it() blocks.
20  -- The TEST_X strings are repeated in the description of the blocks to make
21  -- it easier to incorporate upstream changes.
22
23  local function test_1_and_2()
24    eq('AA', eval("substitute('A', 'A', '&&', '')"))
25    eq('&', eval([[substitute('B', 'B', '\&', '')]]))
26    eq(
27      'C123456789987654321',
28      eval(
29        [[substitute('C123456789', ]]
30          .. [['C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]]
31          .. [['\0\9\8\7\6\5\4\3\2\1', '')]]
32      )
33    )
34    eq('d', eval("substitute('D', 'D', 'd', '')"))
35    eq('~', eval("substitute('E', 'E', '~', '')"))
36    eq('~', eval([[substitute('F', 'F', '\~', '')]]))
37    eq('Gg', eval([[substitute('G', 'G', '\ugg', '')]]))
38    eq('Hh', eval([[substitute('H', 'H', '\Uh\Eh', '')]]))
39    eq('iI', eval([[substitute('I', 'I', '\lII', '')]]))
40    eq('jJ', eval([[substitute('J', 'J', '\LJ\EJ', '')]]))
41    eq('Kk', eval([[substitute('K', 'K', '\Uk\ek', '')]]))
42    eq('l\rl', eval("substitute('lLl', 'L', '\r', '')"))
43    eq('m\rm', eval([[substitute('mMm', 'M', '\r', '')]]))
44    eq('n\rn', eval("substitute('nNn', 'N', '\\\r', '')"))
45    eq('o\no', eval([[substitute('oOo', 'O', '\n', '')]]))
46    eq('p\bp', eval([[substitute('pPp', 'P', '\b', '')]]))
47    eq('q\tq', eval([[substitute('qQq', 'Q', '\t', '')]]))
48    eq('r\\r', eval([[substitute('rRr', 'R', '\\', '')]]))
49    eq('scs', eval([[substitute('sSs', 'S', '\c', '')]]))
50    eq('t\rt', eval([[substitute('tTt', 'T', "\r", '')]]))
51    eq('u\nu', eval([[substitute('uUu', 'U', "\n", '')]]))
52    eq('v\bv', eval([[substitute('vVv', 'V', "\b", '')]]))
53    eq('w\\w', eval([[substitute('wWw', 'W', "\\", '')]]))
54    eq('XxxX', eval([[substitute('X', 'X', '\L\uxXx\l\EX', '')]]))
55    eq('yYYy', eval([[substitute('Y', 'Y', '\U\lYyY\u\Ey', '')]]))
56  end
57
58  it('with "set magic" (TEST_1)', function()
59    feed_command('set magic')
60    test_1_and_2()
61  end)
62
63  it('with "set nomagic" (TEST_2)', function()
64    feed_command('set nomagic')
65    test_1_and_2()
66  end)
67
68  it('with sub-replace-expression (TEST_3)', function()
69    feed_command('set magic&')
70    eq('a\\a', eval([[substitute('aAa', 'A', '\="\\"', '')]]))
71    eq('b\\\\b', eval([[substitute('bBb', 'B', '\="\\\\"', '')]]))
72    eq('c\rc', eval([[substitute('cCc', 'C', '\="]] .. '\r' .. [["', '')]]))
73    eq('d\\\rd', eval([[substitute('dDd', 'D', '\="\\]] .. '\r' .. [["', '')]]))
74    eq('e\\\\\re', eval([[substitute('eEe', 'E', '\="\\\\]] .. '\r' .. [["', '')]]))
75    eq('f\\rf', eval([[substitute('fFf', 'F', '\="\\r"', '')]]))
76    eq('j\\nj', eval([[substitute('jJj', 'J', '\="\\n"', '')]]))
77    eq('k\rk', eval([[substitute('kKk', 'K', '\="\r"', '')]]))
78    eq('l\nl', eval([[substitute('lLl', 'L', '\="\n"', '')]]))
79  end)
80
81  it('with submatch() (TEST_4)', function()
82    feed_command('set magic&')
83    eq(
84      'a\\a',
85      eval([[substitute('aAa', 'A', ]] .. [['\=substitute(submatch(0), ".", "\\", "")', '')]])
86    )
87    eq(
88      'b\\b',
89      eval([[substitute('bBb', 'B', ]] .. [['\=substitute(submatch(0), ".", "\\\\", "")', '')]])
90    )
91    eq(
92      'c\rc',
93      eval(
94        [[substitute('cCc', 'C', ]]
95          .. [['\=substitute(submatch(0), ".", "]]
96          .. '\r'
97          .. [[", "")', '')]]
98      )
99    )
100    eq(
101      'd\rd',
102      eval(
103        [[substitute('dDd', 'D', ]]
104          .. [['\=substitute(submatch(0), ".", "\\]]
105          .. '\r'
106          .. [[", "")', '')]]
107      )
108    )
109    eq(
110      'e\\\re',
111      eval(
112        [[substitute('eEe', 'E', ]]
113          .. [['\=substitute(submatch(0), ".", "\\\\]]
114          .. '\r'
115          .. [[", "")', '')]]
116      )
117    )
118    eq(
119      'f\rf',
120      eval([[substitute('fFf', 'F', ]] .. [['\=substitute(submatch(0), ".", "\\r", "")', '')]])
121    )
122    eq(
123      'j\nj',
124      eval([[substitute('jJj', 'J', ]] .. [['\=substitute(submatch(0), ".", "\\n", "")', '')]])
125    )
126    eq(
127      'k\rk',
128      eval([[substitute('kKk', 'K', ]] .. [['\=substitute(submatch(0), ".", "\r", "")', '')]])
129    )
130    eq(
131      'l\nl',
132      eval([[substitute('lLl', 'L', ]] .. [['\=substitute(submatch(0), ".", "\n", "")', '')]])
133    )
134  end)
135
136  it('with submatch() (TEST_5)', function()
137    feed_command('set magic&')
138    eq(
139      'A123456789987654321',
140      eval(
141        [[substitute('A123456789', ]]
142          .. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]]
143          .. [['\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . ]]
144          .. [[submatch(6) . submatch(5) . submatch(4) . submatch(3) . ]]
145          .. [[submatch(2) . submatch(1)', '')]]
146      )
147    )
148    eq(
149      "[['A123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], " .. "['2'], ['1']]",
150      eval(
151        [[substitute('A123456789', ]]
152          .. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]]
153          .. [['\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), ]]
154          .. [[submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), ]]
155          .. [[submatch(3, 1), submatch(2, 1), submatch(1, 1)])', '')]]
156      )
157    )
158  end)
159
160  -- TEST_6 was about the 'cpoptions' flag / which was removed in pull request
161  -- #2943.
162
163  it('with submatch or \\ze (TEST_7)', function()
164    feed_command('set magic&')
165    eq('A\rA', eval("substitute('A\rA', 'A.', '\\=submatch(0)', '')"))
166    eq('B\nB', eval([[substitute("B\nB", 'B.', '\=submatch(0)', '')]]))
167    eq("['B\n']B", eval([[substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')]]))
168    eq('-abab', eval([[substitute('-bb', '\zeb', 'a', 'g')]]))
169    eq('c-cbcbc', eval([[substitute('-bb', '\ze', 'c', 'g')]]))
170  end)
171
172  it('with \\zs and \\ze (TEST_10)', function()
173    feed_command('set magic&')
174    eq('a1a2a3a', eval([[substitute('123', '\zs', 'a', 'g')]]))
175    eq('aaa', eval([[substitute('123', '\zs.', 'a', 'g')]]))
176    eq('1a2a3a', eval([[substitute('123', '.\zs', 'a', 'g')]]))
177    eq('a1a2a3a', eval([[substitute('123', '\ze', 'a', 'g')]]))
178    eq('a1a2a3', eval([[substitute('123', '\ze.', 'a', 'g')]]))
179    eq('aaa', eval([[substitute('123', '.\ze', 'a', 'g')]]))
180    eq('aa2a3a', eval([[substitute('123', '1\|\ze', 'a', 'g')]]))
181    eq('1aaa', eval([[substitute('123', '1\zs\|[23]', 'a', 'g')]]))
182  end)
183end)
184
185describe(':substitute', function()
186  before_each(clear)
187
188  it('with \\ze and \\zs and confirmation dialog (TEST_8)', function()
189    insert([[
190      ,,X
191      ,,Y
192      ,,Z]])
193    feed_command('set magic&')
194    feed_command([[1s/\(^\|,\)\ze\(,\|X\)/\1N/g]])
195    feed_command([[2s/\(^\|,\)\ze\(,\|Y\)/\1N/gc]])
196    feed('a') -- For the dialog of the previous :s command.
197    feed_command([[3s/\(^\|,\)\ze\(,\|Z\)/\1N/gc]])
198    feed('yy') -- For the dialog of the previous :s command.
199    expect([[
200      N,,NX
201      N,,NY
202      N,,NZ]])
203  end)
204
205  it('with confirmation dialog (TEST_9)', function()
206    insert('xxx')
207    feed_command('set magic&')
208    feed_command('s/x/X/gc')
209    feed('yyq') -- For the dialog of the previous :s command.
210    expect('XXx')
211  end)
212
213  it('first char is highlighted with confirmation dialog and empty match', function()
214    local screen = Screen.new(60, 8)
215    exec([[
216      set nohlsearch noincsearch
217      call setline(1, ['one', 'two', 'three'])
218    ]])
219    feed(':%s/^/   /c<CR>')
220    screen:expect([[
221      {2:o}ne                                                         |
222      two                                                         |
223      three                                                       |
224      {1:~                                                           }|*2
225      {3:                                                            }|
226      {6:replace with    ? (y)es/(n)o/(a)ll/(q)uit/(l)ast/scroll up(^}|
227      {6:E)/down(^Y)}^                                                 |
228    ]])
229  end)
230end)