aboutsummaryrefslogtreecommitdiff
path: root/test/functional/eval
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/eval')
-rw-r--r--test/functional/eval/changedtick_spec.lua142
-rw-r--r--test/functional/eval/writefile_spec.lua140
2 files changed, 282 insertions, 0 deletions
diff --git a/test/functional/eval/changedtick_spec.lua b/test/functional/eval/changedtick_spec.lua
new file mode 100644
index 0000000000..60ea9fa12b
--- /dev/null
+++ b/test/functional/eval/changedtick_spec.lua
@@ -0,0 +1,142 @@
+local helpers = require('test.functional.helpers')(after_each)
+
+local eq = helpers.eq
+local eval = helpers.eval
+local feed = helpers.feed
+local clear = helpers.clear
+local funcs = helpers.funcs
+local meths = helpers.meths
+local command = helpers.command
+local exc_exec = helpers.exc_exec
+local redir_exec = helpers.redir_exec
+local meth_pcall = helpers.meth_pcall
+local curbufmeths = helpers.curbufmeths
+
+before_each(clear)
+
+local function changedtick()
+ local ct = curbufmeths.get_changedtick()
+ eq(ct, curbufmeths.get_var('changedtick'))
+ eq(ct, curbufmeths.get_var('changedtick'))
+ eq(ct, eval('b:changedtick'))
+ eq(ct, eval('b:["changedtick"]'))
+ eq(ct, eval('b:.changedtick'))
+ eq(ct, funcs.getbufvar('%', 'changedtick'))
+ eq(ct, funcs.getbufvar('%', '').changedtick)
+ eq(ct, eval('b:').changedtick)
+ return ct
+end
+
+describe('b:changedtick', function()
+ -- Ported tests from Vim-8.0.333
+ it('increments', function() -- Test_changedtick_increments
+ -- New buffer has an empty line, tick starts at 2
+ eq(2, changedtick())
+ funcs.setline(1, 'hello')
+ eq(3, changedtick())
+ eq(0, exc_exec('undo'))
+ -- Somehow undo counts as two changes
+ eq(5, changedtick())
+ end)
+ it('is present in b: dictionary', function()
+ eq(2, changedtick())
+ command('let d = b:')
+ eq(2, meths.get_var('d').changedtick)
+ end)
+ it('increments at bdel', function()
+ command('new')
+ eq(2, changedtick())
+ local bnr = curbufmeths.get_number()
+ eq(2, bnr)
+ command('bdel')
+ eq(3, funcs.getbufvar(bnr, 'changedtick'))
+ eq(1, curbufmeths.get_number())
+ end)
+ it('fails to be changed by user', function()
+ local ct = changedtick()
+ local ctn = ct + 100500
+ eq(0, exc_exec('let d = b:'))
+ eq('\nE46: Cannot change read-only variable "b:changedtick"',
+ redir_exec('let b:changedtick = ' .. ctn))
+ eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
+ redir_exec('let b:["changedtick"] = ' .. ctn))
+ eq('\nE46: Cannot change read-only variable "b:.changedtick"',
+ redir_exec('let b:.changedtick = ' .. ctn))
+ eq('\nE46: Cannot change read-only variable "d.changedtick"',
+ redir_exec('let d.changedtick = ' .. ctn))
+ eq({false, 'Key is read-only: changedtick'},
+ meth_pcall(curbufmeths.set_var, 'changedtick', ctn))
+
+ eq('\nE795: Cannot delete variable b:changedtick',
+ redir_exec('unlet b:changedtick'))
+ eq('\nE46: Cannot change read-only variable "b:.changedtick"',
+ redir_exec('unlet b:.changedtick'))
+ eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
+ redir_exec('unlet b:["changedtick"]'))
+ eq('\nE46: Cannot change read-only variable "d.changedtick"',
+ redir_exec('unlet d.changedtick'))
+ eq({false, 'Key is read-only: changedtick'},
+ meth_pcall(curbufmeths.del_var, 'changedtick'))
+ eq(ct, changedtick())
+
+ eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
+ redir_exec('let b:["changedtick"] += ' .. ctn))
+ eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
+ redir_exec('let b:["changedtick"] -= ' .. ctn))
+ eq('\nE46: Cannot change read-only variable "b:["changedtick"]"',
+ redir_exec('let b:["changedtick"] .= ' .. ctn))
+
+ eq(ct, changedtick())
+
+ funcs.setline(1, 'hello')
+
+ eq(ct + 1, changedtick())
+ end)
+ it('is listed in :let output', function()
+ eq('\nb:changedtick #2',
+ redir_exec(':let b:'))
+ end)
+ it('fails to unlock b:changedtick', function()
+ eq(0, exc_exec('let d = b:'))
+ eq(0, funcs.islocked('b:changedtick'))
+ eq(0, funcs.islocked('d.changedtick'))
+ eq('\nE940: Cannot lock or unlock variable b:changedtick',
+ redir_exec('unlockvar b:changedtick'))
+ eq('\nE46: Cannot change read-only variable "d.changedtick"',
+ redir_exec('unlockvar d.changedtick'))
+ eq(0, funcs.islocked('b:changedtick'))
+ eq(0, funcs.islocked('d.changedtick'))
+ eq('\nE940: Cannot lock or unlock variable b:changedtick',
+ redir_exec('lockvar b:changedtick'))
+ eq('\nE46: Cannot change read-only variable "d.changedtick"',
+ redir_exec('lockvar d.changedtick'))
+ eq(0, funcs.islocked('b:changedtick'))
+ eq(0, funcs.islocked('d.changedtick'))
+ end)
+ it('is being completed', function()
+ feed(':echo b:<Tab><Home>let cmdline="<End>"<CR>')
+ eq('echo b:changedtick', meths.get_var('cmdline'))
+ end)
+ it('cannot be changed by filter() or map()', function()
+ eq(2, changedtick())
+ eq('\nE795: Cannot delete variable filter() argument',
+ redir_exec('call filter(b:, 0)'))
+ eq('\nE742: Cannot change value of map() argument',
+ redir_exec('call map(b:, 0)'))
+ eq('\nE742: Cannot change value of map() argument',
+ redir_exec('call map(b:, "v:val")'))
+ eq(2, changedtick())
+ end)
+ it('cannot be remove()d', function()
+ eq(2, changedtick())
+ eq('\nE795: Cannot delete variable remove() argument',
+ redir_exec('call remove(b:, "changedtick")'))
+ eq(2, changedtick())
+ end)
+ it('does not inherit VAR_FIXED when copying dictionary over', function()
+ eq(2, changedtick())
+ eq('', redir_exec('let d1 = copy(b:)|let d1.changedtick = 42'))
+ eq('', redir_exec('let d2 = copy(b:)|unlet d2.changedtick'))
+ eq(2, changedtick())
+ end)
+end)
diff --git a/test/functional/eval/writefile_spec.lua b/test/functional/eval/writefile_spec.lua
new file mode 100644
index 0000000000..3052c616e0
--- /dev/null
+++ b/test/functional/eval/writefile_spec.lua
@@ -0,0 +1,140 @@
+local helpers = require('test.functional.helpers')(after_each)
+local lfs = require('lfs')
+
+local clear = helpers.clear
+local eq = helpers.eq
+local funcs = helpers.funcs
+local meths = helpers.meths
+local exc_exec = helpers.exc_exec
+local read_file = helpers.read_file
+local write_file = helpers.write_file
+local redir_exec = helpers.redir_exec
+
+local fname = 'Xtest-functional-eval-writefile'
+local dname = fname .. '.d'
+local dfname_tail = '1'
+local dfname = dname .. '/' .. dfname_tail
+local ddname_tail = '2'
+local ddname = dname .. '/' .. ddname_tail
+
+before_each(function()
+ lfs.mkdir(dname)
+ lfs.mkdir(ddname)
+ clear()
+end)
+
+after_each(function()
+ os.remove(fname)
+ os.remove(dfname)
+ lfs.rmdir(ddname)
+ lfs.rmdir(dname)
+end)
+
+describe('writefile()', function()
+ it('writes empty list to a file', function()
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({}, fname))
+ eq('', read_file(fname))
+ os.remove(fname)
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({}, fname, 'b'))
+ eq('', read_file(fname))
+ os.remove(fname)
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({}, fname, 'ab'))
+ eq('', read_file(fname))
+ os.remove(fname)
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({}, fname, 'a'))
+ eq('', read_file(fname))
+ end)
+
+ it('writes list with an empty string to a file', function()
+ eq(0, exc_exec(
+ ('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s", "b")'):format(
+ fname)))
+ eq('', read_file(fname))
+ eq(0, exc_exec(('call writefile([$XXX_NONEXISTENT_VAR_XXX], "%s")'):format(
+ fname)))
+ eq('\n', read_file(fname))
+ end)
+
+ it('appends to a file', function()
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({'abc', 'def', 'ghi'}, fname))
+ eq('abc\ndef\nghi\n', read_file(fname))
+ eq(0, funcs.writefile({'jkl'}, fname, 'a'))
+ eq('abc\ndef\nghi\njkl\n', read_file(fname))
+ os.remove(fname)
+ eq(nil, read_file(fname))
+ eq(0, funcs.writefile({'abc', 'def', 'ghi'}, fname, 'b'))
+ eq('abc\ndef\nghi', read_file(fname))
+ eq(0, funcs.writefile({'jkl'}, fname, 'ab'))
+ eq('abc\ndef\nghijkl', read_file(fname))
+ end)
+
+ it('correctly treats NLs', function()
+ eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b'))
+ eq('\0a\0b\0', read_file(fname))
+ eq(0, funcs.writefile({'a\n\n\nb'}, fname, 'b'))
+ eq('a\0\0\0b', read_file(fname))
+ end)
+
+ it('correctly overwrites file', function()
+ eq(0, funcs.writefile({'\na\nb\n'}, fname, 'b'))
+ eq('\0a\0b\0', read_file(fname))
+ eq(0, funcs.writefile({'a\n'}, fname, 'b'))
+ eq('a\0', read_file(fname))
+ end)
+
+ it('shows correct file name when supplied numbers', function()
+ meths.set_current_dir(dname)
+ eq('\nE482: Can\'t open file 2 for writing: illegal operation on a directory',
+ redir_exec(('call writefile([42], %s)'):format(ddname_tail)))
+ end)
+
+ it('errors out with invalid arguments', function()
+ write_file(fname, 'TEST')
+ eq('\nE119: Not enough arguments for function: writefile',
+ redir_exec('call writefile()'))
+ eq('\nE119: Not enough arguments for function: writefile',
+ redir_exec('call writefile([])'))
+ eq('\nE118: Too many arguments for function: writefile',
+ redir_exec(('call writefile([], "%s", "b", 1)'):format(fname)))
+ for _, arg in ipairs({'0', '0.0', 'function("tr")', '{}', '"test"'}) do
+ eq('\nE686: Argument of writefile() must be a List',
+ redir_exec(('call writefile(%s, "%s", "b")'):format(arg, fname)))
+ end
+ for _, args in ipairs({'[], %s, "b"', '[], "' .. fname .. '", %s'}) do
+ eq('\nE806: using Float as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('0.0'))))
+ eq('\nE730: using List as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('[]'))))
+ eq('\nE731: using Dictionary as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('{}'))))
+ eq('\nE729: using Funcref as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('function("tr")'))))
+ end
+ eq('TEST', read_file(fname))
+ end)
+
+ it('stops writing to file after error in list', function()
+ local args = '["tset"] + repeat([%s], 3), "' .. fname .. '"'
+ eq('\nE806: using Float as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('0.0'))))
+ eq('tset\n', read_file(fname))
+ write_file(fname, 'TEST')
+ eq('\nE730: using List as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('[]'))))
+ eq('tset\n', read_file(fname))
+ write_file(fname, 'TEST')
+ eq('\nE731: using Dictionary as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('{}'))))
+ eq('tset\n', read_file(fname))
+ write_file(fname, 'TEST')
+ eq('\nE729: using Funcref as a String',
+ redir_exec(('call writefile(%s)'):format(args:format('function("tr")'))))
+ eq('tset\n', read_file(fname))
+ write_file(fname, 'TEST')
+ end)
+end)