036_regexp_character_classes_spec.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. -- Test character classes in regexp using regexpengine 0, 1, 2.
  2. local t = require('test.testutil')
  3. local n = require('test.functional.testnvim')()
  4. local clear, command, expect = n.clear, n.command, n.expect
  5. local source, write_file = n.source, t.write_file
  6. local function sixlines(text)
  7. local result = ''
  8. for _ = 1, 6 do
  9. result = result .. text .. '\n'
  10. end
  11. return result
  12. end
  13. local function diff(text, nodedent)
  14. local fname = t.tmpname()
  15. command('w! ' .. fname)
  16. n.poke_eventloop()
  17. local data = io.open(fname):read('*all')
  18. if nodedent then
  19. t.eq(text, data)
  20. else
  21. t.eq(t.dedent(text), data)
  22. end
  23. os.remove(fname)
  24. end
  25. describe('character classes in regexp', function()
  26. local ctrl1 = '\t\012\r'
  27. local punct1 = " !\"#$%&'()#+'-./"
  28. local digits = '0123456789'
  29. local punct2 = ':;<=>?@'
  30. local upper = 'ABCDEFGHIXYZ'
  31. local punct3 = '[\\]^_`'
  32. local lower = 'abcdefghiwxyz'
  33. local punct4 = '{|}~'
  34. local ctrl2 = '\127\128\130\144\155'
  35. local iso_text = '\166\177\188\199\211\233' -- "¦±¼ÇÓé" in utf-8
  36. setup(function()
  37. -- The original test32.in file was not in utf-8 encoding and did also
  38. -- contain some control characters. We use lua escape sequences to write
  39. -- them to the test file.
  40. local line = ctrl1
  41. .. punct1
  42. .. digits
  43. .. punct2
  44. .. upper
  45. .. punct3
  46. .. lower
  47. .. punct4
  48. .. ctrl2
  49. .. iso_text
  50. write_file('test36.in', sixlines(line))
  51. end)
  52. before_each(function()
  53. clear()
  54. command('e test36.in')
  55. end)
  56. teardown(function()
  57. os.remove('test36.in')
  58. end)
  59. it('is working', function()
  60. source([[
  61. 1 s/\%#=0\d//g
  62. 2 s/\%#=1\d//g
  63. 3 s/\%#=2\d//g
  64. 4 s/\%#=0[0-9]//g
  65. 5 s/\%#=1[0-9]//g
  66. 6 s/\%#=2[0-9]//g]])
  67. diff(
  68. sixlines(ctrl1 .. punct1 .. punct2 .. upper .. punct3 .. lower .. punct4 .. ctrl2 .. iso_text)
  69. )
  70. end)
  71. it('is working', function()
  72. source([[
  73. 1 s/\%#=0\D//g
  74. 2 s/\%#=1\D//g
  75. 3 s/\%#=2\D//g
  76. 4 s/\%#=0[^0-9]//g
  77. 5 s/\%#=1[^0-9]//g
  78. 6 s/\%#=2[^0-9]//g]])
  79. expect([[
  80. 0123456789
  81. 0123456789
  82. 0123456789
  83. 0123456789
  84. 0123456789
  85. 0123456789]])
  86. end)
  87. it('is working', function()
  88. source([[
  89. 1 s/\%#=0\o//g
  90. 2 s/\%#=1\o//g
  91. 3 s/\%#=2\o//g
  92. 4 s/\%#=0[0-7]//g
  93. 5 s/\%#=1[0-7]//g
  94. 6 s/\%#=2[0-7]//g]])
  95. diff(
  96. sixlines(
  97. ctrl1 .. punct1 .. '89' .. punct2 .. upper .. punct3 .. lower .. punct4 .. ctrl2 .. iso_text
  98. )
  99. )
  100. end)
  101. it('is working', function()
  102. source([[
  103. 1 s/\%#=0\O//g
  104. 2 s/\%#=1\O//g
  105. 3 s/\%#=2\O//g
  106. 4 s/\%#=0[^0-7]//g
  107. 5 s/\%#=1[^0-7]//g
  108. 6 s/\%#=2[^0-7]//g]])
  109. expect([[
  110. 01234567
  111. 01234567
  112. 01234567
  113. 01234567
  114. 01234567
  115. 01234567]])
  116. end)
  117. it('is working', function()
  118. source([[
  119. 1 s/\%#=0\x//g
  120. 2 s/\%#=1\x//g
  121. 3 s/\%#=2\x//g
  122. 4 s/\%#=0[0-9A-Fa-f]//g
  123. 5 s/\%#=1[0-9A-Fa-f]//g
  124. 6 s/\%#=2[0-9A-Fa-f]//g]])
  125. diff(
  126. sixlines(
  127. ctrl1 .. punct1 .. punct2 .. 'GHIXYZ' .. punct3 .. 'ghiwxyz' .. punct4 .. ctrl2 .. iso_text
  128. )
  129. )
  130. end)
  131. it('is working', function()
  132. source([[
  133. 1 s/\%#=0\X//g
  134. 2 s/\%#=1\X//g
  135. 3 s/\%#=2\X//g
  136. 4 s/\%#=0[^0-9A-Fa-f]//g
  137. 5 s/\%#=1[^0-9A-Fa-f]//g
  138. 6 s/\%#=2[^0-9A-Fa-f]//g]])
  139. expect([[
  140. 0123456789ABCDEFabcdef
  141. 0123456789ABCDEFabcdef
  142. 0123456789ABCDEFabcdef
  143. 0123456789ABCDEFabcdef
  144. 0123456789ABCDEFabcdef
  145. 0123456789ABCDEFabcdef]])
  146. end)
  147. it('is working', function()
  148. source([[
  149. 1 s/\%#=0\w//g
  150. 2 s/\%#=1\w//g
  151. 3 s/\%#=2\w//g
  152. 4 s/\%#=0[0-9A-Za-z_]//g
  153. 5 s/\%#=1[0-9A-Za-z_]//g
  154. 6 s/\%#=2[0-9A-Za-z_]//g]])
  155. diff(sixlines(ctrl1 .. punct1 .. punct2 .. '[\\]^`' .. punct4 .. ctrl2 .. iso_text))
  156. end)
  157. it('is working', function()
  158. source([[
  159. 1 s/\%#=0\W//g
  160. 2 s/\%#=1\W//g
  161. 3 s/\%#=2\W//g
  162. 4 s/\%#=0[^0-9A-Za-z_]//g
  163. 5 s/\%#=1[^0-9A-Za-z_]//g
  164. 6 s/\%#=2[^0-9A-Za-z_]//g]])
  165. expect([[
  166. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz
  167. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz
  168. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz
  169. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz
  170. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz
  171. 0123456789ABCDEFGHIXYZ_abcdefghiwxyz]])
  172. end)
  173. it('is working', function()
  174. source([[
  175. 1 s/\%#=0\h//g
  176. 2 s/\%#=1\h//g
  177. 3 s/\%#=2\h//g
  178. 4 s/\%#=0[A-Za-z_]//g
  179. 5 s/\%#=1[A-Za-z_]//g
  180. 6 s/\%#=2[A-Za-z_]//g]])
  181. diff(sixlines(ctrl1 .. punct1 .. digits .. punct2 .. '[\\]^`' .. punct4 .. ctrl2 .. iso_text))
  182. end)
  183. it('is working', function()
  184. source([[
  185. 1 s/\%#=0\H//g
  186. 2 s/\%#=1\H//g
  187. 3 s/\%#=2\H//g
  188. 4 s/\%#=0[^A-Za-z_]//g
  189. 5 s/\%#=1[^A-Za-z_]//g
  190. 6 s/\%#=2[^A-Za-z_]//g]])
  191. expect([[
  192. ABCDEFGHIXYZ_abcdefghiwxyz
  193. ABCDEFGHIXYZ_abcdefghiwxyz
  194. ABCDEFGHIXYZ_abcdefghiwxyz
  195. ABCDEFGHIXYZ_abcdefghiwxyz
  196. ABCDEFGHIXYZ_abcdefghiwxyz
  197. ABCDEFGHIXYZ_abcdefghiwxyz]])
  198. end)
  199. it('is working', function()
  200. source([[
  201. 1 s/\%#=0\a//g
  202. 2 s/\%#=1\a//g
  203. 3 s/\%#=2\a//g
  204. 4 s/\%#=0[A-Za-z]//g
  205. 5 s/\%#=1[A-Za-z]//g
  206. 6 s/\%#=2[A-Za-z]//g]])
  207. diff(sixlines(ctrl1 .. punct1 .. digits .. punct2 .. punct3 .. punct4 .. ctrl2 .. iso_text))
  208. end)
  209. it('is working', function()
  210. source([[
  211. 1 s/\%#=0\A//g
  212. 2 s/\%#=1\A//g
  213. 3 s/\%#=2\A//g
  214. 4 s/\%#=0[^A-Za-z]//g
  215. 5 s/\%#=1[^A-Za-z]//g
  216. 6 s/\%#=2[^A-Za-z]//g]])
  217. expect([[
  218. ABCDEFGHIXYZabcdefghiwxyz
  219. ABCDEFGHIXYZabcdefghiwxyz
  220. ABCDEFGHIXYZabcdefghiwxyz
  221. ABCDEFGHIXYZabcdefghiwxyz
  222. ABCDEFGHIXYZabcdefghiwxyz
  223. ABCDEFGHIXYZabcdefghiwxyz]])
  224. end)
  225. it('is working', function()
  226. source([[
  227. 1 s/\%#=0\l//g
  228. 2 s/\%#=1\l//g
  229. 3 s/\%#=2\l//g
  230. 4 s/\%#=0[a-z]//g
  231. 5 s/\%#=1[a-z]//g
  232. 6 s/\%#=2[a-z]//g]])
  233. diff(
  234. sixlines(
  235. ctrl1 .. punct1 .. digits .. punct2 .. upper .. punct3 .. punct4 .. ctrl2 .. iso_text
  236. )
  237. )
  238. end)
  239. it('is working', function()
  240. source([[
  241. 1 s/\%#=0\L//g
  242. 2 s/\%#=1\L//g
  243. 3 s/\%#=2\L//g
  244. 4 s/\%#=0[^a-z]//g
  245. 5 s/\%#=1[^a-z]//g
  246. 6 s/\%#=2[^a-z]//g]])
  247. expect([[
  248. abcdefghiwxyz
  249. abcdefghiwxyz
  250. abcdefghiwxyz
  251. abcdefghiwxyz
  252. abcdefghiwxyz
  253. abcdefghiwxyz]])
  254. end)
  255. it('is working', function()
  256. source([[
  257. 1 s/\%#=0\u//g
  258. 2 s/\%#=1\u//g
  259. 3 s/\%#=2\u//g
  260. 4 s/\%#=0[A-Z]//g
  261. 5 s/\%#=1[A-Z]//g
  262. 6 s/\%#=2[A-Z]//g]])
  263. diff(
  264. sixlines(
  265. ctrl1 .. punct1 .. digits .. punct2 .. punct3 .. lower .. punct4 .. ctrl2 .. iso_text
  266. )
  267. )
  268. end)
  269. it('is working', function()
  270. source([[
  271. 1 s/\%#=0\U//g
  272. 2 s/\%#=1\U//g
  273. 3 s/\%#=2\U//g
  274. 4 s/\%#=0[^A-Z]//g
  275. 5 s/\%#=1[^A-Z]//g
  276. 6 s/\%#=2[^A-Z]//g]])
  277. expect([[
  278. ABCDEFGHIXYZ
  279. ABCDEFGHIXYZ
  280. ABCDEFGHIXYZ
  281. ABCDEFGHIXYZ
  282. ABCDEFGHIXYZ
  283. ABCDEFGHIXYZ]])
  284. end)
  285. it([["\%1l^#.*" does not match on a line starting with "#". (vim-patch:7.4.1305)]], function()
  286. source([[
  287. 1 s/\%#=0\%1l^\t...//g
  288. 2 s/\%#=1\%2l^\t...//g
  289. 3 s/\%#=2\%3l^\t...//g
  290. 4 s/\%#=0\%4l^\t...//g
  291. 5 s/\%#=1\%5l^\t...//g
  292. 6 s/\%#=2\%6l^\t...//g]])
  293. diff(
  294. sixlines(
  295. string.sub(punct1, 1)
  296. .. digits
  297. .. punct2
  298. .. upper
  299. .. punct3
  300. .. lower
  301. .. punct4
  302. .. ctrl2
  303. .. iso_text
  304. )
  305. )
  306. end)
  307. it('does not convert character class ranges to an incorrect class', function()
  308. source([[
  309. 1 s/\%#=0[0-z]//g
  310. 2 s/\%#=1[0-z]//g
  311. 3 s/\%#=2[0-z]//g
  312. 4 s/\%#=0[^0-z]//g
  313. 5 s/\%#=1[^0-z]//g
  314. 6 s/\%#=2[^0-z]//g
  315. ]])
  316. diff(
  317. string.rep(ctrl1 .. punct1 .. punct4 .. ctrl2 .. iso_text .. '\n', 3)
  318. .. string.rep(digits .. punct2 .. upper .. punct3 .. lower .. '\n', 3)
  319. )
  320. end)
  321. end)