LCOV - code coverage report
Current view: directory - js/src/jsapi-tests - testNewObject.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 58 47 81.0 %
Date: 2012-06-02 Functions: 9 7 77.8 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
       2                 :  * vim: set ts=8 sw=4 et tw=99:
       3                 :  */
       4                 : 
       5                 : #include "tests.h"
       6                 : 
       7                 : const size_t N = 1000;
       8                 : static jsval argv[N];
       9                 : 
      10                 : static JSBool
      11               1 : constructHook(JSContext *cx, unsigned argc, jsval *vp)
      12                 : {
      13                 :     // Check that arguments were passed properly from JS_New.
      14               1 :     JSObject *callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, vp));
      15                 : 
      16               1 :     JSObject *obj = JS_NewObjectForConstructor(cx, vp);
      17               1 :     if (!obj) {
      18               0 :         JS_ReportError(cx, "test failed, could not construct object");
      19               0 :         return false;
      20                 :     }
      21               1 :     if (strcmp(JS_GetClass(obj)->name, "Object") != 0) {
      22               0 :         JS_ReportError(cx, "test failed, wrong class for 'this'");
      23               0 :         return false;
      24                 :     }
      25               1 :     if (argc != 3) {
      26               0 :         JS_ReportError(cx, "test failed, argc == %d", argc);
      27               0 :         return false;
      28                 :     }
      29               1 :     if (!JSVAL_IS_INT(argv[2]) || JSVAL_TO_INT(argv[2]) != 2) {
      30               0 :         JS_ReportError(cx, "test failed, wrong value in argv[2]");
      31               0 :         return false;
      32                 :     }
      33               1 :     if (!JS_IsConstructing(cx, vp)) {
      34               0 :         JS_ReportError(cx, "test failed, not constructing");
      35               0 :         return false;
      36                 :     }
      37                 : 
      38                 :     // Perform a side-effect to indicate that this hook was actually called.
      39               1 :     if (!JS_SetElement(cx, callee, 0, &argv[0]))
      40               0 :         return false;
      41                 : 
      42               1 :     *vp = OBJECT_TO_JSVAL(obj);
      43               1 :     argv[0] = argv[1] = argv[2] = JSVAL_VOID;  // trash the argv, perversely
      44               1 :     return true;
      45                 : }
      46                 : 
      47               4 : BEGIN_TEST(testNewObject_1)
      48                 : {
      49                 :     jsval v;
      50               1 :     EVAL("Array", &v);
      51               1 :     JSObject *Array = JSVAL_TO_OBJECT(v);
      52                 : 
      53                 :     // With no arguments.
      54               1 :     JSObject *obj = JS_New(cx, Array, 0, NULL);
      55               1 :     CHECK(obj);
      56               2 :     jsvalRoot rt(cx, OBJECT_TO_JSVAL(obj));
      57               1 :     CHECK(JS_IsArrayObject(cx, obj));
      58                 :     uint32_t len;
      59               1 :     CHECK(JS_GetArrayLength(cx, obj, &len));
      60               1 :     CHECK_EQUAL(len, 0);
      61                 : 
      62                 :     // With one argument.
      63               1 :     argv[0] = INT_TO_JSVAL(4);
      64               1 :     obj = JS_New(cx, Array, 1, argv);
      65               1 :     CHECK(obj);
      66               1 :     rt = OBJECT_TO_JSVAL(obj);
      67               1 :     CHECK(JS_IsArrayObject(cx, obj));
      68               1 :     CHECK(JS_GetArrayLength(cx, obj, &len));
      69               1 :     CHECK_EQUAL(len, 4);
      70                 : 
      71                 :     // With N arguments.
      72            1001 :     for (size_t i = 0; i < N; i++)
      73            1000 :         argv[i] = INT_TO_JSVAL(i);
      74               1 :     obj = JS_New(cx, Array, N, argv);
      75               1 :     CHECK(obj);
      76               1 :     rt = OBJECT_TO_JSVAL(obj);
      77               1 :     CHECK(JS_IsArrayObject(cx, obj));
      78               1 :     CHECK(JS_GetArrayLength(cx, obj, &len));
      79               1 :     CHECK_EQUAL(len, N);
      80               1 :     CHECK(JS_GetElement(cx, obj, N - 1, &v));
      81               1 :     CHECK_SAME(v, INT_TO_JSVAL(N - 1));
      82                 : 
      83                 :     // With JSClass.construct.
      84                 :     static JSClass cls = {
      85                 :         "testNewObject_1",
      86                 :         0,
      87                 :         JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
      88                 :         JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
      89                 :         NULL, NULL, constructHook
      90                 :     };
      91               1 :     JSObject *ctor = JS_NewObject(cx, &cls, NULL, NULL);
      92               1 :     CHECK(ctor);
      93               2 :     jsvalRoot rt2(cx, OBJECT_TO_JSVAL(ctor));
      94               1 :     obj = JS_New(cx, ctor, 3, argv);
      95               1 :     CHECK(obj);
      96               1 :     CHECK(JS_GetElement(cx, ctor, 0, &v));
      97               1 :     CHECK_SAME(v, JSVAL_ZERO);
      98               1 :     return true;
      99                 : }
     100               2 : END_TEST(testNewObject_1)

Generated by: LCOV version 1.7