xdiff_spec.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local exec_lua = n.exec_lua
  5. local eq = t.eq
  6. local pcall_err = t.pcall_err
  7. describe('xdiff bindings', function()
  8. before_each(function()
  9. clear()
  10. end)
  11. describe('can diff text', function()
  12. local a1 = 'Hello\n'
  13. local b1 = 'Helli\n'
  14. local a2 = 'Hello\nbye\nfoo\n'
  15. local b2 = 'Helli\nbye\nbar\nbaz\n'
  16. it('with no callback', function()
  17. eq(
  18. table.concat({
  19. '@@ -1 +1 @@',
  20. '-Hello',
  21. '+Helli',
  22. '',
  23. }, '\n'),
  24. exec_lua(function()
  25. return vim.diff(a1, b1)
  26. end)
  27. )
  28. eq(
  29. table.concat({
  30. '@@ -1 +1 @@',
  31. '-Hello',
  32. '+Helli',
  33. '@@ -3 +3,2 @@',
  34. '-foo',
  35. '+bar',
  36. '+baz',
  37. '',
  38. }, '\n'),
  39. exec_lua(function()
  40. return vim.diff(a2, b2)
  41. end)
  42. )
  43. end)
  44. it('with callback', function()
  45. eq(
  46. { { 1, 1, 1, 1 } },
  47. exec_lua(function()
  48. local exp = {} --- @type table[]
  49. assert(vim.diff(a1, b1, {
  50. on_hunk = function(...)
  51. exp[#exp + 1] = { ... }
  52. end,
  53. }) == nil)
  54. return exp
  55. end)
  56. )
  57. eq(
  58. { { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
  59. exec_lua(function()
  60. local exp = {} --- @type table[]
  61. assert(vim.diff(a2, b2, {
  62. on_hunk = function(...)
  63. exp[#exp + 1] = { ... }
  64. end,
  65. }) == nil)
  66. return exp
  67. end)
  68. )
  69. -- gives higher precedence to on_hunk over result_type
  70. eq(
  71. { { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
  72. exec_lua(function()
  73. local exp = {} --- @type table[]
  74. assert(vim.diff(a2, b2, {
  75. on_hunk = function(...)
  76. exp[#exp + 1] = { ... }
  77. end,
  78. result_type = 'indices',
  79. }) == nil)
  80. return exp
  81. end)
  82. )
  83. end)
  84. it('with error callback', function()
  85. eq(
  86. [[.../xdiff_spec.lua:0: error running function on_hunk: .../xdiff_spec.lua:0: ERROR1]],
  87. pcall_err(exec_lua, function()
  88. vim.diff(a1, b1, {
  89. on_hunk = function()
  90. error('ERROR1')
  91. end,
  92. })
  93. end)
  94. )
  95. end)
  96. it('with hunk_lines', function()
  97. eq(
  98. { { 1, 1, 1, 1 } },
  99. exec_lua(function()
  100. return vim.diff(a1, b1, { result_type = 'indices' })
  101. end)
  102. )
  103. eq(
  104. { { 1, 1, 1, 1 }, { 3, 1, 3, 2 } },
  105. exec_lua(function()
  106. return vim.diff(a2, b2, { result_type = 'indices' })
  107. end)
  108. )
  109. end)
  110. it('can run different algorithms', function()
  111. local a = table.concat({
  112. '.foo1 {',
  113. ' margin: 0;',
  114. '}',
  115. '',
  116. '.bar {',
  117. ' margin: 0;',
  118. '}',
  119. '',
  120. }, '\n')
  121. local b = table.concat({
  122. '.bar {',
  123. ' margin: 0;',
  124. '}',
  125. '',
  126. '.foo1 {',
  127. ' margin: 0;',
  128. ' color: green;',
  129. '}',
  130. '',
  131. }, '\n')
  132. eq(
  133. table.concat({
  134. '@@ -1,4 +0,0 @@',
  135. '-.foo1 {',
  136. '- margin: 0;',
  137. '-}',
  138. '-',
  139. '@@ -7,0 +4,5 @@',
  140. '+',
  141. '+.foo1 {',
  142. '+ margin: 0;',
  143. '+ color: green;',
  144. '+}',
  145. '',
  146. }, '\n'),
  147. exec_lua(function()
  148. return vim.diff(a, b, {
  149. algorithm = 'patience',
  150. })
  151. end)
  152. )
  153. end)
  154. end)
  155. it('can handle bad args', function()
  156. eq([[Expected at least 2 arguments]], pcall_err(exec_lua, [[vim.diff('a')]]))
  157. eq([[bad argument #1 to 'diff' (expected string)]], pcall_err(exec_lua, [[vim.diff(1, 2)]]))
  158. eq(
  159. [[bad argument #3 to 'diff' (expected table)]],
  160. pcall_err(exec_lua, [[vim.diff('a', 'b', true)]])
  161. )
  162. eq([[invalid key: bad_key]], pcall_err(exec_lua, [[vim.diff('a', 'b', { bad_key = true })]]))
  163. eq(
  164. [[on_hunk is not a function]],
  165. pcall_err(exec_lua, [[vim.diff('a', 'b', { on_hunk = true })]])
  166. )
  167. end)
  168. it('can handle strings with embedded NUL characters (GitHub #30305)', function()
  169. eq(
  170. { { 0, 0, 1, 1 }, { 1, 0, 3, 2 } },
  171. exec_lua(function()
  172. return vim.diff('\n', '\0\n\n\nb', { linematch = true, result_type = 'indices' })
  173. end)
  174. )
  175. end)
  176. end)