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:
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/. */
8 #include "js/Id.h" // JS::PropertyKey, JS::GetWellKnownSymbolKey, JS::ToGetterId, JS::ToSetterId
9 #include "js/String.h" // JSString, JS_AtomizeString, JS_StringEqualsAscii
10 #include "js/Symbol.h" // JS::Symbol, JS::SymbolCode
11 #include "jsapi-tests/tests.h"
13 BEGIN_TEST(testPropertyKeyGetterAndSetter
) {
14 JS::Rooted
<JSString
*> str(cx
, JS_AtomizeString(cx
, "prop"));
17 JS::Rooted
<JS::PropertyKey
> strId(cx
, JS::PropertyKey::NonIntAtom(str
));
18 MOZ_ASSERT(strId
.isString());
20 JS::Rooted
<JS::PropertyKey
> symId(
21 cx
, JS::GetWellKnownSymbolKey(cx
, JS::SymbolCode::iterator
));
22 MOZ_ASSERT(symId
.isSymbol());
24 JS::Rooted
<JS::PropertyKey
> numId(cx
, JS::PropertyKey::Int(42));
25 MOZ_ASSERT(numId
.isInt());
29 JS::Rooted
<JS::PropertyKey
> strGetterId(cx
);
30 CHECK(JS::ToGetterId(cx
, strId
, &strGetterId
));
31 CHECK(strGetterId
.isString());
32 CHECK(JS_StringEqualsAscii(cx
, strGetterId
.toString(), "get prop", &match
));
35 JS::Rooted
<JS::PropertyKey
> strSetterId(cx
);
36 CHECK(JS::ToSetterId(cx
, strId
, &strSetterId
));
37 CHECK(strSetterId
.isString());
38 CHECK(JS_StringEqualsAscii(cx
, strSetterId
.toString(), "set prop", &match
));
41 JS::Rooted
<JS::PropertyKey
> symGetterId(cx
);
42 CHECK(JS::ToGetterId(cx
, symId
, &symGetterId
));
43 CHECK(symGetterId
.isString());
44 CHECK(JS_StringEqualsAscii(cx
, symGetterId
.toString(),
45 "get [Symbol.iterator]", &match
));
48 JS::Rooted
<JS::PropertyKey
> symSetterId(cx
);
49 CHECK(JS::ToSetterId(cx
, symId
, &symSetterId
));
50 CHECK(symSetterId
.isString());
51 CHECK(JS_StringEqualsAscii(cx
, symSetterId
.toString(),
52 "set [Symbol.iterator]", &match
));
55 JS::Rooted
<JS::PropertyKey
> numGetterId(cx
);
56 CHECK(JS::ToGetterId(cx
, numId
, &numGetterId
));
57 CHECK(numGetterId
.isString());
58 CHECK(JS_StringEqualsAscii(cx
, numGetterId
.toString(), "get 42", &match
));
61 JS::Rooted
<JS::PropertyKey
> numSetterId(cx
);
62 CHECK(JS::ToSetterId(cx
, numId
, &numSetterId
));
63 CHECK(numSetterId
.isString());
64 CHECK(JS_StringEqualsAscii(cx
, numSetterId
.toString(), "set 42", &match
));
69 END_TEST(testPropertyKeyGetterAndSetter
)