From 8bafe71f17eb8b56f57de736f4eac9ec8bf5b40b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 14 May 2024 00:30:57 -0400 Subject: Add nested indentation for doctests that recursively invoke other doctests while evaluating --- builtins/functions.c | 74 +++++++++++++++++++++++++++++----------------------- builtins/functions.h | 6 ++++- 2 files changed, 47 insertions(+), 33 deletions(-) (limited to 'builtins') diff --git a/builtins/functions.c b/builtins/functions.c index 2b4d45d4..f668b253 100644 --- a/builtins/functions.c +++ b/builtins/functions.c @@ -151,44 +151,54 @@ public CORD builtin_last_err() return CORD_from_char_star(strerror(errno)); } -public void test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end) +static int TEST_DEPTH = 0; +static file_t *file = NULL; + +public void start_test(const char *filename, int64_t start, int64_t end) { - static file_t *file = NULL; if (filename && (file == NULL || strcmp(file->filename, filename) != 0)) file = load_file(filename); - if (filename && file) + if (filename && file) { + for (int i = 0; i < 3*TEST_DEPTH; i++) fputc(' ', stderr); CORD_fprintf(stderr, USE_COLOR ? "\x1b[33;1m>> \x1b[0m%.*s\x1b[m\n" : ">> %.*s\n", (end - start), file->text + start); + } + ++TEST_DEPTH; +} + +public void end_test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end) +{ + --TEST_DEPTH; + if (!expr) return; + + CORD expr_cord = generic_as_text(expr, USE_COLOR, type); + CORD type_name = generic_as_text(NULL, false, type); + + uint8_t buf[512] = {0}; + size_t buf_len = sizeof(buf)-1; + const char *expr_str = CORD_to_const_char_star(expr_cord); + uint8_t *normalized_str = u8_normalize(UNINORM_NFD, (uint8_t*)expr_str, strlen(expr_str), buf, &buf_len); + normalized_str[buf_len] = 0; + if (!normalized_str) errx(1, "Couldn't normalize unicode string!"); + CORD expr_normalized = CORD_from_char_star((char*)normalized_str); + if (normalized_str != buf) + free(normalized_str); + + for (int i = 0; i < 3*TEST_DEPTH; i++) fputc(' ', stderr); + CORD_fprintf(stderr, USE_COLOR ? "\x1b[2m=\x1b[0m %r \x1b[2m: %r\x1b[m\n" : "= %r : %r\n", expr_normalized, type_name); + if (expected) { + CORD expr_plain = USE_COLOR ? generic_as_text(expr, false, type) : expr_normalized; + bool success = Text$equal(&expr_plain, &expected); + if (!success && CORD_chr(expected, 0, ':')) { + CORD with_type = CORD_catn(3, expr_plain, " : ", type_name); + success = Text$equal(&with_type, &expected); + } - if (expr) { - CORD expr_cord = generic_as_text(expr, USE_COLOR, type); - CORD type_name = generic_as_text(NULL, false, type); - - uint8_t buf[512] = {0}; - size_t buf_len = sizeof(buf)-1; - const char *expr_str = CORD_to_const_char_star(expr_cord); - uint8_t *normalized_str = u8_normalize(UNINORM_NFD, (uint8_t*)expr_str, strlen(expr_str), buf, &buf_len); - normalized_str[buf_len] = 0; - if (!normalized_str) errx(1, "Couldn't normalize unicode string!"); - CORD expr_normalized = CORD_from_char_star((char*)normalized_str); - if (normalized_str != buf) - free(normalized_str); - - CORD_fprintf(stderr, USE_COLOR ? "\x1b[2m=\x1b[0m %r \x1b[2m: %r\x1b[m\n" : "= %r : %r\n", expr_normalized, type_name); - if (expected) { - CORD expr_plain = USE_COLOR ? generic_as_text(expr, false, type) : expr_normalized; - bool success = Text$equal(&expr_plain, &expected); - if (!success && CORD_chr(expected, 0, ':')) { - CORD with_type = CORD_catn(3, expr_plain, " : ", type_name); - success = Text$equal(&with_type, &expected); - } - - if (!success) { - fail_source(filename, start, end, - USE_COLOR ? "\x1b[31;1mDoctest failure:\nExpected: \x1b[32;1m%s\x1b[0m\n\x1b[31;1m But got:\x1b[m %s\n" - : "Doctest failure:\nExpected: %s\n But got: %s\n", - CORD_to_const_char_star(expected), CORD_to_const_char_star(expr_normalized)); - } + if (!success) { + fail_source(filename, start, end, + USE_COLOR ? "\x1b[31;1mDoctest failure:\nExpected: \x1b[32;1m%s\x1b[0m\n\x1b[31;1m But got:\x1b[m %s\n" + : "Doctest failure:\nExpected: %s\n But got: %s\n", + CORD_to_const_char_star(expected), CORD_to_const_char_star(expr_normalized)); } } } diff --git a/builtins/functions.h b/builtins/functions.h index ed63ec36..f1a2867e 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -13,7 +13,11 @@ extern const char *TOMO_HASH_VECTOR; void fail(CORD fmt, ...); void fail_source(const char *filename, int64_t start, int64_t end, CORD fmt, ...); CORD builtin_last_err(); -void test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end); +void start_test(const char *filename, int64_t start, int64_t end); +void end_test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end); +#define test(expr, type, expected, filename, start, end) {\ + start_test(filename, start, end); \ + end_test(expr, type, expected, filename, start, end); } void say(CORD text); uint32_t generic_hash(const void *obj, const TypeInfo *type); -- cgit v1.2.3