fileio_spec.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. local helpers = require("test.unit.helpers")(after_each)
  2. local itp = helpers.gen_itp(it)
  3. --{:cimport, :internalize, :eq, :neq, :ffi, :lib, :cstr, :to_cstr} = require 'test.unit.helpers'
  4. local eq = helpers.eq
  5. local ffi = helpers.ffi
  6. local to_cstr = helpers.to_cstr
  7. local NULL = helpers.NULL
  8. local fileio = helpers.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('replaces a glob (*) in the middle of a path with regex multiple any character (.*)',
  31. function()
  32. eq('^foo.*bar$', file_pat_to_reg_pat('foo*bar'))
  33. end)
  34. itp([[unescapes \? to ?]], function()
  35. eq('^foo?bar$', file_pat_to_reg_pat([[foo\?bar]]))
  36. end)
  37. itp([[unescapes \% to %]], function()
  38. eq('^foo%bar$', file_pat_to_reg_pat([[foo\%bar]]))
  39. end)
  40. itp([[unescapes \, to ,]], function()
  41. eq('^foo,bar$', file_pat_to_reg_pat([[foo\,bar]]))
  42. end)
  43. itp([[unescapes '\ ' to ' ']], function()
  44. eq('^foo bar$', file_pat_to_reg_pat([[foo\ bar]]))
  45. end)
  46. itp([[escapes . to \.]], function()
  47. eq([[^foo\.bar$]], file_pat_to_reg_pat('foo.bar'))
  48. end)
  49. itp('Converts bash brace expansion {a,b} to regex options (a|b)', function()
  50. eq([[^foo\(bar\|baz\)$]], file_pat_to_reg_pat('foo{bar,baz}'))
  51. end)
  52. itp('Collapses multiple consecutive * into a single character', function()
  53. eq([[^foo.*bar$]], file_pat_to_reg_pat('foo*******bar'))
  54. eq([[foobar$]], file_pat_to_reg_pat('********foobar'))
  55. eq([[^foobar]], file_pat_to_reg_pat('foobar********'))
  56. end)
  57. itp('Does not escape ^', function()
  58. eq([[^^blah$]], file_pat_to_reg_pat('^blah'))
  59. eq([[^foo^bar$]], file_pat_to_reg_pat('foo^bar'))
  60. end)
  61. itp('Does not escape $', function()
  62. eq([[^blah$$]], file_pat_to_reg_pat('blah$'))
  63. eq([[^foo$bar$]], file_pat_to_reg_pat('foo$bar'))
  64. end)
  65. end)
  66. end)