aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-10 00:59:42 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-10 00:59:42 -0500
commita90f8581c30cf434c7f5ff5a10ded59c57d7e6d3 (patch)
treeb7fd0428c6fe218fc9cb362b46ba762bcb11f039
parentf1c46b199fadd496329d11bbbb868b14973dfa8e (diff)
Clean up doctesting for assignments
-rw-r--r--builtins/functions.c2
-rw-r--r--builtins/functions.h2
-rw-r--r--compile.c26
3 files changed, 13 insertions, 17 deletions
diff --git a/builtins/functions.c b/builtins/functions.c
index ae67fcb9..2bd47286 100644
--- a/builtins/functions.c
+++ b/builtins/functions.c
@@ -130,7 +130,7 @@ public CORD builtin_last_err()
return CORD_from_char_star(strerror(errno));
}
-public void __doctest(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end)
+public void $test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end)
{
static file_t *file = NULL;
if (filename && (file == NULL || strcmp(file->filename, filename) != 0))
diff --git a/builtins/functions.h b/builtins/functions.h
index e476f104..ca849048 100644
--- a/builtins/functions.h
+++ b/builtins/functions.h
@@ -11,7 +11,7 @@ extern const char *SSS_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 __doctest(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end);
+void $test(void *expr, const TypeInfo *type, CORD expected, const char *filename, int64_t start, int64_t end);
void say(CORD text);
uint32_t generic_hash(const void *obj, const TypeInfo *type);
diff --git a/compile.c b/compile.c
index 9d1604cc..f2223805 100644
--- a/compile.c
+++ b/compile.c
@@ -1248,7 +1248,6 @@ CORD compile(env_t *env, ast_t *ast)
}
case DocTest: {
auto test = Match(ast, DocTest);
- CORD src = heap_strn(test->expr->start, (size_t)(test->expr->end - test->expr->start));
type_t *expr_t = get_type(env, test->expr);
if (!expr_t)
code_err(test->expr, "I couldn't figure out the type of this expression");
@@ -1268,7 +1267,7 @@ CORD compile(env_t *env, ast_t *ast)
auto decl = Match(test->expr, Declare);
return CORD_asprintf(
"%r\n"
- "__doctest(&%r, %r, %r, %r, %ld, %ld);",
+ "$test(&%r, %r, %r, %r, %ld, %ld);",
compile(env, test->expr),
compile(env, decl->var),
compile_type_info(env, get_type(env, decl->value)),
@@ -1288,23 +1287,20 @@ CORD compile(env_t *env, ast_t *ast)
CORD_appendf(&code, "%r = $%ld;\n", compile(env, target->ast), i++);
}
- CORD expr_cord = "CORD_all(";
- i = 1;
- for (ast_list_t *target = assign->targets; target; target = target->next) {
- CORD item = expr_as_text(env, CORD_asprintf("$%ld", i++), get_type(env, target->ast), "USE_COLOR");
- expr_cord = CORD_all(expr_cord, item, target->next ? ", \", \", " : CORD_EMPTY);
- }
- expr_cord = CORD_cat(expr_cord, ")");
+ if (test->output && assign->targets->next)
+ code_err(ast, "Sorry, but doctesting with '=' is not supported for multi-assignments");
- CORD_appendf(&code, "$test(%r, %r, %r);",
- compile(env, WrapAST(test->expr, TextLiteral, .cord=src)),
- expr_cord,
- compile(env, WrapAST(test->expr, TextLiteral, .cord=output)));
+ CORD_appendf(&code, "$test(&$1, %r, %r, %r, %ld, %ld);",
+ compile_type_info(env, get_type(env, assign->targets->ast)),
+ compile(env, WrapAST(test->expr, TextLiteral, .cord=test->output)),
+ compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)),
+ (int64_t)(test->expr->start - test->expr->file->text),
+ (int64_t)(test->expr->end - test->expr->file->text));
return CORD_cat(code, "\n}");
} else if (expr_t->tag == VoidType || expr_t->tag == AbortType) {
return CORD_asprintf(
"%r;\n"
- "__doctest(NULL, NULL, NULL, %r, %ld, %ld);",
+ "$test(NULL, NULL, NULL, %r, %ld, %ld);",
compile(env, test->expr),
compile(env, WrapAST(test->expr, TextLiteral, .cord=test->expr->file->filename)),
(int64_t)(test->expr->start - test->expr->file->text),
@@ -1312,7 +1308,7 @@ CORD compile(env_t *env, ast_t *ast)
} else {
return CORD_asprintf(
"{ // Test:\n%r $expr = %r;\n"
- "__doctest(&$expr, %r, %r, %r, %ld, %ld);\n"
+ "$test(&$expr, %r, %r, %r, %ld, %ld);\n"
"}",
compile_type(expr_t),
compile(env, test->expr),