Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions subprojects/vunit/include/vunit/vunit.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct vunit_test {
}

#define TAP_TAB " "
#define YAML_TAB " "

#define VUNIT_ASSERT_YAML(ctx, predicate, yaml) \
__vunit_assert((ctx), (predicate), #predicate, yaml, __FILE__, __LINE__)
Expand Down
38 changes: 30 additions & 8 deletions subprojects/vunit/vunit.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include <sys/wait.h>

#include <vutils/str.h>
#include <vutils/sv.h>
#include <vutils/system_allocator.h>

#include <vunit.h>
Expand Down Expand Up @@ -51,6 +53,23 @@ void __vunit_assert(struct vunit_test_ctx *ctx, const bool predicate, const char
longjmp(ctx->env, -1);
}

static char *indent(const char *str, const char *prefix, struct vut_allocator alloc) {
struct vut_str ret = vut_str_init(alloc);
struct vut_sv base_sv = vut_sv_from_cstr(str);
while (!vut_sv_is_empty(base_sv)) {
vut_str_put_cstr(&ret, prefix);
struct vut_sv line = vut_sv_splitc(&base_sv, '\n');
// Add back the '\n'
line.len++;
vut_str_put_sv(&ret, line);
}

if (ret.len > 0 && ret.base[ret.len - 1] == '\n')
vut_str_put_cstr(&ret, prefix);

return vut_str_move_to_cstr(&ret);
}

void __vunit_assert_strcmp(struct vunit_test_ctx *ctx, const char *lhs, const char *rhs,
const enum __vunit_strcmp_res desire, const char *yaml,
const char *file_path, const size_t linenum) {
Expand All @@ -67,36 +86,39 @@ void __vunit_assert_strcmp(struct vunit_test_ctx *ctx, const char *lhs, const ch
else if (desire == GT && ret > 0)
return;

char *indented_lhs = indent(lhs, TAP_TAB YAML_TAB, ctx->allocator);
char *indented_rhs = indent(rhs, TAP_TAB YAML_TAB, ctx->allocator);

switch (desire) {
case EQ:
new_yaml = alloc_printf(ctx,
TAP_TAB "reason: \"Strings are not equal!\"\n" TAP_TAB
"lhs: '%s'\n" TAP_TAB "rhs: '%s'\n"
"lhs: |\n%s\n" TAP_TAB "rhs: |\n%s\n"
"%s",
lhs, rhs, yaml);
indented_lhs, indented_rhs, yaml);
break;
case NEQ:
new_yaml = alloc_printf(ctx,
TAP_TAB "reason: \"Strings are equal!\"\n" TAP_TAB
"string: '%s'\n"
"string: |\n%s\n"
"%s",
lhs, yaml);
indented_lhs, yaml);
break;
case LT:
new_yaml = alloc_printf(ctx,
TAP_TAB
"reason: \"Lhs string is not smaller than rhs\"\n" TAP_TAB
"lhs: '%s'\n" TAP_TAB "rhs: '%s'\n"
"lhs: |\n%s\n" TAP_TAB "rhs: |\n%s\n"
"%s",
lhs, rhs, yaml);
indented_lhs, indented_rhs, yaml);
break;
case GT:
new_yaml = alloc_printf(ctx,
TAP_TAB
"reason: \"Lhs string is not bigger than rhs\"\n" TAP_TAB
"lhs: '%s'\n" TAP_TAB "rhs: '%s'\n"
"lhs: |\n%s\n" TAP_TAB "rhs: |\n%s\n"
"%s",
lhs, rhs, yaml);
indented_lhs, indented_rhs, yaml);
break;
}

Expand Down
3 changes: 3 additions & 0 deletions subprojects/vutils/include/vutils/sv.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ struct vut_sv vut_sv_from_vut_str(const struct vut_str *str);
char *vut_sv_to_cstr(const struct vut_sv sv, struct vut_allocator alloc);

bool vut_sv_eq(struct vut_sv a, struct vut_sv b);
bool vut_sv_is_empty(struct vut_sv sv);

struct vut_sv vut_sv_splitc(struct vut_sv *sv, char c);

#endif // __VUT_SV_H__
20 changes: 20 additions & 0 deletions subprojects/vutils/sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ bool vut_sv_eq(struct vut_sv a, struct vut_sv b) {

return true;
}

bool vut_sv_is_empty(struct vut_sv sv) {
return sv.len == 0;
}

struct vut_sv vut_sv_splitc(struct vut_sv *sv, char c) {
for (size_t i = 0; i < sv->len; i++) {
if (sv->base[i] == c) {
struct vut_sv ret = vut_sv_from_str(sv->base, i);
sv->base += i + 1;
sv->len -= i + 1;
return ret;
}
}

struct vut_sv ret = *sv;
sv->base += sv->len;
sv->len = 0;
return ret;
}
Loading