From 76c4c2b085def1d42d308d93b883653b86e1e1ac Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Sat, 1 Jan 2011 18:53:27 -0800 Subject: [PATCH] Python unit tests --- moo/medit-app/data/lua/munit.lua | 24 ++++++------- moo/medit-app/data/munit.py | 14 ++++++++ moo/medit-app/data/testmoo.py | 20 +++++++++++ moo/medit-app/run-tests.h | 5 +++ moo/moolua/moo-lua-api.h | 1 + moo/moolua/moo-tests-lua.h | 24 ------------- moo/moopython/Makefile.incl | 4 ++- moo/moopython/medit-python.c | 54 +++++++++++++++++++++-------- moo/moopython/moopython-loader.c | 59 +++----------------------------- moo/moopython/moopython-tests.c | 46 +++++++++++++++++++++++++ moo/moopython/moopython-tests.h | 12 +++++++ moo/moopython/moopython-utils.c | 56 ++++++++++++++++++++++++++++++ moo/moopython/moopython-utils.h | 2 ++ moo/moopython/pygtk/moo.override | 1 + moo/mooutils/moo-test-utils.c | 23 +++++++++++++ moo/mooutils/moo-test-utils.h | 1 + 16 files changed, 240 insertions(+), 106 deletions(-) create mode 100644 moo/medit-app/data/munit.py create mode 100644 moo/medit-app/data/testmoo.py create mode 100644 moo/moopython/moopython-tests.c create mode 100644 moo/moopython/moopython-tests.h diff --git a/moo/medit-app/data/lua/munit.lua b/moo/medit-app/data/lua/munit.lua index aa3e6e94..7a2eb671 100644 --- a/moo/medit-app/data/lua/munit.lua +++ b/moo/medit-app/data/lua/munit.lua @@ -1,13 +1,17 @@ -local test_ret = {} - -local function getline() - return debug.getinfo(4, "l").currentline -end +local medit = require("medit") local function _tassert(cond, msg, ...) - table.insert(test_ret, string.format(msg or '', ...)) - table.insert(test_ret, not not cond) - table.insert(test_ret, getline()) + local info = debug.getinfo(3, "Slf") + if not cond then + local message = string.format(msg or '', ...) + local name = info.name + if name then + message = string.format('in function %s: %s', name, message) + end + medit.test_assert_impl(false, message, info.short_src, info.currentline) + else + medit.test_assert_impl(true, '', info.short_src, info.currentline) + end end function tassert(cond, msg, ...) @@ -57,7 +61,3 @@ function tassert_eq(actual, exp, msg) _tassert(cmp(actual, exp), "%sexpected %s, got %s", msg, pr(exp), pr(actual)) end - -munit_report = function() - return unpack(test_ret) -end diff --git a/moo/medit-app/data/munit.py b/moo/medit-app/data/munit.py new file mode 100644 index 00000000..1e653dca --- /dev/null +++ b/moo/medit-app/data/munit.py @@ -0,0 +1,14 @@ +import moo +import traceback + +def _tassert(cond, msg): + st = traceback.extract_stack() + loc = st[len(st) - 3] + if not msg: + msg = "`%s'" % loc[3] + if loc[2] != '': + msg = 'in function %s: %s' % (loc[2], msg) + moo.test_assert_impl(bool(cond), msg, loc[0], loc[1]) + +def tassert(cond, msg=None): + _tassert(cond, msg) diff --git a/moo/medit-app/data/testmoo.py b/moo/medit-app/data/testmoo.py new file mode 100644 index 00000000..fc09b8ec --- /dev/null +++ b/moo/medit-app/data/testmoo.py @@ -0,0 +1,20 @@ +from munit import * +import moo + +editor = moo.editor_instance() +tassert(editor is not None) +tassert(isinstance(editor, moo.Editor)) +window = editor.get_active_window() +tassert(window is not None) +tassert(isinstance(window, moo.EditWindow)) +doc = window.get_active_doc() +tassert(doc is not None) +tassert(isinstance(doc, moo.Edit)) +tassert(doc == editor.get_active_doc()) +tassert(doc.get_window() == window) +view = doc.get_view() +tassert(view is not None) +tassert(isinstance(view, moo.EditView)) +tassert(isinstance(view, moo.TextView)) +tassert(view is window.get_active_view()) +tassert(view is editor.get_active_view()) diff --git a/moo/medit-app/run-tests.h b/moo/medit-app/run-tests.h index b60537be..e3e75971 100644 --- a/moo/medit-app/run-tests.h +++ b/moo/medit-app/run-tests.h @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -21,6 +22,10 @@ add_tests (void) moo_test_lua (); +#ifdef MOO_ENABLE_PYTHON + moo_test_python (); +#endif + // moo_test_key_file (); moo_test_editor (); } diff --git a/moo/moolua/moo-lua-api.h b/moo/moolua/moo-lua-api.h index 75e36ce5..004b7788 100644 --- a/moo/moolua/moo-lua-api.h +++ b/moo/moolua/moo-lua-api.h @@ -21,6 +21,7 @@ #include "mooutils/moonotebook.h" #include "mooutils/mooundo.h" #include "mooutils/mooutils-script.h" +#include "mooutils/moo-test-utils.h" #include "moofileview/moofileview.h" diff --git a/moo/moolua/moo-tests-lua.h b/moo/moolua/moo-tests-lua.h index df682019..d912ac9f 100644 --- a/moo/moolua/moo-tests-lua.h +++ b/moo/moolua/moo-tests-lua.h @@ -30,7 +30,6 @@ moo_test_run_lua_script (lua_State *L, const char *filename) { int ret; - int i; if (lua_gettop (L) != 0) { @@ -70,29 +69,6 @@ moo_test_run_lua_script (lua_State *L, return; } - - luaL_loadstring (L, "return munit_report()"); - if ((ret = lua_pcall (L, 0, LUA_MULTRET, 0)) != 0) - g_error ("%s: fix me!", G_STRFUNC); - - for (i = 1; i+2 <= lua_gettop (L); i += 3) - { - if (!lua_isstring (L, i) || !lua_isboolean (L, i+1) || !lua_isnumber (L, i+2)) - { - TEST_FAILED_MSG ("script `%s' returned wrong value!", filename); - } - else - { - const char *msg = lua_tostring (L, i); - gboolean success = lua_toboolean (L, i+1); - int line = lua_tointeger (L, i+2); - moo_test_assert_msg (success, filename, line, "%s", msg); - } - } - - if (i != lua_gettop (L) + 1) - TEST_FAILED_MSG ("script `%s' returned wrong number of values (%d)", - filename, lua_gettop (L)); } static void diff --git a/moo/moopython/Makefile.incl b/moo/moopython/Makefile.incl index 48b0879f..cdcd4def 100644 --- a/moo/moopython/Makefile.incl +++ b/moo/moopython/Makefile.incl @@ -12,7 +12,9 @@ moo_python_sources += \ moopython/moopython-loader.h \ moopython/moopython-loader.c \ moopython/moopython-utils.h \ - moopython/moopython-utils.c + moopython/moopython-utils.c \ + moopython/moopython-tests.h \ + moopython/moopython-tests.c moo_sources += \ moopython/medit-python.h \ diff --git a/moo/moopython/medit-python.c b/moo/moopython/medit-python.c index 7b208662..0865a9fb 100644 --- a/moo/moopython/medit-python.c +++ b/moo/moopython/medit-python.c @@ -37,10 +37,12 @@ create_script_dict (const char *name) static PyObject * run_string (const char *str, + const char *filename, PyObject *globals, PyObject *locals) { PyObject *ret; + PyObject *code; g_return_val_if_fail (str != NULL, NULL); @@ -49,13 +51,18 @@ run_string (const char *str, else Py_INCREF ((PyObject*) locals); - g_return_val_if_fail (locals != NULL, NULL); - if (!globals) globals = locals; - ret = PyRun_String (str, Py_file_input, globals, locals); + g_return_val_if_fail (locals != NULL, NULL); + g_return_val_if_fail (globals != NULL, NULL); + code = Py_CompileString (str, filename ? filename : "