aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/functional/helpers.lua102
-rw-r--r--test/functional/legacy/002_filename_recognition_spec.lua42
-rw-r--r--test/functional/shell/viml_system_spec.lua125
-rw-r--r--test/legacy/test2-filename-recognition.vroom40
4 files changed, 269 insertions, 40 deletions
diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua
new file mode 100644
index 0000000000..671e34e592
--- /dev/null
+++ b/test/functional/helpers.lua
@@ -0,0 +1,102 @@
+local function clear()
+ nvim_command('call BeforeEachTest()')
+end
+
+local function feed(...)
+ for _, v in ipairs({...}) do
+ nvim_feed(nvim_replace_termcodes(dedent(v)))
+ end
+end
+
+local function rawfeed(...)
+ for _, v in ipairs({...}) do
+ nvim_feed(dedent(v), 'nt')
+ end
+end
+
+local function insert(...)
+ nvim_feed('i', 'nt')
+ rawfeed(...)
+ nvim_feed(nvim_replace_termcodes('<ESC>'), 'nt')
+end
+
+local function execute(...)
+ for _, v in ipairs({...}) do
+ if v:sub(1, 1) ~= '/' then
+ -- not a search command, prefix with colon
+ nvim_feed(':', 'nt')
+ end
+ nvim_feed(v, 'nt')
+ nvim_feed(nvim_replace_termcodes('<CR>'), 'nt')
+ end
+end
+
+local function eval(expr)
+ local status, result = pcall(function() return nvim_eval(expr) end)
+ if not status then
+ error('Failed to evaluate expression "' .. expr .. '"')
+ end
+ return result
+end
+
+local function eq(expected, actual)
+ return assert.are.same(expected, actual)
+end
+
+local function neq(expected, actual)
+ return assert.are_not.same(expected, actual)
+end
+
+local function expect(contents, first, last, buffer_index)
+ return eq(dedent(contents), buffer_slice(first, last, buffer_idx))
+end
+
+rawfeed([[:function BeforeEachTest()
+ set all&
+ redir => groups
+ silent augroup
+ redir END
+ for group in split(groups)
+ exe 'augroup '.group
+ autocmd!
+ augroup END
+ endfor
+ autocmd!
+ tabnew
+ let curbufnum = eval(bufnr('%'))
+ redir => buflist
+ silent ls!
+ redir END
+ let bufnums = []
+ for buf in split(buflist, '\n')
+ let bufnum = eval(split(buf, '[ u]')[0])
+ if bufnum != curbufnum
+ call add(bufnums, bufnum)
+ endif
+ endfor
+ if len(bufnums) > 0
+ exe 'silent bwipeout! '.join(bufnums, ' ')
+ endif
+ silent tabonly
+ for k in keys(g:)
+ exe 'unlet g:'.k
+ endfor
+ filetype plugin indent off
+ mapclear
+ mapclear!
+ abclear
+ comclear
+endfunction
+]])
+
+return {
+ clear = clear,
+ rawfeed = rawfeed,
+ insert = insert,
+ feed = feed,
+ execute = execute,
+ eval = eval,
+ eq = eq,
+ neq = neq,
+ expect = expect
+}
diff --git a/test/functional/legacy/002_filename_recognition_spec.lua b/test/functional/legacy/002_filename_recognition_spec.lua
new file mode 100644
index 0000000000..569e748631
--- /dev/null
+++ b/test/functional/legacy/002_filename_recognition_spec.lua
@@ -0,0 +1,42 @@
+-- Test if URLs are recognized as filenames by commands such as "gf". Here
+-- we'll use `expand("<cfile>")` since "gf" would need to open the file.
+
+local helpers = require('test.functional.helpers')
+local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
+local execute, expect = helpers.execute, helpers.expect
+
+describe('filename recognition', function()
+ setup(clear)
+
+ it('is working', function()
+ -- insert some lines containing URLs
+ insert([[
+ first test for URL://machine.name/tmp/vimtest2a and other text
+ second test for URL://machine.name/tmp/vimtest2b. And other text
+ third test for URL:\\machine.name\vimtest2c and other text
+ fourth test for URL:\\machine.name\tmp\vimtest2d, and other text]])
+
+ -- Go to the first URL and append it to the beginning
+ execute('/^first', '/tmp', 'call append(0, expand("<cfile>"))')
+
+ -- Repeat for the second URL
+ -- this time, navigate to the word "URL" instead of "tmp"
+ execute('/^second', '/URL', 'call append(1, expand("<cfile>"))')
+
+ -- Repeat for the remaining URLs. This time, the 'isfname' option must be
+ -- set to allow '\' in filenames
+ execute('set isf=@,48-57,/,.,-,_,+,,,$,:,~,\\')
+ execute('/^third', '/name', 'call append(2, expand("<cfile>"))')
+ execute('/^fourth', '/URL', 'call append(3, expand("<cfile>"))')
+
+ -- Delete the initial text, which now starts at line 5
+ feed('5GdG')
+
+ -- The buffer should now contain:
+ expect([[
+ URL://machine.name/tmp/vimtest2a
+ URL://machine.name/tmp/vimtest2b
+ URL:\\machine.name\vimtest2c
+ URL:\\machine.name\tmp\vimtest2d]])
+ end)
+end)
diff --git a/test/functional/shell/viml_system_spec.lua b/test/functional/shell/viml_system_spec.lua
new file mode 100644
index 0000000000..effabe715c
--- /dev/null
+++ b/test/functional/shell/viml_system_spec.lua
@@ -0,0 +1,125 @@
+-- Specs for
+-- - `system()`
+-- - `systemlist()`
+
+local helpers = require('test.functional.helpers')
+local eq, clear, eval, feed =
+ helpers.eq, helpers.clear, helpers.eval, helpers.feed
+
+
+local function create_file_with_nuls(name)
+ return function()
+ feed('ipart1<C-V>000part2<C-V>000part3<ESC>:w '..name..'<CR>')
+ end
+end
+
+local function delete_file(name)
+ return function()
+ eval("delete('"..name.."')")
+ end
+end
+
+
+describe('system()', function()
+ before_each(clear)
+
+ describe('passing no input', function()
+ it('returns the program output', function()
+ eq("echoed", eval('system("echo -n echoed")'))
+ end)
+ end)
+
+ describe('passing input', function()
+ it('returns the program output', function()
+ eq("input", eval('system("cat -", "input")'))
+ end)
+ end)
+
+ describe('passing number as input', function()
+ it('stringifies the input', function()
+ eq('1', eval('system("cat", 1)'))
+ end)
+ end)
+
+ describe('with output containing NULs', function()
+ local fname = 'Xtest'
+
+ setup(create_file_with_nuls(fname))
+ teardown(delete_file(fname))
+
+ it('replaces NULs by SOH characters', function()
+ eq('part1\001part2\001part3\n', eval('system("cat '..fname..'")'))
+ end)
+ end)
+
+ describe('passing list as input', function()
+ it('joins list items with linefeed characters', function()
+ eq('line1\nline2\nline3',
+ eval("system('cat -', ['line1', 'line2', 'line3'])"))
+ end)
+
+ -- Notice that NULs are converted to SOH when the data is read back. This
+ -- is inconsistent and is a good reason for the existence of the
+ -- `systemlist()` function, where input and output map to the same
+ -- characters(see the following tests with `systemlist()` below)
+ describe('with linefeed characters inside list items', function()
+ it('converts linefeed characters to NULs', function()
+ eq('l1\001p2\nline2\001a\001b\nl3',
+ eval([[system('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
+ end)
+ end)
+
+ describe('with leading/trailing whitespace characters on items', function()
+ it('preserves whitespace, replacing linefeeds by NULs', function()
+ eq('line \nline2\001\n\001line3',
+ eval([[system('cat -', ['line ', "line2\n", "\nline3"])]]))
+ end)
+ end)
+ end)
+end)
+
+describe('systemlist()', function()
+ -- behavior is similar to `system()` but it returns a list instead of a
+ -- string.
+ before_each(clear)
+
+ describe('passing string with linefeed characters as input', function()
+ it('splits the output on linefeed characters', function()
+ eq({'abc', 'def', 'ghi'}, eval([[systemlist("cat -", "abc\ndef\nghi")]]))
+ end)
+ end)
+
+ describe('with output containing NULs', function()
+ local fname = 'Xtest'
+
+ setup(create_file_with_nuls(fname))
+ teardown(delete_file(fname))
+
+ it('replaces NULs by newline characters', function()
+ eq({'part1\npart2\npart3'}, eval('systemlist("cat '..fname..'")'))
+ end)
+ end)
+
+ describe('passing list as input', function()
+ it('joins list items with linefeed characters', function()
+ eq({'line1', 'line2', 'line3'},
+ eval("systemlist('cat -', ['line1', 'line2', 'line3'])"))
+ end)
+
+ -- Unlike `system()` which uses SOH to represent NULs, with `systemlist()`
+ -- input and ouput are the same
+ describe('with linefeed characters inside list items', function()
+ it('converts linefeed characters to NULs', function()
+ eq({'l1\np2', 'line2\na\nb', 'l3'},
+ eval([[systemlist('cat -', ["l1\np2", "line2\na\nb", 'l3'])]]))
+ end)
+ end)
+
+ describe('with leading/trailing whitespace characters on items', function()
+ it('preserves whitespace, replacing linefeeds by NULs', function()
+ eq({'line ', 'line2\n', '\nline3'},
+ eval([[systemlist('cat -', ['line ', "line2\n", "\nline3"])]]))
+ end)
+ end)
+ end)
+end)
diff --git a/test/legacy/test2-filename-recognition.vroom b/test/legacy/test2-filename-recognition.vroom
deleted file mode 100644
index b8169ec207..0000000000
--- a/test/legacy/test2-filename-recognition.vroom
+++ /dev/null
@@ -1,40 +0,0 @@
-Test if URLs are recognized as filenames by commands such as "gf". Here
-we'll use `expand("<cfile>")` since "gf" would need to open the file.
-
-Insert some URLs:
-
- % first test for URL://machine.name/tmp/vimtest2a and other text<cr>
- % second test for URL://machine.name/tmp/vimtest2b. And other text<cr>
- % third test for URL:\\machine.name\vimtest2c and other text<cr>
- % fourth test for URL:\\machine.name\tmp\vimtest2d, and other text
-
-Go to the first URL:
-
- :/^first<cr>/tmp
-
-Append the url to the beginning of the buffer:
-
- :call append(0, expand("<cfile>"))
-
-Repeat for the second URL, but navigate to the 'URL' word:
-
- :/^second<cr>/URL<cr>:call append(1, expand("<cfile>"))
-
-Repeat for the other two, but first the 'isfname' option must be set to
-allow '\' in filenames
-
- :set isf=@,48-57,/,.,-,_,+,,,$,:,~,\
- :/^third<cr>/name<cr>:call append(2, expand("<cfile>"))
- :/^fourth<cr>/URL<cr>:call append(3, expand("<cfile>"))
-
-Delete the initial text which starts at line 5:
-
- > 5GdG
-
-Result:
-
- URL://machine.name/tmp/vimtest2a
- URL://machine.name/tmp/vimtest2b
- URL:\\machine.name\vimtest2c
- URL:\\machine.name\tmp\vimtest2d
- @end