aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/vim_spec.lua
diff options
context:
space:
mode:
authorGregory Anders <greg@gpanders.com>2023-04-19 06:45:56 -0600
committerGregory Anders <greg@gpanders.com>2023-04-19 07:04:49 -0600
commit6b96122453fda22dc44a581af1d536988c1adf41 (patch)
tree367cc531cb9589c27630def8d9f8219700f193ae /test/functional/lua/vim_spec.lua
parent0a3645a72307afa563683a6e06c544810e0b65eb (diff)
downloadrneovim-6b96122453fda22dc44a581af1d536988c1adf41.tar.gz
rneovim-6b96122453fda22dc44a581af1d536988c1adf41.tar.bz2
rneovim-6b96122453fda22dc44a581af1d536988c1adf41.zip
fix(iter): add tag to packed table
If pack() is called with a single value, it does not create a table; it simply returns the value it is passed. When unpack is called with a table argument, it interprets that table as a list of values that were packed together into a table. This causes a problem when the single value being packed is _itself_ a table. pack() will not place it into another table, but unpack() sees the table argument and tries to unpack it. To fix this, we add a simple "tag" to packed table values so that unpack() only attempts to unpack tables that have this tag. Other tables are left alone. The tag is simply the length of the table.
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r--test/functional/lua/vim_spec.lua27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index e5caf6f6f7..07b0f0340a 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -3381,6 +3381,33 @@ describe('lua stdlib', function()
end
end)
eq({ A = 2, C = 6 }, it:totable())
+
+ it('handles table values mid-pipeline', function()
+ local map = {
+ item = {
+ file = 'test',
+ },
+ item_2 = {
+ file = 'test',
+ },
+ item_3 = {
+ file = 'test',
+ },
+ }
+
+ local output = vim.iter(map):map(function(key, value)
+ return { [key] = value.file }
+ end):totable()
+
+ table.sort(output, function(a, b)
+ return next(a) < next(b)
+ end)
+
+ eq({
+ { item = 'test' },
+ { item_2 = 'test' },
+ { item_3 = 'test' },
+ }, output)
end)
end)
end)