diff --git a/mocks/.luacheckrc b/mocks/.luacheckrc new file mode 100644 index 0000000..3cf43ac --- /dev/null +++ b/mocks/.luacheckrc @@ -0,0 +1,2 @@ +globals = {'sr', '_ENV'} +ignore = { '212' } diff --git a/mocks/hdr.lua b/mocks/hdr.lua new file mode 100644 index 0000000..7506669 --- /dev/null +++ b/mocks/hdr.lua @@ -0,0 +1,98 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". + +local logging = require('logging') +local log_file = require('logging.file') + +local hdrMock = { + __class__ = 'hdrMock', + headers = {}, + headers_reply = {}, + _logger = log_file('reports/sr_hdr_%s.log', '%Y-%m-%d'), + _logger_levels = { + dbg = logging.DEBUG, + info = logging.INFO, + warn = logging.WARN, + err = logging.ERROR, + crit = logging.FATAL + } +} + function hdrMock.new() + local t = {} + + t.__class__ = 'hdrMock' + t.headers = {} + t.headers_reply = {} + + function t._is_header(text) + local result = string.match(text,'[^:]+: .+\r\n$') + if result then + return true + end + return false + end + + function t._get_header(text) + if text then + local pattern = "^" .. text .. ": (.+)\r\n$" + for _,v in ipairs(t.headers) do + local result = string.match(v, pattern) + --print(string.format("v:%s pattern:%s result:%s", v, pattern, tostring(result))) + if result then + return result + end + end + end + end + + function t.append(text) + if text then + if not t._is_header(text) then + error("text: " .. text .. " malformed header") + end + table.insert(t.headers, text) + end + end + + function t.insert(text) + if text then + if not t._is_header(text) then + error("text: " .. text .. " malformed header") + end + table.insert(t.headers, 1, text) + end + end + + function t.remove(text) + if text then + for i,v in ipairs(t.headers) do + if string.starts(v, text .. ":") then + table.remove(t.headers, i) + return + end + end + end + end + + local hdrMock_MT = { __index = hdrMock } + setmetatable(t, hdrMock_MT) + return t + end +-- end class +return hdrMock diff --git a/mocks/pv.lua b/mocks/pv.lua new file mode 100644 index 0000000..a323218 --- /dev/null +++ b/mocks/pv.lua @@ -0,0 +1,381 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". + +local logging = require ('logging') +local log_file = require ('logging.file') +local utils = require 'ngcp.utils' +local utable = utils.table + +local pvMock = { + __class__ = 'pvMock', + vars = {}, + hdr = nil, + _logger = log_file('reports/sr_pv_%s.log', '%Y-%m-%d'), + _logger_levels = { + dbg = logging.DEBUG, + info = logging.INFO, + warn = logging.WARN, + err = logging.ERROR, + crit = logging.FATAL + } +} + function pvMock.new(hdr) + local t = {} + + t.__class__ = 'pvMock' + -- fake pseudo vars go here + t.vars_pv = { + ro = { + si = "127.0.0.1", + sp = "9090" + }, + rw = { + rU = "noname" + } + } + t.vars = {} + t.hdr = hdr + + function t._is_xavp(id) + local patterns = { + '%$xavp%(([%w_^%[]+)%)$', + '%$xavp%(([%w_^%[]+)%[(%d+)%]%)$', + '%$xavp%(([%w_^%[]+)=>([%w_^%[]+)%)$', + '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%)$', + '%$xavp%(([%w_^%[]+)=>([%w_^%[]+)%[(%d+)%]%)$', + '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%[(%d+)%]%)$', + '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%[%*%]%)$' + } + local logger = logging.file('reports/sr_pv_%s.log', '%Y-%m-%d') + for _,v in pairs(patterns) do + for _id, indx, key, kindx in string.gmatch(id, v) do + logger:log(logging.DEBUG, string.format("_:%d id:%s v:%s _id:%s indx:%s key:%s kindx:%s", _, id, v, tostring(_id), tostring(indx), tostring(key), tostring(kindx))) + if _ == 5 or _ == 3 then + kindx = key + key = indx + indx = nil + else + indx = tonumber(indx) + end + if kindx then + kindx = tonumber(kindx) + end + return { id=_id, key=key, + indx=indx, kindx=kindx, clean=(v==patterns[7]), + type='xavp' } + end + end + end + + function t._clean_id(id) + local k + k = string.gsub(id, 's:', '') + k = string.gsub(k, 'i:', '') + return k + end + + function t._is_avp(id) + local _id + local patterns = { + '%$avp%(([%w_]+)%)$', + '%$%(avp%(([%w_]+)%)%)$', + '%$%(avp%(([%w_]+)%)%[%*%]%)$' + } + _id = t._clean_id(id) + for _,v in pairs(patterns) do + for i in string.gmatch(_id, v) do + return { id=i, clean=(v==patterns[3]), type='avp' } + end + end + end + + function t._is_var(id) + local patterns = { + '%$var%(([%w_]+)%)$', + '%$%(var%(([%w_]+)%)%)$', + } + for _,v in pairs(patterns) do + for key in string.gmatch(id, v) do + return { id=key, clean=false, type='var' } + end + end + end + + function t._is_dlg_var(id) + local patterns = { + '%$dlg_var%(([%w_]+)%)$', + '%$%(dlg_var%(([%w_]+)%)%)$', + } + for _,v in pairs(patterns) do + for key in string.gmatch(id, v) do + return { id=key, clean=false, type='dlg_var' } + end + end + end + + function t._is_hdr(id) + local patterns = { + '%$hdr%(([^:]+)%)$', + } + for _,v in pairs(patterns) do + for key in string.gmatch(id, v) do + return { id=key, clean=false, type='hdr' } + end + end + end + + function t._is_pv(id) + local real_id = string.match(id, '%$(%w+)$') + if not real_id then + return + end + for k,_ in pairs(t.vars_pv) do + for k0,_ in pairs(t.vars_pv[k]) do + --print(string.format("id:%s, k:%s k0:%s", real_id, k, k0)) + if real_id == k0 then + return { id=k0, clean=false, type='pv', mode=k} + end + end + end + end + + function t._is(id) + if not id then + error("id empty") + end + local result = t._is_xavp(id) + + if not result then + result = t._is_avp(id) + end + if not result then + result = t._is_var(id) + end + if not result then + result = t._is_dlg_var(id) + end + if not result then + result = t._is_hdr(id) + end + if not result then + result = t._is_pv(id) + end + if not result then + error(string.format("not implemented or wrong id:%s", id)) + end + result.private_id = result.type .. ':' .. result.id + return result + end + + function t.get(id) + local result = t._is(id) + if not result then + return + end + + if result.type == 'var' or result.type == 'dlg_var' then + return t.vars[result.private_id] + elseif result.type == 'xavp' then + if not t.vars[result.private_id] then + return + end + if not result.kindx then + result.kindx = 0 + end + if not result.key then + if not result.indx then + return t.vars[result.private_id] + end + end + if not result.indx then + result.indx = 0 + end + if t.vars[result.private_id][result.indx] then + if t.vars[result.private_id][result.indx][result.key] then + if result.clean then + return t.vars[result.private_id][result.indx][result.key] + end + if t.vars[result.private_id][result.indx][result.key][result.kindx] then + return t.vars[result.private_id][result.indx][result.key][result.kindx] + end + end + end + elseif result.type == 'avp' then + if t.vars[result.private_id] then + local l = t.vars[result.private_id]:list() + if result.clean then + return l + else + return l[1] + end + end + elseif result.type == 'hdr' then + if t.hdr then + return t.hdr._get_header(result.id) + end + elseif result.type == 'pv' then + return t.vars_pv[result.mode][result.id] + end + end + + function t._addvalue_new(result, value) + local temp + if result.type == 'var' or result.type == 'dlg_var' then + t.vars[result.private_id] = value + elseif result.type == 'xavp' then + if not result.indx then + result.indx = 0 + end + if not result.kindx then + result.kindx = 0 + end + if result.indx ~= 0 or result.kindx ~= 0 then + error(string.format("xavp(%s) has not been initilizated", result.id)) + end + t.vars[result.private_id] = utils.Stack:new() + temp = {} + temp[result.key] = utils.Stack:new() + temp[result.key]:push(value) + t.vars[result.private_id]:push(temp) + elseif result.type == 'avp' then + t.vars[result.private_id] = utils.Stack:new() + t.vars[result.private_id]:push(value) + elseif result.type == 'pv' and result.mode == 'rw' then + t.vars_pv.rw[result.id] = value + end + end + + function t._addvalue_with_value(result, value) + local temp + if result.type == 'var' or result.type == 'dlg_var' then + t.vars[result.private_id] = value + elseif result.type == 'xavp' then + if not result.indx then + if result.kindx and result.kindx ~= 0 then + error(string.format("kindx:%d must be 0", result.kindx)) + end + temp = {} + temp[result.key] = utils.Stack:new() + temp[result.key]:push(value) + t.vars[result.private_id]:push(temp) + else + if t.vars[result.private_id][result.indx] == nil then + error(string.format("xavp(%s[%d]) does not exist", result.id, result.indx)) + elseif t.vars[result.private_id][result.indx] == false then + t.vars[result.private_id][result.indx] = {} + end + if not result.kindx then + result.kindx = 0 + end + if not t.vars[result.private_id][result.indx][result.key] then + t.vars[result.private_id][result.indx][result.key] = utils.Stack:new() + --error(string.format("t:%s result:%s", utable.tostring(t.vars[result.private_id]), utable.tostring(result))) + end + t.vars[result.private_id][result.indx][result.key]:push(value) + end + elseif result.type == 'avp' then + t.vars[result.private_id]:push(value) + elseif result.type == 'pv' and result.mode == 'rw' then + t.vars_pv.rw[result.id] = value + end + end + + function t._addvalue(id, value) + local result = t._is(id) + if result.clean then + -- clean var + t.log("dbg",string.format("sr.pv erase avp[%s]", result.id)) + t.vars[result.private_id] = nil + end + if not t.vars[result.private_id] then + t._addvalue_new(result, value) + else + t._addvalue_with_value(result, value) + end + t.log("dbg", string.format("sr.pv vars:%s", utable.tostring(t.vars))) + end + + function t.seti(id, value) + if type(value) ~= 'number' then + error("value is not a number") + end + t._addvalue(id, value) + end + + function t.sets(id, value) + if type(value) ~= 'string' then + error("value is not a string") + end + t._addvalue(id, value) + end + + function t.unset(id) + local result = t._is(id) + if result.type == 'xavp' then + if t.vars[result.private_id] then + if not result.key then + if not result.indx then + -- xavp(g) -> clean all + t.vars[result.private_id] = nil + return + else + -- xavp(g[0]) + t.vars[result.private_id][result.indx] = false + return + end + else + if not result.indx then + result.indx = 0 + end + end + -- xavp(g[1]=>k) + t.vars[result.private_id][result.indx][result.key] = nil + end + elseif result.type == 'avp' then + t.vars[result.private_id] = nil + elseif result.type == 'var' or result.type == 'dlg_var' then + t.vars[result.private_id] = nil + end + t.log("dbg", string.format("sr.pv vars:%s", utable.tostring(t.vars))) + end + + function t.is_null(id) + local result = t._is(id) + if not result then + return true + end + if not t.vars[result.private_id] then + return true + end + return false + end + + function t.log(level, message) + if not t._logger_levels[level] then + error(string.format("level %s unknown", level)) + end + t._logger:log(t._logger_levels[level], message) + end + + local pvMock_MT = { __index = pvMock } + setmetatable(t, pvMock_MT) + return t + end +-- end class +return pvMock diff --git a/mocks/sr.lua b/mocks/sr.lua index 389654f..2fe2e35 100644 --- a/mocks/sr.lua +++ b/mocks/sr.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,515 +17,18 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -require ('logging.file') -require 'lemock' -require 'ngcp.utils' +local logging = require ('logging') +local log_file = require ('logging.file') +local lemock = require ('lemock') -hdrMock = { - __class__ = 'hdrMock', - headers = {}, - headers_reply = {}, - _logger = logging.file('reports/sr_hdr_%s.log', '%Y-%m-%d'), - _logger_levels = { - dbg = logging.DEBUG, - info = logging.INFO, - warn = logging.WARN, - err = logging.ERROR, - crit = logging.FATAL - } -} - function hdrMock:new() - local t = {} - - t.__class__ = 'hdrMock' - t.headers = {} - t.headers_reply = {} - - function t._is_header(text) - local result = string.match(text,'[^:]+: .+\r\n$') - if result then - return true - end - return false - end - - function t._get_header(text) - local _,v, result - local pattern = "^" .. text .. ": (.+)\r\n$" - if text then - for _,v in ipairs(t.headers) do - result = string.match(v, pattern) - --print(string.format("v:%s pattern:%s result:%s", v, pattern, tostring(result))) - if result then - return result - end - end - end - end - - function t.append(text) - if text then - if not t._is_header(text) then - error("text: " .. text .. " malformed header") - end - table.insert(t.headers, text) - end - end - - function t.insert(text) - if text then - if not t._is_header(text) then - error("text: " .. text .. " malformed header") - end - table.insert(t.headers, 1, text) - end - end - - function t.remove(text) - local i,v - if text then - for i,v in ipairs(t.headers) do - if string.starts(v, text .. ":") then - table.remove(t.headers, i) - return - end - end - end - end - - hdrMock_MT = { __index = hdrMock } - setmetatable(t, hdrMock_MT) - return t - end --- end class - -pvMock = { - __class__ = 'pvMock', - vars = {}, - hdr = nil, - _logger = logging.file('reports/sr_pv_%s.log', '%Y-%m-%d'), - _logger_levels = { - dbg = logging.DEBUG, - info = logging.INFO, - warn = logging.WARN, - err = logging.ERROR, - crit = logging.FATAL - } -} - function pvMock:new(hdr) - local t = {} - - t.__class__ = 'pvMock' - -- fake pseudo vars go here - t.vars_pv = { - ro = { - si = "127.0.0.1", - sp = "9090" - }, - rw = { - rU = "noname" - } - } - t.vars = {} - t.hdr = hdr - - function t._is_xavp(id) - local _id, indx, key - local patterns = { - '%$xavp%(([%w_^%[]+)%)$', - '%$xavp%(([%w_^%[]+)%[(%d+)%]%)$', - '%$xavp%(([%w_^%[]+)=>([%w_^%[]+)%)$', - '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%)$', - '%$xavp%(([%w_^%[]+)=>([%w_^%[]+)%[(%d+)%]%)$', - '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%[(%d+)%]%)$', - '%$xavp%(([%w_^%[]+)%[(%d+)%]=>([%w_^%[]+)%[%*%]%)$' - } - local logger = logging.file('reports/sr_pv_%s.log', '%Y-%m-%d') - for _,v in pairs(patterns) do - for _id, indx, key, kindx in string.gmatch(id, v) do - logger:log(logging.DEBUG, string.format("_:%d id:%s v:%s _id:%s indx:%s key:%s kindx:%s", _, id, v, tostring(_id), tostring(indx), tostring(key), tostring(kindx))) - if _ == 5 or _ == 3 then - kindx = key - key = indx - indx = nil - else - indx = tonumber(indx) - end - if kindx then - kindx = tonumber(kindx) - end - return { id=_id, key=key, - indx=indx, kindx=kindx, clean=(v==patterns[7]), - type='xavp' } - end - end - end - - function t._clean_id(id) - local k - k = string.gsub(id, 's:', '') - k = string.gsub(k, 'i:', '') - return k - end - - function t._is_avp(id) - local i, _id - local patterns = { - '%$avp%(([%w_]+)%)$', - '%$%(avp%(([%w_]+)%)%)$', - '%$%(avp%(([%w_]+)%)%[%*%]%)$' - } - _id = t._clean_id(id) - for _,v in pairs(patterns) do - for i in string.gmatch(_id, v) do - return { id=i, clean=(v==patterns[3]), type='avp' } - end - end - end - - function t._is_var(id) - local key, _, v - local patterns = { - '%$var%(([%w_]+)%)$', - '%$%(var%(([%w_]+)%)%)$', - } - for _,v in pairs(patterns) do - for key in string.gmatch(id, v) do - return { id=key, clean=false, type='var' } - end - end - end - - function t._is_dlg_var(id) - local patterns = { - '%$dlg_var%(([%w_]+)%)$', - '%$%(dlg_var%(([%w_]+)%)%)$', - } - for _,v in pairs(patterns) do - for key in string.gmatch(id, v) do - return { id=key, clean=false, type='dlg_var' } - end - end - end - - function t._is_hdr(id) - local key, _, v - local patterns = { - '%$hdr%(([^:]+)%)$', - } - for _,v in pairs(patterns) do - for key in string.gmatch(id, v) do - return { id=key, clean=false, type='hdr' } - end - end - end - - function t._is_pv(id) - local k0,k,_ - local real_id = string.match(id, '%$(%w+)$') - if not real_id then - return - end - for k,_ in pairs(t.vars_pv) do - for k0,_ in pairs(t.vars_pv[k]) do - --print(string.format("id:%s, k:%s k0:%s", real_id, k, k0)) - if real_id == k0 then - return { id=k0, clean=false, type='pv', mode=k} - end - end - end - end - - function t._is(id) - if not id then - error("id empty") - end - local result = t._is_xavp(id) - - if not result then - result = t._is_avp(id) - end - if not result then - result = t._is_var(id) - end - if not result then - result = t._is_dlg_var(id) - end - if not result then - result = t._is_hdr(id) - end - if not result then - result = t._is_pv(id) - end - if not result then - error(string.format("not implemented or wrong id:%s", id)) - end - result.private_id = result.type .. ':' .. result.id - return result - end - - function t.get(id) - local result = t._is(id) - if not result then - return - end - - if result.type == 'var' or result.type == 'dlg_var' then - return t.vars[result.private_id] - elseif result.type == 'xavp' then - if not t.vars[result.private_id] then - return - end - if not result.kindx then - result.kindx = 0 - end - if not result.key then - if not result.indx then - return t.vars[result.private_id] - end - end - if not result.indx then - result.indx = 0 - end - if t.vars[result.private_id][result.indx] then - if t.vars[result.private_id][result.indx][result.key] then - if result.clean then - return t.vars[result.private_id][result.indx][result.key] - end - if t.vars[result.private_id][result.indx][result.key][result.kindx] then - return t.vars[result.private_id][result.indx][result.key][result.kindx] - end - end - end - elseif result.type == 'avp' then - if t.vars[result.private_id] then - local l = t.vars[result.private_id]:list() - if result.clean then - return l - else - return l[1] - end - end - elseif result.type == 'hdr' then - if t.hdr then - return t.hdr._get_header(result.id) - end - elseif result.type == 'pv' then - return t.vars_pv[result.mode][result.id] - end - end - - function t._addvalue_new(result, value) - local temp - if result.type == 'var' or result.type == 'dlg_var' then - t.vars[result.private_id] = value - elseif result.type == 'xavp' then - if not result.indx then - result.indx = 0 - end - if not result.kindx then - result.kindx = 0 - end - if result.indx ~= 0 or result.kindx ~= 0 then - error(string.format("xavp(%s) has not been initilizated", result.id)) - end - t.vars[result.private_id] = Stack:new() - temp = {} - temp[result.key] = Stack:new() - temp[result.key]:push(value) - t.vars[result.private_id]:push(temp) - elseif result.type == 'avp' then - t.vars[result.private_id] = Stack:new() - t.vars[result.private_id]:push(value) - elseif result.type == 'pv' and result.mode == 'rw' then - t.vars_pv.rw[result.id] = value - end - end - - function t._addvalue_with_value(result, value) - local temp - if result.type == 'var' or result.type == 'dlg_var' then - t.vars[result.private_id] = value - elseif result.type == 'xavp' then - if not result.indx then - if result.kindx and result.kindx ~= 0 then - error(string.format("kindx:%d must be 0", result.kindx)) - end - temp = {} - temp[result.key] = Stack:new() - temp[result.key]:push(value) - t.vars[result.private_id]:push(temp) - else - if t.vars[result.private_id][result.indx] == nil then - error(string.format("xavp(%s[%d]) does not exist", result.id, result.indx)) - elseif t.vars[result.private_id][result.indx] == false then - t.vars[result.private_id][result.indx] = {} - end - if not result.kindx then - result.kindx = 0 - end - if not t.vars[result.private_id][result.indx][result.key] then - t.vars[result.private_id][result.indx][result.key] = Stack:new() - --error(string.format("t:%s result:%s", table.tostring(t.vars[result.private_id]), table.tostring(result))) - end - t.vars[result.private_id][result.indx][result.key]:push(value) - end - elseif result.type == 'avp' then - t.vars[result.private_id]:push(value) - elseif result.type == 'pv' and result.mode == 'rw' then - t.vars_pv.rw[result.id] = value - end - end - - function t._addvalue(id, value) - local result = t._is(id) - if result.clean then - -- clean var - t.log("dbg",string.format("sr.pv erase avp[%s]", result.id)) - t.vars[result.private_id] = nil - end - if not t.vars[result.private_id] then - t._addvalue_new(result, value) - else - t._addvalue_with_value(result, value) - end - t.log("dbg", string.format("sr.pv vars:%s", table.tostring(t.vars))) - end - - function t.seti(id, value) - if type(value) ~= 'number' then - error("value is not a number") - end - t._addvalue(id, value) - end - - function t.sets(id, value) - if type(value) ~= 'string' then - error("value is not a string") - end - t._addvalue(id, value) - end - - function t.unset(id) - local result = t._is(id) - if result.type == 'xavp' then - if t.vars[result.private_id] then - if not result.key then - if not result.indx then - -- xavp(g) -> clean all - t.vars[result.private_id] = nil - return - else - -- xavp(g[0]) - t.vars[result.private_id][result.indx] = false - return - end - else - if not result.indx then - result.indx = 0 - end - end - -- xavp(g[1]=>k) - t.vars[result.private_id][result.indx][result.key] = nil - end - elseif result.type == 'avp' then - t.vars[result.private_id] = nil - elseif result.type == 'var' or result.type == 'dlg_var' then - t.vars[result.private_id] = nil - end - t.log("dbg", string.format("sr.pv vars:%s", table.tostring(t.vars))) - end - - function t.is_null(id) - local result = t._is(id) - if not result then - return true - end - if not t.vars[result.private_id] then - return true - end - return false - end - - function t.log(level, message) - if not t._logger_levels[level] then - error(string.format("level %s unknown", level)) - end - t._logger:log(t._logger_levels[level], message) - end - - pvMock_MT = { __index = pvMock } - setmetatable(t, pvMock_MT) - return t - end --- end class - --- class xavpMock -xavpMock = { - __class__ = 'xavpMock', - _logger = logging.file("reports/xavp_%s.log", "%Y-%m-%d"), - _logger_levels = { - dbg = logging.DEBUG, - info = logging.INFO, - warn = logging.WARN, - err = logging.ERROR, - crit = logging.FATAL - } -} - function xavpMock:new(pv) - local t = {} - - t.__class__ = 'hdrMock' - t.pv = pv - - function t._get_xavp(xavp_name, index, mode) - local private_id = "xavp:" .. xavp_name - local k,v - local temp = {} - if not t.pv.vars[private_id] then - error(string.format("%s not found", xavp_name)) - elseif not t.pv.vars[private_id][index] then - error(string.format("%s[%d] not found", xavp_name, index)) - end - if mode == 0 then - for k,v in pairs(t.pv.vars[private_id][index]) do - temp[k] = v:list() - end - else - for k,v in pairs(t.pv.vars[private_id][index]) do - temp[k] = v[0] - end - end - return temp - end - - function t.get_keys(xavp_name, index) - local k,_ - local output = {} - - xavp = t._get_xavp(xavp_name, index, 1) - for k,_ in pairs(xavp) do - table.insert(output, k) - end - return output - end - - function t.get(xavp_name, index, mode) - if not mode then mode = 0 end - xavp = t._get_xavp(xavp_name, index, mode) - return xavp - end - - xavpMock_MT = { __index = xavpMock } - setmetatable(t, xavpMock_MT) - return t - end ---end class +local hdrMock = require 'mocks.hdr' +local pvMock = require 'mocks.pv' +local xavpMock = require 'mocks.xavp' -- class srMock -srMock = { +local srMock = { __class__ = 'srMock', - _logger = logging.file("reports/sr_%s.log", "%Y-%m-%d"), + _logger = log_file("reports/sr_%s.log", "%Y-%m-%d"), _logger_levels = { dbg = logging.DEBUG, info = logging.INFO, @@ -534,20 +37,20 @@ srMock = { crit = logging.FATAL } } -srMock_MT = { __index = srMock, __newindex = lemock.controller():mock() } - function srMock:new() +local srMock_MT = { __index = srMock, __newindex = lemock.controller():mock() } + function srMock.new() local t = {} - t.hdr = hdrMock:new() - t.pv = pvMock:new(t.hdr) + t.hdr = hdrMock.new() + t.pv = pvMock.new(t.hdr) function t.log(level, message) if not t._logger_levels[level] then error(string.format("level %s unknown", tostring(level))) end t._logger:log(t._logger_levels[level], message) end - t.xavp = xavpMock:new(t.pv) + t.xavp = xavpMock.new(t.pv) setmetatable(t, srMock_MT) return t end -- end class ---EOF \ No newline at end of file +return srMock diff --git a/mocks/xavp.lua b/mocks/xavp.lua new file mode 100644 index 0000000..99d1303 --- /dev/null +++ b/mocks/xavp.lua @@ -0,0 +1,82 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". + +local logging = require ('logging') +local log_file = require ('logging.file') + +-- class xavpMock +local xavpMock = { + __class__ = 'xavpMock', + _logger = log_file("reports/xavp_%s.log", "%Y-%m-%d"), + _logger_levels = { + dbg = logging.DEBUG, + info = logging.INFO, + warn = logging.WARN, + err = logging.ERROR, + crit = logging.FATAL + } +} + function xavpMock.new(pv) + local t = {} + + t.__class__ = 'hdrMock' + t.pv = pv + + function t._get_xavp(xavp_name, index, mode) + local private_id = "xavp:" .. xavp_name + local temp = {} + if not t.pv.vars[private_id] then + error(string.format("%s not found", xavp_name)) + elseif not t.pv.vars[private_id][index] then + error(string.format("%s[%d] not found", xavp_name, index)) + end + if mode == 0 then + for k,v in pairs(t.pv.vars[private_id][index]) do + temp[k] = v:list() + end + else + for k,v in pairs(t.pv.vars[private_id][index]) do + temp[k] = v[0] + end + end + return temp + end + + function t.get_keys(xavp_name, index) + local output = {} + + local xavp = t._get_xavp(xavp_name, index, 1) + for k,_ in pairs(xavp) do + table.insert(output, k) + end + return output + end + + function t.get(xavp_name, index, mode) + if not mode then mode = 0 end + local xavp = t._get_xavp(xavp_name, index, mode) + return xavp + end + + local xavpMock_MT = { __index = xavpMock } + setmetatable(t, xavpMock_MT) + return t + end +--end class +return xavpMock diff --git a/ngcp/.luacheckrc b/ngcp/.luacheckrc index cd11ffc..3cf43ac 100644 --- a/ngcp/.luacheckrc +++ b/ngcp/.luacheckrc @@ -1,2 +1,2 @@ globals = {'sr', '_ENV'} -ignore = { 'NGCP.*', '212' } \ No newline at end of file +ignore = { '212' } diff --git a/ngcp/avp.lua b/ngcp/avp.lua index 63eec83..bd77806 100644 --- a/ngcp/avp.lua +++ b/ngcp/avp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,11 +17,12 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- + -- class NGCPAvp -NGCPAvp = { +local NGCPAvp = { __class__ = 'NGCPAvp' } -NGCPAvp_MT = { +local NGCPAvp_MT = { __index = NGCPAvp, } @@ -60,10 +61,7 @@ NGCPAvp_MT = { end function NGCPAvp:clean() - --print("NGCPAvp:clean") - --print(table.tostring(getmetatable(self))) - --print(table.tostring(self)) sr.pv.unset(self.id) end -- class ---EOF \ No newline at end of file +return NGCPAvp diff --git a/ngcp/config.lua b/ngcp/config.lua index d533e8f..a6e57a9 100644 --- a/ngcp/config.lua +++ b/ngcp/config.lua @@ -25,10 +25,10 @@ if not luasql then luasql = driver end -- class NGCPConfig -NGCPConfig = { +local NGCPConfig = { __class__ = 'NGCPConfig' } -NGCPConfig_MT = { __index = NGCPConfig } +local NGCPConfig_MT = { __index = NGCPConfig } function NGCPConfig:new() local t = { @@ -116,4 +116,5 @@ NGCPConfig_MT = { __index = NGCPConfig } end return defs end --- class \ No newline at end of file +-- class +return NGCPConfig diff --git a/ngcp/cp.lua b/ngcp/cp.lua index 22bbf53..013f537 100644 --- a/ngcp/cp.lua +++ b/ngcp/cp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,14 +17,16 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.pref' +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPXAvp = require 'ngcp.xavp' +local NGCPPrefs = require 'ngcp.pref' -- class NGCPContractPrefs -NGCPContractPrefs = { +local NGCPContractPrefs = { __class__ = 'NGCPContractPrefs' } -NGCPContractPrefs_MT = { __index = NGCPContractPrefs } +local NGCPContractPrefs_MT = { __index = NGCPContractPrefs } NGCPContractPrefs_MT.__tostring = function () local output @@ -85,9 +87,8 @@ NGCPContractPrefs_MT.__tostring = function () if row then while row do - --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) - table.add(keys, row.attribute) + utable.add(keys, row.attribute) defaults[row.attribute] = nil row = cur:fetch({}, "a") end @@ -120,4 +121,4 @@ NGCPContractPrefs_MT.__tostring = function () end end -- class ---EOF +return NGCPContractPrefs diff --git a/ngcp/dlgcnt.lua b/ngcp/dlgcnt.lua index 73aa1a6..dee824d 100644 --- a/ngcp/dlgcnt.lua +++ b/ngcp/dlgcnt.lua @@ -21,7 +21,8 @@ local NGCPDlgCounters = { __class__ = 'NGCPDlgCounters' } local redis = require 'redis'; -require 'ngcp.utils'; +local utils = require 'ngcp.utils'; +local utable = utils.table; _ENV = NGCPDlgCounters @@ -30,8 +31,8 @@ local NGCPDlgCounters_MT = { __index = NGCPDlgCounters } NGCPDlgCounters_MT.__tostring = function (t) return string.format("config:%s central:%s pair:%s", - table.tostring(t.config), table.tostring(t.central), - table.tostring(t.pair)); + utable.tostring(t.config), utable.tostring(t.central), + utable.tostring(t.pair)); end function NGCPDlgCounters.new() @@ -107,7 +108,7 @@ end self.pair = self._connect(self.config.pair); end local res = self.pair:lrange(callid, 0, -1); - return table.contains(res, key); + return utable.contains(res, key); end function NGCPDlgCounters:set(callid, key) diff --git a/ngcp/dlglist.lua b/ngcp/dlglist.lua index d4c388d..5423047 100644 --- a/ngcp/dlglist.lua +++ b/ngcp/dlglist.lua @@ -21,7 +21,8 @@ local NGCPDlgList = { __class__ = 'NGCPDlgList' } local redis = require 'redis'; -require 'ngcp.utils'; +local utils = require 'ngcp.utils'; +local utable = utils.table _ENV = NGCPDlgList @@ -30,8 +31,8 @@ local NGCPDlgList_MT = { __index = NGCPDlgList } NGCPDlgList_MT.__tostring = function (t) return string.format("config:%s central:%s pair:%s", - table.tostring(t.config), table.tostring(t.central), - table.tostring(t.pair)); + utable.tostring(t.config), utable.tostring(t.central), + utable.tostring(t.pair)); end function NGCPDlgList.new() diff --git a/ngcp/dp.lua b/ngcp/dp.lua index 85b3498..aea0b60 100644 --- a/ngcp/dp.lua +++ b/ngcp/dp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,14 +17,16 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.pref' +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPXAvp = require 'ngcp.xavp' +local NGCPPrefs = require 'ngcp.pref' -- class NGCPDomainPrefs -NGCPDomainPrefs = { +local NGCPDomainPrefs = { __class__ = 'NGCPDomainPrefs' } -NGCPDomainPrefs_MT = { __index = NGCPDomainPrefs } +local NGCPDomainPrefs_MT = { __index = NGCPDomainPrefs } NGCPDomainPrefs_MT.__tostring = function () local xavp = NGCPXAvp:new('caller','dom_prefs') @@ -84,9 +86,8 @@ NGCPDomainPrefs_MT.__tostring = function () if row then while row do - --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) - table.add(keys, row.attribute) + utable.add(keys, row.attribute) defaults[row.attribute] = nil row = cur:fetch({}, "a") end @@ -119,4 +120,4 @@ NGCPDomainPrefs_MT.__tostring = function () end end -- class ---EOF \ No newline at end of file +return NGCPDomainPrefs diff --git a/ngcp/ngcp.lua b/ngcp/ngcp.lua index 504dfbf..57b0909 100644 --- a/ngcp/ngcp.lua +++ b/ngcp/ngcp.lua @@ -17,19 +17,23 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.cp' -require 'ngcp.pprof' -require 'ngcp.pp' -require 'ngcp.dp' -require 'ngcp.up' -require 'ngcp.rp' -require 'ngcp.config' + +local NGCPXAvp = require 'ngcp.xavp' +local NGCPContractPrefs = require 'ngcp.cp' +local NGCPProfilePrefs = require 'ngcp.pprof' +local NGCPPeerPrefs = require 'ngcp.pp' +local NGCPDomainPrefs = require 'ngcp.dp' +local NGCPUserPrefs = require 'ngcp.up' +local NGCPRealPrefs = require 'ngcp.rp' +local NGCPConfig = require 'ngcp.config' +local utils = require 'ngcp.utils' +local utable = utils.table -- class NGCP -NGCP = { +local NGCP = { __class__ = 'NGCP' } -NGCP_MT = { __index = NGCP } +local NGCP_MT = { __index = NGCP } NGCP_MT.__tostring = function (t) local output = '' @@ -94,9 +98,9 @@ end prof = self.prefs.prof:caller_load(uuid), user = self.prefs.usr:caller_load(uuid) } - local unique_keys = table.deepcopy(keys.domain) - table.merge(unique_keys, keys.prof) - table.merge(unique_keys, keys.user) + local unique_keys = utable.deepcopy(keys.domain) + utable.merge(unique_keys, keys.prof) + utable.merge(unique_keys, keys.user) self.prefs.real:caller_usr_load(unique_keys) NGCPXAvp:new('caller', 'dom') @@ -110,9 +114,9 @@ end prof = self.prefs.prof:callee_load(uuid), user = self.prefs.usr:callee_load(uuid) } - local unique_keys = table.deepcopy(keys.domain) - table.merge(unique_keys, keys.prof) - table.merge(unique_keys, keys.user) + local unique_keys = utable.deepcopy(keys.domain) + utable.merge(unique_keys, keys.prof) + utable.merge(unique_keys, keys.user) self.prefs.real:callee_usr_load(unique_keys) @@ -163,4 +167,4 @@ end end end -- class ---EOF +return NGCP diff --git a/ngcp/pp.lua b/ngcp/pp.lua index 9f4b318..c925182 100644 --- a/ngcp/pp.lua +++ b/ngcp/pp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,14 +17,16 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.xavp' +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPXAvp = require 'ngcp.xavp' +local NGCPPrefs = require 'ngcp.pref' -- class NGCPPeerPrefs -NGCPPeerPrefs = { +local NGCPPeerPrefs = { __class__ = 'NGCPPeerPrefs' } -NGCPPeerPrefs_MT = { __index = NGCPPeerPrefs } +local NGCPPeerPrefs_MT = { __index = NGCPPeerPrefs } NGCPPeerPrefs_MT.__tostring = function () local xavp = NGCPXAvp:new('caller','peer_prefs') @@ -86,9 +88,8 @@ NGCPPeerPrefs_MT.__tostring = function () if row then while row do - --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) - table.add(keys, row.attribute) + utable.add(keys, row.attribute) defaults[row.attribute] = nil row = cur:fetch({}, "a") end @@ -121,4 +122,4 @@ NGCPPeerPrefs_MT.__tostring = function () end end -- class ---EOF \ No newline at end of file +return NGCPPeerPrefs diff --git a/ngcp/pprof.lua b/ngcp/pprof.lua index 1223330..15802d1 100644 --- a/ngcp/pprof.lua +++ b/ngcp/pprof.lua @@ -1,5 +1,5 @@ -- --- Copyright 2014 SipWise Team +-- Copyright 2014-2015 SipWise Team -- -- 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 @@ -17,15 +17,16 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.xavp' -require 'ngcp.pref' +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPXAvp = require 'ngcp.xavp' +local NGCPPrefs = require 'ngcp.pref' -- class NGCPProfilePrefs -NGCPProfilePrefs = { +local NGCPProfilePrefs = { __class__ = 'NGCPProfilePrefs' } -NGCPProfilePrefs_MT = { __index = NGCPProfilePrefs } +local NGCPProfilePrefs_MT = { __index = NGCPProfilePrefs } NGCPProfilePrefs_MT.__tostring = function () local xavp = NGCPXAvp:new('caller','prof_prefs') @@ -70,18 +71,18 @@ NGCPProfilePrefs_MT.__tostring = function () local result = {} local row = cur:fetch({}, "a") - if table.size(row) > 0 then - while table.size(row) > 0 do + if utable.size(row) > 0 then + while utable.size(row) > 0 do --sr.log("debug", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) - table.add(keys, row.attribute) + utable.add(keys, row.attribute) row = cur:fetch({}, "a") end else sr.log("dbg", string.format("no results for query:%s", query)) end cur:close() - if table.size(result) > 0 then + if utable.size(result) > 0 then self:xavp(level, result) end return keys @@ -103,4 +104,4 @@ NGCPProfilePrefs_MT.__tostring = function () end end -- class ---EOF \ No newline at end of file +return NGCPProfilePrefs diff --git a/ngcp/pref.lua b/ngcp/pref.lua index 614761a..3ae2c7c 100644 --- a/ngcp/pref.lua +++ b/ngcp/pref.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,14 +17,13 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.avp' -require 'ngcp.xavp' + +local NGCPXAvp = require 'ngcp.xavp' -- class NGCPPrefs -NGCPPrefs = { +local NGCPPrefs = { __class__ = 'NGCPPrefs' } -NGCPPrefs_MT = { __index = NGCPPrefs } function NGCPPrefs.init(group) local levels = {"caller", "callee"} @@ -33,4 +32,4 @@ NGCPPrefs_MT = { __index = NGCPPrefs } end end -- class ---EOF \ No newline at end of file +return NGCPPrefs diff --git a/ngcp/recentcalls.lua b/ngcp/recentcalls.lua index 649bdfe..80ab044 100644 --- a/ngcp/recentcalls.lua +++ b/ngcp/recentcalls.lua @@ -21,8 +21,8 @@ local NGCPRecentCalls = { __class__ = 'NGCPRecentCalls' } local redis = require 'redis'; -require 'ngcp.utils'; - +local utils = require 'ngcp.utils'; +local utable = utils.table _ENV = NGCPRecentCalls -- class NGCPRecentCalls @@ -30,7 +30,7 @@ local NGCPRecentCalls_MT = { __index = NGCPRecentCalls } NGCPRecentCalls_MT.__tostring = function (t) return string.format("config:%s central:%s", - table.tostring(t.config), table.tostring(t.central)) + utable.tostring(t.config), utable.tostring(t.central)) end function NGCPRecentCalls.new() diff --git a/ngcp/rp.lua b/ngcp/rp.lua index baf01cc..4bebc52 100644 --- a/ngcp/rp.lua +++ b/ngcp/rp.lua @@ -17,18 +17,21 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.pref' -require 'ngcp.dp' -require 'ngcp.pp' -require 'ngcp.up' -require 'ngcp.pprof' +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPPrefs = require 'ngcp.pref' +local NGCPDomainPrefs = require 'ngcp.dp' +local NGCPPeerPrefs = require 'ngcp.pp' +local NGCPUserPrefs = require 'ngcp.up' +local NGCPProfilePrefs = require 'ngcp.pprof' +local NGCPContractPrefs = require 'ngcp.cp' +local NGCPXAvp = require 'ngcp.xavp' -- class NGCPRealPrefs -NGCPRealPrefs = { +local NGCPRealPrefs = { __class__ = 'NGCPRealPrefs' } -NGCPRealPrefs_MT = { __index = NGCPRealPrefs } +local NGCPRealPrefs_MT = { __index = NGCPRealPrefs } NGCPRealPrefs_MT.__tostring = function () local xavp = NGCPXAvp:new('caller','real_prefs') @@ -78,7 +81,7 @@ NGCPRealPrefs_MT.__tostring = function () for _,v in pairs(keys) do local value = values[v] if value then - table.add(contract_keys, v) + utable.add(contract_keys, v) end end return contract_keys @@ -93,7 +96,7 @@ NGCPRealPrefs_MT.__tostring = function () for _,v in pairs(keys) do local value = values[v] if value then - table.add(peer_keys, v) + utable.add(peer_keys, v) end end return peer_keys @@ -148,4 +151,4 @@ NGCPRealPrefs_MT.__tostring = function () end end -- class ---EOF +return NGCPRealPrefs diff --git a/ngcp/up.lua b/ngcp/up.lua index c5061f5..e20f52a 100644 --- a/ngcp/up.lua +++ b/ngcp/up.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,14 +17,17 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' -require 'ngcp.pref' + +local utils = require 'ngcp.utils' +local utable = utils.table +local NGCPPrefs = require 'ngcp.pref' +local NGCPXAvp = require 'ngcp.xavp' -- class NGCPUserPrefs -NGCPUserPrefs = { +local NGCPUserPrefs = { __class__ = 'NGCPUserPrefs' } -NGCPUserPrefs_MT = { __index = NGCPUserPrefs } +local NGCPUserPrefs_MT = { __index = NGCPUserPrefs } NGCPUserPrefs_MT.__tostring = function () local xavp = NGCPXAvp:new('caller','usr_prefs') @@ -87,7 +90,7 @@ NGCPUserPrefs_MT.__tostring = function () while row do --sr.log("info", string.format("result:%s row:%s", table.tostring(result), table.tostring(row))) table.insert(result, row) - table.add(keys, row.attribute) + utable.add(keys, row.attribute) defaults[row.attribute] = nil row = cur:fetch({}, "a") end @@ -120,4 +123,4 @@ NGCPUserPrefs_MT.__tostring = function () end end -- class ---EOF \ No newline at end of file +return NGCPUserPrefs diff --git a/ngcp/utils.lua b/ngcp/utils.lua index c0282b6..3bfcd0c 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -19,28 +19,34 @@ -- -- Lua utils --- luacheck: globals math table string +local utils = {} +utils.table = {} +utils.string = {} +utils.math = {} +local ut = utils.table +local us = utils.string + -- improving the built-in pseudorandom generator -- http://lua-users.org/wiki/MathLibraryTutorial -do - local oldrandom = math.random - local randomtable - math.random = function () - if randomtable == nil then - randomtable = {} - for i = 1, 97 do - randomtable[i] = oldrandom() - end - end - local x = oldrandom() - local i = 1 + math.floor(97*x) - x, randomtable[i] = randomtable[i], x - return x - end -end +local oldrandom = math.random +local randomtable +function utils.math.random() + if randomtable == nil then + randomtable = {} + for i = 1, 97 do + randomtable[i] = oldrandom() + end + end + local x = oldrandom() + local i = 1 + math.floor(97*x) + x, randomtable[i] = randomtable[i], x + return x + end + -- luacheck: ignore math +math.random = utils.math.random -- copy a table -function table.deepcopy(object) +function ut.deepcopy(object) local lookup_table = {} local function _copy(obj) if type(obj) ~= "table" then @@ -58,7 +64,7 @@ function table.deepcopy(object) return _copy(object) end -function table.contains(t, element) +function ut.contains(t, element) if t then for _, value in pairs(t) do if value == element then @@ -70,13 +76,13 @@ function table.contains(t, element) end -- add if element is not in table -function table.add(t, element) - if not table.contains(t, element) then +function ut.add(t, element) + if not ut.contains(t, element) then table.insert(t, element) end end -function table.del(t, element) +function ut.del(t, element) local i local pos = {} @@ -96,16 +102,16 @@ function table.del(t, element) end end -function table.merge(t, other) +function ut.merge(t, other) if t and other then for _, value in ipairs(other) do - table.add(t, value) + ut.add(t, value) end end return t; end -function table.size(t) +function ut.size(t) if t then local c = 0 for _ in pairs(t) do @@ -116,7 +122,7 @@ function table.size(t) return 0 end -function table.val_to_str ( v ) +function ut.val_to_str ( v ) if "string" == type( v ) then v = string.gsub( v, "\n", "\\n" ) if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then @@ -124,36 +130,36 @@ function table.val_to_str ( v ) end return '"' .. string.gsub(v,'"', '\\"' ) .. '"' else - return "table" == type( v ) and table.tostring( v ) or + return "table" == type( v ) and ut.tostring( v ) or tostring( v ) end end -function table.key_to_str ( k ) +function ut.key_to_str ( k ) if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then return k else - return "[" .. table.val_to_str( k ) .. "]" + return "[" .. ut.val_to_str( k ) .. "]" end end -function table.tostring( tbl ) +function ut.tostring( tbl ) local result, done = {}, {} if not tbl then return "nil" end for k, v in ipairs( tbl ) do - table.insert( result, table.val_to_str( v ) ) + table.insert( result, ut.val_to_str( v ) ) done[ k ] = true end for k, v in pairs( tbl ) do if not done[ k ] then table.insert( result, - table.key_to_str( k ) .. "=" .. table.val_to_str( v ) ) + ut.key_to_str( k ) .. "=" .. ut.val_to_str( v ) ) end end return "{" .. table.concat( result, "," ) .. "}" end -function table.shuffle(tab) +function ut.shuffle(tab) local n, order, res = #tab, {}, {} math.randomseed( os.time() ); for i=1,n do order[i] = { rnd = math.random(), idx = i } end @@ -162,11 +168,10 @@ function table.shuffle(tab) return res end --- luacheck: ignore range -- range(start) returns an iterator from 1 to a (step = 1) -- range(start, stop) returns an iterator from a to b (step = 1) -- range(start, stop, step) returns an iterator from a to b, counting by step. -range = function (i, to, inc) +utils.range = function (i, to, inc) -- range(--[[ no args ]]) -> return "nothing" to fail the loop in the caller if i == nil then return end @@ -185,17 +190,16 @@ range = function (i, to, inc) return function () if i == to then return nil end i = i + inc return i, i end end -function table.shift(t, position) +function ut.shift(t, position) local res = {} local p = position % #t if p == 0 then return end - for k in range(1, p) do - local v = table.remove(t, k-#res) - table.insert(res, v) + for k in utils.range(1, p) do + table.insert(res, table.remove(t, k-#res)) end - for _,_v in ipairs(res) do - table.insert(t, _v) + for _,v in ipairs(res) do + table.insert(t, v) end end @@ -205,8 +209,7 @@ end -- "'a','b'" -- implode("#",t) -- "a#b" --- luacheck: ignore implode explode -function implode(delimiter, list, quoter) +function utils.implode(delimiter, list, quoter) local len = #list if not delimiter then error("delimiter is nil") @@ -225,7 +228,7 @@ function implode(delimiter, list, quoter) end -- from string to table -function explode(delimiter, text) +function utils.explode(delimiter, text) local list = {} local pos = 1 @@ -253,11 +256,11 @@ function explode(delimiter, text) return list end -function string.starts(String,Start) +function us.starts(String,Start) return string.sub(String,1,string.len(Start))==Start end -function string.ends(String,End) +function us.ends(String,End) return End=='' or string.sub(String,-string.len(End))==End end @@ -266,13 +269,12 @@ end -- Lua 5.1 compatible -- GLOBAL --- luacheck: ignore Stack.* -Stack = { +local Stack = { __class__ = 'Stack' } -Stack_MT = { +local Stack_MT = { __tostring = function(t) - return table.tostring(Stack.list(t)) + return ut.tostring(Stack.list(t)) end, -- this works only on Lua5.2 __len = function(t) @@ -291,84 +293,86 @@ Stack_MT = { end } - -- Create a Table with stack functions - function Stack:new() - local t = { _et = {} } - setmetatable(t, Stack_MT) - return t - end +-- Create a Table with stack functions +function Stack.new() + local t = { _et = {} } + setmetatable(t, Stack_MT) + return t +end - -- push a value on to the stack - function Stack:push(...) - if ... then - local targs = {...} - -- add values - for _,v in pairs(targs) do - table.insert(self._et, v) - end +-- push a value on to the stack +function Stack:push(...) + if ... then + local targs = {...} + -- add values + for _,v in pairs(targs) do + table.insert(self._et, v) end end +end - -- pop a value from the stack - function Stack:pop(n) - -- get num values from stack - local num = n or 1 - - -- return table - local entries = {} - - -- get values into entries - -- luacheck: ignore i - for i = 1, num do - -- get last entry - if #self._et ~= 0 then - table.insert(entries, self._et[#self._et]) - -- remove last value - table.remove(self._et) - else - break - end +-- pop a value from the stack +function Stack:pop(num) + -- get num values from stack + local n = num or 1 + + -- return table + local entries = {} + + -- get values into entries + for _ = 1, n do + -- get last entry + if #self._et ~= 0 then + table.insert(entries, self._et[#self._et]) + -- remove last value + table.remove(self._et) + else + break end - -- return unpacked entries - return unpack(entries) end + -- return unpacked entries + return unpack(entries) +end - -- get pos ( starts on 0) - function Stack:get(pos) - assert(pos) - assert(pos>=0) - local indx = #self._et - pos - if indx>0 then - return self._et[indx] - end +-- get pos ( starts on 0) +function Stack:get(pos) + assert(pos) + assert(pos>=0) + local indx = #self._et - pos + if indx>0 then + return self._et[indx] end +end - -- set a value in a pos (stars on 0) - function Stack:set(pos, value) - assert(pos) - assert(pos>=0) - local indx = #self._et - pos - if indx>0 then - if self._et[indx] then - self._et[indx] = value - else - error("No pos:"..pos) - end +-- set a value in a pos (stars on 0) +function Stack:set(pos, value) + assert(pos) + assert(pos>=0) + local indx = #self._et - pos + if indx>0 then + if self._et[indx] then + self._et[indx] = value + else + error("No pos:"..pos) end end +end - -- get entries - function Stack:size() - return #self._et - end +-- get entries +function Stack:size() + return #self._et +end - -- list values - function Stack:list() - local entries = {} - for i = #self._et, 1, -1 do - table.insert(entries, self._et[i]) - end - return entries +-- list values +function Stack:list() + local entries = {} + for i = #self._et, 1, -1 do + table.insert(entries, self._et[i]) end + return entries +end -- end class ---EOF \ No newline at end of file +--EOF + +utils.Stack = Stack +return utils diff --git a/ngcp/xavp.lua b/ngcp/xavp.lua index 96aafc5..2f60979 100644 --- a/ngcp/xavp.lua +++ b/ngcp/xavp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,13 +17,14 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'ngcp.utils' +local utils = require 'ngcp.utils' +local utable = utils.table -- class NGCPXAvp -NGCPXAvp = { +local NGCPXAvp = { __class__ = 'NGCPXAvp' } -NGCPXAvp_MT = { +local NGCPXAvp_MT = { __index = NGCPXAvp } function NGCPXAvp:new(level,group,l) @@ -37,15 +38,15 @@ NGCPXAvp_MT = { if not value then return sr.pv.get(id) elseif type(value) == "number" then - table.add(s.keys, key) + utable.add(s.keys, key) --sr.log("dbg", string.format("seti: [%s]:%d", id, value)) sr.pv.seti(id, value) elseif type(value) == "string" then - table.add(s.keys, key) + utable.add(s.keys, key) --sr.log("dbg", string.format("sets: [%s]:%s", id, value)) sr.pv.sets(id, value) elseif type(value) == "table" then - table.add(s.keys, key) + utable.add(s.keys, key) for i = #value, 1, -1 do local v = value[i] if type(v) == "number" then @@ -65,7 +66,7 @@ NGCPXAvp_MT = { local ll = sr.xavp.get(s.name, 0) if ll then - output = table.tostring(ll) + output = utable.tostring(ll) end sr.log("dbg", string.format("output:%s", output)) return output @@ -117,7 +118,7 @@ NGCPXAvp_MT = { check = sr.pv.get(id) if check then if type(check) == 'table' then - table.tostring(check) + utable.tostring(check) end else --error(string.format("%s:nil", id)) @@ -134,7 +135,7 @@ NGCPXAvp_MT = { end for i=1,#l do name = string.format("$xavp(%s[0]=>%s)", tostring(self.name), tostring(l[i].attribute)) - table.add(self.keys, l[i].attribute) + utable.add(self.keys, l[i].attribute) NGCPXAvp._setvalue(name, l[i].type, l[i].value) end end @@ -158,4 +159,4 @@ NGCPXAvp_MT = { end end -- class ---EOF \ No newline at end of file +return NGCPXAvp diff --git a/tests/.luacheckrc b/tests/.luacheckrc index 793f6e7..aca5d1b 100644 --- a/tests/.luacheckrc +++ b/tests/.luacheckrc @@ -1,3 +1,3 @@ globals = {'sr', 'srMock'} -- until we fix the luaunit new format at run_tests.sh -ignore = {'assert.*'} \ No newline at end of file +ignore = {'assert.*', '212'} diff --git a/tests/example_with_luaunit.lua.no b/tests/example_with_luaunit.lua.no deleted file mode 100644 index 95baa7b..0000000 --- a/tests/example_with_luaunit.lua.no +++ /dev/null @@ -1,104 +0,0 @@ - -require('luaunit') - -TestToto = {} --class - - function TestToto:setUp() - -- set up tests - self.a = 1 - self.s = 'hop' - end - - function TestToto:test1_withFailure() - print( "some stuff test 1" ) - assertEquals( self.a , 1 ) - -- will fail - assertEquals( self.a , 2 ) - assertEquals( self.a , 2 ) - end - - function TestToto:test2_withFailure() - print( "some stuff test 2" ) - assertEquals( self.a , 1 ) - assertEquals( self.s , 'hop' ) - -- will fail - assertEquals( self.s , 'bof' ) - assertEquals( self.s , 'bof' ) - end - - function TestToto:test3() - print( "some stuff test 3" ) - assertEquals( self.a , 1 ) - assertEquals( self.s , 'hop' ) - assertEquals( type(self.a), 'number' ) - end --- class TestToto - -TestTiti = {} --class - function TestTiti:setUp() - -- set up tests - self.a = 1 - self.s = 'hop' - print( 'TestTiti:setUp' ) - end - - function TestTiti:tearDown() - -- some tearDown() code if necessary - print( 'TestTiti:tearDown' ) - end - - function TestTiti:test1_withFailure() - print( "some stuff test 1" ) - assertEquals( self.a , 1 ) - -- will fail - assertEquals( self.a , 2 ) - assertEquals( self.a , 2 ) - end - - function TestTiti:test2_withFailure() - print( "some stuff test 2" ) - assertEquals( self.a , 1 ) - assertEquals( self.s , 'hop' ) - -- will fail - assertEquals( self.s , 'bof' ) - assertEquals( self.s , 'bof' ) - end - - function TestTiti:test3() - print( "some stuff test 3" ) - assertEquals( self.a , 1 ) - assertEquals( self.s , 'hop' ) - end --- class TestTiti - --- simple test functions that were written previously can be integrated --- in luaunit too -function test1_withFailure() - assert( 1 == 1) - -- will fail - assert( 1 == 2) -end - -function test2_withFailure() - assert( 'a' == 'a') - -- will fail - assert( 'a' == 'b') -end - -function test3() - assert( 1 == 1) - assert( 'a' == 'a') -end - --- LuaUnit:run( 'test2_withFailure' ) -- run only one test function --- LuaUnit:run( 'TestFunctions:test1_withFailure' ) --- LuaUnit:run( 'TestToto' ) -- run only on test class --- LuaUnit:run( 'TestTiti:test3') -- run only one test method of a test class --- LuaUnit:run() -- run everything - ----- Control test output: -lu = LuaUnit --- lu:setOutputType( "NIL" ) -lu:setOutputType( "TAP" ) -lu:setVerbosity( 0 ) -lu:run() diff --git a/tests/mocks.lua b/tests/mocks.lua deleted file mode 100644 index d30465b..0000000 --- a/tests/mocks.lua +++ /dev/null @@ -1,574 +0,0 @@ --- --- Copyright 2013 SipWise Team --- --- 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 --- the Free Software Foundation, either version 3 of the License, or --- (at your option) any later version. --- --- This package is distributed in the hope that it will be useful, --- but WITHOUT ANY WARRANTY; without even the implied warranty of --- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --- GNU General Public License for more details. --- --- You should have received a copy of the GNU General Public License --- along with this program. If not, see . --- . --- On Debian systems, the complete text of the GNU General --- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". --- -require('luaunit') -require 'mocks.sr' - -TestMock = {} - function TestMock:testMock() - mc = lemock.controller() - m = mc:mock() - m.pv = mc:mock() - m.titi( 42 ) - m.toto( 33, "abc", { 21} ) - end - -TestHDRMock = {} - function TestHDRMock:setUp() - self.hdr = hdrMock:new() - end - - function TestHDRMock:tearDown() - self.hdr.headers = {} - self.hdr.headers_reply = {} - end - - function TestHDRMock:test_is_header() - assertTrue(self.hdr._is_header("From: hi@there.com\r\n")) - assertFalse(self.hdr._is_header("From hi@there.com\r\n")) - assertFalse(self.hdr._is_header("From: hi@there.com\r")) - assertFalse(self.hdr._is_header("From : hi@there.com\n")) - assertFalse(self.hdr._is_header("From : hi@there.com\n\r")) - assertTrue(self.hdr._is_header("From: hi@there.com:8080\r\n")) - end - - function TestHDRMock:test_append() - assertFalse(self.hdr._get_header("From")) - self.hdr.append("From: hi@there.com\r\n") - assertEquals(self.hdr.headers, {"From: hi@there.com\r\n"}) - self.hdr.append("To: bye@there.com\r\n") - assertEquals(self.hdr.headers, {"From: hi@there.com\r\n", "To: bye@there.com\r\n"}) - end - - function TestHDRMock:test_insert() - assertFalse(self.hdr._get_header("From")) - self.hdr.insert("From: hi@there.com\r\n") - assertEquals(self.hdr.headers, {"From: hi@there.com\r\n"}) - self.hdr.insert("To: bye@there.com\r\n") - assertEquals(self.hdr.headers, {"To: bye@there.com\r\n", "From: hi@there.com\r\n"}) - end - - function TestHDRMock:test_get_header() - self:test_append() - assertEquals(self.hdr._get_header("From"), "hi@there.com") - end --- end class - -TestSRMock = {} - function TestSRMock:setUp() - self.sr = srMock:new() - end - - function TestSRMock:tearDown() - self.sr.pv.vars = {} - end - - function TestSRMock:test_ini() - assertTrue(self.sr.pv) - end - - function TestSRMock:test_clean_id() - assertEquals(self.sr.pv._clean_id('s:u25'), 'u25') - assertEquals(self.sr.pv._clean_id('i:u25'), 'u25') - assertEquals(self.sr.pv._clean_id('u25'), 'u25') - end - - function TestSRMock:test_is_pv_simple() - local result - result = self.sr.pv._is_pv("$si") - assertTrue(result) - assertEquals(result.type, 'pv') - assertEquals(result.id, 'si') - assertEquals(result.key, nil) - assertEquals(result.mode, 'ro') - assertFalse(result.clean) - end - - function TestSRMock:test_is_pv_rw() - local result - result = self.sr.pv._is_pv("$rU") - assertTrue(result) - assertEquals(result.type, 'pv') - assertEquals(result.id, 'rU') - assertEquals(result.key, nil) - assertEquals(result.mode, 'rw') - assertFalse(result.clean) - end - - function TestSRMock:test_is_hdr_simple() - local result - result = self.sr.pv._is_hdr("$hdr(id)") - assertTrue(result) - assertEquals(result.type, 'hdr') - assertEquals(result.id, 'id') - assertEquals(result.key, nil) - assertFalse(result.clean) - end - - function TestSRMock:test_is_hdr_complex() - local result - result = self.sr.pv._is_hdr("$hdr($si)") - assertTrue(result) - assertEquals(result.type, 'hdr') - assertEquals(result.id, '$si') - assertEquals(result.key, nil) - assertFalse(result.clean) - end - - function TestSRMock:test_is_xavp_simple() - local result - result = self.sr.pv._is_xavp("$xavp(id=>key)") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id') - assertEquals(result.key, 'key') - assertIsNil(result.indx) - assertIsNil(result.kindx) - assertFalse(result.clean) - end - - function TestSRMock:test_is_xavp_complex() - local result - result = self.sr.pv._is_xavp("$xavp(id1[8]=>key3g2)") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id1') - assertEquals(result.key, 'key3g2') - assertEquals(result.indx, 8) - assertIsNil(result.kindx) - assertFalse(result.clean) - result = self.sr.pv._is_xavp("$xavp(id2g1f[9]=>keygg33_f)") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id2g1f') - assertEquals(result.key, 'keygg33_f') - assertEquals(result.indx, 9) - assertIsNil(result.kindx) - assertFalse(result.clean) - end - - function TestSRMock:test_is_xavp_complex_indx() - local result - result = self.sr.pv._is_xavp("$xavp(id1[8]=>key3g2)") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id1') - assertEquals(result.key, 'key3g2') - assertEquals(result.indx, 8) - assertIsNil(result.kindx) - assertFalse(result.clean) - result = self.sr.pv._is_xavp("$xavp(id2g1f[9]=>keygg33_f[2])") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id2g1f') - assertEquals(result.key, 'keygg33_f') - assertEquals(result.indx, 9) - assertEquals(result.kindx, 2) - assertFalse(result.clean) - end - - function TestSRMock:test_is_xavp_complex_indx2() - result = self.sr.pv._is_xavp("$xavp(gogo[9]=>gogo[*])") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'gogo') - assertEquals(result.key, 'gogo') - assertEquals(result.indx, 9) - assertFalse(result.kindx) - assertTrue(result.clean) - end - - function TestSRMock:test_is_xavp_simple_nokey() - local result - result = self.sr.pv._is_xavp("$xavp(id1[8])") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id1') - assertFalse(result.key) - assertEquals(result.indx, 8) - assertFalse(result.clean) - end - - function TestSRMock:test_is_xavp_simple_nokey_noindx() - local result - result = self.sr.pv._is_xavp("$xavp(id1)") - assertTrue(result) - assertEquals(result.type, 'xavp') - assertEquals(result.id, 'id1') - assertFalse(result.key) - assertIsNil(result.indx) - assertIsNil(result.kindx) - assertFalse(result.clean) - end - - function TestSRMock:test_is_avp_simple() - local result - result = self.sr.pv._is_avp("$avp(id2_f)") - assertTrue(result) - assertEquals(result.type, 'avp') - --print(table.tostring(result)) - assertEquals(result.id, 'id2_f') - assertFalse(result.clean) - end - - function TestSRMock:test_is_avp_simple1() - local result - result = self.sr.pv._is_avp("$(avp(s:id))") - assertTrue(result) - assertEquals(result.type, 'avp') - --print(table.tostring(result)) - assertEquals(result.id, 'id') - assertFalse(result.clean) - end - - function TestSRMock:test_is_avp_simple2() - local result - result = self.sr.pv._is_avp("$(avp(id))") - assertTrue(result) - assertEquals(result.type, 'avp') - --print(table.tostring(result)) - assertEquals(result.id, 'id') - assertFalse(result.clean) - end - - function TestSRMock:test_is_avp_simple3() - local result - result = self.sr.pv._is_avp("$(avp(s:id)[*])") - assertTrue(result) - assertEquals(result.type, 'avp') - assertEquals(result.id, 'id') - --print(table.tostring(result)) - assertTrue(result.clean) - end - - function TestSRMock:test_is_var_simple() - local result - result = self.sr.pv._is_var("$var(id)") - assertTrue(result) - assertEquals(result.type, 'var') - assertEquals(result.id, 'id') - --print(table.tostring(result)) - assertFalse(result.clean) - end - - function TestSRMock:test_var_sets() - self.sr.pv.sets("$var(hithere)", "value") - assertEquals(self.sr.pv.get("$var(hithere)"), "value") - assertError(self.sr.pv.sets, "$var(hithere)", 1) - assertError(self.sr.pv.sets, "$var(s:hithere)", "1") - assertError(self.sr.pv.sets, "$(var(hithere)[*])", "1") - assertError(self.sr.pv.sets, "$(var(s:hithere))", "1") - self.sr.pv.sets("$(var(hithere))", "new_value") - assertEquals(self.sr.pv.get("$var(hithere)"), "new_value") - assertEquals(self.sr.pv.vars["var:hithere"], "new_value") - end - - function TestSRMock:test_var_seti() - self.sr.pv.seti("$var(hithere)", 0) - assertEquals(self.sr.pv.get("$var(hithere)"), 0) - assertError(self.sr.pv.seti, "$var(hithere)", "1") - assertError(self.sr.pv.sets, "$var(s:hithere)", 1) - assertError(self.sr.pv.sets, "$(var(hithere)[*])", 1) - assertError(self.sr.pv.sets, "$(var(s:hithere))", 1) - assertEquals(self.sr.pv.get("$var(hithere)"), 0) - self.sr.pv.seti("$var(hithere)", 1) - assertEquals(self.sr.pv.get("$var(hithere)"), 1) - assertEquals(self.sr.pv.vars["var:hithere"], 1) - end - - function TestSRMock:test_avp_sets() - self.sr.pv.sets("$avp(s:hithere)", "value") - assertEquals(self.sr.pv.get("$avp(hithere)"), "value") - assertError(self.sr.pv.sets, "$avp(hithere)", 1) - self.sr.pv.sets("$(avp(hithere)[*])", "1") - assertEquals(self.sr.pv.get("$avp(s:hithere)"), "1") - self.sr.pv.sets("$(avp(hithere))", "new_value") - assertEquals(self.sr.pv.vars["avp:hithere"]:list(), {"new_value","1"}) - assertEquals(self.sr.pv.get("$avp(hithere)"), "new_value") - assertEquals(self.sr.pv.get("$(avp(hithere))"), "new_value") - end - - function TestSRMock:test_avp_sets_all() - self.sr.pv.sets("$avp(s:hithere)", "value") - assertEquals(self.sr.pv.get("$avp(hithere)"), "value") - assertEquals(self.sr.pv.get("$(avp(hithere)[*])"), {"value"}) - self.sr.pv.sets("$avp(s:hithere)", "value1") - assertEquals(self.sr.pv.get("$(avp(hithere)[*])"), {"value1","value"}) - end - - function TestSRMock:test_avp_seti() - self.sr.pv.seti("$avp(s:hithere)", 0) - assertEquals(self.sr.pv.get("$avp(s:hithere)"), 0) - assertError(self.sr.pv.seti, "$avp(s:hithere)", "1") - self.sr.pv.seti("$(avp(hithere))", 2) - assertEquals(self.sr.pv.vars["avp:hithere"]:list(), {2,0}) - assertEquals(self.sr.pv.get("$avp(hithere)"), 2) - assertEquals(self.sr.pv.get("$(avp(hithere))"), 2) - end - - function TestSRMock:test_xavp_sets() - self.sr.pv.sets("$xavp(g=>hithere)", "value") - assertEquals(self.sr.pv.get("$xavp(g=>hithere)"), "value") - self.sr.pv.sets("$xavp(g=>bythere)", "value_bye") - assertEquals(self.sr.pv.get("$xavp(g=>bythere)"), "value_bye") - end - - function TestSRMock:test_xavp_sets_multi() - self.sr.pv.sets("$xavp(g=>hithere)", "value1") - assertEquals(self.sr.pv.get("$xavp(g=>hithere)"), "value1") - self.sr.pv.sets("$xavp(g[0]=>hithere)", "value0") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value0") - assertEquals(self.sr.pv.get("$xavp(g=>hithere[1])"), "value1") - end - - function TestSRMock:test_xavp_sets1() - self.sr.pv.sets("$xavp(g=>hithere)", "value") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value") - self.sr.pv.sets("$xavp(g=>hithere)", "value_bye") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value_bye") - assertEquals(self.sr.pv.get("$xavp(g[1]=>hithere)"), "value") - end - - function TestSRMock:test_xavp_sets1_multi() - self.sr.pv.sets("$xavp(g=>hithere)", "value1") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value1") - self.sr.pv.sets("$xavp(g[0]=>hithere)", "value0") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value0") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere[1])"), "value1") - self.sr.pv.sets("$xavp(g=>hithere)", "value_bye") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere)"), "value_bye") - assertEquals(self.sr.pv.get("$xavp(g[1]=>hithere)"), "value0") - end - - function TestSRMock:test_xavp_seti() - self.sr.pv.seti("$xavp(t=>hithere)", 0) - assertEquals(self.sr.pv.get("$xavp(t[0]=>hithere)"), 0) - assertEquals(self.sr.pv.get("$xavp(t=>hithere)"), 0) - assertError(self.sr.pv.seti, "$xavp(t=>hithere)", "1") - assertError(self.sr.pv.seti, "$xavp(t[6]=>hithere)", "1") - end - - function TestSRMock:test_xavp_get() - self.sr.pv.sets("$xavp(g=>hithere)", "value") - assertTrue(self.sr.pv.get, "$xavp(g)") - end - - function TestSRMock:test_xavp_get_multi() - self.sr.pv.sets("$xavp(g=>hithere)", "value1") - self.sr.pv.sets("$xavp(g[0]=>hithere)", "value2") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere[0])"), "value2") - assertEquals(self.sr.pv.get("$xavp(g[0]=>hithere[1])"), "value1") - end - - function TestSRMock:test_avp_get_simple() - self.sr.pv.sets("$avp(s:hithere)", "value") - assertEquals(self.sr.pv.get("$avp(s:hithere)"), "value") - end - - function TestSRMock:test_avp_get_simple2() - self.sr.pv.seti("$avp(s:hithere)", 1) - assertEquals(self.sr.pv.get("$avp(s:hithere)"), 1) - end - - function TestSRMock:test_avp_get() - local vals = {1,2,3} - for i=1,#vals do - self.sr.pv.seti("$avp(s:hithere)", vals[i]) - end - local l = self.sr.pv.get("$(avp(s:hithere)[*])") - assertTrue(type(l), 'table') - assertEquals(#l,#vals) - --print(table.tostring(l)) - v = 1 - for i=#vals,1,-1 do - assertEquals(l[i],vals[v]) - v = v + 1 - end - end - - function TestSRMock:test_avp_get_all() - self.sr.pv.sets("$avp(s:hithere)", "value") - assertEquals(self.sr.pv.get("$avp(hithere)"), "value") - assertEquals(self.sr.pv.get("$(avp(hithere)[*])"), {"value"}) - self.sr.pv.sets("$avp(s:hithere)", "value1") - assertEquals(self.sr.pv.get("$(avp(hithere)[*])"), {"value1","value"}) - self.sr.pv.sets("$(avp(s:hithere)[*])", "new_value") - assertEquals(self.sr.pv.get("$avp(s:hithere)"), "new_value") - assertEquals(self.sr.pv.get("$(avp(s:hithere)[*])"), {"new_value"}) - end - - function TestSRMock:test_hdr_get() - self.sr.hdr.insert("From: hola\r\n") - assertEquals(self.sr.hdr.headers, {"From: hola\r\n"}) - assertEquals(self.sr.pv.get("$hdr(From)"), "hola") - end - - function TestSRMock:test_pv_seti() - self.sr.pv.seti("$rU", 0) - assertEquals(self.sr.pv.get("$rU"), 0) - end - - function TestSRMock:test_pv_sets() - self.sr.pv.sets("$rU", "0") - assertEquals(self.sr.pv.get("$rU"), "0") - end - - function TestSRMock:test_unset_var() - self.sr.pv.sets("$var(hithere)", "value") - assertEquals(self.sr.pv.get("$var(hithere)"), "value") - self.sr.pv.unset("$var(hithere)") - assertEquals(self.sr.pv.get("$var(hithere)"), nil) - self.sr.pv.unset("$var(hithere)") - end - - function TestSRMock:test_unset_avp() - self.sr.pv.sets("$avp(s:hithere)", "value") - assertEquals(self.sr.pv.get("$avp(hithere)"), "value") - self.sr.pv.unset("$avp(s:hithere)") - assertEquals(self.sr.pv.get("$avp(hithere)"), nil) - self.sr.pv.unset("$avp(s:hithere)") - assertEquals(self.sr.pv.get("$avp(s:hithere)"), nil) - end - - function TestSRMock:test_unset_xavp() - self.sr.pv.sets("$xavp(g=>t)", "value") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), "value") - self.sr.pv.sets("$xavp(g=>t)", "value1") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), "value1") - assertEquals(self.sr.pv.get("$xavp(g[1]=>t)"), "value") - -- - self.sr.pv.unset("$xavp(g[0]=>t)") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), nil) - assertEquals(self.sr.pv.get("$xavp(g[1]=>t)"), "value") - -- - self.sr.pv.unset("$xavp(g[1])") - assertFalse(self.sr.pv.get("$xavp(g[1])")) - self.sr.pv.unset("$xavp(g)") - assertEquals(self.sr.pv.get("$xavp(g)"), nil) - end - - function TestSRMock:test_unset_xavp1() - self.sr.pv.sets("$xavp(g=>t)", "value") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), "value") - self.sr.pv.sets("$xavp(g=>t)", "value1") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), "value1") - assertEquals(self.sr.pv.get("$xavp(g[1]=>t)"), "value") - self.sr.pv.sets("$xavp(g[1]=>z)", "value_z") - assertEquals(self.sr.pv.get("$xavp(g[1]=>z)"), "value_z") - -- - self.sr.pv.unset("$xavp(g[0])") - assertEquals(self.sr.pv.get("$xavp(g[0]=>t)"), nil) - assertEquals(self.sr.pv.get("$xavp(g[1]=>t)"), "value") - assertEquals(self.sr.pv.get("$xavp(g[1]=>z)"), "value_z") - assertFalse(self.sr.pv.get("$xavp(g[0])")) - -- - self.sr.pv.unset("$xavp(g[1])") - assertFalse(self.sr.pv.get("$xavp(g[1])")) - self.sr.pv.unset("$xavp(g)") - assertEquals(self.sr.pv.get("$xavp(g)"), nil) - end - - function TestSRMock:test_is_null() - assertTrue(self.sr.pv.is_null("$avp(s:hithere)")) - self.sr.pv.unset("$avp(s:hithere)") - assertTrue(self.sr.pv.is_null("$avp(s:hithere)")) - self.sr.pv.sets("$avp(s:hithere)", "value") - assertFalse(self.sr.pv.is_null("$avp(s:hithere)")) - self.sr.pv.sets("$avp(s:hithere)", "value") - assertFalse(self.sr.pv.is_null("$avp(s:hithere)")) - end - - function TestSRMock:test_log() - assertTrue(self.sr.log) - self.sr.log("dbg", "Hi dude!") - assertError(self.sr.log, "debug", "Hi dude!") - end - - function TestSRMock:test_avp_set_clean() - self.sr.pv.seti("$(avp(s:hithere)[*])", 0) - assertEquals(self.sr.pv.get("$avp(s:hithere)"), 0) - assertEquals(self.sr.pv.get("$(avp(s:hithere)[*])"), {0}) - self.sr.pv.seti("$(avp(s:hithere)[*])", 1) - assertEquals(self.sr.pv.get("$avp(s:hithere)"), 1) - assertEquals(self.sr.pv.get("$(avp(s:hithere)[*])"), {1}) - end --- end class - -TestXAVPMock = {} - function TestXAVPMock:setUp() - self.pv = pvMock:new() - self.xavp = xavpMock:new(self.pv) - - self.pv.sets("$xavp(test=>uno)", "uno") - assertEquals(self.pv.get("$xavp(test[0]=>uno)"), "uno") - self.pv.seti("$xavp(test[0]=>dos)", 4) - self.pv.seti("$xavp(test[0]=>dos)", 2) - assertEquals(self.pv.get("$xavp(test[0]=>dos)"), 2) - self.pv.seti("$xavp(test=>uno)", 3) - self.pv.seti("$xavp(test[0]=>uno)", 1) - assertEquals(self.pv.get("$xavp(test[0]=>uno)"), 1) - self.pv.sets("$xavp(test[0]=>dos)", "dos") - assertEquals(self.pv.get("$xavp(test[0]=>dos)"), "dos") - self.pv.seti("$xavp(test[0]=>tres)", 3) - assertEquals(self.pv.get("$xavp(test[0]=>tres)"), 3) - -- - assertEquals(self.pv.get("$xavp(test[1]=>uno)"), "uno") - assertEquals(self.pv.get("$xavp(test[1]=>dos)"), 2) - end - - function TestXAVPMock:tearDown() - self.pv.vars = {} - end - - function TestXAVPMock:test_get_keys() - local l = self.xavp.get_keys("test", 0) - assertTrue(l) - assertItemsEquals(l, {"uno", "dos", "tres"}) - end - - function TestXAVPMock:test_get_keys_1() - local l = self.xavp.get_keys("test", 1) - assertTrue(l) - assertItemsEquals(l, {"uno", "dos"}) - end - - function TestXAVPMock:test_get_simple() - local l = self.xavp.get("test", 0, 1) - assertTrue(l) - assertItemsEquals(l, {uno=1, dos="dos", tres=3}) - end - - function TestXAVPMock:test_get_simple_1() - local l = self.xavp.get("test", 1, 1) - assertTrue(l) - assertItemsEquals(l, {uno="uno", dos=2}) - end - - function TestXAVPMock:test_get() - local l = self.xavp.get("test", 0, 0) - assertTrue(l) - assertItemsEquals(l, {uno={1,3}, dos={"dos"}, tres={3}}) - end - - function TestXAVPMock:test_get_1() - local l = self.xavp.get("test", 1, 0) - assertTrue(l) - assertItemsEquals(l, {uno={"uno"}, dos={2,4}}) - end ---EOF \ No newline at end of file diff --git a/tests/mocks_hdr.lua b/tests/mocks_hdr.lua new file mode 100644 index 0000000..5a19269 --- /dev/null +++ b/tests/mocks_hdr.lua @@ -0,0 +1,68 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- + +require('luaunit') +local hdrMock = require 'mocks.hdr' + +-- luacheck: ignore TestHDRMock +TestHDRMock = {} + function TestHDRMock:setUp() + self.hdr = hdrMock.new() + end + + function TestHDRMock:tearDown() + self.hdr.headers = {} + self.hdr.headers_reply = {} + end + + function TestHDRMock:test_is_header() + assertTrue(self.hdr._is_header("From: hi@there.com\r\n")) + assertFalse(self.hdr._is_header("From hi@there.com\r\n")) + assertFalse(self.hdr._is_header("From: hi@there.com\r")) + assertFalse(self.hdr._is_header("From : hi@there.com\n")) + assertFalse(self.hdr._is_header("From : hi@there.com\n\r")) + assertTrue(self.hdr._is_header("From: hi@there.com:8080\r\n")) + end + + function TestHDRMock:test_append() + assertFalse(self.hdr._get_header("From")) + self.hdr.append("From: hi@there.com\r\n") + assertEquals(self.hdr.headers, {"From: hi@there.com\r\n"}) + self.hdr.append("To: bye@there.com\r\n") + assertEquals(self.hdr.headers, {"From: hi@there.com\r\n", "To: bye@there.com\r\n"}) + end + + function TestHDRMock:test_insert() + assertFalse(self.hdr._get_header("From")) + self.hdr.insert("From: hi@there.com\r\n") + assertEquals(self.hdr.headers, {"From: hi@there.com\r\n"}) + self.hdr.insert("To: bye@there.com\r\n") + assertEquals(self.hdr.headers, {"To: bye@there.com\r\n", "From: hi@there.com\r\n"}) + end + + function TestHDRMock:test_get_header() + self:test_append() + assertEquals(self.hdr._get_header("From"), "hi@there.com") + end + + function TestHDRMock:test_hdr_get() + self.hdr.insert("From: hola\r\n") + assertEquals(self.hdr.headers, {"From: hola\r\n"}) + end diff --git a/tests/mocks_pv.lua b/tests/mocks_pv.lua new file mode 100644 index 0000000..366a73b --- /dev/null +++ b/tests/mocks_pv.lua @@ -0,0 +1,443 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- + +require('luaunit') +local hdrMock = require 'mocks.hdr' +local pvMock = require 'mocks.pv' + +-- luacheck: ignore TestPVMock +TestPVMock = {} + function TestPVMock:setUp() + local hdr = hdrMock.new() + self.pv = pvMock.new(hdr) + end + + function TestPVMock:tearDown() + self.pv.vars = {} + end + + function TestPVMock:test_ini() + assertTrue(self.pv) + end + + function TestPVMock:test_clean_id() + assertEquals(self.pv._clean_id('s:u25'), 'u25') + assertEquals(self.pv._clean_id('i:u25'), 'u25') + assertEquals(self.pv._clean_id('u25'), 'u25') + end + + function TestPVMock:test_is_pv_simple() + local result = self.pv._is_pv("$si") + assertTrue(result) + assertEquals(result.type, 'pv') + assertEquals(result.id, 'si') + assertEquals(result.key, nil) + assertEquals(result.mode, 'ro') + assertFalse(result.clean) + end + + function TestPVMock:test_is_pv_rw() + local result = self.pv._is_pv("$rU") + assertTrue(result) + assertEquals(result.type, 'pv') + assertEquals(result.id, 'rU') + assertEquals(result.key, nil) + assertEquals(result.mode, 'rw') + assertFalse(result.clean) + end + + function TestPVMock:test_is_hdr_simple() + local result = self.pv._is_hdr("$hdr(id)") + assertTrue(result) + assertEquals(result.type, 'hdr') + assertEquals(result.id, 'id') + assertEquals(result.key, nil) + assertFalse(result.clean) + end + + function TestPVMock:test_is_hdr_complex() + local result = self.pv._is_hdr("$hdr($si)") + assertTrue(result) + assertEquals(result.type, 'hdr') + assertEquals(result.id, '$si') + assertEquals(result.key, nil) + assertFalse(result.clean) + end + + function TestPVMock:test_is_xavp_simple() + local result = self.pv._is_xavp("$xavp(id=>key)") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id') + assertEquals(result.key, 'key') + assertIsNil(result.indx) + assertIsNil(result.kindx) + assertFalse(result.clean) + end + + function TestPVMock:test_is_xavp_complex() + local result = self.pv._is_xavp("$xavp(id1[8]=>key3g2)") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id1') + assertEquals(result.key, 'key3g2') + assertEquals(result.indx, 8) + assertIsNil(result.kindx) + assertFalse(result.clean) + result = self.pv._is_xavp("$xavp(id2g1f[9]=>keygg33_f)") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id2g1f') + assertEquals(result.key, 'keygg33_f') + assertEquals(result.indx, 9) + assertIsNil(result.kindx) + assertFalse(result.clean) + end + + function TestPVMock:test_is_xavp_complex_indx() + local result = self.pv._is_xavp("$xavp(id1[8]=>key3g2)") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id1') + assertEquals(result.key, 'key3g2') + assertEquals(result.indx, 8) + assertIsNil(result.kindx) + assertFalse(result.clean) + result = self.pv._is_xavp("$xavp(id2g1f[9]=>keygg33_f[2])") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id2g1f') + assertEquals(result.key, 'keygg33_f') + assertEquals(result.indx, 9) + assertEquals(result.kindx, 2) + assertFalse(result.clean) + end + + function TestPVMock:test_is_xavp_complex_indx2() + local result = self.pv._is_xavp("$xavp(gogo[9]=>gogo[*])") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'gogo') + assertEquals(result.key, 'gogo') + assertEquals(result.indx, 9) + assertFalse(result.kindx) + assertTrue(result.clean) + end + + function TestPVMock:test_is_xavp_simple_nokey() + local result = self.pv._is_xavp("$xavp(id1[8])") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id1') + assertFalse(result.key) + assertEquals(result.indx, 8) + assertFalse(result.clean) + end + + function TestPVMock:test_is_xavp_simple_nokey_noindx() + local result = self.pv._is_xavp("$xavp(id1)") + assertTrue(result) + assertEquals(result.type, 'xavp') + assertEquals(result.id, 'id1') + assertFalse(result.key) + assertIsNil(result.indx) + assertIsNil(result.kindx) + assertFalse(result.clean) + end + + function TestPVMock:test_is_avp_simple() + local result = self.pv._is_avp("$avp(id2_f)") + assertTrue(result) + assertEquals(result.type, 'avp') + --print(table.tostring(result)) + assertEquals(result.id, 'id2_f') + assertFalse(result.clean) + end + + function TestPVMock:test_is_avp_simple1() + local result = self.pv._is_avp("$(avp(s:id))") + assertTrue(result) + assertEquals(result.type, 'avp') + --print(table.tostring(result)) + assertEquals(result.id, 'id') + assertFalse(result.clean) + end + + function TestPVMock:test_is_avp_simple2() + local result = self.pv._is_avp("$(avp(id))") + assertTrue(result) + assertEquals(result.type, 'avp') + --print(table.tostring(result)) + assertEquals(result.id, 'id') + assertFalse(result.clean) + end + + function TestPVMock:test_is_avp_simple3() + local result = self.pv._is_avp("$(avp(s:id)[*])") + assertTrue(result) + assertEquals(result.type, 'avp') + assertEquals(result.id, 'id') + --print(table.tostring(result)) + assertTrue(result.clean) + end + + function TestPVMock:test_is_var_simple() + local result = self.pv._is_var("$var(id)") + assertTrue(result) + assertEquals(result.type, 'var') + assertEquals(result.id, 'id') + --print(table.tostring(result)) + assertFalse(result.clean) + end + + function TestPVMock:test_var_sets() + self.pv.sets("$var(hithere)", "value") + assertEquals(self.pv.get("$var(hithere)"), "value") + assertError(self.pv.sets, "$var(hithere)", 1) + assertError(self.pv.sets, "$var(s:hithere)", "1") + assertError(self.pv.sets, "$(var(hithere)[*])", "1") + assertError(self.pv.sets, "$(var(s:hithere))", "1") + self.pv.sets("$(var(hithere))", "new_value") + assertEquals(self.pv.get("$var(hithere)"), "new_value") + assertEquals(self.pv.vars["var:hithere"], "new_value") + end + + function TestPVMock:test_var_seti() + self.pv.seti("$var(hithere)", 0) + assertEquals(self.pv.get("$var(hithere)"), 0) + assertError(self.pv.seti, "$var(hithere)", "1") + assertError(self.pv.sets, "$var(s:hithere)", 1) + assertError(self.pv.sets, "$(var(hithere)[*])", 1) + assertError(self.pv.sets, "$(var(s:hithere))", 1) + assertEquals(self.pv.get("$var(hithere)"), 0) + self.pv.seti("$var(hithere)", 1) + assertEquals(self.pv.get("$var(hithere)"), 1) + assertEquals(self.pv.vars["var:hithere"], 1) + end + + function TestPVMock:test_avp_sets() + self.pv.sets("$avp(s:hithere)", "value") + assertEquals(self.pv.get("$avp(hithere)"), "value") + assertError(self.pv.sets, "$avp(hithere)", 1) + self.pv.sets("$(avp(hithere)[*])", "1") + assertEquals(self.pv.get("$avp(s:hithere)"), "1") + self.pv.sets("$(avp(hithere))", "new_value") + assertEquals(self.pv.vars["avp:hithere"]:list(), {"new_value","1"}) + assertEquals(self.pv.get("$avp(hithere)"), "new_value") + assertEquals(self.pv.get("$(avp(hithere))"), "new_value") + end + + function TestPVMock:test_avp_sets_all() + self.pv.sets("$avp(s:hithere)", "value") + assertEquals(self.pv.get("$avp(hithere)"), "value") + assertEquals(self.pv.get("$(avp(hithere)[*])"), {"value"}) + self.pv.sets("$avp(s:hithere)", "value1") + assertEquals(self.pv.get("$(avp(hithere)[*])"), {"value1","value"}) + end + + function TestPVMock:test_avp_seti() + self.pv.seti("$avp(s:hithere)", 0) + assertEquals(self.pv.get("$avp(s:hithere)"), 0) + assertError(self.pv.seti, "$avp(s:hithere)", "1") + self.pv.seti("$(avp(hithere))", 2) + assertEquals(self.pv.vars["avp:hithere"]:list(), {2,0}) + assertEquals(self.pv.get("$avp(hithere)"), 2) + assertEquals(self.pv.get("$(avp(hithere))"), 2) + end + + function TestPVMock:test_xavp_sets() + self.pv.sets("$xavp(g=>hithere)", "value") + assertEquals(self.pv.get("$xavp(g=>hithere)"), "value") + self.pv.sets("$xavp(g=>bythere)", "value_bye") + assertEquals(self.pv.get("$xavp(g=>bythere)"), "value_bye") + end + + function TestPVMock:test_xavp_sets_multi() + self.pv.sets("$xavp(g=>hithere)", "value1") + assertEquals(self.pv.get("$xavp(g=>hithere)"), "value1") + self.pv.sets("$xavp(g[0]=>hithere)", "value0") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value0") + assertEquals(self.pv.get("$xavp(g=>hithere[1])"), "value1") + end + + function TestPVMock:test_xavp_sets1() + self.pv.sets("$xavp(g=>hithere)", "value") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value") + self.pv.sets("$xavp(g=>hithere)", "value_bye") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value_bye") + assertEquals(self.pv.get("$xavp(g[1]=>hithere)"), "value") + end + + function TestPVMock:test_xavp_sets1_multi() + self.pv.sets("$xavp(g=>hithere)", "value1") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value1") + self.pv.sets("$xavp(g[0]=>hithere)", "value0") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value0") + assertEquals(self.pv.get("$xavp(g[0]=>hithere[1])"), "value1") + self.pv.sets("$xavp(g=>hithere)", "value_bye") + assertEquals(self.pv.get("$xavp(g[0]=>hithere)"), "value_bye") + assertEquals(self.pv.get("$xavp(g[1]=>hithere)"), "value0") + end + + function TestPVMock:test_xavp_seti() + self.pv.seti("$xavp(t=>hithere)", 0) + assertEquals(self.pv.get("$xavp(t[0]=>hithere)"), 0) + assertEquals(self.pv.get("$xavp(t=>hithere)"), 0) + assertError(self.pv.seti, "$xavp(t=>hithere)", "1") + assertError(self.pv.seti, "$xavp(t[6]=>hithere)", "1") + end + + function TestPVMock:test_xavp_get() + self.pv.sets("$xavp(g=>hithere)", "value") + assertTrue(self.pv.get, "$xavp(g)") + end + + function TestPVMock:test_xavp_get_multi() + self.pv.sets("$xavp(g=>hithere)", "value1") + self.pv.sets("$xavp(g[0]=>hithere)", "value2") + assertEquals(self.pv.get("$xavp(g[0]=>hithere[0])"), "value2") + assertEquals(self.pv.get("$xavp(g[0]=>hithere[1])"), "value1") + end + + function TestPVMock:test_avp_get_simple() + self.pv.sets("$avp(s:hithere)", "value") + assertEquals(self.pv.get("$avp(s:hithere)"), "value") + end + + function TestPVMock:test_avp_get_simple2() + self.pv.seti("$avp(s:hithere)", 1) + assertEquals(self.pv.get("$avp(s:hithere)"), 1) + end + + function TestPVMock:test_avp_get() + local vals = {1,2,3} + for i=1,#vals do + self.pv.seti("$avp(s:hithere)", vals[i]) + end + local l = self.pv.get("$(avp(s:hithere)[*])") + assertTrue(type(l), 'table') + assertEquals(#l,#vals) + --print(table.tostring(l)) + local v = 1 + for i=#vals,1,-1 do + assertEquals(l[i],vals[v]) + v = v + 1 + end + end + + function TestPVMock:test_avp_get_all() + self.pv.sets("$avp(s:hithere)", "value") + assertEquals(self.pv.get("$avp(hithere)"), "value") + assertEquals(self.pv.get("$(avp(hithere)[*])"), {"value"}) + self.pv.sets("$avp(s:hithere)", "value1") + assertEquals(self.pv.get("$(avp(hithere)[*])"), {"value1","value"}) + self.pv.sets("$(avp(s:hithere)[*])", "new_value") + assertEquals(self.pv.get("$avp(s:hithere)"), "new_value") + assertEquals(self.pv.get("$(avp(s:hithere)[*])"), {"new_value"}) + end + + function TestPVMock:test_hdr_get() + self.pv.hdr.insert("From: hola\r\n") + assertEquals(self.pv.get("$hdr(From)"), "hola") + end + + function TestPVMock:test_pv_seti() + self.pv.seti("$rU", 0) + assertEquals(self.pv.get("$rU"), 0) + end + + function TestPVMock:test_pv_sets() + self.pv.sets("$rU", "0") + assertEquals(self.pv.get("$rU"), "0") + end + + function TestPVMock:test_unset_var() + self.pv.sets("$var(hithere)", "value") + assertEquals(self.pv.get("$var(hithere)"), "value") + self.pv.unset("$var(hithere)") + assertEquals(self.pv.get("$var(hithere)"), nil) + self.pv.unset("$var(hithere)") + end + + function TestPVMock:test_unset_avp() + self.pv.sets("$avp(s:hithere)", "value") + assertEquals(self.pv.get("$avp(hithere)"), "value") + self.pv.unset("$avp(s:hithere)") + assertEquals(self.pv.get("$avp(hithere)"), nil) + self.pv.unset("$avp(s:hithere)") + assertEquals(self.pv.get("$avp(s:hithere)"), nil) + end + + function TestPVMock:test_unset_xavp() + self.pv.sets("$xavp(g=>t)", "value") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), "value") + self.pv.sets("$xavp(g=>t)", "value1") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), "value1") + assertEquals(self.pv.get("$xavp(g[1]=>t)"), "value") + -- + self.pv.unset("$xavp(g[0]=>t)") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), nil) + assertEquals(self.pv.get("$xavp(g[1]=>t)"), "value") + -- + self.pv.unset("$xavp(g[1])") + assertFalse(self.pv.get("$xavp(g[1])")) + self.pv.unset("$xavp(g)") + assertEquals(self.pv.get("$xavp(g)"), nil) + end + + function TestPVMock:test_unset_xavp1() + self.pv.sets("$xavp(g=>t)", "value") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), "value") + self.pv.sets("$xavp(g=>t)", "value1") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), "value1") + assertEquals(self.pv.get("$xavp(g[1]=>t)"), "value") + self.pv.sets("$xavp(g[1]=>z)", "value_z") + assertEquals(self.pv.get("$xavp(g[1]=>z)"), "value_z") + -- + self.pv.unset("$xavp(g[0])") + assertEquals(self.pv.get("$xavp(g[0]=>t)"), nil) + assertEquals(self.pv.get("$xavp(g[1]=>t)"), "value") + assertEquals(self.pv.get("$xavp(g[1]=>z)"), "value_z") + assertFalse(self.pv.get("$xavp(g[0])")) + -- + self.pv.unset("$xavp(g[1])") + assertFalse(self.pv.get("$xavp(g[1])")) + self.pv.unset("$xavp(g)") + assertEquals(self.pv.get("$xavp(g)"), nil) + end + + function TestPVMock:test_is_null() + assertTrue(self.pv.is_null("$avp(s:hithere)")) + self.pv.unset("$avp(s:hithere)") + assertTrue(self.pv.is_null("$avp(s:hithere)")) + self.pv.sets("$avp(s:hithere)", "value") + assertFalse(self.pv.is_null("$avp(s:hithere)")) + self.pv.sets("$avp(s:hithere)", "value") + assertFalse(self.pv.is_null("$avp(s:hithere)")) + end + + function TestPVMock:test_avp_set_clean() + self.pv.seti("$(avp(s:hithere)[*])", 0) + assertEquals(self.pv.get("$avp(s:hithere)"), 0) + assertEquals(self.pv.get("$(avp(s:hithere)[*])"), {0}) + self.pv.seti("$(avp(s:hithere)[*])", 1) + assertEquals(self.pv.get("$avp(s:hithere)"), 1) + assertEquals(self.pv.get("$(avp(s:hithere)[*])"), {1}) + end diff --git a/tests/test_all.lua b/tests/mocks_sr.lua similarity index 56% rename from tests/test_all.lua rename to tests/mocks_sr.lua index 36f9d23..0bec81a 100644 --- a/tests/test_all.lua +++ b/tests/mocks_sr.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,19 +17,24 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'tests.utils' -require 'tests.mocks' -require 'tests.ngcp_avp' -require 'tests.ngcp_xavp' -require 'tests.ngcp_pref' -require 'tests.ngcp_pprof' -require 'tests.ngcp_dp' -require 'tests.ngcp_up' -require 'tests.ngcp_pp' -require 'tests.ngcp_rp' -require 'tests.ngcp' ----- Control test output: -lu = LuaUnit -lu:setOutputType( "TAP" ) -lu:setVerbosity( 1 ) -lu:run() \ No newline at end of file + +require('luaunit') +local srMock = require 'mocks.sr' + +-- luacheck: ignore TestSRMock +TestSRMock = {} + function TestSRMock:setUp() + self.sr = srMock.new() + end + + function TestSRMock:test_hdr_get() + self.sr.hdr.insert("From: hola\r\n") + assertEquals(self.sr.hdr.headers, {"From: hola\r\n"}) + assertEquals(self.sr.pv.get("$hdr(From)"), "hola") + end + + function TestSRMock:test_log() + assertTrue(self.sr.log) + self.sr.log("dbg", "Hi dude!") + assertError(self.sr.log, "debug", "Hi dude!") + end diff --git a/tests/mocks_xavp.lua b/tests/mocks_xavp.lua new file mode 100644 index 0000000..499c233 --- /dev/null +++ b/tests/mocks_xavp.lua @@ -0,0 +1,87 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- + +require('luaunit') +local pvMock = require 'mocks.pv' +local xavpMock = require 'mocks.xavp' + +-- luacheck: ignore TestXAVPMock +TestXAVPMock = {} + function TestXAVPMock:setUp() + self.pv = pvMock.new() + self.xavp = xavpMock.new(self.pv) + + self.pv.sets("$xavp(test=>uno)", "uno") + assertEquals(self.pv.get("$xavp(test[0]=>uno)"), "uno") + self.pv.seti("$xavp(test[0]=>dos)", 4) + self.pv.seti("$xavp(test[0]=>dos)", 2) + assertEquals(self.pv.get("$xavp(test[0]=>dos)"), 2) + self.pv.seti("$xavp(test=>uno)", 3) + self.pv.seti("$xavp(test[0]=>uno)", 1) + assertEquals(self.pv.get("$xavp(test[0]=>uno)"), 1) + self.pv.sets("$xavp(test[0]=>dos)", "dos") + assertEquals(self.pv.get("$xavp(test[0]=>dos)"), "dos") + self.pv.seti("$xavp(test[0]=>tres)", 3) + assertEquals(self.pv.get("$xavp(test[0]=>tres)"), 3) + -- + assertEquals(self.pv.get("$xavp(test[1]=>uno)"), "uno") + assertEquals(self.pv.get("$xavp(test[1]=>dos)"), 2) + end + + function TestXAVPMock:tearDown() + self.pv.vars = {} + end + + function TestXAVPMock:test_get_keys() + local l = self.xavp.get_keys("test", 0) + assertTrue(l) + assertItemsEquals(l, {"uno", "dos", "tres"}) + end + + function TestXAVPMock:test_get_keys_1() + local l = self.xavp.get_keys("test", 1) + assertTrue(l) + assertItemsEquals(l, {"uno", "dos"}) + end + + function TestXAVPMock:test_get_simple() + local l = self.xavp.get("test", 0, 1) + assertTrue(l) + assertItemsEquals(l, {uno=1, dos="dos", tres=3}) + end + + function TestXAVPMock:test_get_simple_1() + local l = self.xavp.get("test", 1, 1) + assertTrue(l) + assertItemsEquals(l, {uno="uno", dos=2}) + end + + function TestXAVPMock:test_get() + local l = self.xavp.get("test", 0, 0) + assertTrue(l) + assertItemsEquals(l, {uno={1,3}, dos={"dos"}, tres={3}}) + end + + function TestXAVPMock:test_get_1() + local l = self.xavp.get("test", 1, 0) + assertTrue(l) + assertItemsEquals(l, {uno={"uno"}, dos={2,4}}) + end +--EOF diff --git a/tests/ngcp.lua b/tests/ngcp.lua index d3760e0..a3e3b92 100644 --- a/tests/ngcp.lua +++ b/tests/ngcp.lua @@ -18,19 +18,17 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require('lemock') -require 'ngcp.utils' -require 'tests_v.dp_vars' -require 'tests_v.pp_vars' -require 'tests_v.pprof_vars' -require 'tests_v.up_vars' - -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local lemock = require('lemock') +local NGCPXAvp = require 'ngcp.xavp' +local DPFetch = require 'tests_v.dp_vars' +local PPFetch = require 'tests_v.pp_vars' +local PProfFetch = require 'tests_v.pprof_vars' +local UPFetch = require 'tests_v.up_vars' +local utils = require 'ngcp.utils' +local utable = utils.table + +local srMock = require 'mocks.sr' +sr = srMock:new() local mc,env local dp_vars = DPFetch:new() @@ -40,14 +38,16 @@ local pprof_vars = PProfFetch:new() package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end end -require 'ngcp.ngcp' - +local NGCP = require 'ngcp.ngcp' +local NGCPConfig = require 'ngcp.config' +local NGCPDomainPrefs = require 'ngcp.dp' +-- luacheck: ignore TestNGCP TestNGCP = {} --class function TestNGCP:setUp() @@ -92,7 +92,7 @@ TestNGCP = {} --class function TestNGCP:test_config_get_defaults_real() local defaults = NGCPConfig.get_defaults(self.ngcp.config, 'usr') - local usr_defaults = table.deepcopy(self.ngcp.config.default.usr) + local usr_defaults = utable.deepcopy(self.ngcp.config.default.usr) assertItemsEquals(defaults, usr_defaults) end @@ -165,6 +165,18 @@ TestNGCP = {} --class local keys = self.ngcp:caller_usr_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c") mc:verify() + local lkeys = { + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac", + "no_nat_sipping" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>dummy)"), "caller") assertEquals(sr.pv.get("$xavp(caller_real_prefs=>account_id)"), 2) assertEquals(sr.pv.get("$xavp(caller_real_prefs=>cli)"), "4311001") @@ -186,6 +198,18 @@ TestNGCP = {} --class local keys = self.ngcp:caller_usr_load(nil, "192.168.51.56") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn" + } + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_dom_prefs=>dummy)"), "caller") assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>dummy)"), "caller") --- the default is on real and dom NOT in usr @@ -234,6 +258,28 @@ TestNGCP = {} --class local keys = self.ngcp:caller_usr_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c", "192.168.51.56") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn", + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac", + "no_nat_sipping", + "force_outbound_calls_to_peer" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_usr_prefs[0]=>dummy)"), "caller") --- the default is on real NOT in usr assertIsNil(sr.pv.get("$xavp(caller_usr_prefs[0]=>sst_enable)")) @@ -284,6 +330,27 @@ TestNGCP = {} --class local keys = self.ngcp:callee_usr_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c", "192.168.51.56") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn", + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac", + "no_nat_sipping" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_dom_prefs=>sst_enable)"), "no") --- the default is on real NOT in usr @@ -329,6 +396,27 @@ TestNGCP = {} --class local keys = self.ngcp:callee_usr_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c", "192.168.51.56") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn", + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac", + "no_nat_sipping" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_dom_prefs=>sst_enable)"), "no") assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_enable)"), "yes") @@ -371,6 +459,23 @@ TestNGCP = {} --class local keys = self.ngcp:callee_usr_load("ah736f72-21d1-4ea6-a3ea-4d7f56b3887c", "192.168.51.56") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn", + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_dom_prefs=>sst_enable)"), "no") assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_enable)"), "yes") @@ -396,6 +501,20 @@ TestNGCP = {} --class local keys = self.ngcp:caller_peer_load("2") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "inbound_npn", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>dummy)"), "caller") assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>sst_enable)"), "no") assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -418,6 +537,20 @@ TestNGCP = {} --class local keys = self.ngcp:callee_peer_load("2") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "inbound_npn", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>sst_enable)"), "no") assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -439,10 +572,9 @@ TestNGCP = {} --class function TestNGCP:test_clean_caller_groups() local groups = {"peer", "usr", "dom", "real", "prof"} - local _,v for _,v in pairs(groups) do - xavp = self.ngcp.prefs[v]:xavp("caller") + local xavp = self.ngcp.prefs[v]:xavp("caller") xavp(string.format("test_%s", v), v) assertEquals(sr.pv.get(string.format("$xavp(caller_%s_prefs=>test_%s)", v, v)), v) assertEquals(sr.pv.get(string.format("$xavp(caller_%s_prefs=>dummy)", v)), "caller") @@ -455,10 +587,9 @@ TestNGCP = {} --class function TestNGCP:test_clean_callee_groups() local groups = {"peer", "usr", "dom", "real", "prof"} - local _,v, xavp for _,v in pairs(groups) do - xavp = self.ngcp.prefs[v]:xavp("callee") + local xavp = self.ngcp.prefs[v]:xavp("callee") xavp(string.format("test_%s", v), v) assertEquals(sr.pv.get(string.format("$xavp(callee_%s_prefs=>test_%s)", v, v)), v) assertEquals(sr.pv.get(string.format("$xavp(callee_%s_prefs=>dummy)", v)), "callee") diff --git a/tests/ngcp_avp.lua b/tests/ngcp_avp.lua index 1eef410..ebda0d4 100644 --- a/tests/ngcp_avp.lua +++ b/tests/ngcp_avp.lua @@ -18,11 +18,12 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'mocks.sr' -require 'ngcp.avp' +local srMock = require 'mocks.sr' +local NGCPAvp = require 'ngcp.avp' -sr = srMock:new() +sr = srMock.new() +-- luacheck: ignore TestNGCPAvp TestNGCPAvp = {} --class function TestNGCPAvp:setUp() self.avp = NGCPAvp:new("testid") @@ -47,7 +48,6 @@ TestNGCPAvp = {} --class function TestNGCPAvp:test_avp_set() local vals = {1,2,3} local okvals = {3,2,1} - local i for i=1,#vals do self.avp(vals[i]) assertEquals(self.avp(),vals[i]) @@ -58,7 +58,6 @@ TestNGCPAvp = {} --class function TestNGCPAvp:test_avp_set2() local vals = {1,2,"3"} local okvals = {"3",2,1} - local i for i=1,#vals do self.avp(vals[i]) assertEquals(self.avp(),vals[i]) @@ -93,4 +92,4 @@ TestNGCPAvp = {} --class assertEquals(tostring(self.avp), "$avp(s:testid):hola") end -- class TestNGCPAvp ---EOF \ No newline at end of file +--EOF diff --git a/tests/test_kamailio.lua b/tests/ngcp_cp.lua similarity index 74% rename from tests/test_kamailio.lua rename to tests/ngcp_cp.lua index 5cf5bd5..3b71e0f 100644 --- a/tests/test_kamailio.lua +++ b/tests/ngcp_cp.lua @@ -17,16 +17,9 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -require 'tests.utils' -require 'tests.ngcp_avp' -require 'tests.ngcp_xavp' -require 'tests.ngcp_pref' -require 'tests.ngcp_dp' -require 'tests.ngcp_up' -require 'tests.ngcp_pp' -require 'tests.ngcp_rp' ----- Control test output: -lu = LuaUnit -lu:setOutputType( "TAP" ) -lu:setVerbosity( 1 ) -lu:run() \ No newline at end of file +require('luaunit') +local NGCPContractPrefs = require 'ngcp.cp' + +-- luacheck: ignore TestNGCPContractPrefs +TestNGCPContractPrefs = {} --class +-- class TestNGCP diff --git a/tests/ngcp_dlg_var.lua b/tests/ngcp_dlg_var.lua index e6c8783..cf55654 100644 --- a/tests/ngcp_dlg_var.lua +++ b/tests/ngcp_dlg_var.lua @@ -18,11 +18,11 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'mocks.sr' +local srMock = require 'mocks.sr' local NGCPDlgVar = require 'ngcp.dlg_var' sr = srMock:new() - +-- luacheck: ignore TestNGCPDlgVar TestNGCPDlgVar = {} --class function TestNGCPDlgVar:setUp() self.var = NGCPDlgVar:new("testid") @@ -67,4 +67,4 @@ TestNGCPDlgVar = {} --class assertEquals(tostring(self.var), "$dlg_var(testid):hola") end -- class TestNGCPDlgVar ---EOF \ No newline at end of file +--EOF diff --git a/tests/ngcp_dlgcnt.lua b/tests/ngcp_dlgcnt.lua index 55812b3..611b52a 100644 --- a/tests/ngcp_dlgcnt.lua +++ b/tests/ngcp_dlgcnt.lua @@ -1,5 +1,5 @@ -- --- Copyright 2014 SipWise Team +-- Copyright 2014-2015 SipWise Team -- -- 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 @@ -19,17 +19,12 @@ -- local lemock = require('lemock') require('luaunit') -require 'ngcp.utils' -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local srMock = require 'mocks.sr' +sr = srMock:new() local mc - +-- luacheck: ignore TestNGCPDlgCnt TestNGCPDlgCnt = {} --class function TestNGCPDlgCnt:setUp() @@ -84,7 +79,6 @@ TestNGCPDlgCnt = {} --class end function TestNGCPDlgCnt:test_set_1() - local c = self.dlg.config self.central:ping() ;mc :returns(true) self.central:incr("total") ;mc :returns(1) @@ -97,7 +91,6 @@ TestNGCPDlgCnt = {} --class end function TestNGCPDlgCnt:test_set_2() - local c = self.dlg.config self.central:ping() ;mc :returns(true) self.central:incr("total") ;mc :returns(1) @@ -116,7 +109,6 @@ TestNGCPDlgCnt = {} --class end function TestNGCPDlgCnt:test_del() - local c = self.dlg.config self.pair:ping() ;mc :returns(true) self.pair:lpop("callid0") ;mc :returns("total") self.pair:lpop("callid0") ;mc :returns(nil) @@ -133,7 +125,6 @@ TestNGCPDlgCnt = {} --class end function TestNGCPDlgCnt:test_del_zero() - local c = self.dlg.config self.pair:ping() ;mc :returns(true) self.pair:lpop("callid0") ;mc :returns("total") self.pair:lpop("callid0") ;mc :returns(nil) @@ -188,7 +179,6 @@ TestNGCPDlgCnt = {} --class end function TestNGCPDlgCnt:test_del_multy() - local c = self.dlg.config self.pair:ping() ;mc :returns(true) self.pair:lpop("callid0") ;mc :returns("total") @@ -214,5 +204,31 @@ TestNGCPDlgCnt = {} --class assertIs(self.dlg.pair, self.pair) end + function TestNGCPDlgCnt:test_is_in_set_fail() + self.pair:ping() ;mc :returns(true) + self.pair:lrange("callid0", 0, -1) ;mc :returns(nil) + + mc:replay() + local res = self.dlg:is_in_set("callid0", "fake") + mc:verify() + + assertIs(self.dlg.central, self.central) + assertIs(self.dlg.pair, self.pair) + assertFalse(res) + end + + function TestNGCPDlgCnt:test_is_in_set_ok() + self.pair:ping() ;mc :returns(true) + self.pair:lrange("callid0", 0, -1) ;mc :returns({"whatever", "fake", "jojo"}) + + mc:replay() + local res = self.dlg:is_in_set("callid0", "fake") + mc:verify() + + assertIs(self.dlg.central, self.central) + assertIs(self.dlg.pair, self.pair) + assertTrue(res) + end + -- class TestNGCPDlgCnt --EOF diff --git a/tests/ngcp_dlglist.lua b/tests/ngcp_dlglist.lua new file mode 100644 index 0000000..baa8ecf --- /dev/null +++ b/tests/ngcp_dlglist.lua @@ -0,0 +1,48 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- + +local lemock = require('lemock') +require('luaunit') + +local srMock = require 'mocks.sr' +sr = srMock:new() + +local mc + +-- luacheck: ignore TestNGCPDlgList +TestNGCPDlgList = {} --class + +function TestNGCPDlgList:setUp() + mc = lemock.controller() + self.fake_redis = mc:mock() + self.central = mc:mock() + self.pair = mc:mock() + + package.loaded.redis = self.fake_redis + local NGCPDlgList = require 'ngcp.dlglist' + + self.dlg = NGCPDlgList.new() + assertTrue(self.dlg) + + self.dlg.central = self.central; + self.dlg.pair = self.pair +end + +-- class diff --git a/tests/ngcp_dp.lua b/tests/ngcp_dp.lua index 22e3112..3fb6a23 100644 --- a/tests/ngcp_dp.lua +++ b/tests/ngcp_dp.lua @@ -18,30 +18,28 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require('lemock') -require 'ngcp.utils' -require 'tests_v.dp_vars' - -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local utils = require 'ngcp.utils' +local utable = utils.table +local lemock = require('lemock') +local DPFetch = require 'tests_v.dp_vars' + +local srMock = require 'mocks.sr' +sr = srMock:new() local mc,env,con local dp_vars = DPFetch:new() package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end end -require 'ngcp.config' -require 'ngcp.dp' +local NGCPConfig = require 'ngcp.config' +local NGCPDomainPrefs = require 'ngcp.dp' +-- luacheck: ignore TestNGCPDomainPrefs TestNGCPDomainPrefs = {} --class function TestNGCPDomainPrefs:setUp() @@ -89,10 +87,9 @@ TestNGCPDomainPrefs = {} --class function TestNGCPDomainPrefs:get_defaults() local keys_expected = {"sst_enable", "sst_refresh_method"} local defaults = NGCPConfig.get_defaults(self.d.config, 'dom') - local k,_ for k,_ in pairs(defaults) do - table.add(keys_expected, k) + utable.add(keys_expected, k) end return keys_expected end @@ -198,4 +195,4 @@ TestNGCPDomainPrefs = {} --class assertEquals(tostring(self.d), 'caller_dom_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_dom_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end -- class TestNGCPDomainPrefs ---EOF \ No newline at end of file +--EOF diff --git a/tests/ngcp_pp.lua b/tests/ngcp_pp.lua index eff5b0b..5474328 100644 --- a/tests/ngcp_pp.lua +++ b/tests/ngcp_pp.lua @@ -18,30 +18,28 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require('lemock') -require 'ngcp.utils' -require 'tests_v.pp_vars' - -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local lemock = require('lemock') +local utils = require 'ngcp.utils' +local utable = utils.table +local PPFetch = require 'tests_v.pp_vars' + + +local srMock = require 'mocks.sr' +sr = srMock:new() local mc,env,con local pp_vars = PPFetch:new() package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end end -require 'ngcp.config' -require 'ngcp.rp' - +local NGCPConfig = require 'ngcp.config' +local NGCPPeerPrefs = require 'ngcp.pp' +-- luacheck: ignore TestNGCPPeerPrefs TestNGCPPeerPrefs = {} --class function TestNGCPPeerPrefs:setUp() @@ -52,7 +50,7 @@ TestNGCPPeerPrefs = {} --class package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end @@ -88,18 +86,17 @@ TestNGCPPeerPrefs = {} --class function TestNGCPPeerPrefs:get_defaults(level, set) local keys_expected = {} local defaults = self.d.config:get_defaults('peer') - local k,v if set then - keys_expected = table.deepcopy(set) - for k,v in pairs(keys_expected) do + keys_expected = utable.deepcopy(set) + for _,v in pairs(keys_expected) do sr.log("dbg", string.format("removed key:%s is been loaded.", v)) defaults[v] = nil end end for k,v in pairs(defaults) do - table.add(keys_expected, k) + utable.add(keys_expected, k) assertEquals(sr.pv.get("$xavp("..level.."_peer_prefs=>"..k..")"), v) end return keys_expected @@ -127,6 +124,20 @@ TestNGCPPeerPrefs = {} --class local keys = self.d:caller_load("2") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "inbound_npn", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>dummy)"), "caller") assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>sst_enable)"),"no") assertEquals(sr.pv.get("$xavp(caller_peer_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -146,6 +157,20 @@ TestNGCPPeerPrefs = {} --class local keys = self.d:callee_load("2") mc:verify() + local lkeys = { + "ip_header", + "sst_enable", + "outbound_from_user", + "inbound_upn", + "sst_expires", + "sst_max_timer", + "inbound_npn", + "sst_min_timer", + "sst_refresh_method", + "inbound_uprn" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>sst_enable)"),"no") assertEquals(sr.pv.get("$xavp(callee_peer_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -221,4 +246,4 @@ TestNGCPPeerPrefs = {} --class assertEquals(tostring(self.d), 'caller_peer_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_peer_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end -- class TestNGCPPeerPrefs ---EOF \ No newline at end of file +--EOF diff --git a/tests/ngcp_pprof.lua b/tests/ngcp_pprof.lua index 5b0f7f9..45a5d08 100644 --- a/tests/ngcp_pprof.lua +++ b/tests/ngcp_pprof.lua @@ -18,30 +18,25 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require('lemock') -require 'ngcp.utils' -require 'tests_v.pprof_vars' - -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local lemock = require('lemock') +local PProfFetch = require 'tests_v.pprof_vars' + +local srMock = require 'mocks.sr' +sr = srMock:new() local mc,env,con local pprof_vars = PProfFetch:new() package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end end -require 'ngcp.config' -require 'ngcp.pprof' - +local NGCPConfig = require 'ngcp.config' +local NGCPProfilePrefs = require 'ngcp.pprof' +-- luacheck: ignore TestNGCPProfilePrefs TestNGCPProfilePrefs = {} --class function TestNGCPProfilePrefs:setUp() @@ -52,7 +47,7 @@ TestNGCPProfilePrefs = {} --class package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end @@ -110,6 +105,7 @@ TestNGCPProfilePrefs = {} --class local keys = self.d:caller_load("ah736f72-21d1-4ea6-a3ea-4d7f56b3887c") mc:verify() + assertItemsEquals(keys, {"sst_enable", "sst_refresh_method", "outbound_from_user"}) assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>dummy)"), "caller") assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>sst_enable)"),"yes") assertEquals(sr.pv.get("$xavp(caller_prof_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -129,6 +125,7 @@ TestNGCPProfilePrefs = {} --class local keys = self.d:callee_load("ah736f72-21d1-4ea6-a3ea-4d7f56b3887c") mc:verify() + assertItemsEquals(keys, {"sst_enable", "sst_refresh_method", "outbound_from_user"}) assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>dummy)"), "callee") assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_enable)"),"yes") assertEquals(sr.pv.get("$xavp(callee_prof_prefs=>sst_refresh_method)"), "UPDATE_FALLBACK_INVITE") @@ -203,4 +200,4 @@ TestNGCPProfilePrefs = {} --class assertEquals(tostring(self.d), 'caller_prof_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_prof_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end -- class TestNGCPProfilePrefs ---EOF \ No newline at end of file +--EOF diff --git a/tests/ngcp_pref.lua b/tests/ngcp_pref.lua index 3b1b4e4..507e64d 100644 --- a/tests/ngcp_pref.lua +++ b/tests/ngcp_pref.lua @@ -18,8 +18,8 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'ngcp.pref' +local NGCPPrefs = require 'ngcp.pref' +-- luacheck: ignore TestNGCPPrefs TestNGCPPrefs = {} --class -- class TestNGCP ---EOF \ No newline at end of file diff --git a/tests/ngcp_recentcalls.lua b/tests/ngcp_recentcalls.lua index 725e892..b050ebc 100644 --- a/tests/ngcp_recentcalls.lua +++ b/tests/ngcp_recentcalls.lua @@ -17,19 +17,16 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -local lemock = require('lemock') + require('luaunit') -require 'ngcp.utils' +local lemock = require('lemock') +local srMock = require 'mocks.sr' -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +sr = srMock:new() local mc +-- luacheck: ignore TestNGCPRecentCalls TestNGCPRecentCalls = {} --class function TestNGCPRecentCalls:setUp() diff --git a/tests/ngcp_rp.lua b/tests/ngcp_rp.lua index dbcc20f..37da13b 100644 --- a/tests/ngcp_rp.lua +++ b/tests/ngcp_rp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -18,45 +18,15 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'ngcp.utils' -require 'ngcp.rp' -require 'tests_v.dp_vars' -require 'tests_v.up_vars' -require 'lemock' +local NGCPDomainPrefs = require 'ngcp.dp' +local NGCPUserPrefs = require 'ngcp.up' +local NGCPPeerPrefs = require 'ngcp.pp' +local NGCPRealPrefs = require 'ngcp.rp' -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end -local mc = nil - -PFetch = { - __class__ = 'PFetch', - _i = { domain=1, user=1 }, - _var = { domain=dp_vars, user=up_vars} -} - function PFetch:new() - local t = {} - return setmetatable(t, { __index = PFetch }) - end - - function PFetch:val(group, uuid) - if not self._i[group] then - error(string.format("group:%s unknown", group)) - end - self._i[group] = self._i[group] + 1 - local temp = self._var[group][uuid][self._i[group]-1] - if not temp then - print("var nil") - end - end - - function PFetch:reset(group) - self._i[group] = 1 - end +local srMock = require 'mocks.sr' +sr = srMock:new() +-- luacheck: ignore TestNGCPRealPrefs TestNGCPRealPrefs = {} --class function TestNGCPRealPrefs:setUp() @@ -263,4 +233,4 @@ TestNGCPRealPrefs = {} --class assertEquals(tostring(self.real),'caller_real_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_real_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end -- class TestNGCPRealPrefs ---EOF \ No newline at end of file +--EOF diff --git a/tests/ngcp_up.lua b/tests/ngcp_up.lua index 3c5d600..f6607ec 100644 --- a/tests/ngcp_up.lua +++ b/tests/ngcp_up.lua @@ -18,31 +18,28 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require('lemock') -require 'ngcp.utils' -require 'tests_v.up_vars' - -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local lemock = require('lemock') +local utils = require 'ngcp.utils' +local utable = utils.table +local UPFetch = require 'tests_v.up_vars' + +local srMock = require 'mocks.sr' +sr = srMock:new() local mc,env,con local up_vars = UPFetch:new() package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end end -require 'ngcp.config' -require 'ngcp.up' - +local NGCPConfig = require 'ngcp.config' +local NGCPUserPrefs = require 'ngcp.up' +-- luacheck: ignore TestNGCPUserPrefs TestNGCPUserPrefs = {} --class function TestNGCPUserPrefs:setUp() @@ -53,7 +50,7 @@ TestNGCPUserPrefs = {} --class package.loaded.luasql = nil package.preload['luasql.mysql'] = function () - luasql = {} + local luasql = {} luasql.mysql = function () return env end @@ -99,18 +96,17 @@ TestNGCPUserPrefs = {} --class function TestNGCPUserPrefs:get_defaults(level, set) local keys_expected = {} local defaults = self.d.config:get_defaults('usr') - local k,v if set then - keys_expected = table.deepcopy(set) - for k,v in pairs(keys_expected) do + keys_expected = utable.deepcopy(set) + for _,v in pairs(keys_expected) do sr.log("dbg", string.format("removed key:%s is been loaded.", v)) defaults[v] = nil end end for k,v in pairs(defaults) do - table.add(keys_expected, k) + utable.add(keys_expected, k) assertEquals(sr.pv.get("$xavp("..level.."_usr_prefs=>"..k..")"), v) end return keys_expected @@ -130,6 +126,17 @@ TestNGCPUserPrefs = {} --class local keys = self.d:caller_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c") mc:verify() + local lkeys = { + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>account_id)"),2) assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>cli)"),"4311001") assertEquals(sr.pv.get("$xavp(caller_usr_prefs=>cc)"),"43") @@ -152,6 +159,17 @@ TestNGCPUserPrefs = {} --class local keys = self.d:callee_load("ae736f72-21d1-4ea6-a3ea-4d7f56b3887c") mc:verify() + local lkeys = { + "ext_subscriber_id", + "ringtimeout", + "account_id", + "ext_contract_id", + "cli", + "cc", + "ac" + } + + assertItemsEquals(keys, lkeys) assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>account_id)"),2) assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>cli)"),"4311001") assertEquals(sr.pv.get("$xavp(callee_usr_prefs=>cc)"),"43") @@ -226,4 +244,3 @@ TestNGCPUserPrefs = {} --class assertEquals(tostring(self.d), 'caller_usr_prefs:{other={1},otherfoo={"foo"},dummy={"caller"}}\ncallee_usr_prefs:{dummy={"callee"},testid={1},foo={"foo"}}\n') end -- class TestNGCPUserPrefs ---EOF \ No newline at end of file diff --git a/tests/ngcp_xavp.lua b/tests/ngcp_xavp.lua index d56076d..2d53623 100644 --- a/tests/ngcp_xavp.lua +++ b/tests/ngcp_xavp.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -18,16 +18,12 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'ngcp.xavp' +local NGCPXAvp = require 'ngcp.xavp' -if not sr then - require 'mocks.sr' - sr = srMock:new() -else - argv = {} -end +local srMock = require 'mocks.sr' +sr = srMock:new() -vals = { +local vals = { { id = 1, uuid = "ae736f72-21d1-4ea6-a3ea-4d7f56b3887c", @@ -59,6 +55,7 @@ vals = { last_modified = "1900-01-01 00:00:01" } } +-- luacheck: ignore TestNGCPXAvp TestNGCPXAvp = {} --class function TestNGCPXAvp:test_create() local xavp = NGCPXAvp:new("caller", "peer", {}) @@ -76,7 +73,7 @@ TestNGCPXAvp = {} --class end function TestNGCPXAvp:test_xavp_get() - xavp = NGCPXAvp:new("caller", "peer", vals) + local xavp = NGCPXAvp:new("caller", "peer", vals) sr.pv.sets("$xavp(caller_peer=>testid)", "value") assertEquals(xavp("testid"), "value") sr.pv.sets("$xavp(caller_peer=>testid)", "1") @@ -84,7 +81,7 @@ TestNGCPXAvp = {} --class end function TestNGCPXAvp:test_xavp_get_all() - xavp = NGCPXAvp:new("caller", "peer", vals) + local xavp = NGCPXAvp:new("caller", "peer", vals) sr.pv.sets("$xavp(caller_peer=>testid)", "value") assertEquals(xavp("testid"), "value") sr.pv.sets("$xavp(caller_peer[0]=>testid)", "1") @@ -93,16 +90,16 @@ TestNGCPXAvp = {} --class function TestNGCPXAvp:test_xavp_set() local xavp = NGCPXAvp:new("caller", "peer", vals) - local vals = {1,"2",3,nil} - for i=1,#vals do - xavp("testid",vals[i]) - assertEquals(xavp("testid"), vals[i]) - assertEquals(sr.pv.get("$xavp(caller_peer=>testid)"),vals[i]) + local lvals = {1,"2",3,nil} + for i=1,#lvals do + xavp("testid",lvals[i]) + assertEquals(xavp("testid"), lvals[i]) + assertEquals(sr.pv.get("$xavp(caller_peer=>testid)"),lvals[i]) end end function TestNGCPXAvp:test_clean() - xavp = NGCPXAvp:new("caller", "peer", vals) + local xavp = NGCPXAvp:new("caller", "peer", vals) xavp("testid", 1) assertEquals(sr.pv.get("$xavp(caller_peer=>testid)"),1) assertEquals(sr.pv.get("$xavp(caller_peer=>dummy)"),"caller") @@ -129,11 +126,11 @@ TestNGCPXAvp = {} --class function TestNGCPXAvp:test_clean_key() local xavp = NGCPXAvp:new("caller", "peer", vals) - local vals = {1,"2",3,nil} - for i=1,#vals do - xavp("testid",vals[i]) - assertEquals(xavp("testid"), vals[i]) - assertEquals(sr.pv.get("$xavp(caller_peer=>testid)"),vals[i]) + local lvals = {1,"2",3,nil} + for i=1,#lvals do + xavp("testid",lvals[i]) + assertEquals(xavp("testid"), lvals[i]) + assertEquals(sr.pv.get("$xavp(caller_peer=>testid)"),lvals[i]) end xavp("other", 1) xavp("other", 2) @@ -158,4 +155,3 @@ TestNGCPXAvp = {} --class end -- class TestNGCPXAvp ---EOF \ No newline at end of file diff --git a/tests/utils.lua b/tests/utils.lua index ba038f4..95b97f5 100644 --- a/tests/utils.lua +++ b/tests/utils.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -18,9 +18,10 @@ -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- require('luaunit') -require 'ngcp.utils' +local utils = require 'ngcp.utils' -TestUtils = {} --class +-- luacheck: ignore TestUtils +TestUtils = {} function TestUtils:setUp() self.simple_hash = { @@ -36,35 +37,35 @@ TestUtils = {} --class end function TestUtils:test_table_deepcopy() - assertNotIs(table.deepcopy(self.simple_hash), self.simple_hash) + assertNotIs(utils.table.deepcopy(self.simple_hash), self.simple_hash) -- if the parameter is not a table... it has te be the same - assertIs(table.deepcopy("hola"), "hola") + assertIs(utils.table.deepcopy("hola"), "hola") end function TestUtils:test_table_contains() - assertTrue(table.contains(self.simple_hash, 3)) - assertFalse(table.contains(self.simple_hash, 4)) - assertFalse(table.contains(nil)) - assertError(table.contains, "hola",1) + assertTrue(utils.table.contains(self.simple_hash, 3)) + assertFalse(utils.table.contains(self.simple_hash, 4)) + assertFalse(utils.table.contains(nil)) + assertError(utils.table.contains, "hola",1) end function TestUtils:test_table_add() assertEquals(self.simple_list, {1,2,3}) - table.add(self.simple_list, 1) + utils.table.add(self.simple_list, 1) assertEquals(self.simple_list, {1,2,3}) - table.add(self.simple_list, 5) + utils.table.add(self.simple_list, 5) assertEquals(self.simple_list, {1,2,3,5}) - table.add(self.simple_list, 4) + utils.table.add(self.simple_list, 4) assertEquals(self.simple_list, {1,2,3,5,4}) end function TestUtils:test_table_del() assertEquals(self.simple_list, {1,2,3}) - table.del(self.simple_list, 1) + utils.table.del(self.simple_list, 1) assertEquals(self.simple_list, {2,3}) - table.del(self.simple_list, 3) + utils.table.del(self.simple_list, 3) assertEquals(self.simple_list, {2}) - table.del(self.simple_list, 2) + utils.table.del(self.simple_list, 2) assertEquals(self.simple_list, {}) end @@ -72,40 +73,40 @@ TestUtils = {} --class assertEquals(self.simple_list, {1,2,3}) table.insert(self.simple_list, 2) assertEquals(self.simple_list, {1,2,3,2}) - table.del(self.simple_list, 1) + utils.table.del(self.simple_list, 1) assertEquals(self.simple_list, {2,3,2}) - table.del(self.simple_list, 2) + utils.table.del(self.simple_list, 2) assertEquals(self.simple_list, {3}) - table.del(self.simple_list, 3) + utils.table.del(self.simple_list, 3) assertEquals(self.simple_list, {}) end function TestUtils:test_table_del_empty() local t = {} - table.del(t, 4) + utils.table.del(t, 4) assertEquals(t, {}) end function TestUtils:test_table_size() - local t = table.size(nil) + local t = utils.table.size(nil) assertEquals(t, 0) - t = table.size({1,2}) + t = utils.table.size({1,2}) assertEquals(t, 2) - t = table.size({}) + t = utils.table.size({}) assertEquals(t, 0) - t = table.size({hola={1,2},adios=2}) + t = utils.table.size({hola={1,2},adios=2}) assertEquals(t, 2) end function TestUtils:test_table_shuffle() assertEquals(self.simple_list, {1,2,3}) - table.add(self.simple_list, 4) - table.add(self.simple_list, 5) - table.add(self.simple_list, 6) - local tmp = table.shuffle(self.simple_list) + utils.table.add(self.simple_list, 4) + utils.table.add(self.simple_list, 5) + utils.table.add(self.simple_list, 6) + local tmp = utils.table.shuffle(self.simple_list) assertItemsEquals(self.simple_list, tmp) assertNotEquals(self.simple_list, tmp) - local tmp2 = table.shuffle(self.simple_list) + local tmp2 = utils.table.shuffle(self.simple_list) assertItemsEquals(self.simple_list, tmp2) --print(table.tostring(tmp)) --print(table.tostring(tmp2)) @@ -114,207 +115,85 @@ TestUtils = {} --class function TestUtils:test_table_shift() assertEquals(self.simple_list, {1,2,3}) - table.add(self.simple_list, 4) - table.add(self.simple_list, 5) - table.add(self.simple_list, 6) - table.shift(self.simple_list, 2) + utils.table.add(self.simple_list, 4) + utils.table.add(self.simple_list, 5) + utils.table.add(self.simple_list, 6) + utils.table.shift(self.simple_list, 2) assertEquals(self.simple_list, {3,4,5,6,1,2}) end function TestUtils:test_table_shift2() - local tmp = table.deepcopy(self.simple_list) + local tmp = utils.table.deepcopy(self.simple_list) assertEquals(tmp, {1,2,3}) - table.shift(tmp, 0) + utils.table.shift(tmp, 0) assertEquals(tmp, {1,2,3}) - tmp = table.deepcopy(self.simple_list) - table.shift(tmp, 1) + tmp = utils.table.deepcopy(self.simple_list) + utils.table.shift(tmp, 1) assertEquals(tmp, {2,3,1}) - tmp = table.deepcopy(self.simple_list) - table.shift(tmp, 2) + tmp = utils.table.deepcopy(self.simple_list) + utils.table.shift(tmp, 2) assertEquals(tmp, {3,1,2}) - tmp = table.deepcopy(self.simple_list) - table.shift(tmp, 3) + tmp = utils.table.deepcopy(self.simple_list) + utils.table.shift(tmp, 3) assertEquals(tmp, {1,2,3}) - tmp = table.deepcopy(self.simple_list) - table.shift(tmp, 4) + tmp = utils.table.deepcopy(self.simple_list) + utils.table.shift(tmp, 4) assertEquals(tmp, {2,3,1}) end function TestUtils:test_table_tostring() - assertError(table.tostring, "nil") - assertEquals(table.tostring(self.simple_list), "{1,2,3}") - assertTrue(table.tostring(self.simple_hash)) + assertError(utils.table.tostring, "nil") + assertEquals(utils.table.tostring(self.simple_list), "{1,2,3}") + assertTrue(utils.table.tostring(self.simple_hash)) --print(table.tostring(self.simple_hash) .. "\n") - assertTrue(table.tostring(self.complex_hash)) + assertTrue(utils.table.tostring(self.complex_hash)) --print(table.tostring(self.complex_hash)) end function TestUtils:test_implode() - assertEquals(implode(',', self.simple_list, "'"), "'1','2','3'") - assertError(implode, nil, self.simple_list, "'") - assertError(implode, ',', nil, "'") + assertEquals(utils.implode(',', self.simple_list, "'"), "'1','2','3'") + assertError(utils.implode, nil, self.simple_list, "'") + assertError(utils.implode, ',', nil, "'") end function TestUtils:test_explode() - assertItemsEquals(explode(',',"1,2,3"), {'1','2','3'}) - assertItemsEquals(explode('=>',"1=>2=>3"), {'1','2','3'}) + assertItemsEquals(utils.explode(',',"1,2,3"), {'1','2','3'}) + assertItemsEquals(utils.explode('=>',"1=>2=>3"), {'1','2','3'}) end function TestUtils:test_starts() - assertError(string.stats, nil, "g") - assertTrue(string.starts("goga", "g")) - assertTrue(string.starts("goga", "go")) - assertTrue(string.starts("goga", "gog")) - assertTrue(string.starts("goga", "goga")) - assertFalse(string.starts("goga", "a")) - assertError(string.starts, "goga", nil) - assertTrue(string.starts("$goga", "$")) - assertTrue(string.starts("(goga)", "(")) + assertError(utils.string.stats, nil, "g") + assertTrue(utils.string.starts("goga", "g")) + assertTrue(utils.string.starts("goga", "go")) + assertTrue(utils.string.starts("goga", "gog")) + assertTrue(utils.string.starts("goga", "goga")) + assertFalse(utils.string.starts("goga", "a")) + assertError(utils.string.starts, "goga", nil) + assertTrue(utils.string.starts("$goga", "$")) + assertTrue(utils.string.starts("(goga)", "(")) end function TestUtils:test_ends() - assertError(string.ends, nil, "g") - assertTrue(string.ends("goga", "a")) - assertTrue(string.ends("goga", "ga")) - assertTrue(string.ends("goga", "oga")) - assertTrue(string.ends("goga", "goga")) - assertFalse(string.ends("goga", "f")) - assertError(string.ends, "goga", nil) + assertError(utils.string.ends, nil, "g") + assertTrue(utils.string.ends("goga", "a")) + assertTrue(utils.string.ends("goga", "ga")) + assertTrue(utils.string.ends("goga", "oga")) + assertTrue(utils.string.ends("goga", "goga")) + assertFalse(utils.string.ends("goga", "f")) + assertError(utils.string.ends, "goga", nil) end function TestUtils:test_table_merge() assertEquals(self.simple_list, {1,2,3}) - table.merge(self.simple_list, {1}) + utils.table.merge(self.simple_list, {1}) assertEquals(self.simple_list, {1,2,3}) - table.merge(self.simple_list, {5}) + utils.table.merge(self.simple_list, {5}) assertEquals(self.simple_list, {1,2,3,5}) - table.merge(self.simple_list, {5,4}) + utils.table.merge(self.simple_list, {5,4}) assertEquals(self.simple_list, {1,2,3,5,4}) - table.merge(nil, nil) - table.merge(nil, {}) + utils.table.merge(nil, nil) + utils.table.merge(nil, {}) local tmp = {} - table.merge(tmp, {1,2,3,5,4}) + utils.table.merge(tmp, {1,2,3,5,4}) assertEquals(tmp, {1,2,3,5,4}) end --- class TestUtils - -TestStack = {} - function TestStack:test() - local s = Stack:new() - assertEquals(type(s), 'table') - assertEquals(s.__class__, 'Stack') - end - - function TestStack:test_size() - local s = Stack:new() - assertEquals(s:size(),0) - s:push(1) - assertEquals(s:size(),1) - s:pop() - assertEquals(s:size(),0) - end - - function TestStack:test_push() - local s s = Stack:new() - s:push(1) - assertEquals(s:size(),1) - end - - function TestStack:test_pop() - local s = Stack:new() - assertEquals(s:pop(), nil) - s:push(1) - assertEquals(s:size(),1) - assertEquals(s:pop(),1) - assertEquals(s:size(),0) - end - - function TestStack:test_get() - local s = Stack:new() - s:push(1) - assertEquals(s:get(0),1) - s:push({1,2,3}) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),1) - assertError(s.get, s, -1) - assertIsNil(s:get(2)) - end - - function TestStack:test_get_op() - local s = Stack:new() - s:push(1) - assertEquals(s[0],1) - s:push({1,2,3}) - assertEquals(s[0],{1,2,3}) - assertEquals(s[1],1) - assertIsNil(s[2]) - end - - function TestStack:test_set() - local s = Stack:new() - s:push(1) - s:push({1,2,3}) - assertEquals(s:size(),2) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),1) - s:set(1, 2) - assertEquals(s:size(),2) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),2) - s:set(2, 3) - assertEquals(s:size(),2) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),2) - assertIsNil(s:get(2)) - assertError(s.set, s, "no", -1) - assertError(s.set, s, -1, 2) - end - - function TestStack:test_set_op() - local s = Stack:new() - s:push(1) - s:push({1,2,3}) - assertEquals(s:size(),2) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),1) - s[1] = 2 - assertEquals(s:size(),2) - assertEquals(s:get(0),{1,2,3}) - assertEquals(s:get(1),2) - s[0] = "new" - assertEquals(s:size(),2) - assertEquals(s:get(0),"new") - assertEquals(s:get(1),2) - s[1] = "old" - assertEquals(s:get(0),"new") - assertEquals(s:get(1),"old") - assertEquals(s:size(),2) - s[2] = "error" - assertEquals(s:get(0),"new") - assertEquals(s:get(1),"old") - assertIsNil(s:get(2)) - assertEquals(s:size(),2) - end - - function TestStack:test_list() - local s = Stack:new() - local l = s:list() - assertEquals(#l, 0) - s:push(1) - s:push({1,2,3}) - assertEquals(s:size(),2) - l = s:list() - assertItemsEquals(l[1],{1,2,3}) - assertEquals(l[2],1) - assertEquals(s:size(),2) - end - - function TestStack:test_tostring() - local s = Stack:new() - s:push(1) - assertEquals(tostring(s), "{1}") - s:push(2) - assertEquals(tostring(s), "{2,1}") - end --- class TestStack ---EOF \ No newline at end of file diff --git a/tests/utils_stack.lua b/tests/utils_stack.lua new file mode 100644 index 0000000..5019ba8 --- /dev/null +++ b/tests/utils_stack.lua @@ -0,0 +1,142 @@ +-- +-- Copyright 2013-2015 SipWise Team +-- +-- 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 +-- the Free Software Foundation, either version 3 of the License, or +-- (at your option) any later version. +-- +-- This package is distributed in the hope that it will be useful, +-- but WITHOUT ANY WARRANTY; without even the implied warranty of +-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +-- GNU General Public License for more details. +-- +-- You should have received a copy of the GNU General Public License +-- along with this program. If not, see . +-- . +-- On Debian systems, the complete text of the GNU General +-- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". +-- +require('luaunit') +local utils = require 'ngcp.utils' +local Stack = utils.Stack + +-- luacheck: ignore TestStack +TestStack = {} + function TestStack:test() + local s = Stack.new() + assertEquals(type(s), 'table') + assertEquals(s.__class__, 'Stack') + end + + function TestStack:test_size() + local s = Stack:new() + assertEquals(s:size(),0) + s:push(1) + assertEquals(s:size(),1) + s:pop() + assertEquals(s:size(),0) + end + + function TestStack:test_push() + local s s = Stack:new() + s:push(1) + assertEquals(s:size(),1) + end + + function TestStack:test_pop() + local s = Stack:new() + assertEquals(s:pop(), nil) + s:push(1) + assertEquals(s:size(),1) + assertEquals(s:pop(),1) + assertEquals(s:size(),0) + end + + function TestStack:test_get() + local s = Stack:new() + s:push(1) + assertEquals(s:get(0),1) + s:push({1,2,3}) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),1) + assertError(s.get, s, -1) + assertIsNil(s:get(2)) + end + + function TestStack:test_get_op() + local s = Stack:new() + s:push(1) + assertEquals(s[0],1) + s:push({1,2,3}) + assertEquals(s[0],{1,2,3}) + assertEquals(s[1],1) + assertIsNil(s[2]) + end + + function TestStack:test_set() + local s = Stack:new() + s:push(1) + s:push({1,2,3}) + assertEquals(s:size(),2) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),1) + s:set(1, 2) + assertEquals(s:size(),2) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),2) + s:set(2, 3) + assertEquals(s:size(),2) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),2) + assertIsNil(s:get(2)) + assertError(s.set, s, "no", -1) + assertError(s.set, s, -1, 2) + end + + function TestStack:test_set_op() + local s = Stack:new() + s:push(1) + s:push({1,2,3}) + assertEquals(s:size(),2) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),1) + s[1] = 2 + assertEquals(s:size(),2) + assertEquals(s:get(0),{1,2,3}) + assertEquals(s:get(1),2) + s[0] = "new" + assertEquals(s:size(),2) + assertEquals(s:get(0),"new") + assertEquals(s:get(1),2) + s[1] = "old" + assertEquals(s:get(0),"new") + assertEquals(s:get(1),"old") + assertEquals(s:size(),2) + s[2] = "error" + assertEquals(s:get(0),"new") + assertEquals(s:get(1),"old") + assertIsNil(s:get(2)) + assertEquals(s:size(),2) + end + + function TestStack:test_list() + local s = Stack:new() + local l = s:list() + assertEquals(#l, 0) + s:push(1) + s:push({1,2,3}) + assertEquals(s:size(),2) + l = s:list() + assertItemsEquals(l[1],{1,2,3}) + assertEquals(l[2],1) + assertEquals(s:size(),2) + end + + function TestStack:test_tostring() + local s = Stack:new() + s:push(1) + assertEquals(tostring(s), "{1}") + s:push(2) + assertEquals(tostring(s), "{2,1}") + end diff --git a/tests_v/dp_vars.lua b/tests_v/dp_vars.lua index da6e8fb..f1fe68b 100644 --- a/tests_v/dp_vars.lua +++ b/tests_v/dp_vars.lua @@ -17,7 +17,8 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -dp_vars = { + +local dp_vars = { voip_sipwise_local = { { id = 1, @@ -154,12 +155,12 @@ dp_vars = { } } -DPFetch = { +local DPFetch = { __class__ = 'DPFetch', _i = 1 } - function DPFetch:new() - t = {} + function DPFetch.new() + local t = {} return setmetatable(t, { __index = DPFetch }) end @@ -171,4 +172,4 @@ DPFetch = { function DPFetch:reset() self._i = 1 end ---EOF \ No newline at end of file +return DPFetch diff --git a/tests_v/pp_vars.lua b/tests_v/pp_vars.lua index 93f1127..09b12fa 100644 --- a/tests_v/pp_vars.lua +++ b/tests_v/pp_vars.lua @@ -17,7 +17,7 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -pp_vars = { +local pp_vars = { p_1 = { { id = 1, @@ -174,12 +174,12 @@ pp_vars = { } } -PPFetch = { +local PPFetch = { __class__ = 'PPFetch', _i = 1 } - function PPFetch:new() - t = {} + function PPFetch.new() + local t = {} return setmetatable(t, { __index = PPFetch }) end @@ -191,4 +191,5 @@ PPFetch = { function PPFetch:reset() self._i = 1 end ---EOF \ No newline at end of file + +return PPFetch diff --git a/tests_v/pprof_vars.lua b/tests_v/pprof_vars.lua index 7c7c2d4..127d26e 100644 --- a/tests_v/pprof_vars.lua +++ b/tests_v/pprof_vars.lua @@ -1,5 +1,5 @@ -- --- Copyright 2013 SipWise Team +-- Copyright 2013-2015 SipWise Team -- -- 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 @@ -17,7 +17,7 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -pprof_vars = { +local pprof_vars = { prof_1 = { { id = 1, @@ -174,12 +174,12 @@ pprof_vars = { } } -PProfFetch = { +local PProfFetch = { __class__ = 'PProfFetch', _i = 1 } - function PProfFetch:new() - t = {} + function PProfFetch.new() + local t = {} return setmetatable(t, { __index = PProfFetch }) end @@ -191,4 +191,5 @@ PProfFetch = { function PProfFetch:reset() self._i = 1 end ---EOF \ No newline at end of file + +return PProfFetch diff --git a/tests_v/up_vars.lua b/tests_v/up_vars.lua index e5ea63a..c291c8d 100644 --- a/tests_v/up_vars.lua +++ b/tests_v/up_vars.lua @@ -17,7 +17,7 @@ -- On Debian systems, the complete text of the GNU General -- Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". -- -up_vars = { +local up_vars = { ae736f72_21d1_4ea6_a3ea_4d7f56b3887c = { { id = 1, @@ -136,12 +136,12 @@ up_vars = { } } -UPFetch = { +local UPFetch = { __class__ = 'UPFetch', _i = 1 } - function UPFetch:new() - t = {} + function UPFetch.new() + local t = {} return setmetatable(t, { __index = UPFetch }) end @@ -153,4 +153,5 @@ UPFetch = { function UPFetch:reset() self._i = 1 end ---EOF \ No newline at end of file + +return UPFetch