robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
3.2 KiB101 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, setup = t.describe, t.it, t.setup
5local clear = n.clear
6local eq = t.eq
7local pcall_err = t.pcall_err
8local eval = n.eval
9local fn = n.fn
10local api = n.api
11local command = n.command
12
13describe('printf()', function()
14  setup(clear)
15
16  it('works with zero and %b', function()
17    eq('0', fn.printf('%lb', 0))
18    eq('0', fn.printf('%llb', 0))
19    eq('0', fn.printf('%zb', 0))
20  end)
21  it('works with one and %b', function()
22    eq('1', fn.printf('%b', 1))
23    eq('1', fn.printf('%lb', 1))
24    eq('1', fn.printf('%llb', 1))
25    eq('1', fn.printf('%zb', 1))
26  end)
27  it('works with 0xff and %b', function()
28    eq('11111111', fn.printf('%b', 0xff))
29    eq('11111111', fn.printf('%lb', 0xff))
30    eq('11111111', fn.printf('%llb', 0xff))
31    eq('11111111', fn.printf('%zb', 0xff))
32  end)
33  it('accepts width modifier with %b', function()
34    eq('  1', fn.printf('%3b', 1))
35  end)
36  it('accepts prefix modifier with %b', function()
37    eq('0b1', fn.printf('%#b', 1))
38  end)
39  it('writes capital B with %B', function()
40    eq('0B1', fn.printf('%#B', 1))
41  end)
42  it('accepts prefix, zero-fill and width modifiers with %b', function()
43    eq('0b001', fn.printf('%#05b', 1))
44  end)
45  it('accepts prefix and width modifiers with %b', function()
46    eq('  0b1', fn.printf('%#5b', 1))
47  end)
48  it('does not write prefix for zero with prefix and width modifier used with %b', function()
49    eq('    0', fn.printf('%#5b', 0))
50  end)
51  it('accepts precision modifier with %b', function()
52    eq('00000', fn.printf('%.5b', 0))
53  end)
54  it('accepts all modifiers with %b at once', function()
55    -- zero-fill modifier is ignored when used with left-align
56    -- force-sign and add-blank are ignored
57    -- use-grouping-characters modifier is ignored always
58    eq('0b00011   ', fn.printf("% '+#0-10.5b", 3))
59  end)
60  it('errors out when %b modifier is used for a list', function()
61    eq('Vim(call):E745: Using a List as a Number', pcall_err(command, 'call printf("%b", [])'))
62  end)
63  it('errors out when %b modifier is used for a float', function()
64    eq(
65      'Vim(call):E805: Using a Float as a Number',
66      pcall_err(command, 'call printf("%b", 3.1415926535)')
67    )
68  end)
69  it('works with %p correctly', function()
70    local null_ret = nil
71    local seen_rets = {}
72    -- Collect all args in an array to avoid possible allocation of the same
73    -- address after freeing unreferenced values.
74    api.nvim_set_var('__args', {})
75    local function check_printf(expr, is_null)
76      command('call add(__args, ' .. expr .. ')')
77      command('let __result = printf("%p", __args[-1])')
78      local id_ret = eval('id(__args[-1])')
79      eq(id_ret, api.nvim_get_var('__result'))
80      if is_null then
81        if null_ret then
82          eq(null_ret, id_ret)
83        else
84          null_ret = id_ret
85        end
86      else
87        eq(nil, seen_rets[id_ret])
88        seen_rets[id_ret] = expr
89      end
90      api.nvim_del_var('__result')
91    end
92    check_printf('v:_null_string', true)
93    check_printf('v:_null_list', true)
94    check_printf('v:_null_dict', true)
95    check_printf('v:_null_blob', true)
96    check_printf('[]')
97    check_printf('{}')
98    check_printf('0z')
99    check_printf('function("tr", ["a"])')
100  end)
101end)