testGetPropertyDescriptor.cpp (2017B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #include "js/CallAndConstruct.h" // JS::IsCallable 6 #include "js/PropertyAndElement.h" 7 #include "js/PropertyDescriptor.h" // JS::FromPropertyDescriptor, JS_GetPropertyDescriptor 8 #include "js/RootingAPI.h" 9 #include "jsapi-tests/tests.h" 10 11 BEGIN_TEST(test_GetPropertyDescriptor) { 12 JS::RootedValue v(cx); 13 EVAL("({ somename : 123 })", &v); 14 CHECK(v.isObject()); 15 16 JS::RootedObject obj(cx, &v.toObject()); 17 JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> desc(cx); 18 JS::RootedObject holder(cx); 19 20 CHECK(JS_GetPropertyDescriptor(cx, obj, "somename", &desc, &holder)); 21 CHECK(desc.isSome()); 22 CHECK_SAME(desc->value(), JS::Int32Value(123)); 23 24 JS::RootedValue descValue(cx); 25 CHECK(JS::FromPropertyDescriptor(cx, desc, &descValue)); 26 CHECK(descValue.isObject()); 27 JS::RootedObject descObj(cx, &descValue.toObject()); 28 JS::RootedValue value(cx); 29 CHECK(JS_GetProperty(cx, descObj, "value", &value)); 30 CHECK_EQUAL(value.toInt32(), 123); 31 CHECK(JS_GetProperty(cx, descObj, "get", &value)); 32 CHECK(value.isUndefined()); 33 CHECK(JS_GetProperty(cx, descObj, "set", &value)); 34 CHECK(value.isUndefined()); 35 CHECK(JS_GetProperty(cx, descObj, "writable", &value)); 36 CHECK(value.isTrue()); 37 CHECK(JS_GetProperty(cx, descObj, "configurable", &value)); 38 CHECK(value.isTrue()); 39 CHECK(JS_GetProperty(cx, descObj, "enumerable", &value)); 40 CHECK(value.isTrue()); 41 42 CHECK(JS_GetPropertyDescriptor(cx, obj, "not-here", &desc, &holder)); 43 CHECK(desc.isNothing()); 44 45 CHECK(JS_GetPropertyDescriptor(cx, obj, "toString", &desc, &holder)); 46 JS::RootedObject objectProto(cx, JS::GetRealmObjectPrototype(cx)); 47 CHECK(objectProto); 48 CHECK_EQUAL(holder, objectProto); 49 CHECK(desc->value().isObject()); 50 CHECK(JS::IsCallable(&desc->value().toObject())); 51 52 return true; 53 } 54 END_TEST(test_GetPropertyDescriptor)