neovim

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

function_sort_spec.lua (1846B)


      1 local t = require('test.testutil')
      2 local n = require('test.functional.testnvim')()
      3 
      4 local eq = t.eq
      5 local neq = t.neq
      6 local eval = n.eval
      7 local clear = n.clear
      8 local source = n.source
      9 local exc_exec = n.exc_exec
     10 
     11 describe('sort', function()
     12  before_each(clear)
     13 
     14  it('numbers compared as strings', function()
     15    eq({ 1, 2, 3 }, eval('sort([3, 2, 1])'))
     16    eq({ 13, 28, 3 }, eval('sort([3, 28, 13])'))
     17  end)
     18 
     19  it('numbers compared as numeric', function()
     20    eq({ 1, 2, 3 }, eval("sort([3, 2, 1], 'n')"))
     21    eq({ 3, 13, 28 }, eval("sort([3, 28, 13], 'n')"))
     22    -- Strings are not sorted.
     23    eq({ '13', '28', '3' }, eval("sort(['13', '28', '3'], 'n')"))
     24  end)
     25 
     26  it('numbers compared as numbers', function()
     27    eq({ 3, 13, 28 }, eval("sort([13, 28, 3], 'N')"))
     28    eq({ '3', '13', '28' }, eval("sort(['13', '28', '3'], 'N')"))
     29  end)
     30 
     31  it('numbers compared as float', function()
     32    eq({ 0.28, 3, 13.5 }, eval("sort([13.5, 0.28, 3], 'f')"))
     33  end)
     34 
     35  it('ability to call sort() from a compare function', function()
     36    source([[
     37      function Compare1(a, b) abort
     38        call sort(range(3), 'Compare2')
     39        return a:a - a:b
     40      endfunc
     41 
     42      function Compare2(a, b) abort
     43        return a:a - a:b
     44      endfunc
     45    ]])
     46 
     47    eq({ 1, 3, 5 }, eval("sort([3, 1, 5], 'Compare1')"))
     48  end)
     49 
     50  it('default sort', function()
     51    -- docs say omitted, empty or zero argument sorts on string representation
     52    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"])'))
     53    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval([[sort([3.3, 1, "2", "A", "a", "AA"], '')]]))
     54    eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 0)'))
     55    eq({ '2', 'A', 'a', 'AA', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 1)'))
     56    neq(nil, exc_exec('call sort([3.3, 1, "2"], 3)'):find('E474:'))
     57  end)
     58 end)