neovim

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

null_spec.lua (9422B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local exc_exec = n.exc_exec
      5 local command = n.command
      6 local clear = n.clear
      7 local api = n.api
      8 local fn = n.fn
      9 local eq = t.eq
     10 
     11 local function redir_exec(cmd)
     12  api.nvim_set_var('__redir_exec_cmd', cmd)
     13  command([[
     14    redir => g:__redir_exec_output
     15      silent! execute g:__redir_exec_cmd
     16    redir END
     17  ]])
     18  local ret = api.nvim_get_var('__redir_exec_output')
     19  api.nvim_del_var('__redir_exec_output')
     20  api.nvim_del_var('__redir_exec_cmd')
     21  return ret
     22 end
     23 
     24 describe('NULL', function()
     25  before_each(function()
     26    clear()
     27    command('let L = v:_null_list')
     28    command('let D = v:_null_dict')
     29    command('let S = v:_null_string')
     30    command('let V = $XXX_NONEXISTENT_VAR_XXX')
     31  end)
     32  local tmpfname = 'Xtest-functional-viml-null'
     33  after_each(function()
     34    os.remove(tmpfname)
     35  end)
     36  local null_test = function(name, cmd, err)
     37    it(name, function()
     38      eq(err, exc_exec(cmd))
     39    end)
     40  end
     41  local null_expr_test = function(name, expr, err, val, after)
     42    it(name, function()
     43      eq((err == 0) and '' or ('\n' .. err), redir_exec('let g:_var = ' .. expr))
     44      if val == nil then
     45        eq(0, fn.exists('g:_var'))
     46      else
     47        eq(val, api.nvim_get_var('_var'))
     48      end
     49      if after ~= nil then
     50        after()
     51      end
     52    end)
     53  end
     54  describe('list', function()
     55    -- Subjectable behaviour
     56    null_expr_test('is equal to empty list', 'L == []', 0, 1)
     57    null_expr_test('is equal to empty list (reverse order)', '[] == L', 0, 1)
     58 
     59    -- Correct behaviour
     60    null_test(
     61      'can be :unlet item with error message for empty list',
     62      ':unlet L[0]',
     63      'Vim(unlet):E684: List index out of range: 0'
     64    )
     65    null_expr_test(
     66      'can be indexed with error message for empty list',
     67      'L[0]',
     68      'E684: List index out of range: 0',
     69      nil
     70    )
     71    null_expr_test('can be splice-indexed', 'L[:]', 0, {})
     72    null_expr_test('is not locked', 'islocked("v:_null_list")', 0, 0)
     73    null_test('is accepted by :for', 'for x in L|throw x|endfor', 0)
     74    null_expr_test('does not crash append()', 'append(0, L)', 0, 0, function()
     75      eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
     76    end)
     77    null_expr_test('does not crash setline()', 'setline(1, L)', 0, 0, function()
     78      eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
     79    end)
     80    null_expr_test('is identical to itself', 'L is L', 0, 1)
     81    null_expr_test('can be sliced', 'L[:]', 0, {})
     82    null_expr_test('can be copied', 'copy(L)', 0, {})
     83    null_expr_test('can be deepcopied', 'deepcopy(L)', 0, {})
     84    null_expr_test('does not crash when indexed', 'L[1]', 'E684: List index out of range: 1', nil)
     85    null_expr_test('does not crash call()', 'call("arglistid", L)', 0, 0)
     86    null_expr_test('does not crash col()', 'col(L)', 0, 0)
     87    null_expr_test('does not crash virtcol()', 'virtcol(L)', 0, 0)
     88    null_expr_test('does not crash line()', 'line(L)', 0, 0)
     89    null_expr_test('does not crash line() with window id', 'line(L, 1000)', 0, 0)
     90    null_expr_test('does not crash count()', 'count(L, 1)', 0, 0)
     91    null_expr_test('does not crash cursor()', 'cursor(L)', 'E474: Invalid argument', -1)
     92    null_expr_test('does not crash map()', 'map(L, "v:val")', 0, {})
     93    null_expr_test('does not crash filter()', 'filter(L, "1")', 0, {})
     94    null_expr_test('is empty', 'empty(L)', 0, 1)
     95    null_expr_test('does not crash get()', 'get(L, 1, 10)', 0, 10)
     96    null_expr_test('has zero length', 'len(L)', 0, 0)
     97    null_expr_test('is accepted as an empty list by max()', 'max(L)', 0, 0)
     98    null_expr_test('is accepted as an empty list by min()', 'min(L)', 0, 0)
     99    null_expr_test('is stringified correctly', 'string(L)', 0, '[]')
    100    null_expr_test('is JSON encoded correctly', 'json_encode(L)', 0, '[]')
    101    null_test('does not crash lockvar', 'lockvar! L', 0)
    102    null_expr_test('can be added to itself', '(L + L)', 0, {})
    103    null_expr_test('can be added to itself', '(L + L) is L', 0, 1)
    104    null_expr_test('can be added to non-empty list', '([1] + L)', 0, { 1 })
    105    null_expr_test('can be added to non-empty list (reversed)', '(L + [1])', 0, { 1 })
    106    null_expr_test('is equal to itself', 'L == L', 0, 1)
    107    null_expr_test('is not not equal to itself', 'L != L', 0, 0)
    108    null_expr_test('counts correctly', 'count([L], L)', 0, 1)
    109    null_expr_test('makes map() return v:_null_list', 'map(L, "v:val") is# L', 0, 1)
    110    null_expr_test('makes filter() return v:_null_list', 'filter(L, "1") is# L', 0, 1)
    111    null_test(
    112      'is treated by :let as empty list',
    113      ':let [l] = L',
    114      'Vim(let):E688: More targets than List items'
    115    )
    116    null_expr_test(
    117      'is accepted as an empty list by inputlist()',
    118      '[feedkeys("\\n"), inputlist(L)]',
    119      '',
    120      { 0, 0 }
    121    )
    122    null_expr_test(
    123      'is accepted as an empty list by writefile()',
    124      ('[writefile(L, "%s"), readfile("%s")]'):format(tmpfname, tmpfname),
    125      0,
    126      { 0, {} }
    127    )
    128    null_expr_test(
    129      'makes add() error out',
    130      'add(L, 0)',
    131      'E742: Cannot change value of add() argument',
    132      1
    133    )
    134    null_expr_test(
    135      'makes insert() error out',
    136      'insert(L, 1)',
    137      'E742: Cannot change value of insert() argument',
    138      0
    139    )
    140    null_expr_test(
    141      'does not crash remove()',
    142      'remove(L, 0)',
    143      'E742: Cannot change value of remove() argument',
    144      0
    145    )
    146    null_expr_test(
    147      'makes reverse() error out',
    148      'reverse(L)',
    149      'E742: Cannot change value of reverse() argument',
    150      0
    151    )
    152    null_expr_test(
    153      'makes sort() error out',
    154      'sort(L)',
    155      'E742: Cannot change value of sort() argument',
    156      0
    157    )
    158    null_expr_test(
    159      'makes uniq() error out',
    160      'uniq(L)',
    161      'E742: Cannot change value of uniq() argument',
    162      0
    163    )
    164    null_expr_test(
    165      'does not crash extend()',
    166      'extend(L, [1])',
    167      'E742: Cannot change value of extend() argument',
    168      0
    169    )
    170    null_expr_test('does not crash extend() (second position)', 'extend([1], L)', 0, { 1 })
    171    null_expr_test('makes join() return empty string', 'join(L, "")', 0, '')
    172    null_expr_test('makes msgpackdump() return empty list', 'msgpackdump(L)', 0, {})
    173    null_expr_test('does not crash system()', 'system("cat", L)', 0, '')
    174    null_expr_test('does not crash setreg', 'setreg("x", L)', 0, 0)
    175    null_expr_test('does not crash systemlist()', 'systemlist("cat", L)', 0, {})
    176    null_test(
    177      'does not make Neovim crash when v:oldfiles gets assigned to that',
    178      ':let v:oldfiles = L|oldfiles',
    179      0
    180    )
    181    null_expr_test(
    182      'does not make complete() crash or error out',
    183      'execute(":normal i\\<C-r>=complete(1, L)[-1]\\n")',
    184      0,
    185      '',
    186      function()
    187        eq({ '' }, api.nvim_buf_get_lines(0, 0, -1, false))
    188      end
    189    )
    190    null_expr_test('is accepted by setmatches()', 'setmatches(L)', 0, 0)
    191    null_expr_test('is accepted by setqflist()', 'setqflist(L)', 0, 0)
    192    null_expr_test('is accepted by setloclist()', 'setloclist(1, L)', 0, 0)
    193    null_test('is accepted by :cexpr', 'cexpr L', 0)
    194    null_test('is accepted by :lexpr', 'lexpr L', 0)
    195    null_expr_test('does not crash execute()', 'execute(L)', 0, '')
    196  end)
    197  describe('dict', function()
    198    it('does not crash when indexing NULL dict', function()
    199      eq('\nE716: Key not present in Dictionary: "test"', redir_exec('echo v:_null_dict.test'))
    200    end)
    201    null_expr_test(
    202      'makes extend error out',
    203      'extend(D, {})',
    204      'E742: Cannot change value of extend() argument',
    205      0
    206    )
    207    null_expr_test('makes extend do nothing', 'extend({1: 2}, D)', 0, { ['1'] = 2 })
    208    null_expr_test('does not crash map()', 'map(D, "v:val")', 0, {})
    209    null_expr_test('does not crash filter()', 'filter(D, "1")', 0, {})
    210    null_expr_test('makes map() return v:_null_dict', 'map(D, "v:val") is# D', 0, 1)
    211    null_expr_test('makes filter() return v:_null_dict', 'filter(D, "1") is# D', 0, 1)
    212  end)
    213  describe('string', function()
    214    null_test('does not crash :echomsg', 'echomsg S', 0)
    215    null_test('does not crash :execute', 'execute S', 0)
    216    null_expr_test('does not crash execute()', 'execute(S)', 0, '')
    217    null_expr_test('does not crash executable()', 'executable(S)', 0, 0)
    218    null_expr_test(
    219      'makes timer_start() error out',
    220      'timer_start(0, S)',
    221      'E921: Invalid callback argument',
    222      -1
    223    )
    224    null_expr_test('does not crash filereadable()', 'filereadable(S)', 0, 0)
    225    null_expr_test('does not crash filewritable()', 'filewritable(S)', 0, 0)
    226    null_expr_test('does not crash fnamemodify()', 'fnamemodify(S, S)', 0, '')
    227    null_expr_test('does not crash getfperm()', 'getfperm(S)', 0, '')
    228    null_expr_test('does not crash getfsize()', 'getfsize(S)', 0, -1)
    229    null_expr_test('does not crash getftime()', 'getftime(S)', 0, -1)
    230    null_expr_test('does not crash getftype()', 'getftype(S)', 0, '')
    231    null_expr_test('does not crash glob()', 'glob(S)', 0, '')
    232    null_expr_test('does not crash globpath()', 'globpath(S, S)', 0, '')
    233    null_expr_test('does not crash mkdir()', 'mkdir(S)', 0, 0)
    234    null_expr_test('does not crash sort()', 'sort(["b", S, "a"])', 0, { '', 'a', 'b' })
    235    null_expr_test('does not crash split()', 'split(S)', 0, {})
    236    null_test('can be used to set an option', 'let &grepprg = S', 0)
    237 
    238    null_expr_test('is equal to non-existent variable', 'S == V', 0, 1)
    239  end)
    240 end)