robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.8 KiB94 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, setup = t.describe, t.it, t.setup
5local eq = t.eq
6local NIL = vim.NIL
7local eval = n.eval
8local clear = n.clear
9local api = n.api
10local fn = n.fn
11local command = n.command
12local pcall_err = t.pcall_err
13
14setup(clear)
15
16describe('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      pcall_err(command, '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)
94end)