robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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