neovim

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

editorconfig_spec.lua (6362B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local clear = n.clear
      5 local command = n.command
      6 local eq = t.eq
      7 local pathsep = n.get_pathsep()
      8 local fn = n.fn
      9 local api = n.api
     10 
     11 local testdir = 'Xtest-editorconfig'
     12 
     13 --- @param name string
     14 --- @param expected table<string,any>
     15 local function test_case(name, expected)
     16  local filename = testdir .. pathsep .. name
     17  command('edit ' .. filename)
     18 
     19  for opt, val in pairs(expected) do
     20    local opt_info = api.nvim_get_option_info2(opt, {})
     21    if opt_info.scope == 'win' then
     22      eq(val, api.nvim_get_option_value(opt, { win = 0 }), name)
     23    elseif opt_info.scope == 'buf' then
     24      eq(val, api.nvim_get_option_value(opt, { buf = 0 }), name)
     25    else
     26      eq(val, api.nvim_get_option_value(opt, {}), name)
     27    end
     28  end
     29 end
     30 
     31 setup(function()
     32  n.mkdir_p(testdir)
     33  t.write_file(
     34    testdir .. pathsep .. '.editorconfig',
     35    [[
     36    root = true
     37 
     38    [3_space.txt]
     39    indent_style = space
     40    indent_size = 3
     41    tab_width = 3
     42 
     43    [4_space.py]
     44    indent_style = space
     45    indent_size = 4
     46    tab_width = 8
     47 
     48    [space.txt]
     49    indent_style = space
     50    indent_size = tab
     51 
     52    [tab.txt]
     53    indent_style = tab
     54 
     55    [4_tab.txt]
     56    indent_style = tab
     57    indent_size = 4
     58    tab_width = 4
     59 
     60    [4_tab_width_of_8.txt]
     61    indent_style = tab
     62    indent_size = 4
     63    tab_width = 8
     64 
     65    [lf.txt]
     66    end_of_line = lf
     67 
     68    [crlf.txt]
     69    end_of_line = crlf
     70 
     71    [cr.txt]
     72    end_of_line = cr
     73 
     74    [utf-8.txt]
     75    charset = utf-8
     76 
     77    [utf-8-bom.txt]
     78    charset = utf-8-bom
     79 
     80    [utf-16be.txt]
     81    charset = utf-16be
     82 
     83    [utf-16le.txt]
     84    charset = utf-16le
     85 
     86    [latin1.txt]
     87    charset = latin1
     88 
     89    [with_newline.txt]
     90    insert_final_newline = true
     91 
     92    [without_newline.txt]
     93    insert_final_newline = false
     94 
     95    [trim.txt]
     96    trim_trailing_whitespace = true
     97 
     98    [no_trim.txt]
     99    trim_trailing_whitespace = false
    100 
    101    [max_line_length.txt]
    102    max_line_length = 42
    103 
    104    [short_spelling_language.txt]
    105    spelling_language = de
    106 
    107    [long_spelling_language.txt]
    108    spelling_language = en-NZ
    109 
    110    [custom_properties.txt]
    111    property1 = something1
    112    property2 = x[something2]x
    113    ]]
    114  )
    115 end)
    116 
    117 teardown(function()
    118  n.rmdir(testdir)
    119 end)
    120 
    121 describe('editorconfig', function()
    122  before_each(function()
    123    -- Remove -u NONE so that plugins (i.e. editorconfig.lua) are loaded
    124    clear({ args_rm = { '-u' } })
    125  end)
    126 
    127  it('sets indent options', function()
    128    test_case('3_space.txt', {
    129      expandtab = true,
    130      shiftwidth = 3,
    131      softtabstop = -1,
    132      tabstop = 3,
    133    })
    134 
    135    test_case('4_space.py', {
    136      expandtab = true,
    137      shiftwidth = 4,
    138      softtabstop = -1,
    139      tabstop = 8,
    140    })
    141 
    142    test_case('space.txt', {
    143      expandtab = true,
    144      shiftwidth = 0,
    145      softtabstop = 0,
    146    })
    147 
    148    test_case('tab.txt', {
    149      expandtab = false,
    150      shiftwidth = 0,
    151      softtabstop = 0,
    152    })
    153 
    154    test_case('4_tab.txt', {
    155      expandtab = false,
    156      shiftwidth = 4,
    157      softtabstop = -1,
    158      tabstop = 4,
    159    })
    160 
    161    test_case('4_tab_width_of_8.txt', {
    162      expandtab = false,
    163      shiftwidth = 4,
    164      softtabstop = -1,
    165      tabstop = 8,
    166    })
    167  end)
    168 
    169  it('sets end-of-line options', function()
    170    test_case('lf.txt', { fileformat = 'unix' })
    171    test_case('crlf.txt', { fileformat = 'dos' })
    172    test_case('cr.txt', { fileformat = 'mac' })
    173  end)
    174 
    175  it('sets encoding options', function()
    176    test_case('utf-8.txt', { fileencoding = 'utf-8', bomb = false })
    177    test_case('utf-8-bom.txt', { fileencoding = 'utf-8', bomb = true })
    178    test_case('utf-16be.txt', { fileencoding = 'utf-16', bomb = false })
    179    test_case('utf-16le.txt', { fileencoding = 'utf-16le', bomb = false })
    180    test_case('latin1.txt', { fileencoding = 'latin1', bomb = false })
    181  end)
    182 
    183  it('sets newline options', function()
    184    test_case('with_newline.txt', { fixendofline = true })
    185    test_case('without_newline.txt', { fixendofline = false })
    186  end)
    187 
    188  it('respects trim_trailing_whitespace', function()
    189    local filename = testdir .. pathsep .. 'trim.txt'
    190    -- luacheck: push ignore 613
    191    local untrimmed = [[
    192 This line ends in whitespace 
    193 So does this one    
    194 And this one         
    195 But not this one
    196 ]]
    197    -- luacheck: pop
    198    local trimmed = untrimmed:gsub('%s+\n', '\n')
    199 
    200    t.write_file(filename, untrimmed)
    201    command('edit ' .. filename)
    202    command('write')
    203    command('bdelete')
    204    eq(trimmed, t.read_file(filename))
    205 
    206    filename = testdir .. pathsep .. 'no_trim.txt'
    207    t.write_file(filename, untrimmed)
    208    command('edit ' .. filename)
    209    command('write')
    210    command('bdelete')
    211    eq(untrimmed, t.read_file(filename))
    212  end)
    213 
    214  it('sets textwidth', function()
    215    test_case('max_line_length.txt', { textwidth = 42 })
    216  end)
    217 
    218  it('can be disabled globally', function()
    219    api.nvim_set_var('editorconfig', false)
    220    api.nvim_set_option_value('shiftwidth', 42, {})
    221    test_case('3_space.txt', { shiftwidth = 42 })
    222  end)
    223 
    224  it('can be disabled per-buffer', function()
    225    api.nvim_set_option_value('shiftwidth', 42, {})
    226    local bufnr = fn.bufadd(testdir .. pathsep .. '3_space.txt')
    227    api.nvim_buf_set_var(bufnr, 'editorconfig', false)
    228    test_case('3_space.txt', { shiftwidth = 42 })
    229    test_case('4_space.py', { shiftwidth = 4 })
    230  end)
    231 
    232  it('does not operate on invalid buffers', function()
    233    local ok, err = unpack(n.exec_lua(function()
    234      vim.cmd.edit('test.txt')
    235      local bufnr = vim.api.nvim_get_current_buf()
    236      vim.cmd.bwipeout(bufnr)
    237      return { pcall(require('editorconfig').config, bufnr) }
    238    end))
    239 
    240    eq(true, ok, err)
    241  end)
    242 
    243  it('sets spelllang', function()
    244    test_case('short_spelling_language.txt', { spelllang = 'de' })
    245    test_case('long_spelling_language.txt', { spelllang = 'en_nz' })
    246  end)
    247 
    248  it('set custom properties', function()
    249    n.exec_lua(function()
    250      local editorconfig = require('editorconfig')
    251      editorconfig.properties.property1 = function(bufnr, val, _opts)
    252        vim.b[bufnr].property1 = val
    253      end
    254      editorconfig.properties.property2 = function(bufnr, val, _opts)
    255        vim.b[bufnr].property2 = val
    256      end
    257    end)
    258 
    259    command('edit ' .. testdir .. pathsep .. 'custom_properties.txt')
    260    eq('something1', fn.luaeval('vim.b.property1'), 'property1')
    261    eq('x[something2]x', fn.luaeval('vim.b.property2'), 'property2')
    262  end)
    263 end)