neovim

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

match_functions_spec.lua (4941B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 local Screen = require('test.functional.ui.screen')
      4 
      5 local eq = t.eq
      6 local clear = n.clear
      7 local fn = n.fn
      8 local command = n.command
      9 local exc_exec = n.exc_exec
     10 
     11 before_each(clear)
     12 
     13 describe('setmatches()', function()
     14  it('correctly handles case when both group and pattern entries are numbers', function()
     15    command('hi def link 1 PreProc')
     16    eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4 } }))
     17    eq({
     18      {
     19        group = '1',
     20        pattern = '2',
     21        id = 3,
     22        priority = 4,
     23      },
     24    }, fn.getmatches())
     25    eq(0, fn.setmatches({ { group = 1, pattern = 2, id = 3, priority = 4, conceal = 5 } }))
     26    eq({
     27      {
     28        group = '1',
     29        pattern = '2',
     30        id = 3,
     31        priority = 4,
     32        conceal = '5',
     33      },
     34    }, fn.getmatches())
     35    eq(
     36      0,
     37      fn.setmatches({
     38        { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
     39      })
     40    )
     41    eq({
     42      {
     43        group = '1',
     44        pos1 = { 2 },
     45        pos2 = { 6 },
     46        id = 3,
     47        priority = 4,
     48        conceal = '5',
     49      },
     50    }, fn.getmatches())
     51  end)
     52 
     53  it('does not fail if highlight group is not defined', function()
     54    eq(0, fn.setmatches { { group = 1, pattern = 2, id = 3, priority = 4 } })
     55    eq({ { group = '1', pattern = '2', id = 3, priority = 4 } }, fn.getmatches())
     56    eq(
     57      0,
     58      fn.setmatches {
     59        { group = 1, pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = 5 },
     60      }
     61    )
     62    eq(
     63      { { group = '1', pos1 = { 2 }, pos2 = { 6 }, id = 3, priority = 4, conceal = '5' } },
     64      fn.getmatches()
     65    )
     66  end)
     67 end)
     68 
     69 describe('matchadd()', function()
     70  it('correctly works when first two arguments and conceal are numbers at once', function()
     71    command('hi def link 1 PreProc')
     72    eq(4, fn.matchadd(1, 2, 3, 4, { conceal = 5 }))
     73    eq({
     74      {
     75        group = '1',
     76        pattern = '2',
     77        priority = 3,
     78        id = 4,
     79        conceal = '5',
     80      },
     81    }, fn.getmatches())
     82  end)
     83 end)
     84 
     85 describe('matchaddpos()', function()
     86  it('errors out on invalid input', function()
     87    command('hi clear PreProc')
     88    eq(
     89      'Vim(let):E5030: Empty list at position 0',
     90      exc_exec('let val = matchaddpos("PreProc", [[]])')
     91    )
     92    eq(
     93      'Vim(let):E5030: Empty list at position 1',
     94      exc_exec('let val = matchaddpos("PreProc", [1, v:_null_list])')
     95    )
     96    eq(
     97      'Vim(let):E5031: List or number required at position 1',
     98      exc_exec('let val = matchaddpos("PreProc", [1, v:_null_dict])')
     99    )
    100  end)
    101  it('works with 0 lnum', function()
    102    command('hi clear PreProc')
    103    eq(4, fn.matchaddpos('PreProc', { 1 }, 3, 4))
    104    eq({
    105      {
    106        group = 'PreProc',
    107        pos1 = { 1 },
    108        priority = 3,
    109        id = 4,
    110      },
    111    }, fn.getmatches())
    112    fn.matchdelete(4)
    113    eq(4, fn.matchaddpos('PreProc', { { 0 }, 1 }, 3, 4))
    114    eq({
    115      {
    116        group = 'PreProc',
    117        pos1 = { 1 },
    118        priority = 3,
    119        id = 4,
    120      },
    121    }, fn.getmatches())
    122    fn.matchdelete(4)
    123    eq(4, fn.matchaddpos('PreProc', { 0, 1 }, 3, 4))
    124    eq({
    125      {
    126        group = 'PreProc',
    127        pos1 = { 1 },
    128        priority = 3,
    129        id = 4,
    130      },
    131    }, fn.getmatches())
    132  end)
    133  it('works with negative numbers', function()
    134    command('hi clear PreProc')
    135    eq(4, fn.matchaddpos('PreProc', { -10, 1 }, 3, 4))
    136    eq({
    137      {
    138        group = 'PreProc',
    139        pos1 = { 1 },
    140        priority = 3,
    141        id = 4,
    142      },
    143    }, fn.getmatches())
    144    fn.matchdelete(4)
    145    eq(4, fn.matchaddpos('PreProc', { { -10 }, 1 }, 3, 4))
    146    eq({
    147      {
    148        group = 'PreProc',
    149        pos1 = { 1 },
    150        priority = 3,
    151        id = 4,
    152      },
    153    }, fn.getmatches())
    154    fn.matchdelete(4)
    155    eq(4, fn.matchaddpos('PreProc', { { 2, -1 }, 1 }, 3, 4))
    156    eq({
    157      {
    158        group = 'PreProc',
    159        pos1 = { 1 },
    160        priority = 3,
    161        id = 4,
    162      },
    163    }, fn.getmatches())
    164    fn.matchdelete(4)
    165    eq(4, fn.matchaddpos('PreProc', { { 2, 0, -1 }, 1 }, 3, 4))
    166    eq({
    167      {
    168        group = 'PreProc',
    169        pos1 = { 1 },
    170        priority = 3,
    171        id = 4,
    172      },
    173    }, fn.getmatches())
    174  end)
    175  it('works with zero length', function()
    176    local screen = Screen.new(40, 5)
    177    fn.setline(1, 'abcdef')
    178    command('hi PreProc guifg=Red')
    179    eq(4, fn.matchaddpos('PreProc', { { 1, 2, 0 } }, 3, 4))
    180    eq({
    181      {
    182        group = 'PreProc',
    183        pos1 = { 1, 2, 0 },
    184        priority = 3,
    185        id = 4,
    186      },
    187    }, fn.getmatches())
    188    screen:expect(
    189      [[
    190      ^a{1:b}cdef                                  |
    191      {2:~                                       }|*3
    192                                              |
    193    ]],
    194      {
    195        [1] = { foreground = Screen.colors.Red },
    196        [2] = { bold = true, foreground = Screen.colors.Blue1 },
    197      }
    198    )
    199  end)
    200 end)