diff --git a/ngcp/avp.lua b/ngcp/avp.lua new file mode 100644 index 0000000..50d0441 --- /dev/null +++ b/ngcp/avp.lua @@ -0,0 +1,37 @@ +#!/usr/bin/env lua5.1 + +-- class NGCPAvp +NGCPAvp = { + __class__ = 'NGCPAvp' +} +NGCPAvp_MT = { + __index = NGCPAvp, +} + + function NGCPAvp:new(id) + local t = { id = "$(avp(s:" .. id .. "))" } + NGCPAvp_MT.__call = function(t, value) + if not value then + --print(table.tostring(sr.pv.vars)) + --print(t.id) + return sr.pv.get(t.id) + elseif type(value) == "number" then + sr.pv.seti(t.id, value) + elseif type(value) == "string" then + sr.pv.sets(t.id, value) + else + error("value is not a number or string") + end + end + setmetatable( t, NGCPAvp_MT ) + return t + 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 diff --git a/tests/mocks.lua b/tests/mocks.lua index 94bbe90..7e9376f 100644 --- a/tests/mocks.lua +++ b/tests/mocks.lua @@ -17,7 +17,7 @@ TestSRMock = {} end function TestSRMock:tearDown() - self.sr = nil + self.sr.pv.vars = {} end function TestSRMock:test_ini() @@ -25,31 +25,28 @@ TestSRMock = {} end function TestSRMock:test_sets() - self.sr.pv.unset("$avp('hithere')") - self.sr.pv.sets("$avp('hithere')", "value") - assertEquals(self.sr.pv.vars["$avp('hithere')"], "value") - assertError(self.sr.pv.sets, "$avp('hithere')", 1) + self.sr.pv.sets("$(avp(s:hithere))", "value") + assertEquals(self.sr.pv.vars["$(avp(s:hithere))"], "value") + assertError(self.sr.pv.sets, "$(avp(s:hithere))", 1) end function TestSRMock:test_seti() - self.sr.pv.unset("$avp('hithere')") - self.sr.pv.seti("$avp('hithere')", 0) - assertEquals(self.sr.pv.vars["$avp('hithere')"], 0) - assertError(self.sr.pv.seti, "$avp('hithere')", "1") + self.sr.pv.seti("$(avp(s:hithere))", 0) + assertEquals(self.sr.pv.vars["$(avp(s:hithere))"], 0) + assertError(self.sr.pv.seti, "$(avp(s:hithere))", "1") end function TestSRMock:test_get() local vals = {1,2,3} - self.sr.pv.unset("$avp('hithere')") - self.sr.pv.sets("$avp('hithere')", "value") - assertEquals(self.sr.pv.get("$avp('hithere')"), "value") - self.sr.pv.unset("$avp('hithere')") - self.sr.pv.seti("$avp('hithere')", 1) - assertEquals(self.sr.pv.get("$avp('hithere')"), 1) + self.sr.pv.sets("$(avp(s:hithere))", "value") + assertEquals(self.sr.pv.get("$(avp(s:hithere))"), "value") + self.sr.pv.unset("$(avp(s:hithere))") + self.sr.pv.seti("$(avp(s:hithere))", 1) + assertEquals(self.sr.pv.get("$(avp(s:hithere))"), 1) for i=1,#vals do - self.sr.pv.seti("$avp('hithere')", vals[i]) + self.sr.pv.seti("$(avp(s:hithere))", vals[i]) end - local l = self.sr.pv.get("$avp('hithere')") + local l = self.sr.pv.get("$(avp(s:hithere))") assertTrue(type(l), 'table') --print(table.tostring(l)) v = 1 @@ -60,20 +57,21 @@ TestSRMock = {} end function TestSRMock:test_unset() - self.sr.pv.sets("$avp('hithere')", "value") - self.sr.pv.unset("$avp('hithere')") - assertEquals(self.sr.pv.vars["$avp('hithere')"], nil) - self.sr.pv.unset("$avp('hithere')") - assertEquals(self.sr.pv.vars["$avp('hithere')"], nil) + self.sr.pv.sets("$(avp(s:hithere))", "value") + self.sr.pv.unset("$(avp(s:hithere))") + assertEquals(self.sr.pv.vars["$(avp(s:hithere))"], nil) + self.sr.pv.unset("$(avp(s:hithere))") + assertEquals(self.sr.pv.vars["$(avp(s:hithere))"], nil) end function TestSRMock:test_is_null() - self.sr.pv.unset("$avp('hithere')") - assertTrue(self.sr.pv.is_null("$avp('hithere')")) - self.sr.pv.sets("$avp('hithere')", "value") - assertFalse(self.sr.pv.is_null("$avp('hithere')")) - self.sr.pv.sets("$avp('hithere')", "value") - assertFalse(self.sr.pv.is_null("$avp('hithere')")) + 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 ---- Control test output: lu = LuaUnit diff --git a/tests/ngcp_avp.lua b/tests/ngcp_avp.lua new file mode 100644 index 0000000..bdd271d --- /dev/null +++ b/tests/ngcp_avp.lua @@ -0,0 +1,70 @@ +#!/usr/bin/env lua5.1 +require('luaunit') +require 'mocks.sr' +require 'ngcp.avp' + +sr = srMock:new() + +TestNGCPAvp = {} --class + function TestNGCPAvp:setUp() + self.avp = NGCPAvp:new("testid") + end + + function TestNGCPAvp:tearDown() + sr.pv.vars = {} + end + + function TestNGCPAvp:test_avp_id() + assertEquals(self.avp.id, "$(avp(s:testid))") + end + + function TestNGCPAvp:test_avp_get() + sr.pv.sets("$(avp(s:testid))", "value") + assertEquals(self.avp(), "value") + sr.pv.sets("$(avp(s:testid))", "1") + assertItemsEquals(self.avp(),{"1","value"}) + end + + function TestNGCPAvp:test_avp_set() + local vals = {1,2,3} + for i=1,#vals do + self.avp(vals[i]) + end + local l = self.avp() + assertTrue(type(l), 'table') + --print(table.tostring(l)) + v = 1 + for i=#vals,1,-1 do + assertEquals(l[i],vals[v]) + v = v + 1 + end + end + + function TestNGCPAvp:test_avp_set2() + local vals = {1,2,"3"} + for i=1,#vals do + self.avp(vals[i]) + end + local l = self.avp() + assertTrue(type(l), 'table') + --print(table.tostring(l)) + v = 1 + for i=#vals,1,-1 do + assertEquals(l[i],vals[v]) + v = v + 1 + end + end + + function TestNGCPAvp:test_clean() + self.avp(1) + self.avp:clean() + assertFalse(self.avp()) + end +-- class TestNGCPAvp + +---- Control test output: +lu = LuaUnit +lu:setOutputType( "TAP" ) +lu:setVerbosity( 1 ) +lu:run() +--EOF \ No newline at end of file