neovim/test/unit/fileio_spec.lua

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.7 KiB
Lua
Raw Normal View History

local t = require('test.unit.testutil')
local itp = t.gen_itp(it)
--{:cimport, :internalize, :eq, :neq, :ffi, :lib, :cstr, :to_cstr} = require 'test.unit.testutil'
2014-07-19 20:51:04 -07:00
local eq = t.eq
local ffi = t.ffi
local to_cstr = t.to_cstr
local NULL = t.NULL
2014-07-19 20:51:04 -07:00
local fileio = t.cimport('./src/nvim/fileio.h')
2014-07-19 20:51:04 -07:00
describe('file_pat functions', function()
describe('file_pat_to_reg_pat', function()
local file_pat_to_reg_pat = function(pat)
local res = fileio.file_pat_to_reg_pat(to_cstr(pat), NULL, NULL, 0)
return ffi.string(res)
end
itp('returns ^path$ regex for literal path input', function()
eq('^path$', file_pat_to_reg_pat('path'))
2014-07-19 20:51:04 -07:00
end)
itp('does not prepend ^ when there is a starting glob (*)', function()
2014-07-19 20:51:04 -07:00
eq('path$', file_pat_to_reg_pat('*path'))
end)
itp('does not append $ when there is an ending glob (*)', function()
2014-07-19 20:51:04 -07:00
eq('^path', file_pat_to_reg_pat('path*'))
end)
itp('does not include ^ or $ when surrounded by globs (*)', function()
2014-07-19 20:51:04 -07:00
eq('path', file_pat_to_reg_pat('*path*'))
end)
itp('replaces the bash any character (?) with the regex any character (.)', function()
eq('^foo.bar$', file_pat_to_reg_pat('foo?bar'))
2014-07-19 20:51:04 -07:00
end)
itp(
'replaces a glob (*) in the middle of a path with regex multiple any character (.*)',
function()
eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar'))
end
)
2014-07-19 20:51:04 -07:00
itp([[unescapes \? to ?]], function()
2014-07-19 20:51:04 -07:00
eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]]))
end)
itp([[unescapes \% to %]], function()
2014-07-19 20:51:04 -07:00
eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]]))
end)
itp([[unescapes \, to ,]], function()
2014-07-19 20:51:04 -07:00
eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]]))
end)
itp([[unescapes '\ ' to ' ']], function()
2014-07-19 20:51:04 -07:00
eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]]))
end)
itp([[escapes . to \.]], function()
2014-07-19 20:51:04 -07:00
eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar'))
end)
itp('Converts bash brace expansion {a,b} to regex options (a|b)', function()
2014-07-19 20:51:04 -07:00
eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}'))
end)
itp('Collapses multiple consecutive * into a single character', function()
2014-07-19 20:51:04 -07:00
eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar'))
eq([[foobar$]], file_pat_to_reg_pat('********foobar'))
eq([[^foobar]], file_pat_to_reg_pat('foobar********'))
end)
itp('Does not escape ^', function()
2014-07-19 20:51:04 -07:00
eq([[^^blah$]], file_pat_to_reg_pat('^blah'))
eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar'))
end)
itp('Does not escape $', function()
2014-07-19 20:51:04 -07:00
eq([[^blah$$]], file_pat_to_reg_pat('blah$'))
eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar'))
end)
end)
end)