Skip to content
Snippets Groups Projects
Commit e4aac334 authored by Brian Terlson's avatar Brian Terlson
Browse files

Merge pull request #282 from bocoup/Proxy

Add tests for Proxy
parents a1437652 2c4077c1
No related branches found
No related tags found
No related merge requests found
Showing
with 405 additions and 0 deletions
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
4. If handler is a Proxy exotic object and the value of the
[[ProxyHandler]] internal slot of handler is null, throw a
TypeError exception.
...
---*/
var revocable = Proxy.revocable({}, {});
revocable.revoke();
assert.throws(TypeError, function() {
new Proxy({}, revocable.proxy);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy({}, false);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy({}, null);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy({}, 0);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy({}, "");
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
assert.throws(TypeError, function() {
new Proxy({}, Symbol());
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
3. If Type(handler) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy({}, undefined);
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
A Proxy exotic object is only callable if the given target is callable.
info: >
Proxy ( target, handler )
7. If IsCallable(target) is true, then
a. Set the [[Call]] internal method of P as specified in 9.5.13.
...
12.3.4.3 Runtime Semantics: EvaluateDirectCall( func, thisValue, arguments,
tailPosition )
4. If IsCallable(func) is false, throw a TypeError exception.
---*/
var p = new Proxy({}, {});
assert.throws(TypeError, function() {
p.call();
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
A Proxy exotic object only accepts a constructor call if target is
constructor.
info: >
Proxy ( target, handler )
7. If IsCallable(target) is true, then
b. If target has a [[Construct]] internal method, then
i. Set the [[Construct]] internal method of P as specified in
9.5.14.
...
12.3.3.1.1 Runtime Semantics: EvaluateNew(constructProduction, arguments)
8. If IsConstructor (constructor) is false, throw a TypeError exception.
---*/
var p = new Proxy(eval, {});
p(); // the Proxy object is callable
assert.throws(TypeError, function() {
new p();
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
2. If target is a Proxy exotic object and the value of the
[[ProxyHandler]] internal slot of target is null, throw a
TypeError exception.
...
---*/
var revocable = Proxy.revocable({}, {});
revocable.revoke();
assert.throws(TypeError, function() {
new Proxy(revocable.proxy, {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy(false, {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy(null, {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy(0, {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy("", {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
assert.throws(TypeError, function() {
new Proxy(Symbol(), {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.15
description: >
Proxy ( target, handler )
...
1. If Type(target) is not Object, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
new Proxy(undefined, {});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.6
description: >
Trap is called with handler as context and parameters are target, P, and the
descriptor object.
info: >
[[DefineOwnProperty]] (P, Desc)
...
9. Let descObj be FromPropertyDescriptor(Desc).
10. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, P,
descObj»)).
...
---*/
var _handler, _target, _prop, _desc;
var target = {};
var descriptor = {
configurable: true,
enumerable: true,
writable: true,
value: 1
};
var handler = {
defineProperty: function(t, prop, desc) {
_handler = this;
_target = t;
_prop = prop;
_desc = desc;
return true;
}
};
var p = new Proxy(target, handler);
Object.defineProperty(p, "attr", descriptor);
assert.sameValue(_handler, handler);
assert.sameValue(_target, target);
assert.sameValue(_prop, "attr");
assert.sameValue(
Object.keys(_desc).length, 4,
"descriptor arg has the same amount of keys as given descriptor"
);
assert(_desc.configurable);
assert(_desc.writable);
assert(_desc.enumerable);
assert.sameValue(_desc.value, 1);
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.6
description: >
Throws a TypeError exception if handler is null.
---*/
var p = Proxy.revocable({}, {});
p.revoke();
assert.throws(TypeError, function() {
Object.defineProperty(p.proxy, "foo", {
configurable: true,
enumerable: true
});
});
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.6
description: >
If a property has a corresponding target object property then applying the
Property Descriptor of the property to the target object using
[[DefineOwnProperty]] will not throw an exception.
features: [Reflect]
includes: [propertyHelper.js]
---*/
var target = {};
var p = new Proxy(target, {
defineProperty: function(t, prop, desc) {
return Object.defineProperty(t, prop, desc);
}
});
var result = Reflect.defineProperty(p, "attr", {
configurable: true,
enumerable: true,
writable: true,
value: 1
});
assert.sameValue(result, true, "result === true");
verifyEqualTo(target, "attr", 1);
verifyWritable(target, "attr");
verifyEnumerable(target, "attr");
verifyConfigurable(target, "attr");
result = Reflect.defineProperty(p, "attr", {
configurable: false,
enumerable: false,
writable: false,
value: 2
});
assert.sameValue(result, true, "result === true");
verifyEqualTo(target, "attr", 2);
verifyNotWritable(target, "attr");
verifyNotEnumerable(target, "attr");
verifyNotConfigurable(target, "attr");
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 9.5.6
description: >
Trap return is an abrupt.
info: >
[[DefineOwnProperty]] (P, Desc)
...
10. Let booleanTrapResult be ToBoolean(Call(trap, handler, «target, P,
descObj»)).
11. ReturnIfAbrupt(booleanTrapResult).
...
includes: [Test262Error.js]
---*/
var p = new Proxy({}, {
defineProperty: function(t, prop, desc) {
throw new Test262Error();
}
});
assert.throws(Test262Error, function() {
Object.defineProperty(p, "foo", {});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment