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