-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests for Proxy property descriptor behavior
Also add property descriptor print helpers and change a few test cases to use them.
- Loading branch information
Showing
6 changed files
with
244 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Proxy (ES2015) 'defineProperty'. | ||
*/ | ||
|
||
/*@include util-object.js@*/ | ||
|
||
/*=== | ||
value=123,writable=false,enumerable=false,configurable=true | ||
get=function,set=function,enumerable=false,configurable=true | ||
===*/ | ||
|
||
function passThroughTest() { | ||
var T = {}; | ||
var P = new Proxy(T, {}); | ||
var pd; | ||
|
||
Object.defineProperty(P, 'foo', { | ||
value: 123, | ||
writable: false, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(P, 'bar', { | ||
get: function getter() {}, | ||
set: function setter() {}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
|
||
pd = Object.getOwnPropertyDescriptor(T, 'foo'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(T, 'bar'); | ||
printPropDesc(pd); | ||
} | ||
|
||
try { | ||
passThroughTest(); | ||
} catch (e) { | ||
print(e.stack || e); | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/ecmascript/test-bi-proxy-getownpropertydescriptor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Proxy (ES2015) 'getOwnPropertyDescriptor'. | ||
*/ | ||
|
||
/*@include util-object.js@*/ | ||
|
||
/*=== | ||
value=123,writable=true,enumerable=false,configurable=true | ||
get=function,enumerable=true,configurable=false | ||
set=function,enumerable=false,configurable=false | ||
get=function,set=function,enumerable=true,configurable=true | ||
value=123,writable=true,enumerable=false,configurable=true | ||
get=function,enumerable=true,configurable=false | ||
set=function,enumerable=false,configurable=false | ||
get=function,set=function,enumerable=true,configurable=true | ||
===*/ | ||
|
||
function passThroughTest() { | ||
var T = {}; | ||
var P = new Proxy(T, {}); | ||
var pd; | ||
|
||
Object.defineProperty(T, 'foo', { value: 123, writable: true, enumerable: false, configurable: true }); | ||
Object.defineProperty(T, 'bar', { get: function getter() {}, enumerable: true, configurable: false }); | ||
Object.defineProperty(T, 'quux', { set: function setter() {}, enumerable: false, configurable: false }); | ||
Object.defineProperty(T, 'baz', { get: function getter() {}, set: function setter() {}, enumerable: true, configurable: true }); | ||
|
||
pd = Object.getOwnPropertyDescriptor(T, 'foo'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(T, 'bar'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(T, 'quux'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(T, 'baz'); | ||
printPropDesc(pd); | ||
|
||
// As of Duktape 2.2 getOwnPropertyDescriptor() properly passes through | ||
// to the target object. | ||
pd = Object.getOwnPropertyDescriptor(P, 'foo'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(P, 'bar'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(P, 'quux'); | ||
printPropDesc(pd); | ||
pd = Object.getOwnPropertyDescriptor(P, 'baz'); | ||
printPropDesc(pd); | ||
} | ||
|
||
try { | ||
passThroughTest(); | ||
} catch (e) { | ||
print(e.stack || e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Example of virtualizing enumeration using a Proxy. | ||
*/ | ||
|
||
/*=== | ||
ownKeys trap: true | ||
getOwnPropertyDescriptor trap: true true string 0 | ||
getOwnPropertyDescriptor trap: true true string 1 | ||
getOwnPropertyDescriptor trap: true true string 2 | ||
getOwnPropertyDescriptor trap: true true string 3 | ||
getOwnPropertyDescriptor trap: true true string 4 | ||
getOwnPropertyDescriptor trap: true true string noSuch | ||
getOwnPropertyDescriptor trap: true true string length | ||
0 | ||
1 | ||
2 | ||
3 | ||
4 | ||
noSuch | ||
===*/ | ||
|
||
function test() { | ||
var target = [ 'foo', 'bar', 'quux' ]; | ||
var handler = { | ||
ownKeys: function () { | ||
// The ownKeys result set is validated against the Proxy to ensure | ||
// enumerated keys are enumerable; unless a getOwnPropertyDescriptor | ||
// trap exists these checks will be made against the target which | ||
// means non-existent properties won't be enumerated.§ | ||
print('ownKeys trap:', this === handler); | ||
return [ '0', '1', '2', '3', '4', 'noSuch', 'length' ] | ||
}, | ||
getOwnPropertyDescriptor: function (targ, key) { | ||
// Cannot report 'length' as enumerable (that would violate | ||
// getOwnPropertyDescriptor trap constraints). However, | ||
// Duktape 2.2 (at least) won't enforce that constraint yet. | ||
print('getOwnPropertyDescriptor trap:', this === handler, targ === target, typeof key, key); | ||
if (key === 'length') { | ||
return Object.getOwnPropertyDescriptor(target, key); | ||
} | ||
return { | ||
value: target[key], | ||
enumerable: true, | ||
writable: true, | ||
configurable: true | ||
}; | ||
} | ||
}; | ||
var proxy = new Proxy(target, handler); | ||
|
||
// Without 'getOwnPropertyDescriptor' trap the only keys visible would be | ||
// '0', '1', and '2' which backing in the target object. | ||
for (var k in proxy) { | ||
print(k); | ||
} | ||
} | ||
|
||
try { | ||
test(); | ||
} catch (e) { | ||
print(e.stack || e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters