From 39311f7bc69192a8ea3bf6bcf1415e621d65b000 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Mon, 29 Dec 2014 14:48:10 +0200 Subject: [PATCH] API tests for new vararg calls --- api-testcases/test-error.c | 34 ++++++++++++++++-- api-testcases/test-logging.c | 22 +++++++++++- api-testcases/test-push-error-object.c | 48 +++++++++++++++++++++++++- 3 files changed, 99 insertions(+), 5 deletions(-) diff --git a/api-testcases/test-error.c b/api-testcases/test-error.c index de4e2075..5b178f1b 100644 --- a/api-testcases/test-error.c +++ b/api-testcases/test-error.c @@ -6,7 +6,7 @@ name: RangeError message: range error: 123 code: undefined fileName is a string: 1 -lineNumber: 34 +lineNumber: 43 isNative: undefined *** test_2 (duk_pcall) ==> rc=1 @@ -15,7 +15,7 @@ name: Error message: arbitrary error code code: undefined fileName is a string: 1 -lineNumber: 43 +lineNumber: 52 isNative: undefined *** test_3 (duk_pcall) ==> rc=1 @@ -24,7 +24,16 @@ name: TypeError message: 105 code: undefined fileName is a string: 1 -lineNumber: 53 +lineNumber: 62 +isNative: undefined +*** test_4 (duk_pcall) +==> rc=1 +ToString(error): RangeError: my error 123 234 foobar +name: RangeError +message: my error 123 234 foobar +code: undefined +fileName is a string: 1 +lineNumber: 73 isNative: undefined ===*/ @@ -56,6 +65,24 @@ static duk_ret_t test_3(duk_context *ctx) { return 0; } +/* Vararg */ +static void my_error(duk_context *ctx, duk_errcode_t errcode, const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + duk_error_va(ctx, errcode, fmt, ap); + va_end(ap); +} + +static duk_ret_t test_4(duk_context *ctx) { + duk_set_top(ctx, 0); + + my_error(ctx, DUK_ERR_RANGE_ERROR, "my error %d %d %s", 123, 234, "foobar"); + + printf("final top: %ld\n", (long) duk_get_top(ctx)); + return 0; +} + void dump_error(duk_context *ctx) { duk_dup(ctx, -1); printf("ToString(error): %s\n", duk_to_string(ctx, -1)); @@ -104,4 +131,5 @@ void test(duk_context *ctx) { TEST(test_1); TEST(test_2); TEST(test_3); + TEST(test_4); } diff --git a/api-testcases/test-logging.c b/api-testcases/test-logging.c index bb7ae42a..7f1337e0 100644 --- a/api-testcases/test-logging.c +++ b/api-testcases/test-logging.c @@ -13,13 +13,16 @@ TIMESTAMP FTL C: clamped fatal: 123 TIMESTAMP INF C: long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long and formatted: 123 final top: 0 ==> rc=0, result='undefined' +*** test_2 (duk_safe_call) +TIMESTAMP FTL C: fatal error: 123 quux +==> rc=0, result='undefined' ===*/ #define CHARS_100 \ "long long long long long long long long long long " \ "long long long long long long long long long long " -int test_1(duk_context *ctx) { +static duk_ret_t test_1(duk_context *ctx) { /* Force log level to output all logs. */ duk_eval_string(ctx, "Duktape.Logger.clog.l = 0;"); duk_pop(ctx); @@ -60,6 +63,23 @@ int test_1(duk_context *ctx) { return 0; } +/* Vararg */ +static void my_log_write(duk_context *ctx, duk_int_t level, const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + duk_log_va(ctx, level, fmt, ap); + va_end(ap); +} + +static duk_ret_t test_2(duk_context *ctx) { + /* Rely on the log "censoring" set up in test_1(). */ + + my_log_write(ctx, DUK_LOG_FATAL, "fatal error: %d %s", 123, "quux"); + return 0; +} + void test(duk_context *ctx) { TEST_SAFE_CALL(test_1); + TEST_SAFE_CALL(test_2); } diff --git a/api-testcases/test-push-error-object.c b/api-testcases/test-push-error-object.c index 47f02c64..4352da08 100644 --- a/api-testcases/test-push-error-object.c +++ b/api-testcases/test-push-error-object.c @@ -1,12 +1,20 @@ /*=== +*** test_1 (duk_safe_call) err_idx: 2 name: TypeError message: invalid argument: 234 code: undefined final top: 3 +==> rc=0, result='undefined' +*** test_2 (duk_safe_call) +err_idx: 2 +name: RangeError +message: range error: 123 234 +final top: 3 +==> rc=0, result='undefined' ===*/ -void test(duk_context *ctx) { +static duk_ret_t test_1(duk_context *ctx) { duk_idx_t err_idx; /* dummy values */ @@ -32,4 +40,42 @@ void test(duk_context *ctx) { duk_pop(ctx); printf("final top: %ld\n", (long) duk_get_top(ctx)); + return 0; +} + +/* Vararg variants */ +static void push_1(duk_context *ctx, duk_errcode_t errcode, const char *fmt, ...) { + duk_idx_t err_idx; + va_list ap; + + va_start(ap, fmt); + + err_idx = duk_push_error_object_va(ctx, errcode, fmt, ap); + printf("err_idx: %ld\n", (long) err_idx); + + duk_get_prop_string(ctx, -1, "name"); + printf("name: %s\n", duk_to_string(ctx, -1)); + duk_pop(ctx); + + duk_get_prop_string(ctx, -1, "message"); + printf("message: %s\n", duk_to_string(ctx, -1)); + duk_pop(ctx); + + va_end(ap); +} + +static duk_ret_t test_2(duk_context *ctx) { + /* dummy values */ + duk_push_int(ctx, 123); + duk_push_int(ctx, 123); + + push_1(ctx, DUK_ERR_RANGE_ERROR, "range error: %d %d", 123, 234); + + printf("final top: %ld\n", (long) duk_get_top(ctx)); + return 0; +} + +void test(duk_context *ctx) { + TEST_SAFE_CALL(test_1); + TEST_SAFE_CALL(test_2); }