aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/vim_spec.lua
diff options
context:
space:
mode:
authorHirokazu Hata <h.hata.ai.t@gmail.com>2020-03-02 16:38:43 +0900
committerGitHub <noreply@github.com>2020-03-01 23:38:43 -0800
commite35ff7371f4a61621587744a7620200380abbbe9 (patch)
tree9ea54d157518273c8180ba218bb43bce9d5025cb /test/functional/lua/vim_spec.lua
parente2e99b9f81c31458a97787ea66cb8befae7f0787 (diff)
downloadrneovim-e35ff7371f4a61621587744a7620200380abbbe9.tar.gz
rneovim-e35ff7371f4a61621587744a7620200380abbbe9.tar.bz2
rneovim-e35ff7371f4a61621587744a7620200380abbbe9.zip
lua: add vim.tbl_len() #11889
Diffstat (limited to 'test/functional/lua/vim_spec.lua')
-rw-r--r--test/functional/lua/vim_spec.lua63
1 files changed, 23 insertions, 40 deletions
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 532368c37c..5ce4bf2973 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -315,10 +315,7 @@ describe('lua stdlib', function()
local a = { x = { 1, 2 }, y = 5}
local b = vim.deepcopy(a)
- local count = 0
- for _ in pairs(b) do count = count + 1 end
-
- return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and count == 2
+ return b.x[1] == 1 and b.x[2] == 2 and b.y == 5 and vim.tbl_count(b) == 2
and tostring(a) ~= tostring(b)
]]))
@@ -326,31 +323,22 @@ describe('lua stdlib', function()
local a = {}
local b = vim.deepcopy(a)
- local count = 0
- for _ in pairs(b) do count = count + 1 end
-
- return vim.tbl_islist(b) and count == 0 and tostring(a) ~= tostring(b)
+ return vim.tbl_islist(b) and vim.tbl_count(b) == 0 and tostring(a) ~= tostring(b)
]]))
ok(exec_lua([[
local a = vim.empty_dict()
local b = vim.deepcopy(a)
- local count = 0
- for _ in pairs(b) do count = count + 1 end
-
- return not vim.tbl_islist(b) and count == 0
+ return not vim.tbl_islist(b) and vim.tbl_count(b) == 0
]]))
ok(exec_lua([[
local a = {x = vim.empty_dict(), y = {}}
local b = vim.deepcopy(a)
- local count = 0
- for _ in pairs(b) do count = count + 1 end
-
return not vim.tbl_islist(b.x) and vim.tbl_islist(b.y)
- and count == 2
+ and vim.tbl_count(b) == 2
and tostring(a) ~= tostring(b)
]]))
end)
@@ -430,10 +418,7 @@ describe('lua stdlib', function()
local b = {y = 2}
local c = vim.tbl_extend("keep", a, b)
- local count = 0
- for _ in pairs(c) do count = count + 1 end
-
- return c.x == 1 and b.y == 2 and count == 2
+ return c.x == 1 and b.y == 2 and vim.tbl_count(c) == 2
]]))
ok(exec_lua([[
@@ -442,10 +427,7 @@ describe('lua stdlib', function()
local c = {z = 3}
local d = vim.tbl_extend("keep", a, b, c)
- local count = 0
- for _ in pairs(d) do count = count + 1 end
-
- return d.x == 1 and d.y == 2 and d.z == 3 and count == 3
+ return d.x == 1 and d.y == 2 and d.z == 3 and vim.tbl_count(d) == 3
]]))
ok(exec_lua([[
@@ -453,10 +435,7 @@ describe('lua stdlib', function()
local b = {x = 3}
local c = vim.tbl_extend("keep", a, b)
- local count = 0
- for _ in pairs(c) do count = count + 1 end
-
- return c.x == 1 and count == 1
+ return c.x == 1 and vim.tbl_count(c) == 1
]]))
ok(exec_lua([[
@@ -464,10 +443,7 @@ describe('lua stdlib', function()
local b = {x = 3}
local c = vim.tbl_extend("force", a, b)
- local count = 0
- for _ in pairs(c) do count = count + 1 end
-
- return c.x == 3 and count == 1
+ return c.x == 3 and vim.tbl_count(c) == 1
]]))
ok(exec_lua([[
@@ -475,10 +451,7 @@ describe('lua stdlib', function()
local b = {}
local c = vim.tbl_extend("keep", a, b)
- local count = 0
- for _ in pairs(c) do count = count + 1 end
-
- return not vim.tbl_islist(c) and count == 0
+ return not vim.tbl_islist(c) and vim.tbl_count(c) == 0
]]))
ok(exec_lua([[
@@ -486,10 +459,7 @@ describe('lua stdlib', function()
local b = vim.empty_dict()
local c = vim.tbl_extend("keep", a, b)
- local count = 0
- for _ in pairs(c) do count = count + 1 end
-
- return vim.tbl_islist(c) and count == 0
+ return vim.tbl_islist(c) and vim.tbl_count(c) == 0
]]))
eq('Error executing lua: .../shared.lua: invalid "behavior": nil',
@@ -511,6 +481,19 @@ describe('lua stdlib', function()
)
end)
+ it('vim.tbl_count', function()
+ eq(0, exec_lua [[ return vim.tbl_count({}) ]])
+ eq(0, exec_lua [[ return vim.tbl_count(vim.empty_dict()) ]])
+ eq(0, exec_lua [[ return vim.tbl_count({nil}) ]])
+ eq(0, exec_lua [[ return vim.tbl_count({a=nil}) ]])
+ eq(1, exec_lua [[ return vim.tbl_count({1}) ]])
+ eq(2, exec_lua [[ return vim.tbl_count({1, 2}) ]])
+ eq(2, exec_lua [[ return vim.tbl_count({1, nil, 3}) ]])
+ eq(1, exec_lua [[ return vim.tbl_count({a=1}) ]])
+ eq(2, exec_lua [[ return vim.tbl_count({a=1, b=2}) ]])
+ eq(2, exec_lua [[ return vim.tbl_count({a=1, b=nil, c=3}) ]])
+ end)
+
it('vim.deep_equal', function()
eq(true, exec_lua [[ return vim.deep_equal({a=1}, {a=1}) ]])
eq(true, exec_lua [[ return vim.deep_equal({a={b=1}}, {a={b=1}}) ]])