check for double test registration
[protos/libecoli.git] / lib / ecoli_test.h
index fa3fe6c..f28dfdb 100644 (file)
 #include <sys/queue.h>
 
 #include <ecoli_log.h>
-#include <ecoli_node.h>
 
-// XXX check if already exists?
+struct ec_node;
+enum ec_completed_type;
+
 #define EC_TEST_REGISTER(t)                                            \
        static void ec_test_init_##t(void);                             \
        static void __attribute__((constructor, used))                  \
        ec_test_init_##t(void)                                          \
        {                                                               \
-                ec_test_register(&t);                                  \
+               if (ec_test_register(&t) < 0)                           \
+                       fprintf(stderr, "cannot register test %s\n",    \
+                               t.name);                                \
        }
 
 /**
@@ -64,8 +67,10 @@ struct ec_test {
  * @param test
  *   A pointer to a ec_test structure describing the test
  *   to be registered.
+ * @return
+ *   0 on success, -1 on error (errno is set).
  */
-void ec_test_register(struct ec_test *test);
+int ec_test_register(struct ec_test *test);
 
 int ec_test_all(void);
 int ec_test_one(const char *name);
@@ -77,16 +82,15 @@ int ec_test_check_parse(struct ec_node *node, int expected, ...);
        EC_LOG(EC_LOG_ERR, "%s:%d: error: " fmt "\n",                   \
                __FILE__, __LINE__, ##__VA_ARGS__);                     \
 
-/* XXX this is not an assert, it does not abort */
-// XXX use it instead of ec_log to have the file:line
-#define EC_TEST_ASSERT_STR(cond, fmt, ...)                             \
-       do {                                                            \
-               if (!(cond))                                            \
-                       EC_TEST_ERR("assert failure: (" #cond ") " fmt, \
-                               ##__VA_ARGS__);                         \
-       } while (0)
-
-#define EC_TEST_ASSERT(cond) EC_TEST_ASSERT_STR(cond, "")
+#define EC_TEST_CHECK(cond, fmt, ...) ({                               \
+       int ret_ = 0;                                                   \
+       if (!(cond)) {                                                  \
+               EC_TEST_ERR("(" #cond ") is wrong. "                    \
+                       ##__VA_ARGS__);                                 \
+               ret_ = -1;                                              \
+       }                                                               \
+       ret_;                                                           \
+})
 
 /* node, input, [expected1, expected2, ...] */
 #define EC_TEST_CHECK_PARSE(node, args...) ({                          \
@@ -96,10 +100,18 @@ int ec_test_check_parse(struct ec_node *node, int expected, ...);
        ret_;                                                           \
 })
 
-int ec_test_check_complete(struct ec_node *node, ...);
+int ec_test_check_complete(struct ec_node *node,
+                       enum ec_completed_type type, ...);
 
 #define EC_TEST_CHECK_COMPLETE(node, args...) ({                       \
-       int ret_ = ec_test_check_complete(node, args);                  \
+       int ret_ = ec_test_check_complete(node, EC_COMP_FULL, args);    \
+       if (ret_)                                                       \
+               EC_TEST_ERR("complete test failed");                    \
+       ret_;                                                           \
+})
+
+#define EC_TEST_CHECK_COMPLETE_PARTIAL(node, args...) ({               \
+       int ret_ = ec_test_check_complete(node, EC_COMP_PARTIAL, args); \
        if (ret_)                                                       \
                EC_TEST_ERR("complete test failed");                    \
        ret_;                                                           \