text_spec.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. describe('vim.text', function()
  6. before_each(clear)
  7. describe('indent()', function()
  8. it('validation', function()
  9. t.matches('size%: expected number, got string', t.pcall_err(vim.text.indent, 'x', 'x'))
  10. t.matches('size%: expected number, got nil', t.pcall_err(vim.text.indent, nil, 'x'))
  11. t.matches('opts%: expected table, got string', t.pcall_err(vim.text.indent, 0, 'x', 'z'))
  12. end)
  13. it('basic cases', function()
  14. -- Basic cases.
  15. eq({ '', 0 }, { vim.text.indent(0, '') })
  16. eq({ '', 0 }, { vim.text.indent(2, '') })
  17. eq({ ' a', 4 }, { vim.text.indent(2, ' a') })
  18. eq({ ' a\n b', 4 }, { vim.text.indent(2, ' a\n b') })
  19. eq({ '\t\ta', 1 }, { vim.text.indent(2, '\ta') })
  20. eq({ ' a\n\n', 5 }, { vim.text.indent(1, ' a\n\n') })
  21. -- Indent 1 (tab) => 0. Starting with empty + blank lines.
  22. eq({ '\n\naa a aa', 1 }, { vim.text.indent(0, '\n \n aa a aa') })
  23. -- Indent 1 (tab) => 2 (tabs). Starting with empty + blank lines, 1-tab indent.
  24. eq({ '\n\t\t\n\t\taa a aa', 1 }, { vim.text.indent(2, '\n\t\n\taa a aa') })
  25. -- Indent 4 => 2, expandtab=false preserves tabs after the common indent.
  26. eq(
  27. { ' foo\n bar\n \tbaz\n', 4 },
  28. { vim.text.indent(2, ' foo\n bar\n \tbaz\n') }
  29. )
  30. -- Indent 9 => 3, expandtab=true.
  31. eq(
  32. { ' foo\n\n bar \t baz\n', 9 },
  33. { vim.text.indent(3, '\t foo\n\n bar \t baz\n', { expandtab = 8 }) }
  34. )
  35. -- Indent 9 => 8, expandtab=true.
  36. eq(
  37. { ' foo\n\n bar\n', 9 },
  38. { vim.text.indent(8, '\t foo\n\n bar\n', { expandtab = 8 }) }
  39. )
  40. -- Dedent: 5 => 0.
  41. eq({ ' foo\n\nbar\n', 5 }, { vim.text.indent(0, ' foo\n\n bar\n') })
  42. -- Dedent: 1 => 0. Empty lines are ignored when deciding "common indent".
  43. eq(
  44. { ' \n \nfoo\n\nbar\nbaz\n \n', 1 },
  45. { vim.text.indent(0, ' \n \n foo\n\n bar\n baz\n \n') }
  46. )
  47. end)
  48. it('real-world cases', function()
  49. -- Dedent.
  50. eq({
  51. [[
  52. bufs:
  53. nvim args: 3
  54. lua args: {
  55. [0] = "foo.lua"
  56. }
  57. ]],
  58. 10,
  59. }, {
  60. vim.text.indent(
  61. 0,
  62. [[
  63. bufs:
  64. nvim args: 3
  65. lua args: {
  66. [0] = "foo.lua"
  67. }
  68. ]]
  69. ),
  70. })
  71. -- Indent 0 => 2.
  72. eq({
  73. [[
  74. # yay
  75. local function foo()
  76. if true then
  77. # yay
  78. end
  79. end
  80. return
  81. ]],
  82. 0,
  83. }, {
  84. vim.text.indent(
  85. 2,
  86. [[
  87. # yay
  88. local function foo()
  89. if true then
  90. # yay
  91. end
  92. end
  93. return
  94. ]]
  95. ),
  96. })
  97. -- 1-tab indent, last line spaces < tabsize.
  98. -- Preserves tab char immediately following the indent.
  99. eq({ 'text\n\tmatch\nmatch\ntext\n', 1 }, {
  100. vim.text.indent(0, (([[
  101. text
  102. match
  103. match
  104. text
  105. ]]):gsub('\n%s-([\n]?)$', '\n%1'))),
  106. })
  107. -- 1-tab indent, last line spaces=tabsize.
  108. eq({ 'text\n match\nmatch\ntext\n', 6 }, {
  109. vim.text.indent(
  110. 0,
  111. [[
  112. text
  113. match
  114. match
  115. text
  116. ]],
  117. { expandtab = 6 }
  118. ),
  119. })
  120. end)
  121. end)
  122. describe('hexencode(), hexdecode()', function()
  123. it('works', function()
  124. local cases = {
  125. { 'Hello world!', '48656C6C6F20776F726C6421' },
  126. { '😂', 'F09F9882' },
  127. }
  128. for _, v in ipairs(cases) do
  129. local input, output = unpack(v)
  130. eq(output, vim.text.hexencode(input))
  131. eq(input, vim.text.hexdecode(output))
  132. end
  133. end)
  134. it('with very large strings', function()
  135. local input, output = string.rep('😂', 2 ^ 16), string.rep('F09F9882', 2 ^ 16)
  136. eq(output, vim.text.hexencode(input))
  137. eq(input, vim.text.hexdecode(output))
  138. end)
  139. it('invalid input', function()
  140. -- Odd number of hex characters
  141. do
  142. local res, err = vim.text.hexdecode('ABC')
  143. eq(nil, res)
  144. eq('string must have an even number of hex characters', err)
  145. end
  146. -- Non-hexadecimal input
  147. do
  148. local res, err = vim.text.hexdecode('nothex')
  149. eq(nil, res)
  150. eq('string must contain only hex characters', err)
  151. end
  152. end)
  153. end)
  154. end)