src/test/fail.h
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 01:17:36 +0300
branchnew-lib-errors
changeset 219 cefec18b8268
parent 196 873796250c60
permissions -rw-r--r--
some of the lib/transport stuff compiles
#ifndef TEST_FAIL_H
#define TEST_FAIL_H

/**
 * @file
 *
 * Various ways to mark the current test as failed.
 */
#include "../error.h"
#include <stdarg.h>

#define NORETURN __attribute((noreturn))

/**
 * Output a message representing the given failure using log_output.
 *
 * Also dumps out a stacktrace for the calling function using log_debug, skipping this func, the backtrace func, and
 * the given number of frames.
 */
void test_fail_va (const char *func, const char *file, int line, int skip, const char *fmt, va_list vargs) NORETURN;

/**
 * Output given fail using test_fail_va().
 */
void test_fail (const char *func, const char *file, int line, int skip, const char *fmt, ...) NORETURN;

/**
 * Fail with a simple error message
 */
#define fail(...) test_fail(__func__, __FILE__, __LINE__, 0, __VA_ARGS__)

/**
 * Fail conditionally
 */
#define fail_if(cond, ...) do { \
        if (cond)   \
            fail(__VA_ARGS__);   \
    } while (0)

/**
 * Fail with an error code
 */
#define fail_err(err, fmt, ...) fail(fmt ": %s", __VA_ARGS__, ## error_name(err))

/**
 * Fail with an error info
 */
#define fail_error(error, fmt, ...) fail(fmt ": %s", __VA_ARGS__, ## error_msg(error))

#endif /* TEST_FAIL_H */