From e7035e6a861c1d5e8a4917e31b1e8b722b54d55b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 11 Mar 2025 14:23:41 -0400 Subject: Clean up doctest code a bit --- stdlib/stdlib.c | 36 +++++++++++++++++++----------------- stdlib/stdlib.h | 22 +++++++++++----------- 2 files changed, 30 insertions(+), 28 deletions(-) (limited to 'stdlib') diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index 56d8ce99..28174d6c 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -566,15 +566,16 @@ public Text_t builtin_last_err() return Text$from_str(strerror(errno)); } -static int TEST_DEPTH = 0; +static int _inspect_depth = 0; static file_t *file = NULL; -public void start_test(const char *filename, int64_t start, int64_t end) +__attribute__((nonnull)) +public void start_inspect(const char *filename, int64_t start, int64_t end) { - if (filename && (file == NULL || strcmp(file->filename, filename) != 0)) + if (file == NULL || strcmp(file->filename, filename) != 0) file = load_file(filename); - if (filename && file) { + if (file) { const char *spaces = " "; int64_t first_line_len = (int64_t)strcspn(file->text + start, "\r\n"); const char *slash = strrchr(filename, '/'); @@ -582,8 +583,8 @@ public void start_test(const char *filename, int64_t start, int64_t end) int64_t line_num = get_line_number(file, file->text + start); fprintf(stderr, USE_COLOR ? "%.*s\x1b[33;1m>> \x1b[m%.*s %.*s\x1b[32;2m[%s:%ld]\x1b[m\n" : "%.*s>> %.*s %.*s[%s:%ld]\n", - 3*TEST_DEPTH, spaces, first_line_len, file->text + start, - MAX(0, 35-first_line_len-3*TEST_DEPTH), spaces, file_base, line_num); + 3*_inspect_depth, spaces, first_line_len, file->text + start, + MAX(0, 35-first_line_len-3*_inspect_depth), spaces, file_base, line_num); // For multi-line expressions, dedent each and print it on a new line with ".. " in front: if (end > start + first_line_len) { @@ -594,29 +595,30 @@ public void start_test(const char *filename, int64_t start, int64_t end) if ((int64_t)strspn(line, " \t") >= indent_len) line += indent_len; fprintf(stderr, USE_COLOR ? "%.*s\x1b[33m.. \x1b[m%.*s\n" : "%.*s.. %.*s\n", - 3*TEST_DEPTH, spaces, strcspn(line, "\r\n"), line); + 3*_inspect_depth, spaces, strcspn(line, "\r\n"), line); } } } - ++TEST_DEPTH; + _inspect_depth += 1; } -public void end_test(const void *expr, const TypeInfo_t *type) +__attribute__((nonnull)) +public void end_inspect(const void *expr, const TypeInfo_t *type) { - --TEST_DEPTH; - if (!expr || !type) return; + _inspect_depth -= 1; - Text_t expr_text = generic_as_text(expr, USE_COLOR, type); - Text_t type_name = generic_as_text(NULL, false, type); + if (type->metamethods.as_text) { + Text_t expr_text = generic_as_text(expr, USE_COLOR, type); + Text_t type_name = generic_as_text(NULL, false, type); - for (int i = 0; i < 3*TEST_DEPTH; i++) fputc(' ', stderr); - fprintf(stderr, USE_COLOR ? "\x1b[2m=\x1b[0m %k \x1b[2m: \x1b[36m%k\x1b[m\n" : "= %k : %k\n", &expr_text, &type_name); + for (int i = 0; i < 3*_inspect_depth; i++) fputc(' ', stderr); + fprintf(stderr, USE_COLOR ? "\x1b[2m=\x1b[0m %k \x1b[2m: \x1b[36m%k\x1b[m\n" : "= %k : %k\n", &expr_text, &type_name); + } } +__attribute__((nonnull)) public void test_value(const void *expr, const TypeInfo_t *type, const char *expected) { - if (!expr || !type || !expected) return; - Text_t expr_text = generic_as_text(expr, USE_COLOR, type); Text_t type_name = generic_as_text(NULL, false, type); diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h index 93bd4a02..1b633dff 100644 --- a/stdlib/stdlib.h +++ b/stdlib/stdlib.h @@ -29,20 +29,20 @@ _Noreturn void fail_text(Text_t message); __attribute__((format(printf, 4, 5))) _Noreturn void fail_source(const char *filename, int64_t start, int64_t end, const char *fmt, ...); Text_t builtin_last_err(); -void start_test(const char *filename, int64_t start, int64_t end); -void end_test(const void *expr, const TypeInfo_t *type); +__attribute__((nonnull)) +void start_inspect(const char *filename, int64_t start, int64_t end); +__attribute__((nonnull)) +void end_inspect(const void *expr, const TypeInfo_t *type); +#define inspect(expr, typeinfo, start, end) {\ + start_inspect(__SOURCE_FILE__, start, end); \ + auto _expr = expr; \ + end_inspect(&_expr, typeinfo); \ +} +__attribute__((nonnull)) void test_value(const void *expr, const TypeInfo_t *type, const char *expected); #define test(expr, typeinfo, expected, start, end) {\ - const char *_expected = expected; \ - if (!_expected || !_expected[0]) { \ - start_test(__SOURCE_FILE__, start, end); \ - } \ auto _expr = expr; \ - if (!_expected || !_expected[0]) { \ - end_test(&_expr, typeinfo); \ - } else { \ - test_value(&_expr, typeinfo, _expected); \ - } \ + test_value(&_expr, typeinfo, expected); \ } void say(Text_t text, bool newline); -- cgit v1.2.3