neovim

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

text_spec.lua (4285B)


      1 local t = require('test.testutil')
      2 
      3 local eq = t.eq
      4 
      5 describe('vim.text', function()
      6  describe('indent()', function()
      7    it('validation', function()
      8      t.matches('size%: expected number, got string', t.pcall_err(vim.text.indent, 'x', 'x'))
      9      t.matches('size%: expected number, got nil', t.pcall_err(vim.text.indent, nil, 'x'))
     10      t.matches('opts%: expected table, got string', t.pcall_err(vim.text.indent, 0, 'x', 'z'))
     11    end)
     12 
     13    it('basic cases', function()
     14      -- Basic cases.
     15      eq({ '', 0 }, { vim.text.indent(0, '') })
     16      eq({ '', 0 }, { vim.text.indent(2, '') })
     17      eq({ '  a', 4 }, { vim.text.indent(2, '    a') })
     18      eq({ '  a\n  b', 4 }, { vim.text.indent(2, '    a\n    b') })
     19      eq({ '\t\ta', 1 }, { vim.text.indent(2, '\ta') })
     20      eq({ ' a\n\n', 5 }, { vim.text.indent(1, '     a\n\n') })
     21      -- Indent 1 (tab) => 0. Starting with empty + blank lines.
     22      eq({ '\n\naa a aa', 1 }, { vim.text.indent(0, '\n	\n	aa a aa') })
     23      -- Indent 1 (tab) => 2 (tabs). Starting with empty + blank lines, 1-tab indent.
     24      eq({ '\n\t\t\n\t\taa a aa', 1 }, { vim.text.indent(2, '\n\t\n\taa a aa') })
     25 
     26      -- Indent 4 => 2, expandtab=false preserves tabs after the common indent.
     27      eq(
     28        { '  foo\n    bar\n  \tbaz\n', 4 },
     29        { vim.text.indent(2, '    foo\n      bar\n    \tbaz\n') }
     30      )
     31      -- Indent 9 => 3, expandtab=true.
     32      eq(
     33        { '    foo\n\n   bar \t baz\n', 9 },
     34        { vim.text.indent(3, '\t  foo\n\n         bar \t baz\n', { expandtab = 8 }) }
     35      )
     36      -- Indent 9 => 8, expandtab=true.
     37      eq(
     38        { '         foo\n\n        bar\n', 9 },
     39        { vim.text.indent(8, '\t  foo\n\n         bar\n', { expandtab = 8 }) }
     40      )
     41      -- Dedent: 5 => 0.
     42      eq({ '  foo\n\nbar\n', 5 }, { vim.text.indent(0, '       foo\n\n     bar\n') })
     43      -- Dedent: 1 => 0. Empty lines are ignored when deciding "common indent".
     44      eq(
     45        { ' \n  \nfoo\n\nbar\nbaz\n    \n', 1 },
     46        { vim.text.indent(0, '  \n   \n foo\n\n bar\n baz\n     \n') }
     47      )
     48    end)
     49 
     50    it('real-world cases', function()
     51      -- Dedent.
     52      eq({
     53        [[
     54 bufs:
     55 nvim args: 3
     56 lua args: {
     57  [0] = "foo.lua"
     58 }
     59 ]],
     60        10,
     61      }, {
     62        vim.text.indent(
     63          0,
     64          [[
     65          bufs:
     66          nvim args: 3
     67          lua args: {
     68            [0] = "foo.lua"
     69          }
     70          ]]
     71        ),
     72      })
     73 
     74      -- Indent 0 => 2.
     75      eq({
     76        [[
     77  # yay
     78 
     79  local function foo()
     80    if true then
     81      # yay
     82    end
     83  end
     84 
     85  return
     86 ]],
     87        0,
     88      }, {
     89        vim.text.indent(
     90          2,
     91          [[
     92 # yay
     93 
     94 local function foo()
     95  if true then
     96    # yay
     97  end
     98 end
     99 
    100 return
    101 ]]
    102        ),
    103      })
    104 
    105      -- 1-tab indent, last line spaces < tabsize.
    106      -- Preserves tab char immediately following the indent.
    107      eq({ 'text\n\tmatch\nmatch\ntext\n', 1 }, {
    108        vim.text.indent(0, (([[
    109 text
    110 	match
    111 match
    112 text
    113 ]]):gsub('\n%s-([\n]?)$', '\n%1'))),
    114      })
    115 
    116      -- 1-tab indent, last line spaces=tabsize.
    117      eq({ 'text\n      match\nmatch\ntext\n', 6 }, {
    118        vim.text.indent(
    119          0,
    120          [[
    121 text
    122 	match
    123 match
    124 text
    125      ]],
    126          { expandtab = 6 }
    127        ),
    128      })
    129    end)
    130  end)
    131 
    132  describe('hexencode(), hexdecode()', function()
    133    it('works', function()
    134      local cases = {
    135        { 'Hello world!', '48656C6C6F20776F726C6421' },
    136        { '😂', 'F09F9882' },
    137      }
    138 
    139      for _, v in ipairs(cases) do
    140        local input, output = unpack(v)
    141        eq(output, vim.text.hexencode(input))
    142        eq(input, vim.text.hexdecode(output))
    143      end
    144    end)
    145 
    146    it('with very large strings', function()
    147      local input, output = string.rep('😂', 2 ^ 16), string.rep('F09F9882', 2 ^ 16)
    148      eq(output, vim.text.hexencode(input))
    149      eq(input, vim.text.hexdecode(output))
    150    end)
    151 
    152    it('invalid input', function()
    153      -- Odd number of hex characters
    154      do
    155        local res, err = vim.text.hexdecode('ABC')
    156        eq(nil, res)
    157        eq('string must have an even number of hex characters', err)
    158      end
    159 
    160      -- Non-hexadecimal input
    161      do
    162        local res, err = vim.text.hexdecode('nothex')
    163        eq(nil, res)
    164        eq('string must contain only hex characters', err)
    165      end
    166    end)
    167  end)
    168 end)