aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/036_regexp_character_classes_spec.lua
blob: 6f66efcb678eb579be4065c9b37487767fdea396 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
-- Test character classes in regexp using regexpengine 0, 1, 2.

local helpers = require('test.functional.helpers')(after_each)
local clear, command, expect = helpers.clear, helpers.command, helpers.expect
local source, write_file = helpers.source, helpers.write_file

local function sixlines(text)
    local result = ''
    for _ = 1, 6 do
      result = result .. text .. '\n'
    end
    return result
end

local function diff(text, nodedent)
  local fname = helpers.tmpname()
  command('w! '..fname)
  helpers.poke_eventloop()
  local data = io.open(fname):read('*all')
  if nodedent then
    helpers.eq(text, data)
  else
    helpers.eq(helpers.dedent(text), data)
  end
  os.remove(fname)
end

describe('character classes in regexp', function()
  local ctrl1 = '\t\012\r'
  local punct1 = " !\"#$%&'()#+'-./"
  local digits = '0123456789'
  local punct2 = ':;<=>?@'
  local upper = 'ABCDEFGHIXYZ'
  local punct3 = '[\\]^_`'
  local lower = 'abcdefghiwxyz'
  local punct4 = '{|}~'
  local ctrl2 = '\127\128\130\144\155'
  local iso_text = '\166\177\188\199\211\233' -- "¦±¼ÇÓé" in utf-8
  setup(function()
    -- The original test32.in file was not in utf-8 encoding and did also
    -- contain some control characters.  We use lua escape sequences to write
    -- them to the test file.
    local line = ctrl1..punct1..digits..punct2..upper..punct3..lower..punct4..ctrl2..iso_text
    write_file('test36.in', sixlines(line))
  end)
  before_each(function()
    clear()
    command('e test36.in')
  end)
  teardown(function()
    os.remove('test36.in')
  end)

  it('is working', function()
    source([[
      1 s/\%#=0\d//g
      2 s/\%#=1\d//g
      3 s/\%#=2\d//g
      4 s/\%#=0[0-9]//g
      5 s/\%#=1[0-9]//g
      6 s/\%#=2[0-9]//g]])
    diff(sixlines(ctrl1..punct1..punct2..upper..punct3..lower..punct4..
        ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\D//g
      2 s/\%#=1\D//g
      3 s/\%#=2\D//g
      4 s/\%#=0[^0-9]//g
      5 s/\%#=1[^0-9]//g
      6 s/\%#=2[^0-9]//g]])
    expect([[
      0123456789
      0123456789
      0123456789
      0123456789
      0123456789
      0123456789]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\o//g
      2 s/\%#=1\o//g
      3 s/\%#=2\o//g
      4 s/\%#=0[0-7]//g
      5 s/\%#=1[0-7]//g
      6 s/\%#=2[0-7]//g]])
    diff(sixlines(ctrl1..punct1..'89'..punct2..upper..punct3..lower..punct4..ctrl2..
      iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\O//g
      2 s/\%#=1\O//g
      3 s/\%#=2\O//g
      4 s/\%#=0[^0-7]//g
      5 s/\%#=1[^0-7]//g
      6 s/\%#=2[^0-7]//g]])
    expect([[
      01234567
      01234567
      01234567
      01234567
      01234567
      01234567]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\x//g
      2 s/\%#=1\x//g
      3 s/\%#=2\x//g
      4 s/\%#=0[0-9A-Fa-f]//g
      5 s/\%#=1[0-9A-Fa-f]//g
      6 s/\%#=2[0-9A-Fa-f]//g]])
      diff(sixlines(ctrl1..punct1..punct2..'GHIXYZ'..punct3..'ghiwxyz'..punct4..ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\X//g
      2 s/\%#=1\X//g
      3 s/\%#=2\X//g
      4 s/\%#=0[^0-9A-Fa-f]//g
      5 s/\%#=1[^0-9A-Fa-f]//g
      6 s/\%#=2[^0-9A-Fa-f]//g]])
    expect([[
      0123456789ABCDEFabcdef
      0123456789ABCDEFabcdef
      0123456789ABCDEFabcdef
      0123456789ABCDEFabcdef
      0123456789ABCDEFabcdef
      0123456789ABCDEFabcdef]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\w//g
      2 s/\%#=1\w//g
      3 s/\%#=2\w//g
      4 s/\%#=0[0-9A-Za-z_]//g
      5 s/\%#=1[0-9A-Za-z_]//g
      6 s/\%#=2[0-9A-Za-z_]//g]])
      diff(sixlines(ctrl1..punct1..punct2..'[\\]^`'..punct4..ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\W//g
      2 s/\%#=1\W//g
      3 s/\%#=2\W//g
      4 s/\%#=0[^0-9A-Za-z_]//g
      5 s/\%#=1[^0-9A-Za-z_]//g
      6 s/\%#=2[^0-9A-Za-z_]//g]])
    expect([[
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz
      0123456789ABCDEFGHIXYZ_abcdefghiwxyz]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\h//g
      2 s/\%#=1\h//g
      3 s/\%#=2\h//g
      4 s/\%#=0[A-Za-z_]//g
      5 s/\%#=1[A-Za-z_]//g
      6 s/\%#=2[A-Za-z_]//g]])
      diff(sixlines(ctrl1..punct1..digits..punct2..'[\\]^`'..punct4..ctrl2..
        iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\H//g
      2 s/\%#=1\H//g
      3 s/\%#=2\H//g
      4 s/\%#=0[^A-Za-z_]//g
      5 s/\%#=1[^A-Za-z_]//g
      6 s/\%#=2[^A-Za-z_]//g]])
    expect([[
      ABCDEFGHIXYZ_abcdefghiwxyz
      ABCDEFGHIXYZ_abcdefghiwxyz
      ABCDEFGHIXYZ_abcdefghiwxyz
      ABCDEFGHIXYZ_abcdefghiwxyz
      ABCDEFGHIXYZ_abcdefghiwxyz
      ABCDEFGHIXYZ_abcdefghiwxyz]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\a//g
      2 s/\%#=1\a//g
      3 s/\%#=2\a//g
      4 s/\%#=0[A-Za-z]//g
      5 s/\%#=1[A-Za-z]//g
      6 s/\%#=2[A-Za-z]//g]])
    diff(sixlines(ctrl1..punct1..digits..punct2..punct3..punct4..ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\A//g
      2 s/\%#=1\A//g
      3 s/\%#=2\A//g
      4 s/\%#=0[^A-Za-z]//g
      5 s/\%#=1[^A-Za-z]//g
      6 s/\%#=2[^A-Za-z]//g]])
    expect([[
      ABCDEFGHIXYZabcdefghiwxyz
      ABCDEFGHIXYZabcdefghiwxyz
      ABCDEFGHIXYZabcdefghiwxyz
      ABCDEFGHIXYZabcdefghiwxyz
      ABCDEFGHIXYZabcdefghiwxyz
      ABCDEFGHIXYZabcdefghiwxyz]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\l//g
      2 s/\%#=1\l//g
      3 s/\%#=2\l//g
      4 s/\%#=0[a-z]//g
      5 s/\%#=1[a-z]//g
      6 s/\%#=2[a-z]//g]])
    diff(sixlines(ctrl1..punct1..digits..punct2..upper..punct3..punct4..
      ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\L//g
      2 s/\%#=1\L//g
      3 s/\%#=2\L//g
      4 s/\%#=0[^a-z]//g
      5 s/\%#=1[^a-z]//g
      6 s/\%#=2[^a-z]//g]])
    expect([[
      abcdefghiwxyz
      abcdefghiwxyz
      abcdefghiwxyz
      abcdefghiwxyz
      abcdefghiwxyz
      abcdefghiwxyz]])
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\u//g
      2 s/\%#=1\u//g
      3 s/\%#=2\u//g
      4 s/\%#=0[A-Z]//g
      5 s/\%#=1[A-Z]//g
      6 s/\%#=2[A-Z]//g]])
    diff(sixlines(ctrl1..punct1..digits..punct2..punct3..lower..punct4..
      ctrl2..iso_text))
  end)
  it('is working', function()
    source([[
      1 s/\%#=0\U//g
      2 s/\%#=1\U//g
      3 s/\%#=2\U//g
      4 s/\%#=0[^A-Z]//g
      5 s/\%#=1[^A-Z]//g
      6 s/\%#=2[^A-Z]//g]])
    expect([[
      ABCDEFGHIXYZ
      ABCDEFGHIXYZ
      ABCDEFGHIXYZ
      ABCDEFGHIXYZ
      ABCDEFGHIXYZ
      ABCDEFGHIXYZ]])
  end)
  it([["\%1l^#.*" does not match on a line starting with "#". (vim-patch:7.4.1305)]], function()
    source([[
      1 s/\%#=0\%1l^\t...//g
      2 s/\%#=1\%2l^\t...//g
      3 s/\%#=2\%3l^\t...//g
      4 s/\%#=0\%4l^\t...//g
      5 s/\%#=1\%5l^\t...//g
      6 s/\%#=2\%6l^\t...//g]])
    diff(sixlines(string.sub(punct1, 1)..digits..punct2..upper..punct3..
      lower..punct4..ctrl2..iso_text))
  end)
  it('does not convert character class ranges to an incorrect class', function()
    source([[
      1 s/\%#=0[0-z]//g
      2 s/\%#=1[0-z]//g
      3 s/\%#=2[0-z]//g
      4 s/\%#=0[^0-z]//g
      5 s/\%#=1[^0-z]//g
      6 s/\%#=2[^0-z]//g
    ]])
    diff(string.rep(ctrl1..punct1..punct4..ctrl2..iso_text..'\n', 3)
      ..string.rep(digits..punct2..upper..punct3..lower..'\n', 3))
  end)
end)