Chromium Code Reviews| Index: test/mjsunit/harmony/toMethod.js |
| diff --git a/test/mjsunit/harmony/toMethod.js b/test/mjsunit/harmony/toMethod.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..524b0c9fe0ac4a4096dac96f1202d4f33573269d |
| --- /dev/null |
| +++ b/test/mjsunit/harmony/toMethod.js |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2013 the V8 project authors. All rights reserved. |
| +// Redistribution and use in source and binary forms, with or without |
| +// modification, are permitted provided that the following conditions are |
| +// met: |
| +// |
| +// * Redistributions of source code must retain the above copyright |
| +// notice, this list of conditions and the following disclaimer. |
| +// * Redistributions in binary form must reproduce the above |
| +// copyright notice, this list of conditions and the following |
| +// disclaimer in the documentation and/or other materials provided |
| +// with the distribution. |
| +// * Neither the name of Google Inc. nor the names of its |
| +// contributors may be used to endorse or promote products derived |
| +// from this software without specific prior written permission. |
| +// |
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| +// |
| +// Flags: --harmony-classes --allow-natives-syntax |
| + |
| + |
| +(function ToMethodSingleClass() { |
| + function f(x) { |
| + var a = [0, 1, 2] |
| + return a[x]; |
| + } |
| + |
| + function ClassD() { } |
| + |
| + assertEquals(1, f(1)); |
| + var g = f.toMethod(ClassD.prototype); |
| + assertEquals(1, g(1)); |
| + assertEquals(undefined, f[%HomeObjectSymbol()]); |
| + assertEquals(ClassD.prototype, g[%HomeObjectSymbol()]); |
| +}()); |
| + |
| + |
| +(function ToMethodClassHierarchy() { |
| + function f(x) { |
| + return function g(y) { x++; return x + y; }; |
| + } |
| + |
| + function Base() {} |
| + function Derived() { } |
| + Derived.prototype = Object.create(Base.prototype); |
| + |
| + var q = f(0); |
| + assertEquals(2, q(1)); |
| + assertEquals(3, q(1)); |
| + var g = q.toMethod(Derived.prototype); |
| + assertFalse(g === q); |
| + assertEquals(4, g(1)); |
| + assertEquals(5, q(1)); |
| +}()); |
| + |
| + |
| +(function ToMethodErrorCases() { |
| + var sFun = Function.prototype.toMethod; |
| + assertThrows(function() { sFun.call({}); }, TypeError); |
| + assertThrows(function() { sFun.call({}, {}); }, TypeError); |
| + function f(){}; |
| + assertThrows(function() { f.toMethod(1); }, TypeError); |
| +}()); |
| + |
| + |
| +(function ToMethodPrototypeChain() { |
| + var o = {}; |
| + var o1 = {}; |
| + function f() { } |
| + |
| + function g() { } |
| + |
| + var fMeth = f.toMethod(o); |
| + assertEquals(o, fMeth[%HomeObjectSymbol()]); |
| + g.__proto__ = fMeth; |
| + assertEquals(undefined, g[%HomeObjectSymbol()]); |
| + var gMeth = g.toMethod(o1); |
| + assertEquals(fMeth, gMeth.__proto__); |
| + assertEquals(o1, gMeth[%HomeObjectSymbol()]); |
| +}()); |
| + |
| + |
| +(function ToMethodBoundFunction() { |
| + var o = {}; |
| + var p = {}; |
| + |
| + |
| + function f(x, y, z) { |
|
Toon Verwaest
2014/08/18 14:15:35
Add flag "called = false" on the outside, and set
Dmitry Lomov (no reviews)
2014/08/19 20:02:48
Checking return value instead.
|
| + assertEquals(o, this); |
| + assertEquals(1, x); |
| + assertEquals(2, y); |
| + assertEquals(3, z); |
| + } |
| + |
| + var fBound = f.bind(o, 1, 2, 3); |
| + var fMeth = fBound.toMethod(p); |
| + fMeth(); |
| + fMeth.call(p); |
| +}()); |
|
Toon Verwaest
2014/08/18 14:15:35
What about tests for rebinding a method, given tha
Dmitry Lomov (no reviews)
2014/08/19 20:02:48
Done.
|