|
From: Kouhei S. <nul...@cl...> - 2014-01-19 15:13:32
|
Kouhei Sutou 2014-01-20 00:13:03 +0900 (Mon, 20 Jan 2014) New Revision: 5f0dc33980afd1d22700d00bd68aff58b25805c0 https://github.com/clear-code/cutter/commit/5f0dc33980afd1d22700d00bd68aff58b25805c0 Message: Handle unhandled C++ exception in test GitHub: fix #8 I use loader-customizer new module mechanism instead of changing cut-test.c. Because I want to avoid that libcutter depends on C++. TODO: Iterated test isn't supported yet. Suggested by Kazuhiro Yamato. Patch by Kazuhiro Yamato. Thanks!!! Added files: cppcutter/cppcut-test.cpp cppcutter/cppcut-test.h module/loader-customizer/Makefile.am module/loader-customizer/cut-exception-loader-customizer-factory.c module/loader-customizer/cut-exception-loader-customizer.c test/cppcutter/test-cppcut-test.cpp Modified files: configure.ac cppcutter/Makefile.am cutter/cut-loader.c cutter/cut-loader.h module/Makefile.am test/cppcutter/Makefile.am Modified: configure.ac (+1 -0) =================================================================== --- configure.ac 2014-01-19 23:41:38 +0900 (0cc44ff) +++ configure.ac 2014-01-20 00:13:03 +0900 (b2669fb) @@ -959,6 +959,7 @@ AC_CONFIG_FILES([Makefile module/ui/Makefile module/report/Makefile module/stream/Makefile + module/loader-customizer/Makefile gst-plugins/Makefile gst-plugins/test/Makefile data/Makefile Modified: cppcutter/Makefile.am (+6 -4) =================================================================== --- cppcutter/Makefile.am 2014-01-19 23:41:38 +0900 (716036d) +++ cppcutter/Makefile.am 2014-01-20 00:13:03 +0900 (dede871) @@ -16,7 +16,8 @@ cppcutter_public_headers = \ cppcut-assertions.h \ cppcut-assertions-helper.h \ cppcut-macros.h \ - cppcut-message.h + cppcut-message.h \ + cppcut-test.h pkginclude_HEADERS = \ cppcutter.h @@ -25,9 +26,10 @@ cppcutter_includedir=$(pkgincludedir)/cppcutter cppcutter_include_HEADERS = \ $(cppcutter_public_headers) -libcppcutter_sources = \ - cppcut-assertions-helper.cpp \ - cppcut-message.cpp +libcppcutter_sources = \ + cppcut-assertions-helper.cpp \ + cppcut-message.cpp \ + cppcut-test.cpp libcppcutter_la_SOURCES = \ $(cppcutter_public_headers) \ Added: cppcutter/cppcut-test.cpp (+93 -0) 100644 =================================================================== --- /dev/null +++ cppcutter/cppcut-test.cpp 2014-01-20 00:13:03 +0900 (866d685) @@ -0,0 +1,93 @@ +/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2014 Kouhei Sutou <ko...@cl...> + * Copyright (C) 2014 Kazuhiro Yamato <kz...@gm...> + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <exception> +#include <typeinfo> + +#include "cppcut-test.h" + +#include <cutter.h> + +G_BEGIN_DECLS + +G_DEFINE_TYPE(CppCutTest, cppcut_test, CUT_TYPE_TEST) + +static void invoke (CutTest *test, + CutTestContext *test_context, + CutRunContext *run_context); + +static void +cppcut_test_class_init (CppCutTestClass *klass) +{ + CutTestClass *cut_test_class; + + cut_test_class = CUT_TEST_CLASS(klass); + + cut_test_class->invoke = invoke; +} + +static void +cppcut_test_init (CppCutTest *test) +{ +} + +CppCutTest * +cppcut_test_new (const gchar *name, CutTestFunction function) +{ + gpointer object; + + object = g_object_new(CPPCUT_TYPE_TEST, + "element-name", "test", + "name", name, + "test-function", function, + NULL); + return CPPCUT_TEST(object); +} + +static void +invoke (CutTest *test, CutTestContext *test_context, CutRunContext *run_context) +{ + CutTestClass *cut_test_class = CUT_TEST_CLASS(cppcut_test_parent_class); + + try { + cut_test_class->invoke(test, test_context, run_context); + } catch (const std::exception &exception) { + const gchar *message; + message = cut_take_printf("Unhandled C++ standard exception is thrown: " + "<%s>: %s", + typeid(exception).name(), + exception.what()); + cut_test_terminate(ERROR, message); + } catch (...) { + const gchar *message; + message = "Unhandled C++ non-standard exception is thrown"; + cut_test_terminate(ERROR, message); + } +} + +G_END_DECLS + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/ Added: cppcutter/cppcut-test.h (+81 -0) 100644 =================================================================== --- /dev/null +++ cppcutter/cppcut-test.h 2014-01-20 00:13:03 +0900 (41a1af9) @@ -0,0 +1,81 @@ +/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2014 Kouhei Sutou <ko...@cl...> + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifndef __CPPCUT_TEST_H__ +#define __CPPCUT_TEST_H__ + +#include <glib-object.h> + +#include <cutter/cut-test.h> + +/** + * SECTION: cppcut-test + * @title: Test class for C++. + * @short_description: Adds C++ exception support to CutTest. + * + */ + +G_BEGIN_DECLS + +#define CPPCUT_TYPE_TEST \ + (cppcut_test_get_type()) +#define CPPCUT_TEST(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + CPPCUT_TYPE_TEST, \ + CppCutTest)) +#define CPPCUT_TEST_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + CPPCUT_TYPE_TEST, \ + CppCutTestClass)) +#define CPPCUT_IS_TEST(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + CPPCUT_TYPE_TEST)) +#define CPPCUT_IS_TEST_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + CPPCUT_TYPE_TEST)) +#define CPPCUT_TEST_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + CPPCUT_TYPE_TEST, \ + CppCutTestClass)) + +typedef struct _CppCutTest CppCutTest; +typedef struct _CppCutTestClass CppCutTestClass; + +struct _CppCutTest +{ + CutTest object; +}; + +struct _CppCutTestClass +{ + CutTestClass parent_class; +}; + +GType cppcut_test_get_type (void) G_GNUC_CONST; + +CppCutTest *cppcut_test_new (const gchar *name, + CutTestFunction function); + +G_END_DECLS + +#endif /* __CPPCUT_TEST_H__ */ + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/ Modified: cutter/cut-loader.c (+31 -2) =================================================================== --- cutter/cut-loader.c 2014-01-19 23:41:38 +0900 (2f6149f) +++ cutter/cut-loader.c 2014-01-20 00:13:03 +0900 (bab89b5) @@ -1,6 +1,6 @@ /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* - * Copyright (C) 2007-2013 Kouhei Sutou <ko...@cl...> + * Copyright (C) 2007-2014 Kouhei Sutou <ko...@cl...> * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -80,6 +80,8 @@ struct _CutLoaderPrivate gboolean keep_opening; gboolean enable_convenience_attribute_definition; gchar *base_directory; + CutCreateTestFunction create_test_function; + gpointer create_test_function_user_data; }; enum @@ -179,6 +181,8 @@ cut_loader_init (CutLoader *loader) priv->keep_opening = FALSE; priv->enable_convenience_attribute_definition = FALSE; priv->base_directory = NULL; + priv->create_test_function = NULL; + priv->create_test_function_user_data = NULL; } static void @@ -325,6 +329,18 @@ cut_loader_set_base_directory (CutLoader *loader, const gchar *base_directory) priv->base_directory = g_strdup(base_directory); } +void +cut_loader_set_create_test_function (CutLoader *loader, + CutCreateTestFunction create_test_function, + gpointer user_data) +{ + CutLoaderPrivate *priv; + + priv = CUT_LOADER_GET_PRIVATE(loader); + priv->create_test_function = create_test_function; + priv->create_test_function_user_data = user_data; +} + static inline const gchar * skip_cpp_namespace_gcc (const gchar *name, GString *namespaces) { @@ -1017,6 +1033,19 @@ apply_attributes (CutLoaderPrivate *priv, CutTest *test, SymbolNames *names) } } +static CutTest * +create_test (CutLoaderPrivate *priv, + const gchar *name, + CutTestFunction test_function) +{ + if (priv->create_test_function) { + return priv->create_test_function(name, test_function, + priv->create_test_function_user_data); + } else { + return cut_test_new(name, test_function); + } +} + static void register_valid_test (CutLoader *loader, CutTestCase *test_case, SymbolNames *names) @@ -1054,7 +1083,7 @@ register_valid_test (CutLoader *loader, CutTestCase *test_case, data_setup_function); test = CUT_TEST(test_iterator); } else { - test = cut_test_new(names->test_name, test_function); + test = create_test(priv, names->test_name, test_function); } cut_test_set_base_directory(test, priv->base_directory); Modified: cutter/cut-loader.h (+8 -1) =================================================================== --- cutter/cut-loader.h 2014-01-19 23:41:38 +0900 (df35a4c) +++ cutter/cut-loader.h 2014-01-20 00:13:03 +0900 (76ffc4d) @@ -1,6 +1,6 @@ /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* - * Copyright (C) 2007 Kouhei Sutou <ko...@co...> + * Copyright (C) 2007-2014 Kouhei Sutou <ko...@cl...> * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -38,6 +38,9 @@ G_BEGIN_DECLS typedef struct _CutLoader CutLoader; typedef struct _CutLoaderClass CutLoaderClass; +typedef CutTest *(*CutCreateTestFunction) (const gchar *name, + CutTestFunction function, + gpointer user_data); struct _CutLoader { GObject object; @@ -62,6 +65,10 @@ void cut_loader_set_enable_convenience_attribute_definition const gchar *cut_loader_get_base_directory(CutLoader *loader); void cut_loader_set_base_directory(CutLoader *loader, const gchar *base_directory); +void cut_loader_set_create_test_function + (CutLoader *loader, + CutCreateTestFunction create_test_function, + gpointer user_data); GList *cut_loader_load_test_cases (CutLoader *loader); CutTestCase *cut_loader_load_test_case (CutLoader *loader); CutTestSuite *cut_loader_load_test_suite (CutLoader *loader); Modified: module/Makefile.am (+5 -1) =================================================================== --- module/Makefile.am 2014-01-19 23:41:38 +0900 (39b9c0c) +++ module/Makefile.am 2014-01-20 00:13:03 +0900 (5d85b90) @@ -1 +1,5 @@ -SUBDIRS = ui report stream +SUBDIRS = \ + ui \ + report \ + stream \ + loader-customizer Added: module/loader-customizer/Makefile.am (+32 -0) 100644 =================================================================== --- /dev/null +++ module/loader-customizer/Makefile.am 2014-01-20 00:13:03 +0900 (575e9ea) @@ -0,0 +1,32 @@ +AM_CPPFLAGS = \ + -I$(top_builddir) \ + -I$(top_srcdir) + +AM_CFLAGS = \ + $(GLIB_CFLAGS) \ + $(CUTTER_CFLAGS) \ + $(CPPCUTTER_CFLAGS) \ + $(COVERAGE_CFLAGS) + +AM_LDFLAGS = \ + -avoid-version \ + -module \ + -no-undefined \ + -export-dynamic \ + $(LIBTOOL_EXPORT_OPTIONS) + +LIBS = \ + $(GLIB_LIBS) \ + $(top_builddir)/cutter/libcutter.la \ + $(top_builddir)/cppcutter/libcppcutter.la + +loader_customizer_module_LTLIBRARIES = \ + exception.la +loader_customizer_factory_module_LTLIBRARIES = \ + exception-factory.la + +exception_la_SOURCES = \ + cut-exception-loader-customizer.c + +exception_factory_la_SOURCES = \ + cut-exception-loader-customizer-factory.c Added: module/loader-customizer/cut-exception-loader-customizer-factory.c (+145 -0) 100644 =================================================================== --- /dev/null +++ module/loader-customizer/cut-exception-loader-customizer-factory.c 2014-01-20 00:13:03 +0900 (834062b) @@ -0,0 +1,145 @@ +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2014 Kouhei Sutou <ko...@cl...> + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif /* HAVE_CONFIG_H */ + +#include <glib.h> +#include <glib/gi18n-lib.h> +#include <gmodule.h> + +#include <cutter/cut-module-impl.h> +#include <cutter/cut-module-factory.h> +#include <cutter/cut-loader-customizer.h> + +#define CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY \ + cut_type_exception_loader_customizer_factory +#define CUT_EXCEPTION_LOADER_CUSTOMIZER_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY, \ + CutExceptionLoaderCustomizerFactory)) +#define CUT_EXCEPTION_LOADER_CUSTOMIZER_FACTORY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY, \ + CutExceptionLoaderCustomizerFactoryClass)) +#define CUT_IS_EXCEPTION_LOADER_CUSTOMIZER_FACTORY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY)) +#define CUT_IS_EXCEPTION_LOADER_CUSTOMIZER_FACTORY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY)) +#define CUT_EXCEPTION_LOADER_CUSTOMIZER_FACTORY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY, \ + CutExceptionLoaderCustomizerFactoryClass)) + +typedef struct _CutExceptionLoaderCustomizerFactory CutExceptionLoaderCustomizerFactory; +typedef struct _CutExceptionLoaderCustomizerFactoryClass CutExceptionLoaderCustomizerFactoryClass; + +struct _CutExceptionLoaderCustomizerFactory +{ + CutModuleFactory object; +}; + +struct _CutExceptionLoaderCustomizerFactoryClass +{ + CutModuleFactoryClass parent_class; +}; + +static GType cut_type_exception_loader_customizer_factory = 0; +static CutModuleFactoryClass *parent_class; + +static GObject *create (CutModuleFactory *factory); + +static void +class_init (CutModuleFactoryClass *klass) +{ + CutModuleFactoryClass *factory_class; + + parent_class = g_type_class_peek_parent(klass); + + factory_class = CUT_MODULE_FACTORY_CLASS(klass); + + factory_class->create = create; +} + +static void +init (CutExceptionLoaderCustomizerFactory *console) +{ +} + +static void +register_type (GTypeModule *type_module) +{ + static const GTypeInfo info = { + sizeof(CutExceptionLoaderCustomizerFactoryClass), + (GBaseInitFunc)NULL, + (GBaseFinalizeFunc)NULL, + (GClassInitFunc)class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof(CutExceptionLoaderCustomizerFactory), + 0, + (GInstanceInitFunc)init, + }; + + cut_type_exception_loader_customizer_factory = + g_type_module_register_type(type_module, + CUT_TYPE_MODULE_FACTORY, + "CutExceptionLoaderCustomizerFactory", + &info, 0); +} + +G_MODULE_EXPORT GList * +CUT_MODULE_IMPL_INIT (GTypeModule *type_module) +{ + GList *registered_types = NULL; + + register_type(type_module); + if (cut_type_exception_loader_customizer_factory) { + registered_types = + g_list_prepend(registered_types, + (gchar *)g_type_name(cut_type_exception_loader_customizer_factory)); + } + + return registered_types; +} + +G_MODULE_EXPORT void +CUT_MODULE_IMPL_EXIT (void) +{ +} + +G_MODULE_EXPORT GObject * +CUT_MODULE_IMPL_INSTANTIATE (const gchar *first_property, va_list var_args) +{ + return g_object_new_valist(CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER_FACTORY, first_property, var_args); +} + +GObject * +create (CutModuleFactory *factory) +{ + return G_OBJECT(cut_loader_customizer_new("exception", + NULL)); +} + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/ Added: module/loader-customizer/cut-exception-loader-customizer.c (+154 -0) 100644 =================================================================== --- /dev/null +++ module/loader-customizer/cut-exception-loader-customizer.c 2014-01-20 00:13:03 +0900 (3e9eabd) @@ -0,0 +1,154 @@ +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2014 Kouhei Sutou <ko...@cl...> + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif /* HAVE_CONFIG_H */ + +#include <glib.h> +#include <gmodule.h> + +#include <cutter/cut-module-impl.h> +#include <cutter/cut-loader-customizer.h> + +#include <cppcutter/cppcut-test.h> +#include <cppcutter/cppcut-iterated-test.h> + +#define CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER \ + cut_type_exception_loader_customizer +#define CUT_EXCEPTION_LOADER_CUSTOMIZER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER, \ + CutExceptionLoaderCustomizer)) +#define CUT_EXCEPTION_LOADER_CUSTOMIZER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER, \ + CutExceptionLoaderCustomizerClass)) +#define CUT_IS_EXCEPTION_LOADER_CUSTOMIZER(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER)) +#define CUT_IS_EXCEPTION_LOADER_CUSTOMIZER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER)) +#define CUT_EXCEPTION_LOADER_CUSTOMIZER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), \ + CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER, \ + CutExceptionLoaderCustomizerClass)) + +typedef struct _CutExceptionLoaderCustomizer CutExceptionLoaderCustomizer; +typedef struct _CutExceptionLoaderCustomizerClass CutExceptionLoaderCustomizerClass; + +struct _CutExceptionLoaderCustomizer +{ + CutLoaderCustomizer parent; +}; + +struct _CutExceptionLoaderCustomizerClass +{ + CutLoaderCustomizerClass parent_class; +}; + +static GType cut_type_exception_loader_customizer = 0; +static GObjectClass *parent_class; + +static void customize (CutLoaderCustomizer *customizer, + CutLoader *loader); + +static void +class_init (CutExceptionLoaderCustomizerClass *klass) +{ + CutLoaderCustomizerClass *loader_customizer_class; + + parent_class = g_type_class_peek_parent(klass); + + loader_customizer_class = CUT_LOADER_CUSTOMIZER_CLASS(klass); + loader_customizer_class->customize = customize; +} + +static void +init (CutExceptionLoaderCustomizer *customizer) +{ +} + +static void +register_type (GTypeModule *type_module) +{ + static const GTypeInfo info = { + sizeof(CutExceptionLoaderCustomizerClass), + (GBaseInitFunc)NULL, + (GBaseFinalizeFunc)NULL, + (GClassInitFunc)class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof(CutExceptionLoaderCustomizer), + 0, + (GInstanceInitFunc)init, + }; + + cut_type_exception_loader_customizer = + g_type_module_register_type(type_module, + CUT_TYPE_LOADER_CUSTOMIZER, + "CutExceptionLoaderCustomizer", + &info, 0); +} + +G_MODULE_EXPORT GList * +CUT_MODULE_IMPL_INIT (GTypeModule *type_module) +{ + GList *registered_types = NULL; + + register_type(type_module); + if (cut_type_exception_loader_customizer) { + registered_types = + g_list_prepend(registered_types, + (gchar *)g_type_name(cut_type_exception_loader_customizer)); + } + + return registered_types; +} + +G_MODULE_EXPORT void +CUT_MODULE_IMPL_EXIT (void) +{ +} + +G_MODULE_EXPORT GObject * +CUT_MODULE_IMPL_INSTANTIATE (const gchar *first_property, va_list var_args) +{ + return g_object_new_valist(CUT_TYPE_EXCEPTION_LOADER_CUSTOMIZER, + first_property, var_args); +} + +static CutTest * +create_test (const gchar *name, + CutTestFunction function, + gpointer user_data) +{ + return CUT_TEST(cppcut_test_new(name, function)); +} + +static void +customize (CutLoaderCustomizer *customizer, CutLoader *loader) +{ + cut_loader_set_create_test_function(loader, create_test, NULL); +} + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4 +*/ Modified: test/cppcutter/Makefile.am (+6 -4) =================================================================== --- test/cppcutter/Makefile.am 2014-01-19 23:41:38 +0900 (8376641) +++ test/cppcutter/Makefile.am 2014-01-20 00:13:03 +0900 (498f6cf) @@ -1,4 +1,4 @@ -AM_CPPFLAGS = \ +AM_CPPFLAGS = \ -I$(top_builddir) \ -I$(top_srcdir) \ -I$(top_srcdir)/cutter \ @@ -10,8 +10,8 @@ AM_CXXFLAGS = $(CUTTER_CFLAGS) CLEANFILES = *.gcno *.gcda -check_LTLIBRARIES = \ - test-cppcut-assertions.la +check_LTLIBRARIES = \ + test-cppcut.la AM_LDFLAGS = \ -module \ @@ -25,4 +25,6 @@ LIBS = \ $(top_builddir)/test/lib/libcuttest-utils.la \ $(GLIB_LIBS) -test_cppcut_assertions_la_SOURCES = test-cppcut-assertions.cpp +test_cppcut_la_SOURCES = \ + test-cppcut-assertions.cpp \ + test-cppcut-test.cpp Added: test/cppcutter/test-cppcut-test.cpp (+164 -0) 100644 =================================================================== --- /dev/null +++ test/cppcutter/test-cppcut-test.cpp 2014-01-20 00:13:03 +0900 (22a984d) @@ -0,0 +1,164 @@ +/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * Copyright (C) 2014 Kouhei Sutou <ko...@cl...> + * Copyright (C) 2014 Kazuhiro Yamato <kz...@gm...> + * + * This library is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This library 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <typeinfo> + +#include <cppcutter.h> + +#include <cppcutter/cppcut-test.h> +#include <cutter/cut-test-runner.h> + +#include "../lib/cuttest-assertions.h" + +#define MARK_FAIL(assertion) do \ +{ \ + fail_line = __LINE__; \ + assertion; \ +} while (0) + +#define FAIL_LOCATION (cut_take_printf("%s:%d", __FILE__, fail_line)) + +namespace cppcut_test +{ + CppCutTest *test; + CutRunContext *run_context; + CutTestContext *test_context; + CutTestResult *test_result; + + gint fail_line; + + static gboolean + run (void) + { + gboolean success; + + run_context = CUT_RUN_CONTEXT(cut_test_runner_new()); + + test_context = cut_test_context_new(run_context, NULL, NULL, NULL, + CUT_TEST(test)); + cut_test_context_current_push(test_context); + success = cut_test_runner_run_test(CUT_TEST_RUNNER(run_context), + CUT_TEST(test), test_context); + cut_test_context_current_pop(); + + return success; + } + + void + cut_setup (void) + { + test = NULL; + run_context = NULL; + test_context = NULL; + test_result = NULL; + + fail_line = 0; + } + + void + cut_teardown (void) + { + if (test) + g_object_unref(test); + if (run_context) + g_object_unref(run_context); + if (test_context) + g_object_unref(test_context); + if (test_result) + g_object_unref(test_result); + } + + class StubException : public std::exception + { + public: + StubException(const char *message) : message_(message) + { + } + + virtual const char * + what(void) const throw() + { + return message_; + } + + private: + const char *message_; + }; + + static void + stub_std_exception (void) + { + throw StubException("std::exception family exception"); + } + + void + test_std_exception (void) + { + test = cppcut_test_new("std::exception test", stub_std_exception); + cut_assert_not_null(test); + + cut_assert_false(run()); + cut_assert_test_result_summary(run_context, 1, 0, 0, 0, 1, 0, 0, 0); + const gchar *system_message = + cut_take_printf("Unhandled C++ standard exception is thrown: " + "<%s>: %s", + typeid(StubException).name(), + "std::exception family exception"); + cut_assert_test_result(run_context, 0, CUT_TEST_RESULT_ERROR, + "std::exception test", + NULL, + system_message, + NULL, NULL, + NULL, + "void cppcut_test::stub_standard_exception()", + NULL); + } + + static void + stub_not_std_exception (void) + { + throw "not std::exception"; + } + + void + test_not_std_exception (void) + { + test = cppcut_test_new("not std::exception test", + stub_not_std_exception); + cut_assert_not_null(test); + + cut_assert_false(run()); + cut_assert_test_result_summary(run_context, 1, 0, 0, 0, 1, 0, 0, 0); + const gchar *system_message = + "Unhandled C++ non-standard exception is thrown"; + cut_assert_test_result(run_context, 0, CUT_TEST_RESULT_ERROR, + "not std::exception test", + NULL, + system_message, + NULL, NULL, + NULL, + "void cppcut_test::stub_not_standard_exception()", + NULL); + } +} + +/* +vi:ts=4:nowrap:ai:expandtab:sw=4:ts=4 +*/ |