printf_spec.lua 3.1 KB

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