# HG changeset patch # Parent 27b689953fb23235c3975cc3bbaa6ba5e8a899ba diff --git a/js/src/tests/ecma_6/Reflect/apply.js b/js/src/tests/ecma_6/Reflect/apply.js new file mode 100644 --- /dev/null +++ b/js/src/tests/ecma_6/Reflect/apply.js @@ -0,0 +1,94 @@ +// Check Reflect.apply +var BUGNUMBER = 987514; +var summary = 'Implement ES6 Reflect'; + +assertEq(typeof Reflect.apply, "function"); +assertEq(Reflect.apply.length, 3); + +// If target is not callable +var stuff = [1, "", {}, true, undefined, new Proxy({}, {construct: function(){}})]; +stuff.forEach((thing) => { + assertThrowsInstanceOf(function () { Reflect.apply(thing, null, {}); }, TypeError); +}); + +// Array-like is not actually an object +stuff = [1, "", false, undefined]; +stuff.forEach((thing) => { + assertThrowsInstanceOf(function () { Reflect.apply(Function, null, thing); }, TypeError); +}); + +// Identity functions +stuff = [function (...args) { + return args; + }, + new Proxy(function (...args) { + return args; + }, {}), + new Proxy(function() {}, { + apply: function (target, thisArg, args) { + return args; + } + })]; + +var stringify = JSON.stringify; + +stuff.forEach((thing) => { + assertEq(stringify(Reflect.apply(thing, null, [1, 2, 3])), "[1,2,3]"); + assertEq(stringify(Reflect.apply(thing, null, ["test"])), "[\"test\"]"); + assertEq(stringify(Reflect.apply(thing, null, [])), "[]"); +}); + +// Function returning this +stuff = [function () { + 'use strict'; + return this; + }, + new Proxy(function () { + 'use strict'; + return this; + }, {}), + new Proxy(function() {}, { + apply: function (target, thisArg, args) { + return thisArg; + } + })]; +stuff.forEach((thing) => { + assertEq(Reflect.apply(thing, 1, []), 1); + assertEq(Reflect.apply(thing, "test", []), "test"); + var obj = {}; + assertEq(Reflect.apply(thing, obj, []), obj); + assertEq(Reflect.apply(thing, null, []), null); + assertEq(Reflect.apply(thing, undefined, []), undefined); +}); + +// Non-strict boxing functions +stuff = [function () { + return this; + }, + new Proxy(function () { + return this; + }, {})]; +stuff.forEach((thing) => { + assertEq(uneval(Reflect.apply(thing, 1, [])), '(new Number(1))'); + var obj = {}; + assertEq(Reflect.apply(thing, obj, []), obj); + assertEq(Reflect.apply(thing, null, []), this); + assertEq(Reflect.apply(thing, undefined, []), this); +}); + +// A few other array like arguments +stuff = [new Int8Array([1, 2, 3]), + new Float32Array([1, 2, 3]), + (function() { return arguments; })(1, 2, 3), + [1, 2, 3]]; +stuff.forEach((thing) => { + var f = function(...args) { return args; }; + assertEq(stringify(Reflect.apply(f, null, thing)), "[1,2,3]"); +}) + +// Edge case: Too many arguments for us to handle +assertThrowsInstanceOf(function() { Reflect.apply(Function, null, {length: 0xfffffff}); }, + RangeError); + +if (typeof reportCompare === "function") + reportCompare(true, true); diff --git a/js/src/tests/ecma_6/Reflect/browser.js b/js/src/tests/ecma_6/Reflect/browser.js new file mode 100644 diff --git a/js/src/tests/ecma_6/Reflect/construct.js b/js/src/tests/ecma_6/Reflect/construct.js new file mode 100644 --- /dev/null +++ b/js/src/tests/ecma_6/Reflect/construct.js @@ -0,0 +1,67 @@ +// Check Reflect.construct +var BUGNUMBER = 987514; +var summary = 'Implement ES6 Reflect'; + +assertEq(typeof Reflect.construct, "function"); +assertEq(Reflect.construct.length, 2); + +// If target is not constructable +var stuff = [1, "", {}, true, undefined, new Proxy({}, {apply: function(){}}), + Function.prototype]; +stuff.forEach((thing) => { + assertThrowsInstanceOf(function () { Reflect.construct(thing, {}); }, TypeError); +}); + +// Array-like is not actually an object +stuff = [1, "", false, undefined]; +stuff.forEach((thing) => { + assertThrowsInstanceOf(function () { Reflect.construct(Function, thing); }, TypeError); +}); + +// Identity functions +stuff = [function (...args) { + return args; + }, + new Proxy(function (...args) { + return args; + }, {}), + new Proxy(function() {}, { + construct: function (target, args) { + return args; + } + })]; + +var stringify = JSON.stringify; + +stuff.forEach((thing) => { + assertEq(stringify(Reflect.construct(thing, [1, 2, 3])), "[1,2,3]"); + assertEq(stringify(Reflect.construct(thing, ["test"])), "[\"test\"]"); + assertEq(stringify(Reflect.construct(thing, [])), "[]"); +}); + +// Constructing behavior +assertEq(Reflect.construct(String, ["abc"]).valueOf(), "abc"); +assertEq(Reflect.construct(Number, [1]).valueOf(), 1); +function f() { + this.a = 1; + assertEq(this.x, 15) +} +f.prototype.x = 15; +assertEq(Reflect.construct(f, []).a, 1); + +// A few other array like arguments +stuff = [new Int8Array([1, 2, 3]), + new Float32Array([1, 2, 3]), + (function() { return arguments; })(1, 2, 3), + [1, 2, 3]]; +stuff.forEach((thing) => { + var f = function(...args) { return args; }; + assertEq(stringify(Reflect.construct(f, thing)), "[1,2,3]"); +}) + +// Edge case: Too many arguments for us to handle +assertThrowsInstanceOf(function() { Reflect.construct(Function, {length: 0xfffffff}); }, + RangeError); + +if (typeof reportCompare === "function") + reportCompare(true, true); diff --git a/js/src/tests/ecma_6/Reflect/shell.js b/js/src/tests/ecma_6/Reflect/shell.js new file mode 100644 diff --git a/js/src/tests/ecma_6/Reflect/surface.js b/js/src/tests/ecma_6/Reflect/surface.js new file mode 100644 --- /dev/null +++ b/js/src/tests/ecma_6/Reflect/surface.js @@ -0,0 +1,22 @@ +// Check superficial properties of the Reflect object. + +var BUGNUMBER = 987514; +var summary = 'Implement ES6 Reflect'; + +print(BUGNUMBER + ": " + summary); + +var desc = Object.getOwnPropertyDescriptor(this, "Reflect"); +assertEq(desc.configurable, true); +assertEq(desc.enumerable, false); +assertEq(desc.writable, true); +assertEq(desc.value, Reflect); + +assertEq(typeof Reflect, "object"); +assertEq(Object.getPrototypeOf(Reflect), Object.prototype); +assertEq(Reflect.hasOwnProperty("prototype"), false); + +assertThrowsInstanceOf(function() { new Reflect() }, TypeError); +assertThrowsInstanceOf(function() { Reflect(); }, TypeError); + +if (typeof reportCompare === "function") + reportCompare(true, true);