neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

044_099_regexp_multibyte_magic_spec.lua (3995B)


      1 -- Tests for regexp with multi-byte encoding and various magic settings.
      2 -- Test matchstr() with a count and multi-byte chars.
      3 --
      4 -- This test contains both "test44" and "test99" from the old test suite.
      5 
      6 local n = require('test.functional.testnvim')()
      7 
      8 local feed, insert = n.feed, n.insert
      9 local clear, feed_command, expect = n.clear, n.feed_command, n.expect
     10 
     11 -- Runs the test protocol with the given 'regexpengine' setting. In the old test
     12 -- suite the test protocol was duplicated in test44 and test99, the only
     13 -- difference being the 'regexpengine' setting. We've extracted it here.
     14 local function run_test_with_regexpengine(regexpengine)
     15  insert([[
     16    1 a aa abb abbccc
     17    2 d dd dee deefff
     18    3 g gg ghh ghhiii
     19    4 j jj jkk jkklll
     20    5 m mm mnn mnnooo
     21    6 x ^aa$ x
     22    7 (a)(b) abbaa
     23    8 axx [ab]xx
     24    9 หม่x อมx
     25    a อมx หม่x
     26    b ちカヨは
     27    c x ¬€x
     28    d 天使x
     29    e ������y
     30    f ������z
     31    g a啷bb
     32    j 0123x
     33    k combinations
     34    l ä ö ü ᾱ̆́]])
     35 
     36  feed_command('set re=' .. regexpengine)
     37 
     38  -- Lines 1-8. Exercise regexp search with various magic settings. On each
     39  -- line the character on which the cursor is expected to land is deleted.
     40  feed('/^1<cr>')
     41  feed([[/a*b\{2}c\+/e<cr>x]])
     42  feed([[/\Md\*e\{2}f\+/e<cr>x]])
     43  feed_command('set nomagic')
     44  feed([[/g\*h\{2}i\+/e<cr>x]])
     45  feed([[/\mj*k\{2}l\+/e<cr>x]])
     46  feed([[/\vm*n{2}o+/e<cr>x]])
     47  feed([[/\V^aa$<cr>x]])
     48  feed_command('set magic')
     49  feed([[/\v(a)(b)\2\1\1/e<cr>x]])
     50  feed([[/\V[ab]\(\[xy]\)\1<cr>x]])
     51 
     52  -- Line 9. Search for multi-byte character without combining character.
     53  feed('/ม<cr>x')
     54 
     55  -- Line a. Search for multi-byte character with combining character.
     56  feed('/ม่<cr>x')
     57 
     58  -- Line b. Find word by change of word class.
     59  -- (The "<" character in this test step seemed to confuse our "feed" test
     60  -- helper, which is why we've resorted to "execute" here.)
     61  feed_command([[/ち\<カヨ\>]])
     62  feed('x')
     63 
     64  -- Lines c-i. Test \%u, [\u], and friends.
     65  feed([[/\%u20ac<cr>x]])
     66  feed([[/[\u4f7f\u5929]\+<cr>x]])
     67  feed([[/\%U12345678<cr>x]])
     68  feed([[/[\U1234abcd\u1234\uabcd]<cr>x]])
     69  feed([[/\%d21879b<cr>x]])
     70 
     71  -- Line j. Test backwards search from a multi-byte character.
     72  feed('/x<cr>x')
     73  feed('?.<cr>x')
     74 
     75  -- Line k. Test substitution with combining characters by executing register
     76  -- contents.
     77  feed_command([[let @w=':%s#comb[i]nations#œ̄ṣ́m̥̄ᾱ̆́#g']])
     78  feed_command('@w')
     79 
     80  -- Line l. Ex command ":s/ \?/ /g" should NOT split multi-byte characters
     81  -- into bytes (fixed by vim-7.3.192).
     82  feed_command([[/^l]])
     83  feed_command([[s/ \?/ /g]])
     84 
     85  -- Additional tests. Test matchstr() with multi-byte characters.
     86  feed('G')
     87  feed_command([[put =matchstr(\"אבגד\", \".\", 0, 2)]])   -- ב
     88  feed_command([[put =matchstr(\"אבגד\", \"..\", 0, 2)]])  -- בג
     89  feed_command([[put =matchstr(\"אבגד\", \".\", 0, 0)]])   -- א
     90  feed_command([[put =matchstr(\"אבגד\", \".\", 4, -1)]])  -- ג
     91 
     92  -- Test that a search with "/e" offset wraps around at the end of the buffer.
     93  feed_command('new')
     94  feed_command([[$put =['dog(a', 'cat('] ]])
     95  feed('/(/e+<cr>')
     96  feed('"ayn')
     97  feed_command('bd!')
     98  feed_command([[$put ='']])
     99  feed('G"ap')
    100 
    101  -- Assert buffer contents.
    102  expect([[
    103    1 a aa abb abbcc
    104    2 d dd dee deeff
    105    3 g gg ghh ghhii
    106    4 j jj jkk jkkll
    107    5 m mm mnn mnnoo
    108    6 x aa$ x
    109    7 (a)(b) abba
    110    8 axx ab]xx
    111    9 หม่x อx
    112    a อมx หx
    113    b カヨは
    114    c x ¬x
    115    d 使x
    116    e y
    117    f z
    118    g abb
    119    j 012❤
    120    k œ̄ṣ́m̥̄ᾱ̆́
    121     l ä ö ü ᾱ̆́
    122    ב
    123    בג
    124    א
    125    ג
    126    a
    127    cat(]])
    128 end
    129 
    130 describe('multi-byte regexp search with magic settings', function()
    131  before_each(clear)
    132 
    133  it('is working with regexpengine=1', function()
    134    -- The old test44.
    135    run_test_with_regexpengine(1)
    136  end)
    137 
    138  it('is working with regexpengine=2', function()
    139    -- The old test99.
    140    run_test_with_regexpengine(2)
    141  end)
    142 end)