neovim

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

sort_spec.lua (2801B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local eq = t.eq
      5 local NIL = vim.NIL
      6 local eval = n.eval
      7 local clear = n.clear
      8 local api = n.api
      9 local fn = n.fn
     10 local command = n.command
     11 local exc_exec = n.exc_exec
     12 local pcall_err = t.pcall_err
     13 
     14 setup(clear)
     15 
     16 describe('sort()', function()
     17  it('errors out when sorting special values', function()
     18    eq(
     19      'Vim(call):E362: Using a boolean value as a Float',
     20      exc_exec('call sort([v:true, v:false], "f")')
     21    )
     22  end)
     23 
     24  it('sorts “wrong” values between -0.0001 and 0.0001, preserving order', function()
     25    api.nvim_set_var('list', {
     26      true,
     27      false,
     28      NIL,
     29      {},
     30      { a = 42 },
     31      'check',
     32      0.0001,
     33      -0.0001,
     34    })
     35    command('call insert(g:list, function("tr"))')
     36    local error_lines = fn.split(fn.execute('silent! call sort(g:list, "f")'), '\n')
     37    local errors = {}
     38    for _, err in ipairs(error_lines) do
     39      errors[err] = true
     40    end
     41    eq({
     42      ['E362: Using a boolean value as a Float'] = true,
     43      ['E891: Using a Funcref as a Float'] = true,
     44      ['E892: Using a String as a Float'] = true,
     45      ['E893: Using a List as a Float'] = true,
     46      ['E894: Using a Dictionary as a Float'] = true,
     47      ['E907: Using a special value as a Float'] = true,
     48    }, errors)
     49    eq(
     50      "[-1.0e-4, function('tr'), v:true, v:false, v:null, [], {'a': 42}, 'check', 1.0e-4]",
     51      eval('string(g:list)')
     52    )
     53  end)
     54 
     55  it('can yield E702 and stop sorting after that', function()
     56    command([[
     57      function Cmp(a, b)
     58        if type(a:a) == type([]) || type(a:b) == type([])
     59          return []
     60        endif
     61        return (a:a > a:b) - (a:a < a:b)
     62      endfunction
     63    ]])
     64    eq(
     65      'Vim(let):E745: Using a List as a Number',
     66      pcall_err(command, 'let sl = sort([1, 0, [], 3, 2], "Cmp")')
     67    )
     68  end)
     69 
     70  it('handles large numbers properly', function()
     71    local to_sort = {
     72      { 229539777187355, 229539777187355 },
     73      { 487766135067138, 491977135306566 },
     74      { 188325333471071, 188931909913550 },
     75      { 264028451845520, 265514296554744 },
     76      { 245727634348687, 249469249579525 },
     77      { 375117820166731, 378942174241518 },
     78      { 535474757750378, 535849288071548 },
     79    }
     80    local expected = {
     81      { 188325333471071, 188931909913550 },
     82      { 229539777187355, 229539777187355 },
     83      { 245727634348687, 249469249579525 },
     84      { 264028451845520, 265514296554744 },
     85      { 375117820166731, 378942174241518 },
     86      { 487766135067138, 491977135306566 },
     87      { 535474757750378, 535849288071548 },
     88    }
     89    eq(expected, fn.sort(to_sort))
     90    api.nvim_set_var('to_sort', to_sort)
     91    eq(expected, api.nvim_eval('sort(g:to_sort)'))
     92    eq(expected, api.nvim_eval('sort(g:to_sort, {a, b -> a[0] - b[0]})'))
     93  end)
     94 end)