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 438 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.13
description: >
trap is called with handler object as its context, and parameters are:
target, the call context and and an array list with the called arguments
info: >
[[Call]] (thisArgument, argumentsList)
9. Return Call(trap, handler, «target, thisArgument, argArray»).
---*/
var _target, _args, _handler, _context;
var target = function(a, b) { return a + b; };
var handler = {
apply: function(t, c, args) {
_handler = this;
_target = t;
_context = c;
_args = args;
}
};
var p = new Proxy(target, handler);
var context = {};
p.call(context, 1, 2);
assert.sameValue(_handler, handler, "trap context is the handler object");
assert.sameValue(_target, target, "first parameter is the target object");
assert.sameValue(_context, context, "second parameter is the call context");
assert.sameValue(_args.length, 2, "arguments list contains all call arguments");
assert.sameValue(_args[0], 1, "arguments list has first call argument");
assert.sameValue(_args[1], 2, "arguments list has second call argument");
// 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.13
description: >
Return the result from the trap method.
info: >
[[Call]] (thisArgument, argumentsList)
9. Return Call(trap, handler, «target, thisArgument, argArray»).
---*/
var target = function(a, b) { return a + b; };
var result = {};
var handler = {
apply: function(t, c, args) {
return result;
}
};
var p = new Proxy(target, handler);
assert.sameValue(p.call(), result);
// 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.13
description: >
[[Call]] (thisArgument, argumentsList)
2. If handler is null, throw a TypeError exception.
---*/
var p = Proxy.revocable(function() {}, {});
p.revoke();
assert.throws(TypeError, function() {
p.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.13
description: >
Return is an abrupt completion
includes: [Test262Error.js]
---*/
var target = function(a, b) { return a + b; };
var p = new Proxy(target, {
apply: function(t, c, args) {
throw new Test262Error();
}
});
assert.throws(Test262Error, 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.13
description: >
Throws if trap is not callable.
---*/
var p = new Proxy(function() {}, {
apply: {}
});
assert.throws(TypeError, function() {
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.13
description: >
If trap is undefined, propagate the call to the target object.
info: >
[[Call]] (thisArgument, argumentsList)
7. If trap is undefined, then Return Call(target, thisArgument,
argumentsList).
---*/
var target = function(a, b) {
return a + b;
};
var p = new Proxy(target, {});
assert.sameValue(p(1, 2), 3);
// 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.13
description: >
If trap is undefined, propagate the call to the target object.
info: >
[[Call]] (thisArgument, argumentsList)
7. If trap is undefined, then Return Call(target, thisArgument,
argumentsList).
---*/
var target = function(a, b) {
return a + b;
};
var p = new Proxy(target, {
apply: undefined
});
assert.sameValue(p(1, 2), 3);
// 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.14
description: >
trap is called with handler object as its context, and parameters are:
target, an array list with the called arguments and the new target, and the
constructor new.target.
info: >
[[Construct]] ( argumentsList, newTarget)
9. Let newObj be Call(trap, handler, «target, argArray, newTarget »).
---*/
var _target, _handler, _args, _P;
function Target() {}
var handler = {
construct: function(t, args, newTarget) {
_handler = this;
_target = t;
_args = args;
_P = newTarget;
return new t(args[0], args[1]);
}
};
var P = new Proxy(Target, handler);
new P(1, 2);
assert.sameValue(_handler, handler, "trap context is the handler object");
assert.sameValue(_target, Target, "first parameter is the target object");
assert.sameValue(_args.length, 2, "arguments list contains all call arguments");
assert.sameValue(_args[0], 1, "arguments list has first call argument");
assert.sameValue(_args[1], 2, "arguments list has second call argument");
assert.sameValue(_P, P, "constructor is sent as the third parameter");
// 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.14
description: >
Return the result from the trap method.
info: >
[[Construct]] ( argumentsList, newTarget)
12. Return newObj
---*/
function Target(a, b) {
this.sum = a + b;
};
var handler = {
construct: function(t, c, args) {
return { sum: 42 };
}
};
var P = new Proxy(Target, handler);
assert.sameValue((new P(1, 2)).sum, 42);
// 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.14
description: >
[[Construct]] ( argumentsList, newTarget)
2. If handler is null, throw a TypeError exception.
---*/
var p = Proxy.revocable(function() {}, {});
p.revoke();
assert.throws(TypeError, function() {
new p.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.14
description: >
Return abrupt from constructor call.
info: >
[[Construct]] ( argumentsList, newTarget)
9. Let newObj be Call(trap, handler, «target, argArray, newTarget »).
10. ReturnIfAbrupt(newObj).
includes: [Test262Error.js]
---*/
function Target() {}
var P = new Proxy(Target, {
construct: function() {
throw new Test262Error();
}
});
assert.throws(Test262Error, 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.14
description: >
Throws a TypeError if trap result is not an Object: Boolean
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return true;
}
});
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.14
description: >
Throws a TypeError if trap result is not an Object: Number
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return 0;
}
});
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.14
description: >
Throws a TypeError if trap result is not an Object: String
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return "";
}
});
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.14
description: >
Throws a TypeError if trap result is not an Object: Symbol
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
features: [Symbol]
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return Symbol();
}
});
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.14
description: >
Throws a TypeError if trap result is not an Object: undefined
info: >
[[Construct]] ( argumentsList, newTarget)
11. If Type(newObj) is not Object, throw a TypeError exception.
---*/
function Target() {
this.attr = "done";
};
var P = new Proxy(Target, {
construct: function() {
return undefined;
}
});
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.14
description: >
Throws if trap is not callable.
---*/
function Target() {}
var p = new Proxy(Target, {
construct: {}
});
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.14
description: >
If trap is undefined, propagate the construct to the target object.
info: >
[[Construct]] ( argumentsList, newTarget)
7. If trap is undefined, then
b. Return Construct(target, argumentsList, newTarget).
---*/
function Target(arg) {
this.attr = arg;
}
var P = new Proxy(Target, {});
assert.sameValue((new P("foo")).attr, "foo");
// 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.14
description: >
If trap is undefined, propagate the construct to the target object.
info: >
[[Construct]] ( argumentsList, newTarget)
7. If trap is undefined, then
b. Return Construct(target, argumentsList, newTarget).
---*/
function Target(arg) {
this.attr = arg;
}
var P = new Proxy(Target, {
construct: undefined
});
assert.sameValue((new P("foo")).attr, "foo");
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 26.2.1
description: >
The Proxy constructor is the %Proxy% intrinsic object and the
initial value of the Proxy property of the global object.
---*/
assert.sameValue(typeof Proxy, "function", "`typeof Proxy` is `'function'`");
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