fileio_spec.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. local eq = t.eq
  5. local ffi = t.ffi
  6. local to_cstr = t.to_cstr
  7. local NULL = t.NULL
  8. local fileio = t.cimport('./src/nvim/fileio.h')
  9. describe('file_pat functions', function()
  10. describe('file_pat_to_reg_pat', function()
  11. local file_pat_to_reg_pat = function(pat)
  12. local res = fileio.file_pat_to_reg_pat(to_cstr(pat), NULL, NULL, 0)
  13. return ffi.string(res)
  14. end
  15. itp('returns ^path$ regex for literal path input', function()
  16. eq('^path$', file_pat_to_reg_pat('path'))
  17. end)
  18. itp('does not prepend ^ when there is a starting glob (*)', function()
  19. eq('path$', file_pat_to_reg_pat('*path'))
  20. end)
  21. itp('does not append $ when there is an ending glob (*)', function()
  22. eq('^path', file_pat_to_reg_pat('path*'))
  23. end)
  24. itp('does not include ^ or $ when surrounded by globs (*)', function()
  25. eq('path', file_pat_to_reg_pat('*path*'))
  26. end)
  27. itp('replaces the bash any character (?) with the regex any character (.)', function()
  28. eq('^foo.bar$', file_pat_to_reg_pat('foo?bar'))
  29. end)
  30. itp(
  31. 'replaces a glob (*) in the middle of a path with regex multiple any character (.*)',
  32. function()
  33. eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar'))
  34. end
  35. )
  36. itp([[unescapes \? to ?]], function()
  37. eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]]))
  38. end)
  39. itp([[unescapes \% to %]], function()
  40. eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]]))
  41. end)
  42. itp([[unescapes \, to ,]], function()
  43. eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]]))
  44. end)
  45. itp([[unescapes '\ ' to ' ']], function()
  46. eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]]))
  47. end)
  48. itp([[escapes . to \.]], function()
  49. eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar'))
  50. end)
  51. itp('Converts bash brace expansion {a,b} to regex options (a|b)', function()
  52. eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}'))
  53. end)
  54. itp('Collapses multiple consecutive * into a single character', function()
  55. eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar'))
  56. eq([[foobar$]], file_pat_to_reg_pat('********foobar'))
  57. eq([[^foobar]], file_pat_to_reg_pat('foobar********'))
  58. end)
  59. itp('Does not escape ^', function()
  60. eq([[^^blah$]], file_pat_to_reg_pat('^blah'))
  61. eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar'))
  62. end)
  63. itp('Does not escape $', function()
  64. eq([[^blah$$]], file_pat_to_reg_pat('blah$'))
  65. eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar'))
  66. end)
  67. end)
  68. end)