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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Feb 5 00:22:26 CST 2009


Author: mnicholson
Date: Thu Feb  5 00:22:25 2009
New Revision: 173550

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=173550
Log:
The test library has been broken out into a psuedo lua module and lib/lua.c
has been reduced to loading that library.  asttest.lua has been renamed to
testlib.lua as well.  The Makefile and other affected files were updated
accordingly.

Added:
    team/mnicholson/asttest/asttest/include/asttest/lua/
    team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h   (with props)
    team/mnicholson/asttest/asttest/lua/testlib.c   (with props)
    team/mnicholson/asttest/asttest/lua/testlib.lua
      - copied, changed from r172776, team/mnicholson/asttest/asttest/lua/asttest.lua
Removed:
    team/mnicholson/asttest/asttest/lua/asttest.lua
Modified:
    team/mnicholson/asttest/asttest/Makefile
    team/mnicholson/asttest/asttest/asttest.c
    team/mnicholson/asttest/asttest/include/asttest/lua.h
    team/mnicholson/asttest/asttest/lib/lua.c

Modified: team/mnicholson/asttest/asttest/Makefile
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/Makefile?view=diff&rev=173550&r1=173549&r2=173550
==============================================================================
--- team/mnicholson/asttest/asttest/Makefile (original)
+++ team/mnicholson/asttest/asttest/Makefile Thu Feb  5 00:22:25 2009
@@ -23,6 +23,7 @@
 # Basic set of sources and flags/libraries/includes
 OBJS:=asttest.o lib/lua.o lib/testsuite.o
 CFLAGS:=-g -c -D_GNU_SOURCE -Wall -I/usr/include/lua5.1 -Iinclude -Ilua
+L_OBJS:=lua/testlib.o
 T_LIBS:=-llua5.1
 
 AST_INSTALL_DIR = $(PWD)/asterisk
@@ -59,18 +60,25 @@
 #nmenuselect:
 #endif
 
-%_lua.h : %.lua tools/mkstring
+lua/%_lua.h : lua/%.lua tools/mkstring
 	./tools/mkstring -n $(*F)_lua -o $@ $<
+
+lib/%.o: lib/%.c include/asttest/%.h
+
+# this line does not seem to work
+#lua/%.o: lua/%.c lua/%_lua.h include/asttest/lua/%.h
 
 asterisk:
 	cd ../ && ./configure --enable-dev-mode --prefix=$(AST_INSTALL_DIR) --localstatedir=$(AST_INSTALL_DIR)/var
 	$(MAKE) -C ../ install
 
-lib/lua.o: lib/lua.c include/asttest/lua.h include/asttest/testsuite.h lua/asttest_lua.h
+lib/lua.o: lib/lua.c include/asttest/lua.h include/asttest/testsuite.h include/asttest/lua/*.h
 lib/testsuite.o: lib/testsuite.c include/asttest/testsuite.h include/asttest/asttest.h
+lua/testlib.o: lua/testlib.c lua/testlib_lua.h include/asttest/lua/testlib.h
+#lua/astlib.o: lua/astlib.c lua/astlib_lua.h include/asttest/lua/astlib.h
 
-asttest: $(OBJS) $(T_OBJS) include/asttest/asttest.h
-	$(CC) -o $@ $^ $(T_LIBS)
+asttest: asttest.c $(OBJS) $(T_OBJS) $(L_OBJS) include/asttest/asttest.h
+	$(CC) -o $@ $(OBJS) $(L_OBJS) $(T_LIBS)
 
 tools/mkstring: tools/mkstring.c
 	$(CC) -D_GNU_SOURCE -Wall -o $@ $^
@@ -81,8 +89,8 @@
 	./asttest tests
 
 clean:
-	rm -f asttest $(OBJS) $(M_OBJS) $(C_OBJS) $(G_OBJS) $(N_OBJS)
-	rm -f lua/asttest_lua.h
+	rm -f asttest $(OBJS) $(M_OBJS) $(C_OBJS) $(G_OBJS) $(N_OBJS) $(L_OBJS)
+	rm -f lua/*.h
 	rm -f tools/mkstring
 	rm -rf $(AST_INSTALL_DIR)
 

Modified: team/mnicholson/asttest/asttest/asttest.c
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/asttest.c?view=diff&rev=173550&r1=173549&r2=173550
==============================================================================
--- team/mnicholson/asttest/asttest/asttest.c (original)
+++ team/mnicholson/asttest/asttest/asttest.c Thu Feb  5 00:22:25 2009
@@ -105,12 +105,12 @@
 				ts_log(ts, test_name, "%s\n", lua_tostring(L, reason_string));
 
 			if (result_equals(L, test_result, "pass")) {
-				if (lua_expected_fail(L))
+				if (testlib_expected_fail(L))
 					res = ts_xpass(ts, test_name);
 				else
 					res = ts_pass(ts, test_name);
 			} else if (result_equals(L, test_result, "fail")) {
-				if (lua_expected_fail(L))
+				if (testlib_expected_fail(L))
 					res = ts_xfail(ts, test_name);
 				else
 					res = ts_fail(ts, test_name);
@@ -211,7 +211,7 @@
 		result = process_test_result(ts, test_name, L);
 	} else {
 		/* we got no explicit result, consider it a pass or xpass*/
-		if (lua_expected_fail(L))
+		if (testlib_expected_fail(L))
 			result = ts_xpass(ts, test_name);
 		else
 			result = ts_pass(ts, test_name);

Modified: team/mnicholson/asttest/asttest/include/asttest/lua.h
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/include/asttest/lua.h?view=diff&rev=173550&r1=173549&r2=173550
==============================================================================
--- team/mnicholson/asttest/asttest/include/asttest/lua.h (original)
+++ team/mnicholson/asttest/asttest/include/asttest/lua.h Thu Feb  5 00:22:25 2009
@@ -23,7 +23,8 @@
 
 #include "asttest/testsuite.h"
 
-int lua_expected_fail(lua_State *L);
+#include "asttest/lua/testlib.h"
+
 lua_State *get_lua_state(struct testsuite *ts, const char *test_name);
 
 #endif

Added: team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h?view=auto&rev=173550
==============================================================================
--- team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h (added)
+++ team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h Thu Feb  5 00:22:25 2009
@@ -1,0 +1,27 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2008, Digium, Inc.
+ *
+ * Matthew Nichiolson <mnicholson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef ASTTEST_LUA_TESTLIB_H
+#define ASTTEST_LUA_TESTLIB_H
+
+#include <lua.h>
+
+int testlib_expected_fail(lua_State *L);
+int luaopen_testlib(lua_State *L);
+
+#endif

Propchange: team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mnicholson/asttest/asttest/include/asttest/lua/testlib.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/mnicholson/asttest/asttest/lib/lua.c
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lib/lua.c?view=diff&rev=173550&r1=173549&r2=173550
==============================================================================
--- team/mnicholson/asttest/asttest/lib/lua.c (original)
+++ team/mnicholson/asttest/asttest/lib/lua.c Thu Feb  5 00:22:25 2009
@@ -23,124 +23,40 @@
 #include "asttest/lua.h"
 #include "asttest/testsuite.h"
 
-#include "asttest_lua.h"
-
-/*
- * \brief Check if the current test was expected to fail or not.
- * \param L the lua state to use
- * \return whether the current test was expected to fail or not
- */
-int lua_expected_fail(lua_State *L) {
-	int res;
-	lua_getfield(L, LUA_REGISTRYINDEX, "asttest_xfail");
-	res = lua_toboolean(L, -1);
-	lua_pop(L, 1);
-	return res;
-}
-	
-
-/*
- * \brief Push the current testsuite on the stack and return a pointer to it.
- * \param L the lua state to use
- */
-struct testsuite *push_ts(lua_State *L) {
-	lua_getfield(L, LUA_REGISTRYINDEX, "asttest_ts");
-	return lua_touserdata(L, -1);
-}
-
-/*
- * \brief Push the current test's name on the stack and return a pointer to it.
- * \param L the lua state to use
- */
-const char *push_test_name(lua_State *L) {
-	lua_getfield(L, LUA_REGISTRYINDEX, "asttest_name");
-	return lua_tostring(L, -1);
-}
-
-/*
- * \brief [lua_CFunction] Log a message for the current test.
- * \param message [lua] the string to log
- */
-static int lua_ts_log(lua_State *L) {
-	const char *string = luaL_checkstring(L, 1);
-	struct testsuite *ts = push_ts(L);
-	const char *name = push_test_name(L);
-
-	ts_log(ts, name, "%s", string);
-
-	return 0;
-}
-
-/*
- * \brief [lua_CFunction] Notify the test driver that this test is expected to
- * fail.
- */
-static int lua_xfail(lua_State *L) {
-	lua_pushboolean(L, 1);
-	lua_setfield(L, LUA_REGISTRYINDEX, "asttest_xfail");
-	return 0;
-}
-
-/*
- * \brief Setup lua registry values.
- * \param ts the test suite
- * \param test_name the name of the current test
- */
-int setup_registry(lua_State *L, struct testsuite *ts, const char *test_name) {
-	lua_pushlightuserdata(L, ts);
-	lua_setfield(L, LUA_REGISTRYINDEX, "asttest_ts");
-
-	lua_pushstring(L, test_name);
-	lua_setfield(L, LUA_REGISTRYINDEX, "asttest_name");
-	
-	lua_pushboolean(L, 0);
-	lua_setfield(L, LUA_REGISTRYINDEX, "asttest_xfail");
-
-	return 0;
-}
-
-luaL_Reg global_functions[] = {
-	{"ts_log", lua_ts_log},
-	{"xfail", lua_xfail},
-	{NULL, NULL},
-};
-
-/*
- * \brief Setup lua global values.
- * \param test_name the name of the current test
- *
- * This function sets up global values such as the name of the current test and
- * global functions and objects that tests can use.
- */
-int setup_globals(lua_State *L, const char *test_name) {
-	lua_pushstring(L, test_name);
-	lua_setglobal(L, "name");
-
-	lua_pushvalue(L, LUA_GLOBALSINDEX);
-	luaL_register(L, NULL, global_functions);
-	lua_pop(L, 1);
-
-	return 0;
-}
+#include "asttest/lua/testlib.h"
 
 lua_State *get_lua_state(struct testsuite *ts, const char *test_name) {
 	lua_State *L = luaL_newstate();
 	if (!L) {
-		return NULL;
+		goto e_return;
 	}
 
 	luaL_openlibs(L);
 
-	setup_registry(L, ts, test_name);
-	setup_globals(L, test_name);
-
-	/* load the test library */
-	if (luaL_loadbuffer(L, asttest_lua, sizeof(asttest_lua), "asttest.lua") || lua_pcall(L, 0, LUA_MULTRET, 0)) {
-		ts_log(ts, test_name, "error loading test library: %s\n", lua_tostring(L, -1));
-		lua_close(L);
-		return NULL;
+	/* load the test lib */
+	lua_pushcfunction(L, luaopen_testlib);
+	lua_pushlightuserdata(L, ts);
+	lua_pushstring(L, test_name);
+	if (lua_pcall(L, 2, 0, 0)) {
+		goto e_print_error;
 	}
+#if 0
+	/* load the asterisk lib */
+	lua_pushcfunction(L, luaopen_astlib);
+	lua_pushstring(L, ts->asterisk_path);
+	if (lua_pcall(L, 1, 0, 0)) {
+		goto e_print_error;
+	}
+#endif
 
 	return L;
+
+e_print_error:
+	/* we expect an error string on the top of the stack */
+	ts_log(ts, test_name, "%s\n", lua_tostring(L, -1));
+/*e_close_lua:*/
+	lua_close(L);
+e_return:
+	return NULL;
 }
 

Added: team/mnicholson/asttest/asttest/lua/testlib.c
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lua/testlib.c?view=auto&rev=173550
==============================================================================
--- team/mnicholson/asttest/asttest/lua/testlib.c (added)
+++ team/mnicholson/asttest/asttest/lua/testlib.c Thu Feb  5 00:22:25 2009
@@ -1,0 +1,130 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2008, Digium, Inc.
+ *
+ * Matthew Nichiolson <mnicholson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+#include "asttest/testsuite.h"
+
+#include "testlib_lua.h"
+
+/*
+ * \brief Check if the current test was expected to fail or not.
+ * \param L the lua state to use
+ * \note This function expects testlib to be loaded, which should always be the
+ * case by the time it is called.
+ * \return whether the current test was expected to fail or not
+ */
+int testlib_expected_fail(lua_State *L) {
+	int res;
+	lua_getfield(L, LUA_REGISTRYINDEX, "testlib_xfail");
+	res = lua_toboolean(L, -1);
+	lua_pop(L, 1);
+	return res;
+}
+
+/*
+ * \brief Push the current testsuite on the stack and return a pointer to it.
+ * \param L the lua state to use
+ */
+static struct testsuite *push_ts(lua_State *L) {
+	lua_getfield(L, LUA_REGISTRYINDEX, "testlib_ts");
+	return lua_touserdata(L, -1);
+}
+
+/*
+ * \brief Push the current test's name on the stack and return a pointer to it.
+ * \param L the lua state to use
+ */
+static const char *push_test_name(lua_State *L) {
+	lua_getfield(L, LUA_REGISTRYINDEX, "testlib_name");
+	return lua_tostring(L, -1);
+}
+
+/*
+ * \brief [lua_CFunction] Log a message for the current test.
+ * \param message [lua] the string to log
+ */
+static int lua_ts_log(lua_State *L) {
+	const char *string = luaL_checkstring(L, 1);
+	struct testsuite *ts = push_ts(L);
+	const char *name = push_test_name(L);
+
+	ts_log(ts, name, "%s", string);
+
+	return 0;
+}
+
+/*
+ * \brief [lua_CFunction] Notify the test driver that this test is expected to
+ * fail.
+ */
+static int lua_xfail(lua_State *L) {
+	lua_pushboolean(L, 1);
+	lua_setfield(L, LUA_REGISTRYINDEX, "testlib_xfail");
+	return 0;
+}
+
+static luaL_Reg testlib[] = {
+	{"log", lua_ts_log},
+	{"xfail", lua_xfail},
+	{NULL, NULL},
+};
+
+int luaopen_testlib(lua_State *L) {
+	const char *test_name;
+	struct testsuite *ts;
+
+	luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
+	ts = lua_touserdata(L, 1);
+	test_name = luaL_checkstring(L, 2);
+
+	/* set up some registry values */
+	lua_pushlightuserdata(L, ts);
+	lua_setfield(L, LUA_REGISTRYINDEX, "testlib_ts");
+
+	lua_pushstring(L, test_name);
+	lua_setfield(L, LUA_REGISTRYINDEX, "testlib_name");
+	
+	lua_pushboolean(L, 0);
+	lua_setfield(L, LUA_REGISTRYINDEX, "testlib_xfail");
+
+	/* register our functions */
+	luaL_register(L, "test", testlib);
+	lua_pushstring(L, test_name);
+	lua_setfield(L, -2, "name");
+	lua_pop(L, 1);
+	
+	/* load the lua portion of the lib */
+	if (luaL_loadbuffer(L, testlib_lua, sizeof(testlib_lua), "testlib"))
+		goto e_lua_error;
+	lua_pushstring(L, "test");
+	if (lua_pcall(L, 1, 1, 0))
+		goto e_lua_error;
+
+	return 1;
+
+e_lua_error:
+	/* format the error message a little */
+	lua_pushstring(L, "error loading test library: ");
+	lua_insert(L, -2);
+	lua_concat(L, 2);
+	return lua_error(L);
+}
+

Propchange: team/mnicholson/asttest/asttest/lua/testlib.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mnicholson/asttest/asttest/lua/testlib.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mnicholson/asttest/asttest/lua/testlib.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: team/mnicholson/asttest/asttest/lua/testlib.lua (from r172776, team/mnicholson/asttest/asttest/lua/asttest.lua)
URL: http://svn.digium.com/svn-view/asterisk/team/mnicholson/asttest/asttest/lua/testlib.lua?view=diff&rev=173550&p1=team/mnicholson/asttest/asttest/lua/asttest.lua&r1=172776&p2=team/mnicholson/asttest/asttest/lua/testlib.lua&r2=173550
==============================================================================
--- team/mnicholson/asttest/asttest/lua/asttest.lua (original)
+++ team/mnicholson/asttest/asttest/lua/testlib.lua Thu Feb  5 00:22:25 2009
@@ -16,25 +16,28 @@
 -- at the top of the source tree.
 --
 
+module(..., package.seeall)
+
 -- 
 -- replacements for global functions
 --
 
 -- print to the test log instead of stdout
-function print(...)
-	local string = ""
+function _G.print(...)
+	local msg = ""
 	for i, v in ipairs(arg) do
 		if i == 1 then
-			string = string .. tostring(v)
+			msg = msg .. tostring(v)
 		else
-			string = string .. "\t" .. tostring(v)
+			msg = msg .. "\t" .. tostring(v)
 		end
 	end
-	string = string .. "\n"
-	ts_log(string)
+	msg = msg .. "\n"
+	log(msg)
 end
 
-lua_error = error
+_G.lua_error = _G.error
+_G.xfail = xfail
 
 --
 -- basic pass/fail/skip/error functions
@@ -44,18 +47,22 @@
 function pass(reason)
 	return lua_error{result = "pass", reason = reason}
 end
+_G.pass = pass
 
 function fail(reason)
 	return lua_error{result = "fail", reason = reason}
 end
+_G.fail = fail
 
 function skip(reason)
 	return lua_error{result = "skip", reason = reason}
 end
+_G.skip = skip
 
 function error(reason)
 	return lua_error{result = "error", reason = reason}
 end
+_G.error = error
 
 --
 -- utility functions
@@ -67,9 +74,11 @@
 		return fail(message)
 	end
 end
+_G.fail_if = fail_if
 
 -- fail if condition is false using message as the reason
 function check(condition, message)
 	fail_if(not condition, message)
 end
+_G.check = check
 




More information about the asterisk-commits mailing list