testDeleteProperty.cpp (1719B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 */ 4 /* This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #include "js/Id.h" 9 #include "js/PropertyAndElement.h" 10 #include "jsapi-tests/tests.h" 11 12 BEGIN_TEST(testDeleteProperty) { 13 JS::RootedValue val(cx); 14 EVAL("var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8}; obj", &val); 15 CHECK(val.isObject()); 16 JS::RootedObject obj(cx, &val.toObject()); 17 18 JS::Rooted<JS::PropertyKey> key(cx); 19 20 auto createPropertyKey = [](JSContext* cx, const char* s, 21 JS::MutableHandle<JS::PropertyKey> key) { 22 JSString* atom = JS_AtomizeString(cx, s); 23 if (!atom) { 24 return false; 25 } 26 key.set(JS::PropertyKey::NonIntAtom(atom)); 27 return true; 28 }; 29 30 // Test delete APIs without an ObjectOpResult argument. 31 CHECK(JS_DeleteProperty(cx, obj, "b")); 32 CHECK(createPropertyKey(cx, "d", &key)); 33 CHECK(JS_DeletePropertyById(cx, obj, key)); 34 35 // Test delete APIs with an ObjectOpResult argument. 36 JS::ObjectOpResult result; 37 CHECK(JS_DeleteProperty(cx, obj, "e", result)); 38 CHECK(result); 39 CHECK(createPropertyKey(cx, "f", &key)); 40 CHECK(JS_DeletePropertyById(cx, obj, key, result)); 41 CHECK(result); 42 43 // Check properties were deleted. 44 EVAL("JSON.stringify(obj)", &val); 45 CHECK(val.isString()); 46 bool match = false; 47 CHECK(JS_StringEqualsAscii(cx, val.toString(), 48 "{\"a\":1,\"c\":3,\"g\":7,\"h\":8}", &match)); 49 CHECK(match); 50 51 return true; 52 } 53 END_TEST(testDeleteProperty)