[asterisk-commits] mnicholson: branch mnicholson/asttest r175025 - in /team/mnicholson/asttest/a...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Feb 11 21:04:12 CST 2009


Author: mnicholson
Date: Wed Feb 11 21:04:12 2009
New Revision: 175025

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=175025
Log:
added the ability to generate asterisk config files from the test scripts

Added:
    team/mnicholson/asttest/asttest/self-tests/test_astlib/
    team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua   (with props)
Modified:
    team/mnicholson/asttest/asttest/lua/astlib.c
    team/mnicholson/asttest/asttest/lua/astlib.lua

Modified: team/mnicholson/asttest/asttest/lua/astlib.c
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lua/astlib.c?view=diff&rev=175025&r1=175024&r2=175025
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.c (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.c Wed Feb 11 21:04:12 2009
@@ -22,7 +22,16 @@
 
 #include "astlib_lua.h"
 
+static int spawn_asterisk(lua_State *L) {
+	return 1;
+}
+
 static luaL_Reg astlib[] = {
+	{NULL, NULL},
+};
+
+static luaL_Reg asterisk_table[] = {
+	{"spawn", spawn_asterisk},
 	{NULL, NULL},
 };
 
@@ -35,8 +44,16 @@
 
 	/* register our functions */
 	luaL_register(L, "ast", astlib);
+
+	/* set up the 'path' variable */
 	lua_pushstring(L, asterisk_path);
 	lua_setfield(L, -2, "path");
+
+	/* set up the 'asterisk' table and add the spawn function to it */
+	lua_newtable(L);
+	luaL_register(L, NULL, asterisk_table);
+	lua_setfield(L, -2, "asterisk");
+
 	lua_pop(L, 1);
 	
 	/* load the lua portion of the lib */

Modified: team/mnicholson/asttest/asttest/lua/astlib.lua
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lua/astlib.lua?view=diff&rev=175025&r1=175024&r2=175025
==============================================================================
--- team/mnicholson/asttest/asttest/lua/astlib.lua (original)
+++ team/mnicholson/asttest/asttest/lua/astlib.lua Wed Feb 11 21:04:12 2009
@@ -18,3 +18,135 @@
 
 module(..., package.seeall)
 
+function new()
+end
+
+--
+-- asterisk table is created in astlib.c
+function asterisk:new()
+	local a = {
+		configs = {},
+	}
+	setmetatable(a, self)
+	self.__index = self
+	return a
+end
+
+function asterisk:__newindex(conffile_name, conffile)
+	if (getmetatable(conffile) ~= config) then
+		error("got " .. type(conffile) .. " expected type config")
+	end
+	self.configs[conffile_name] = conffile
+end
+
+function asterisk:__index(conffile_name)
+	return self.configs[conffile_name]
+end
+
+function asterisk:new_config(name)
+	c = config:new(name)
+	self[name] = c
+	return c
+end
+
+config = {}
+function config:new(name)
+	local ac = {
+		name = name,
+		sections = {},
+		section_index = {},
+	}
+	setmetatable(ac, self)
+	self.__index = self
+	return ac
+end
+
+function config:add_section(new_section)
+	if (getmetatable(new_section) ~= conf_section) then
+		error("got " .. type(new_section) .. " expected type conf_section")
+	end
+	table.insert(self.sections, new_section)
+	if not self.section_index[new_section.name] then
+		self.section_index[new_section.name] = #self.sections
+	end
+end
+
+function config:new_section(section_name)
+	s = conf_section:new(section_name)
+	self:add_section(s)
+	return s
+end
+
+function config:__index(section_name)
+	return self.sections[self.section_index[section_name]]
+end
+
+function config:write(filename)
+	if not filename then
+		filename = self.name
+	end
+
+	f, e = io.open(filename, "w")
+	if not f then
+		return error("error writing config file: " .. e)
+	end
+
+	for _, section in ipairs(self.sections) do
+		f:write("[" .. section.name .. "]")
+		if section.template then
+			f:write("(!")
+			for _, i in ipairs(section.inherit) do
+				f:write("," .. i)
+			end
+			f:write(")")
+		else
+			if #section.inherit ~= 0 then
+				f:write("(")
+				local first = true
+				for _, i in ipairs(section.inherit) do
+					if not first then
+						f:write(",")
+					else
+						first = false
+					end
+					f:write(i)
+				end
+				f:write(")")
+			end
+		end
+		f:write("\n")
+
+		for _, value in ipairs(section.values) do
+			f:write(tostring(value[1]) .. " = " .. tostring(value[2]) .. "\n")
+		end
+		f:write("\n")
+	end
+
+	f:close()
+end
+
+conf_section = {}
+function conf_section:new(name)
+	local s = {
+		name = name,
+		template = false,
+		inherit = {},
+		values = {},
+		value_index = {},
+	}
+	setmetatable(s, self)
+	self.__index = self
+	return s
+end
+
+function conf_section:__newindex(key, value)
+	table.insert(self.values, {key, value})
+	if not self.value_index[key] then
+		self.value_index[key] = #self.values
+	end
+end
+
+function conf_section:__index(key)
+	return self.values[self.value_index[key]]
+end
+

Added: team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua?view=auto&rev=175025
==============================================================================
--- team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua (added)
+++ team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua Wed Feb 11 21:04:12 2009
@@ -1,0 +1,89 @@
+-- test astlib
+
+-- generate what we expect to see
+expected_test_conf = [[
+[test]
+test = 1
+test = test
+value = this
+allow = true
+
+[section]
+value = yes
+setting = on
+ip = 10.10.10.10
+
+[section]
+value = no
+
+[template](!)
+value = default
+
+[inherit](template)
+value = default
+
+[inherit](template,test)
+value = default
+
+[inherit_template](!,template,test)
+value = default
+
+]]
+
+f = io.open("expected_test.conf", "w")
+f:write(expected_test_conf)
+f:close()
+
+
+-- generate a config
+c = ast.config:new("test.conf")
+s = c:new_section("test")
+s["test"] = 1
+s["test"] = "test"
+s["value"] = "this"
+s["allow"] = true
+
+s = c:new_section("section")
+s["value"] = "yes"
+s["setting"] = "on"
+s["ip"] = "10.10.10.10"
+
+s = c:new_section("section")
+s["value"] = "no"
+
+s = c:new_section("template")
+s.template = true
+s["value"] = "default"
+
+s = c:new_section("inherit")
+s.inherit = {"template"}
+s["value"] = "default"
+
+s = c:new_section("inherit")
+s.inherit = {"template", "test"}
+s["value"] = "default"
+
+s = c:new_section("inherit_template")
+s.template = true
+s.inherit = {"template", "test"}
+s["value"] = "default"
+
+c:write()
+
+f = io.open("test.conf")
+test_conf = f:read("*a")
+f:close()
+
+-- diff the two
+os.execute("diff -u expected_test.conf test.conf > diff")
+
+f = io.open("diff")
+diff = f:read("*a")
+f:close()
+
+-- cleanup
+os.execute("rm -f test.conf expected_test.conf diff")
+
+-- check if our two configs match
+fail_if(test_conf ~= expected_test_conf, "test_conf does not match expected_test_conf\n\n" .. diff)
+

Propchange: team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mnicholson/asttest/asttest/self-tests/test_astlib/test.lua
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list