robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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