aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/stdlib.c49
-rw-r--r--stdlib/stdlib.h16
2 files changed, 41 insertions, 24 deletions
diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c
index d48255b7..56d8ce99 100644
--- a/stdlib/stdlib.c
+++ b/stdlib/stdlib.c
@@ -601,7 +601,7 @@ public void start_test(const char *filename, int64_t start, int64_t end)
++TEST_DEPTH;
}
-public void end_test(const void *expr, const TypeInfo_t *type, const char *expected)
+public void end_test(const void *expr, const TypeInfo_t *type)
{
--TEST_DEPTH;
if (!expr || !type) return;
@@ -611,30 +611,37 @@ public void end_test(const void *expr, const TypeInfo_t *type, const char *expec
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);
- if (expected && expected[0]) {
- Text_t expected_text = Text$from_str(expected);
- Text_t expr_plain = USE_COLOR ? generic_as_text(expr, false, type) : expr_text;
- bool success = Text$equal_values(expr_plain, expected_text);
- if (!success) {
- OptionalMatch_t colon = Text$find(expected_text, Text(":"), I_small(1));
- if (colon.index.small) {
- Text_t with_type = Text$concat(expr_plain, Text(" : "), type_name);
- success = Text$equal_values(with_type, expected_text);
- }
- }
+}
+
+public void test_value(const void *expr, const TypeInfo_t *type, const char *expected)
+{
+ if (!expr || !type || !expected) return;
- if (!success) {
- fprintf(stderr,
- USE_COLOR
- ? "\n\x1b[31;7m ==================== TEST FAILED ==================== \x1b[0;1m\n\nExpected: \x1b[1;32m%s\x1b[0m\n\x1b[1m But got:\x1b[m %k\n\n"
- : "\n==================== TEST FAILED ====================\n\nExpected: %s\n But got: %k\n\n",
- expected, &expr_text);
+ Text_t expr_text = generic_as_text(expr, USE_COLOR, type);
+ Text_t type_name = generic_as_text(NULL, false, type);
- print_stack_trace(stderr, 2, 4);
- fflush(stderr);
- raise(SIGABRT);
+ Text_t expected_text = Text$from_str(expected);
+ Text_t expr_plain = USE_COLOR ? generic_as_text(expr, false, type) : expr_text;
+ bool success = Text$equal_values(expr_plain, expected_text);
+ if (!success) {
+ OptionalMatch_t colon = Text$find(expected_text, Text(":"), I_small(1));
+ if (colon.index.small) {
+ Text_t with_type = Text$concat(expr_plain, Text(" : "), type_name);
+ success = Text$equal_values(with_type, expected_text);
}
}
+
+ if (!success) {
+ print_stack_trace(stderr, 2, 4);
+ fprintf(stderr,
+ USE_COLOR
+ ? "\n\x1b[31;7m ==================== TEST FAILED ==================== \x1b[0;1m\n\nExpected: \x1b[1;32m%s\x1b[0m\n\x1b[1m But got:\x1b[m %k\n\n"
+ : "\n==================== TEST FAILED ====================\n\nExpected: %s\n But got: %k\n\n",
+ expected, &expr_text);
+
+ fflush(stderr);
+ raise(SIGABRT);
+ }
}
public void say(Text_t text, bool newline)
diff --git a/stdlib/stdlib.h b/stdlib/stdlib.h
index 1625dd88..93bd4a02 100644
--- a/stdlib/stdlib.h
+++ b/stdlib/stdlib.h
@@ -30,11 +30,21 @@ __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, const char *expected);
+void end_test(const void *expr, const TypeInfo_t *type);
+void test_value(const void *expr, const TypeInfo_t *type, const char *expected);
#define test(expr, typeinfo, expected, start, end) {\
- start_test(__SOURCE_FILE__, start, end); \
+ const char *_expected = expected; \
+ if (!_expected || !_expected[0]) { \
+ start_test(__SOURCE_FILE__, start, end); \
+ } \
auto _expr = expr; \
- end_test(&_expr, typeinfo, expected); }
+ if (!_expected || !_expected[0]) { \
+ end_test(&_expr, typeinfo); \
+ } else { \
+ test_value(&_expr, typeinfo, _expected); \
+ } \
+}
+
void say(Text_t text, bool newline);
Text_t ask(Text_t prompt, bool bold, bool force_tty);
_Noreturn void tomo_exit(Text_t text, int32_t status);