2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2017-04-08 14:12:26 -07:00
|
|
|
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
|
|
|
local neq = t.neq
|
2024-04-20 08:44:13 -07:00
|
|
|
local eval = n.eval
|
|
|
|
local clear = n.clear
|
|
|
|
local source = n.source
|
|
|
|
local exc_exec = n.exc_exec
|
2016-02-20 03:48:31 -07:00
|
|
|
|
|
|
|
describe('sort', function()
|
|
|
|
before_each(clear)
|
|
|
|
|
|
|
|
it('numbers compared as strings', function()
|
|
|
|
eq({ 1, 2, 3 }, eval('sort([3, 2, 1])'))
|
|
|
|
eq({ 13, 28, 3 }, eval('sort([3, 28, 13])'))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('numbers compared as numeric', function()
|
|
|
|
eq({ 1, 2, 3 }, eval("sort([3, 2, 1], 'n')"))
|
|
|
|
eq({ 3, 13, 28 }, eval("sort([3, 28, 13], 'n')"))
|
|
|
|
-- Strings are not sorted.
|
|
|
|
eq({ '13', '28', '3' }, eval("sort(['13', '28', '3'], 'n')"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('numbers compared as numbers', function()
|
|
|
|
eq({ 3, 13, 28 }, eval("sort([13, 28, 3], 'N')"))
|
|
|
|
eq({ '3', '13', '28' }, eval("sort(['13', '28', '3'], 'N')"))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('numbers compared as float', function()
|
|
|
|
eq({ 0.28, 3, 13.5 }, eval("sort([13.5, 0.28, 3], 'f')"))
|
|
|
|
end)
|
2016-05-14 13:00:27 -07:00
|
|
|
|
|
|
|
it('ability to call sort() from a compare function', function()
|
2017-04-08 14:12:26 -07:00
|
|
|
source([[
|
|
|
|
function Compare1(a, b) abort
|
|
|
|
call sort(range(3), 'Compare2')
|
|
|
|
return a:a - a:b
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
function Compare2(a, b) abort
|
|
|
|
return a:a - a:b
|
|
|
|
endfunc
|
|
|
|
]])
|
|
|
|
|
2016-05-14 13:00:27 -07:00
|
|
|
eq({ 1, 3, 5 }, eval("sort([3, 1, 5], 'Compare1')"))
|
|
|
|
end)
|
2016-05-14 13:41:18 -07:00
|
|
|
|
|
|
|
it('default sort', function()
|
|
|
|
-- docs say omitted, empty or zero argument sorts on string representation
|
2016-05-14 13:51:05 -07:00
|
|
|
eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"])'))
|
|
|
|
eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval([[sort([3.3, 1, "2", "A", "a", "AA"], '')]]))
|
|
|
|
eq({ '2', 'A', 'AA', 'a', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 0)'))
|
|
|
|
eq({ '2', 'A', 'a', 'AA', 1, 3.3 }, eval('sort([3.3, 1, "2", "A", "a", "AA"], 1)'))
|
2022-04-25 20:35:05 -07:00
|
|
|
neq(nil, exc_exec('call sort([3.3, 1, "2"], 3)'):find('E474:'))
|
2016-05-14 13:41:18 -07:00
|
|
|
end)
|
2016-02-20 03:48:31 -07:00
|
|
|
end)
|