fileio_spec.lua (2716B)
1 local t = require('test.unit.testutil') 2 local itp = t.gen_itp(it) 3 --{:cimport, :internalize, :eq, :neq, :ffi, :lib, :cstr, :to_cstr} = require 'test.unit.testutil' 4 5 local eq = t.eq 6 local ffi = t.ffi 7 local to_cstr = t.to_cstr 8 local NULL = t.NULL 9 10 local fileio = t.cimport('./src/nvim/fileio.h') 11 12 describe('file_pat functions', function() 13 describe('file_pat_to_reg_pat', function() 14 local file_pat_to_reg_pat = function(pat) 15 local res = fileio.file_pat_to_reg_pat(to_cstr(pat), NULL, NULL, 0) 16 return ffi.string(res) 17 end 18 19 itp('returns ^path$ regex for literal path input', function() 20 eq('^path$', file_pat_to_reg_pat('path')) 21 end) 22 23 itp('does not prepend ^ when there is a starting glob (*)', function() 24 eq('path$', file_pat_to_reg_pat('*path')) 25 end) 26 27 itp('does not append $ when there is an ending glob (*)', function() 28 eq('^path', file_pat_to_reg_pat('path*')) 29 end) 30 31 itp('does not include ^ or $ when surrounded by globs (*)', function() 32 eq('path', file_pat_to_reg_pat('*path*')) 33 end) 34 35 itp('replaces the bash any character (?) with the regex any character (.)', function() 36 eq('^foo.bar$', file_pat_to_reg_pat('foo?bar')) 37 end) 38 39 itp( 40 'replaces a glob (*) in the middle of a path with regex multiple any character (.*)', 41 function() 42 eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar')) 43 end 44 ) 45 46 itp([[unescapes \? to ?]], function() 47 eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]])) 48 end) 49 50 itp([[unescapes \% to %]], function() 51 eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]])) 52 end) 53 54 itp([[unescapes \, to ,]], function() 55 eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]])) 56 end) 57 58 itp([[unescapes '\ ' to ' ']], function() 59 eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]])) 60 end) 61 62 itp([[escapes . to \.]], function() 63 eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar')) 64 end) 65 66 itp('Converts bash brace expansion {a,b} to regex options (a|b)', function() 67 eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}')) 68 end) 69 70 itp('Collapses multiple consecutive * into a single character', function() 71 eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar')) 72 eq([[foobar$]], file_pat_to_reg_pat('********foobar')) 73 eq([[^foobar]], file_pat_to_reg_pat('foobar********')) 74 end) 75 76 itp('Does not escape ^', function() 77 eq([[^^blah$]], file_pat_to_reg_pat('^blah')) 78 eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar')) 79 end) 80 81 itp('Does not escape $', function() 82 eq([[^blah$$]], file_pat_to_reg_pat('blah$')) 83 eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar')) 84 end) 85 end) 86 end)