/* modify the internal state so a success gets counted */
#define mutest_count_suc ++mutest_passed_checks;
+#ifdef __cplusplus
+
+#include <exception>
+
+/* print an error message triggered by a C++ exception */
+#define mu_printex(name, action, ex) \
+ mu_print(MU_ERROR, __FILE__ ":%d: " name " failed, " \
+ "exception thrown (%s), " action \
+ " test case\n", __LINE__, ex);
+
+#define mutest_try try {
+#define mutest_catch(name, action, final) \
+ } catch (const std::exception& e) { \
+ mutest_count_err \
+ mu_printex(name, action, e.what()); \
+ final; \
+ } catch (...) { \
+ mutest_count_err \
+ mu_printex(name, action, "[unknown]"); \
+ final; \
+ }
+
+#else /* !__cplusplus */
+
+#define mutest_try
+#define mutest_catch(name, action, exp)
+
+#endif /* __cplusplus */
+
/* check that an expression evaluates to true, continue if the check fails */
#define mu_check_base(exp, name, action, final) \
do { \
mu_print(MU_CHECK, "\t\t* Checking " name "(" #exp ")...\n"); \
- if (exp) mutest_count_suc \
- else { \
- mutest_count_err \
- mu_printerr(name "(" #exp ")", action); \
- final; \
- } \
+ mutest_try \
+ if (exp) mutest_count_suc \
+ else { \
+ mutest_count_err \
+ mu_printerr(name "(" #exp ")", action); \
+ final; \
+ } \
+ mutest_catch(name, action, final) \
} while (0)
/* check that an expression evaluates to true, continue if the check fails */
*/
#define mu_ensure(exp) mu_check_base(exp, "mu_ensure", "aborting", return)
+#ifdef __cplusplus
+
+#define mu_echeck_base(ex, exp, name, action, final) \
+ do { \
+ mu_print(MU_CHECK, "\t\t* Checking " name "(" #ex ", " #exp \
+ ")...\n"); \
+ try { \
+ exp; \
+ mutest_count_err \
+ mu_printerr(name "(" #ex ", " #exp ")", \
+ "no exception thrown, " action); \
+ final; \
+ } catch (const ex& e) { \
+ mutest_count_suc \
+ } catch (const std::exception& e) { \
+ mutest_count_err \
+ mu_printex(name "(" #ex ", " #exp ")", action, \
+ e.what()); \
+ final; \
+ } catch (...) { \
+ mutest_count_err \
+ mu_printex(name "(" #ex ", " #exp ")", action, \
+ "[unknown]"); \
+ final; \
+ } \
+ } while (0)
+
+/*
+ * check that an expression throws a particular exception, continue if the
+ * check fails
+ */
+#define mu_echeck(ex, exp) \
+ mu_echeck_base(ex, exp, "mu_echeck", "resuming", continue)
+
+/*
+ * ensure that an expression throws a particular exception, abort the current
+ * test case if the check fails
+ */
+#define mu_eensure(ex, exp) \
+ mu_echeck_base(ex, exp, "mu_eensure", "aborting", return)
+
+#endif /* __cplusplus */
+
#ifndef MUTEST_PY /* we are using the C implementation */
/*
#
# Show the tests summary
-V=-v
+V = -v
+
+# Set to 0 if you don't want to compile the C++ test suite
+CPP_SUITE = 1
CFLAGS = -Wall -std=c89
+CXXFLAGS = -Wall -std=c++98
+LD = $(CC)
TARGET = tester
RUNNER_SRC = test_suite_runner.c
ALL_OBJS = $(RUNNER_OBJ) $(OBJS) $(TEST_OBJS) $(MUTEST_OBJ)
TEST_SOS = $(TEST_OBJS:.o=.so)
+ifeq ($(CPP_SUITE), 1)
+TEST_OBJS += exception_test.o
+LD = $(CXX)
+endif
+
all: $(TARGET)
py: $(TEST_SOS)
$(TARGET): $(ALL_OBJS)
- $(CC) $(LDFLAGS) -o $@ $^
+ $(LD) $(LDFLAGS) -o $@ $^
$(RUNNER_SRC): $(MKMUTEST_OBJ) $(MUTEST_H) $(TEST_OBJS)
$(MKMUTEST) $(MUTEST_H) $(TEST_OBJS) > $@
.c.so:
$(CC) $(CFLAGS) $(LDFLAGS) -DMUTEST_PY -fPIC -shared -o $@ $^
+.cpp.so:
+ $(CXX) $(CXXFLAGS) $(LDFLAGS) -DMUTEST_PY -fPIC -shared -o $@ $^
+
.SUFFIXES: .so
.PHONY: all test clean
--- /dev/null
+/*
+ * This file is part of mutest, a simple micro unit testing framework for C.
+ *
+ * mutest was written by Leandro Lucarella <llucax@gmail.com> and is released
+ * under the BOLA license, please see the LICENSE file or visit:
+ * http://blitiri.com.ar/p/bola/
+ *
+ * This is a C++ module test suite. It shows how to use checks involving
+ * exceptions.
+ *
+ * Please, read the README file for more details.
+ */
+
+#include <stdexcept> // std::out_of_range
+#include <vector> // std::vector
+
+#include "../mutest.h"
+
+extern "C" {
+
+void mu_test_exceptions() {
+ std::vector<int> v(1);
+ // ok
+ mu_check(v.at(0) == 0);
+ // throws! This fails
+ mu_check(v.at(1) == 0);
+ // ok, we expect the exception to be thrown, and it does
+ mu_echeck(std::out_of_range, v.at(1));
+ // fails! We expect this to throw, but it doesn't
+ mu_echeck(std::out_of_range, v.at(0));
+ // fails again, but this time the show is over (note the "ensure")
+ mu_eensure(std::out_of_range, v.at(0));
+ // this will never be executed (it should fail if it is)
+ mu_check(v.empty());
+}
+
+} // extern "C"
+