TT#99900 utils: add table.keys()

* don't use 'string' as variable

Change-Id: Ie4412008a3084cb2b8ea793e5c3f82ce8ff86d68
(cherry picked from commit cb5de0e61c)
mr8.5.3
Victor Seva 5 years ago
parent 5da73b515a
commit 8a45af3839
No known key found for this signature in database
GPG Key ID: B1589889727198E0

@ -1,5 +1,5 @@
--
-- Copyright 2013 SipWise Team <development@sipwise.com>
-- Copyright 2013-2020 SipWise Team <development@sipwise.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
@ -159,6 +159,17 @@ function ut.size(t)
return 0
end
function ut.keys(t)
local keys = {}
local n = 0
for k,_ in pairs(t) do
n = n+1
keys[n] = k
end
return keys
end
function ut.val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
@ -257,11 +268,11 @@ function utils.implode(delimiter, list, quoter)
if not quoter then
quoter = ""
end
local string = quoter .. list[1] .. quoter
local str = quoter .. list[1] .. quoter
for i = 2, len do
string = string .. delimiter .. quoter .. list[i] .. quoter
str = str .. delimiter .. quoter .. list[i] .. quoter
end
return string
return str
end
-- from string to table

@ -98,6 +98,20 @@ TestUtils = {}
lu.assertEquals(t, 2)
end
function TestUtils:test_table_keys_nil()
lu.assertError(utils.table.keys, nil)
end
function TestUtils:test_table_keys_array()
local t = utils.table.keys({'a','b'})
lu.assertItemsEquals(t, {1,2})
end
function TestUtils:test_table_keys_ok()
t = utils.table.keys({hola={1,2},adios=2})
lu.assertItemsEquals(t, {'hola', 'adios'})
end
function TestUtils:test_table_shuffle()
lu.assertEquals(self.simple_list, {1,2,3})
utils.table.add(self.simple_list, 4)

Loading…
Cancel
Save