Browse Source

Testcase fixes/additions for func .toString()

pull/554/head
Sami Vaarala 9 years ago
parent
commit
9328d54a6e
  1. 19
      tests/api/test-dev-api-verbose-error-messages-gh441.c
  2. 52
      tests/api/test-dev-func-tostring.c
  3. 2
      tests/api/test-dev-lightfunc.c
  4. 4
      tests/ecmascript/test-conv-tostring.js
  5. 40
      tests/ecmascript/test-dev-func-tostring.js
  6. 8
      tests/ecmascript/test-dev-primary-identifier.js

19
tests/api/test-dev-api-verbose-error-messages-gh441.c

@ -120,14 +120,14 @@ TypeError: buffer required, found [object Function] (stack index -3)
TypeError: pointer required, found [object Function] (stack index -3)
test__c_function ok
top: 1
TypeError: undefined required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: null required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: boolean required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: number required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: string required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: buffer required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: pointer required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: nativefunction required, found function LFUNC() {/= light =/} (stack index -3)
TypeError: undefined required, found function LFUNC() {"light"} (stack index -3)
TypeError: null required, found function LFUNC() {"light"} (stack index -3)
TypeError: boolean required, found function LFUNC() {"light"} (stack index -3)
TypeError: number required, found function LFUNC() {"light"} (stack index -3)
TypeError: string required, found function LFUNC() {"light"} (stack index -3)
TypeError: buffer required, found function LFUNC() {"light"} (stack index -3)
TypeError: pointer required, found function LFUNC() {"light"} (stack index -3)
TypeError: nativefunction required, found function LFUNC() {"light"} (stack index -3)
top: 1
TypeError: undefined required, found [object Function] (stack index -3)
TypeError: null required, found [object Function] (stack index -3)
@ -212,8 +212,7 @@ static duk_ret_t test__c_function(duk_context *ctx) {
rc = duk_safe_call(ctx, (fn), 3, 3); \
if (rc != 0) { \
duk_eval_string(ctx, "(function (v) { print(String(v).replace(/\\(0x.*?\\)/g, '(PTR)')" \
".replace(/light_[0-9a-fA-F_]+/g, 'LFUNC')" \
".replace(/\\*/g, '=')); })"); \
".replace(/light_[0-9a-fA-F_]+/g, 'LFUNC')); })"); \
duk_dup(ctx, -4); \
duk_call(ctx, 1); \
} else { \

52
tests/api/test-dev-func-tostring.c

@ -0,0 +1,52 @@
/*
* Test the updated Function .toString() format in Duktape 1.5.0.
*
* Cover a few cases which cannot be exercised using Ecmascript code alone.
*/
/*===
*** test_1 (duk_safe_call)
function light_PTR() {"light"}
function dummy {() {"native"}
final top: 0
==> rc=0, result='undefined'
===*/
static duk_ret_t dummy_func(duk_context *ctx) {
(void) ctx;
return 0;
}
static duk_ret_t test_1(duk_context *ctx) {
/* Lightfunc, must sanitize the address for the expect string. */
duk_eval_string(ctx,
"(function (v) {\n"
" print(String(v).replace(/light_[0-9a-fA-f_]+/, 'light_PTR'));\n"
"})");
duk_push_c_lightfunc(ctx, dummy_func, 0 /*nargs*/, 0 /*length*/, 0 /*magic*/);
duk_call(ctx, 1);
duk_pop(ctx);
/* Function with a .name containing invalid characters.
*
* This is not currently handled very well: the .toString() output
* uses the name as is, which can make the output technically
* incorrect because it won't parse as a function. However, the
* only way to create such functions is from C code so this is a
* minor issue.
*/
duk_push_c_function(ctx, dummy_func, 0 /*nargs*/);
duk_push_string(ctx, "name");
duk_push_string(ctx, "dummy {");
duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);
printf("%s\n", duk_to_string(ctx, -1));
duk_pop(ctx);
printf("final top: %ld\n", (long) duk_get_top(ctx));
return 0;
}
void test(duk_context *ctx) {
TEST_SAFE_CALL(test_1);
}

2
tests/api/test-dev-lightfunc.c

@ -906,7 +906,7 @@ static duk_ret_t test_to_object(duk_context *ctx) {
/*===
*** test_to_buffer (duk_safe_call)
function light_PTR_4232() {(* light *)}
function light_PTR_4232() {"light"}
final top: 1
==> rc=0, result='undefined'
===*/

4
tests/ecmascript/test-conv-tostring.js

@ -18,7 +18,7 @@
3 string false
4 string 123
5 string foo
6 string function myfunc() {|* ecmascript *|}
6 string function myfunc() {"ecmascript"}
7 string [object Object]
8 TypeError
9 string foo
@ -49,7 +49,7 @@ function test() {
try {
var t = String(v);
if (typeof v === 'function' && typeof t === 'string') {
// expect string hack
// expect string hack (no longer needed in Duktape 1.5.0)
t = t.replace(/\//g, '|');
}
print(i, typeof t, t);

40
tests/ecmascript/test-dev-func-tostring.js

@ -0,0 +1,40 @@
/*
* Test the updated Function .toString() format in Duktape 1.5.0.
*/
/*---
{
"custom": true
}
---*/
/*===
function () {"ecmascript"}
function foo() {"ecmascript"}
function cos() {"native"}
===*/
function test() {
var fn;
// Anonymous function
fn = function (){};
print(fn);
// Named function
fn = function foo(){};
print(fn);
// Native function
fn = Math.cos;
print(fn);
// Lightfunc and some other cases are covered by
// tests/api/test-dev-func-tostring.c.
}
try {
test();
} catch (e) {
print(e.stack || e);
}

8
tests/ecmascript/test-dev-primary-identifier.js

@ -6,10 +6,10 @@
foo
foo
234
function inner() {|* ecmascript *|}
function inner() {"ecmascript"}
123
234
function inner() {|* ecmascript *|}
function inner() {"ecmascript"}
123
===*/
@ -30,12 +30,12 @@ function test(arg) {
// fast path local variable, local function, local argument
print(x);
print(String(inner).replace(/\//g, '|'));
print(String(inner));
print(arg);
// same through slow path eval
eval('print(x);');
eval('print(String(inner).replace(/\\//g, "|"));');
eval('print(String(inner));');
eval('print(arg);');
}

Loading…
Cancel
Save