LCOV - code coverage report
Current view: directory - js/src/jsapi-tests - testSetProperty.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 21 21 100.0 %
Date: 2012-06-02 Functions: 14 11 78.6 %

       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                 : #include "jsxdrapi.h"
       7                 : 
       8                 : static JSBool
       9               1 : nativeGet(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
      10                 : {
      11               1 :     *vp = INT_TO_JSVAL(17);
      12               1 :     return JS_TRUE;
      13                 : }
      14                 : 
      15               4 : BEGIN_TEST(testSetProperty_NativeGetterStubSetter)
      16                 : {
      17               2 :     jsvalRoot vobj(cx);
      18               1 :     JSObject *obj = JS_NewObject(cx, NULL, NULL, NULL);
      19               1 :     CHECK(obj);
      20               1 :     vobj = OBJECT_TO_JSVAL(obj);
      21                 : 
      22               1 :     CHECK(JS_DefineProperty(cx, global, "globalProp", vobj,
      23                 :                             JS_PropertyStub, JS_StrictPropertyStub,
      24                 :                             JSPROP_ENUMERATE));
      25                 : 
      26               1 :     CHECK(JS_DefineProperty(cx, obj, "prop", JSVAL_VOID,
      27                 :                             nativeGet, JS_StrictPropertyStub,
      28                 :                             JSPROP_SHARED));
      29                 : 
      30               1 :     EXEC("'use strict';                                     \n"
      31                 :          "var error, passed = false;                        \n"
      32                 :          "try                                               \n"
      33                 :          "{                                                 \n"
      34                 :          "  this.globalProp.prop = 42;                      \n"
      35                 :          "  throw new Error('setting property succeeded!'); \n"
      36                 :          "}                                                 \n"
      37                 :          "catch (e)                                         \n"
      38                 :          "{                                                 \n"
      39                 :          "  error = e;                                      \n"
      40                 :          "  if (e instanceof TypeError)                     \n"
      41                 :          "    passed = true;                                \n"
      42                 :          "}                                                 \n"
      43                 :          "                                                  \n"
      44                 :          "if (!passed)                                      \n"
      45                 :          "  throw error;                                    \n");
      46                 : 
      47               1 :     EXEC("var error, passed = false;                        \n"
      48                 :          "try                                               \n"
      49                 :          "{                                                 \n"
      50                 :          "  this.globalProp.prop = 42;                      \n"
      51                 :          "  if (this.globalProp.prop === 17)                \n"
      52                 :          "    passed = true;                                \n"
      53                 :          "  else                                            \n"
      54                 :          "    throw new Error('bad value after set!');      \n"
      55                 :          "}                                                 \n"
      56                 :          "catch (e)                                         \n"
      57                 :          "{                                                 \n"
      58                 :          "  error = e;                                      \n"
      59                 :          "}                                                 \n"
      60                 :          "                                                  \n"
      61                 :          "if (!passed)                                      \n"
      62                 :          "  throw error;                                    \n");
      63                 : 
      64               1 :     return true;
      65                 : }
      66               1 : END_TEST(testSetProperty_NativeGetterStubSetter)
      67                 : 
      68               4 : BEGIN_TEST(testSetProperty_InheritedGlobalSetter)
      69                 : {
      70                 :     // This is a JSAPI test because jsapi-test globals do not have a resolve
      71                 :     // hook and therefore can use the property cache in some cases where the
      72                 :     // shell can't.
      73               1 :     JS_ASSERT(JS_GetClass(global)->resolve == &JS_ResolveStub);
      74                 : 
      75               1 :     CHECK(JS_DefineProperty(cx, global, "HOTLOOP", INT_TO_JSVAL(8), NULL, NULL, 0));
      76               1 :     EXEC("var n = 0;\n"
      77                 :          "var global = this;\n"
      78                 :          "function f() { n++; }\n"
      79                 :          "Object.defineProperty(Object.prototype, 'x', {set: f});\n"
      80                 :          "for (var i = 0; i < HOTLOOP; i++)\n"
      81                 :          "    global.x = i;\n");
      82               1 :     EXEC("if (n != HOTLOOP)\n"
      83                 :          "    throw 'FAIL';\n");
      84               1 :     return true;
      85                 : }
      86               3 : END_TEST(testSetProperty_InheritedGlobalSetter)
      87                 : 

Generated by: LCOV version 1.7