/**
* struct kunit_assert - Data for printing a failed assertion or expectation.
- * @message: an optional message to provide additional context.
* @format: a function which formats the data in this kunit_assert to a string.
*
* Represents a failed expectation/assertion. Contains all the data necessary to
* format a string to a user reporting the failure.
*/
struct kunit_assert {
- struct va_format message;
void (*format)(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
};
-/**
- * KUNIT_INIT_VA_FMT_NULL - Default initializer for struct va_format.
- *
- * Used inside a struct initialization block to initialize struct va_format to
- * default values where fmt and va are null.
- */
-#define KUNIT_INIT_VA_FMT_NULL { .fmt = NULL, .va = NULL }
-
-/**
- * KUNIT_INIT_ASSERT_STRUCT() - Initializer for a &struct kunit_assert.
- * @fmt: The formatting function which builds a string out of this kunit_assert.
- *
- * The base initializer for a &struct kunit_assert.
- */
-#define KUNIT_INIT_ASSERT_STRUCT(fmt) { \
- .message = KUNIT_INIT_VA_FMT_NULL, \
- .format = fmt \
-}
-
void kunit_assert_prologue(const struct kunit_loc *loc,
enum kunit_assert_type type,
struct string_stream *stream);
-void kunit_assert_print_msg(const struct kunit_assert *assert,
- struct string_stream *stream);
-
/**
* struct kunit_fail_assert - Represents a plain fail expectation/assertion.
* @assert: The parent of this type.
};
void kunit_fail_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_FAIL_ASSERT_STRUCT { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_fail_assert_format) \
+ .assert = { .format = kunit_fail_assert_format }, \
}
/**
};
void kunit_unary_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_UNARY_ASSERT_STRUCT(cond, expect_true) { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_unary_assert_format), \
+ .assert = { .format = kunit_unary_assert_format }, \
.condition = cond, \
.expected_true = expect_true \
}
};
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
* KUNIT_EXPECT_* and KUNIT_ASSERT_* macros.
*/
#define KUNIT_INIT_PTR_NOT_ERR_STRUCT(txt, val) { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_ptr_not_err_assert_format), \
+ .assert = { .format = kunit_ptr_not_err_assert_format }, \
.text = txt, \
.value = val \
}
};
void kunit_binary_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
left_val, \
right_str, \
right_val) { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_assert_format), \
+ .assert = { .format = kunit_binary_assert_format }, \
.operation = op_str, \
.left_text = left_str, \
.left_value = left_val, \
};
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
left_val, \
right_str, \
right_val) { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_ptr_assert_format), \
+ .assert = { .format = kunit_binary_ptr_assert_format }, \
.operation = op_str, \
.left_text = left_str, \
.left_value = left_val, \
};
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream);
/**
left_val, \
right_str, \
right_val) { \
- .assert = KUNIT_INIT_ASSERT_STRUCT(kunit_binary_str_assert_format), \
+ .assert = { .format = kunit_binary_str_assert_format }, \
.operation = op_str, \
.left_text = left_str, \
.left_value = left_val, \
}
EXPORT_SYMBOL_GPL(kunit_assert_prologue);
-void kunit_assert_print_msg(const struct kunit_assert *assert,
- struct string_stream *stream)
+static void kunit_assert_print_msg(const struct va_format *message,
+ struct string_stream *stream)
{
- if (assert->message.fmt)
- string_stream_add(stream, "\n%pV", &assert->message);
+ if (message->fmt)
+ string_stream_add(stream, "\n%pV", message);
}
-EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
void kunit_fail_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
- string_stream_add(stream, "%pV", &assert->message);
+ string_stream_add(stream, "%pV", message);
}
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
void kunit_unary_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
struct kunit_unary_assert *unary_assert;
string_stream_add(stream,
KUNIT_SUBTEST_INDENT "Expected %s to be false, but is true\n",
unary_assert->condition);
- kunit_assert_print_msg(assert, stream);
+ kunit_assert_print_msg(message, stream);
}
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
struct kunit_ptr_not_err_assert *ptr_assert;
ptr_assert->text,
PTR_ERR(ptr_assert->value));
}
- kunit_assert_print_msg(assert, stream);
+ kunit_assert_print_msg(message, stream);
}
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
}
void kunit_binary_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
struct kunit_binary_assert *binary_assert;
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %lld",
binary_assert->right_text,
binary_assert->right_value);
- kunit_assert_print_msg(assert, stream);
+ kunit_assert_print_msg(message, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
struct kunit_binary_ptr_assert *binary_assert;
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == %px",
binary_assert->right_text,
binary_assert->right_value);
- kunit_assert_print_msg(assert, stream);
+ kunit_assert_print_msg(message, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
}
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
+ const struct va_format *message,
struct string_stream *stream)
{
struct kunit_binary_str_assert *binary_assert;
string_stream_add(stream, KUNIT_SUBSUBTEST_INDENT "%s == \"%s\"",
binary_assert->right_text,
binary_assert->right_value);
- kunit_assert_print_msg(assert, stream);
+ kunit_assert_print_msg(message, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);
}
static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
- enum kunit_assert_type type, struct kunit_assert *assert)
+ enum kunit_assert_type type, struct kunit_assert *assert,
+ const struct va_format *message)
{
struct string_stream *stream;
}
kunit_assert_prologue(loc, type, stream);
- assert->format(assert, stream);
+ assert->format(assert, message, stream);
kunit_print_string_stream(test, stream);
const char *fmt, ...)
{
va_list args;
+ struct va_format message;
va_start(args, fmt);
- assert->message.fmt = fmt;
- assert->message.va = &args;
+ message.fmt = fmt;
+ message.va = &args;
- kunit_fail(test, loc, type, assert);
+ kunit_fail(test, loc, type, assert, &message);
va_end(args);