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