neovim

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

changedtick_spec.lua (5680B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local eq = t.eq
      5 local eval = n.eval
      6 local feed = n.feed
      7 local clear = n.clear
      8 local fn = n.fn
      9 local api = n.api
     10 local command = n.command
     11 local exc_exec = n.exc_exec
     12 local pcall_err = t.pcall_err
     13 local exec_capture = n.exec_capture
     14 
     15 before_each(clear)
     16 
     17 local function changedtick()
     18  local ct = api.nvim_buf_get_changedtick(0)
     19  eq(ct, api.nvim_buf_get_var(0, 'changedtick'))
     20  eq(ct, api.nvim_buf_get_var(0, 'changedtick'))
     21  eq(ct, eval('b:changedtick'))
     22  eq(ct, eval('b:["changedtick"]'))
     23  eq(ct, eval('b:.changedtick'))
     24  eq(ct, fn.getbufvar('%', 'changedtick'))
     25  eq(ct, fn.getbufvar('%', '').changedtick)
     26  eq(ct, eval('b:').changedtick)
     27  return ct
     28 end
     29 
     30 describe('b:changedtick', function()
     31  -- Ported tests from Vim-8.0.333
     32  it('increments', function() -- Test_changedtick_increments
     33    -- New buffer has an empty line, tick starts at 2
     34    eq(2, changedtick())
     35    fn.setline(1, 'hello')
     36    eq(3, changedtick())
     37    eq(0, exc_exec('undo'))
     38    -- Somehow undo counts as two changes
     39    eq(5, changedtick())
     40  end)
     41  it('is present in b: dict', function()
     42    eq(2, changedtick())
     43    command('let d = b:')
     44    eq(2, api.nvim_get_var('d').changedtick)
     45  end)
     46  it('increments at bdel', function()
     47    command('new')
     48    eq(2, changedtick())
     49    local bnr = api.nvim_buf_get_number(0)
     50    eq(2, bnr)
     51    command('bdel')
     52    eq(3, fn.getbufvar(bnr, 'changedtick'))
     53    eq(1, api.nvim_buf_get_number(0))
     54  end)
     55  it('fails to be changed by user', function()
     56    local ct = changedtick()
     57    local ctn = ct + 100500
     58    eq(0, exc_exec('let d = b:'))
     59    eq(
     60      'Vim(let):E46: Cannot change read-only variable "b:changedtick"',
     61      pcall_err(command, 'let b:changedtick = ' .. ctn)
     62    )
     63    eq(
     64      'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
     65      pcall_err(command, 'let b:["changedtick"] = ' .. ctn)
     66    )
     67    eq(
     68      'Vim(let):E46: Cannot change read-only variable "b:.changedtick"',
     69      pcall_err(command, 'let b:.changedtick = ' .. ctn)
     70    )
     71    eq(
     72      'Vim(let):E46: Cannot change read-only variable "d.changedtick"',
     73      pcall_err(command, 'let d.changedtick = ' .. ctn)
     74    )
     75    eq('Key is read-only: changedtick', pcall_err(api.nvim_buf_set_var, 0, 'changedtick', ctn))
     76 
     77    eq(
     78      'Vim(unlet):E795: Cannot delete variable b:changedtick',
     79      pcall_err(command, 'unlet b:changedtick')
     80    )
     81    eq(
     82      'Vim(unlet):E46: Cannot change read-only variable "b:.changedtick"',
     83      pcall_err(command, 'unlet b:.changedtick')
     84    )
     85    eq(
     86      'Vim(unlet):E46: Cannot change read-only variable "b:["changedtick"]"',
     87      pcall_err(command, 'unlet b:["changedtick"]')
     88    )
     89    eq(
     90      'Vim(unlet):E46: Cannot change read-only variable "d.changedtick"',
     91      pcall_err(command, 'unlet d.changedtick')
     92    )
     93    eq('Key is read-only: changedtick', pcall_err(api.nvim_buf_del_var, 0, 'changedtick'))
     94    eq(ct, changedtick())
     95 
     96    eq(
     97      'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
     98      pcall_err(command, 'let b:["changedtick"] += ' .. ctn)
     99    )
    100    eq(
    101      'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
    102      pcall_err(command, 'let b:["changedtick"] -= ' .. ctn)
    103    )
    104    eq(
    105      'Vim(let):E46: Cannot change read-only variable "b:["changedtick"]"',
    106      pcall_err(command, 'let b:["changedtick"] .= ' .. ctn)
    107    )
    108 
    109    eq(ct, changedtick())
    110 
    111    fn.setline(1, 'hello')
    112 
    113    eq(ct + 1, changedtick())
    114  end)
    115  it('is listed in :let output', function()
    116    eq('b:changedtick         #2', exec_capture(':let b:'))
    117  end)
    118  it('fails to unlock b:changedtick', function()
    119    eq(0, exc_exec('let d = b:'))
    120    eq(0, fn.islocked('b:changedtick'))
    121    eq(0, fn.islocked('d.changedtick'))
    122    eq(
    123      'Vim(unlockvar):E940: Cannot lock or unlock variable b:changedtick',
    124      pcall_err(command, 'unlockvar b:changedtick')
    125    )
    126    eq(
    127      'Vim(unlockvar):E46: Cannot change read-only variable "d.changedtick"',
    128      pcall_err(command, 'unlockvar d.changedtick')
    129    )
    130    eq(0, fn.islocked('b:changedtick'))
    131    eq(0, fn.islocked('d.changedtick'))
    132    eq(
    133      'Vim(lockvar):E940: Cannot lock or unlock variable b:changedtick',
    134      pcall_err(command, 'lockvar b:changedtick')
    135    )
    136    eq(
    137      'Vim(lockvar):E46: Cannot change read-only variable "d.changedtick"',
    138      pcall_err(command, 'lockvar d.changedtick')
    139    )
    140    eq(0, fn.islocked('b:changedtick'))
    141    eq(0, fn.islocked('d.changedtick'))
    142  end)
    143  it('is being completed', function()
    144    feed(':echo b:<Tab><Home>let cmdline="<End>"<CR>')
    145    eq('echo b:changedtick', api.nvim_get_var('cmdline'))
    146  end)
    147  it('cannot be changed by filter() or map()', function()
    148    eq(2, changedtick())
    149    eq(
    150      'Vim(call):E795: Cannot delete variable filter() argument',
    151      pcall_err(command, 'call filter(b:, 0)')
    152    )
    153    eq(
    154      'Vim(call):E742: Cannot change value of map() argument',
    155      pcall_err(command, 'call map(b:, 0)')
    156    )
    157    eq(
    158      'Vim(call):E742: Cannot change value of map() argument',
    159      pcall_err(command, 'call map(b:, "v:val")')
    160    )
    161    eq(2, changedtick())
    162  end)
    163  it('cannot be remove()d', function()
    164    eq(2, changedtick())
    165    eq(
    166      'Vim(call):E795: Cannot delete variable remove() argument',
    167      pcall_err(command, 'call remove(b:, "changedtick")')
    168    )
    169    eq(2, changedtick())
    170  end)
    171  it('does not inherit VAR_FIXED when copying dict over', function()
    172    eq(2, changedtick())
    173    eq('', exec_capture('let d1 = copy(b:)|let d1.changedtick = 42'))
    174    eq('', exec_capture('let d2 = copy(b:)|unlet d2.changedtick'))
    175    eq(2, changedtick())
    176  end)
    177 end)