neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

xdiff_spec.lua (4676B)


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