X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/a9ae295484868287ff1b8f2d613fa4c010c06891..0a1c8a90e23304220f6602e98aba5f891a1350b2:/t/cdeclerr.c diff --git a/t/cdeclerr.c b/t/cdeclerr.c index 16ba1be..b4a014c 100644 --- a/t/cdeclerr.c +++ b/t/cdeclerr.c @@ -27,9 +27,10 @@ #include "errmsg.h" #include "tap.h" -#if ENABLE_NLS -void cdecl__init_i18n(void) { } -#endif +const char *cdecl__token_name(unsigned token) +{ + assert(0); +} static char *fmt_char(int c, char *buf) { @@ -57,6 +58,15 @@ static char *fmt_char(int c, char *buf) return buf; } +static void check_code(const struct cdecl_error *err, unsigned expect) +{ + if (!tap_result(err->code == expect, "returned error code")) { + tap_diag("Failed, unexpected result"); + tap_diag(" Received: %u", err->code); + tap_diag(" Expected: %u", expect); + } +} + static void check_fixed_string(size_t len) { const struct cdecl_error *err; @@ -73,15 +83,11 @@ static void check_fixed_string(size_t len) memset(work2, 'X', len - 1); tap_diag("cdecl__err w/ %lu-byte string", (unsigned long)len); - cdecl__err(1234, work1); + cdecl__err(1234, work1, "XX"); memset(work1, 0, len); err = cdecl_get_error(); - if (!tap_result(err->code == 1234, "returned error code")) { - tap_diag("Failed, unexpected result"); - tap_diag(" Received: %u", err->code); - tap_diag(" Expected: 1234"); - } + check_code(err, 1234); errlen = strlen(err->str); if (!tap_result(errlen == len-1, "returned string length")) { @@ -107,6 +113,32 @@ static void check_fixed_string(size_t len) free(work1); } +static void check_format_string(const char *fmt, const char *arg) +{ + size_t sz = strlen(fmt) + strlen(arg); + const struct cdecl_error *err; + char *work; + + work = malloc(sz + 1); + if (!work) + abort(); + sprintf(work, fmt, arg); + + cdecl__err(5432, fmt, arg); + err = cdecl_get_error(); + + tap_diag("cdecl__err(\"%s\", \"%s\")", fmt, arg); + check_code(err, 5432); + + if (!tap_result(!strcmp(err->str, work), "returned string")) { + tap_diag("Failed, unexpected result"); + tap_diag(" Received: %.*s", (int)sz, err->str); + tap_diag(" Expected: %s", work); + } + + free(work); +} + static void check_enomem() { const char expmsg[] = "failed to allocate memory"; @@ -116,12 +148,7 @@ static void check_enomem() cdecl__errmsg(CDECL__ENOMEM); err = cdecl_get_error(); - if (!tap_result(err->code == CDECL_ENOMEM, "returned error code")) { - tap_diag("Failed, unexpected result"); - tap_diag(" Received: %u", err->code); - tap_diag(" Expected: %d", CDECL_ENOMEM); - } - + check_code(err, CDECL_ENOMEM); if (!tap_result(!strcmp(err->str, "failed to allocate memory"), "returned string")) { @@ -138,14 +165,23 @@ static void check_enomem() } } +#define IFNLS(a, b) (ENABLE_NLS ? (a) : (b)) + int main(void) { - tap_plan(3*3 + 2); + tap_plan(3*3 + 2 + 2); check_fixed_string(50); check_fixed_string(500); check_fixed_string(5000); + /* + * When NLS is disabled, for implementation simplicity only format + * strings ending with %s are supported as this is sufficient. + * Otherwise, %s may appear anywhere. + */ + check_format_string(IFNLS("hello %s world", "hello world %s"), "za"); + check_enomem(); tap_done();